init
This commit is contained in:
93
main.py
Normal file
93
main.py
Normal file
@@ -0,0 +1,93 @@
|
||||
import requests
|
||||
import re
|
||||
import os
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
# Configuration
|
||||
URL = "https://gist.githubusercontent.com/aamiaa/204cd9d42013ded9faf646fae7f89fbb/raw"
|
||||
OUTPUT_FILENAME = "discord-quest.js"
|
||||
REPO_REMOTE = "origin" # Change if your remote is named differently
|
||||
REPO_BRANCH = "main" # Change if your branch is named differently
|
||||
|
||||
def fetch_and_extract():
|
||||
print(f"Fetching content from {URL}...")
|
||||
try:
|
||||
response = requests.get(URL)
|
||||
response.raise_for_status()
|
||||
content = response.text
|
||||
except Exception as e:
|
||||
print(f"Error downloading content: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
# Regex to find markdown code blocks (``` ... ```)
|
||||
# This looks for blocks optionally specified as javascript/js
|
||||
code_blocks = re.findall(r'```(?:javascript|js)?\s*(.*?)```', content, re.DOTALL)
|
||||
|
||||
if not code_blocks:
|
||||
print("No code blocks found in the fetched text.")
|
||||
sys.exit(1)
|
||||
|
||||
# Heuristic: Find the block that looks like the actual script.
|
||||
# Based on the known content, the script usually contains 'webpackChunkdiscord_app'
|
||||
# If we can't find that specific keyword, we default to the longest block.
|
||||
target_code = None
|
||||
for block in code_blocks:
|
||||
if "webpackChunkdiscord_app" in block:
|
||||
target_code = block
|
||||
break
|
||||
|
||||
if not target_code:
|
||||
print("Could not identify the specific Discord script block. Using the longest code block found.")
|
||||
target_code = max(code_blocks, key=len)
|
||||
|
||||
return target_code.strip()
|
||||
|
||||
def has_changes(filename):
|
||||
# Check if the file is modified in git
|
||||
result = subprocess.run(["git", "status", "--porcelain", filename], capture_output=True, text=True)
|
||||
return bool(result.stdout.strip())
|
||||
|
||||
def git_commit_and_push(filename):
|
||||
try:
|
||||
print("Changes detected. Committing...")
|
||||
subprocess.run(["git", "add", filename], check=True)
|
||||
subprocess.run(["git", "commit", "-m", "Update discord-quest.js from upstream gist"], check=True)
|
||||
|
||||
print("Pushing to Gitea...")
|
||||
subprocess.run(["git", "push", REPO_REMOTE, REPO_BRANCH], check=True)
|
||||
print("Successfully pushed changes.")
|
||||
except subprocess.CalledProcessError as e:
|
||||
print(f"Git operation failed: {e}")
|
||||
sys.exit(1)
|
||||
|
||||
def main():
|
||||
# 1. Extract new code
|
||||
new_code = fetch_and_extract()
|
||||
|
||||
# 2. Read existing file to compare (avoids git modifying timestamp if identical)
|
||||
if os.path.exists(OUTPUT_FILENAME):
|
||||
with open(OUTPUT_FILENAME, 'r', encoding='utf-8') as f:
|
||||
current_code = f.read().strip()
|
||||
else:
|
||||
current_code = None
|
||||
|
||||
# 3. Write file only if content is different
|
||||
if new_code != current_code:
|
||||
print(f"Updating {OUTPUT_FILENAME}...")
|
||||
with open(OUTPUT_FILENAME, 'w', encoding='utf-8') as f:
|
||||
f.write(new_code + '\n') # Add newline at end of file
|
||||
else:
|
||||
print("No changes in content detected. Exiting.")
|
||||
sys.exit(0)
|
||||
|
||||
# 4. Check Git status and Push (skip in CI - workflow handles it)
|
||||
if os.getenv("CI") or os.getenv("GITHUB_ACTIONS") or os.getenv("GITEA_ACTIONS"):
|
||||
print("Running in CI environment. Skipping git operations - workflow will handle commit/push.")
|
||||
elif has_changes(OUTPUT_FILENAME):
|
||||
git_commit_and_push(OUTPUT_FILENAME)
|
||||
else:
|
||||
print("File updated but git status shows no changes (clean).")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user