| import os |
| import json |
| import time |
| import base64 |
| import logging |
| import requests |
|
|
| logger = logging.getLogger("github") |
|
|
| GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN", "") |
| GITHUB_REPO = "mishableskineetudiant-stack/proxylistfiltered" |
| FILE_PATH = "proxies_live.json" |
|
|
|
|
| def _headers(): |
| h = {"Accept": "application/vnd.github.v3+json"} |
| if GITHUB_TOKEN: |
| h["Authorization"] = f"Bearer {GITHUB_TOKEN}" |
| return h |
|
|
|
|
| def save_to_github(proxies_data): |
| if not GITHUB_TOKEN: |
| return False |
| try: |
| content = json.dumps({ |
| "updated_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()), |
| "count": len(proxies_data), |
| "proxies": proxies_data, |
| }, indent=2) |
| encoded = base64.b64encode(content.encode()).decode() |
| url = f"https://api.github.com/repos/{GITHUB_REPO}/contents/{FILE_PATH}" |
| sha = None |
| try: |
| r = requests.get(url, headers=_headers(), timeout=10) |
| if r.status_code == 200: |
| sha = r.json().get("sha") |
| except Exception: |
| pass |
| body = {"message": f"Update: {len(proxies_data)} proxies", "content": encoded} |
| if sha: |
| body["sha"] = sha |
| r = requests.put(url, json=body, headers=_headers(), timeout=15) |
| if r.status_code in (200, 201): |
| logger.info(f"GitHub save OK: {len(proxies_data)}") |
| return True |
| logger.error(f"GitHub save failed: {r.status_code}") |
| return False |
| except Exception as e: |
| logger.error(f"GitHub save error: {e}") |
| return False |
|
|
|
|
| def load_from_github(): |
| try: |
| url = f"https://api.github.com/repos/{GITHUB_REPO}/contents/{FILE_PATH}" |
| r = requests.get(url, headers=_headers(), timeout=10) |
| if r.status_code != 200: |
| return [] |
| content = base64.b64decode(r.json()["content"]).decode() |
| proxies = json.loads(content).get("proxies", []) |
| logger.info(f"GitHub load OK: {len(proxies)}") |
| return proxies |
| except Exception as e: |
| logger.error(f"GitHub load error: {e}") |
| return [] |