Create github_store.py
Browse files- github_store.py +66 -0
github_store.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import time
|
| 4 |
+
import base64
|
| 5 |
+
import logging
|
| 6 |
+
import requests
|
| 7 |
+
|
| 8 |
+
logger = logging.getLogger("github")
|
| 9 |
+
|
| 10 |
+
GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN", "")
|
| 11 |
+
GITHUB_REPO = "mishableskineetudiant-stack/proxylistfiltered"
|
| 12 |
+
FILE_PATH = "proxies_live.json"
|
| 13 |
+
|
| 14 |
+
|
| 15 |
+
def _headers():
|
| 16 |
+
h = {"Accept": "application/vnd.github.v3+json"}
|
| 17 |
+
if GITHUB_TOKEN:
|
| 18 |
+
h["Authorization"] = f"Bearer {GITHUB_TOKEN}"
|
| 19 |
+
return h
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
def save_to_github(proxies_data):
|
| 23 |
+
if not GITHUB_TOKEN:
|
| 24 |
+
return False
|
| 25 |
+
try:
|
| 26 |
+
content = json.dumps({
|
| 27 |
+
"updated_at": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
|
| 28 |
+
"count": len(proxies_data),
|
| 29 |
+
"proxies": proxies_data,
|
| 30 |
+
}, indent=2)
|
| 31 |
+
encoded = base64.b64encode(content.encode()).decode()
|
| 32 |
+
url = f"https://api.github.com/repos/{GITHUB_REPO}/contents/{FILE_PATH}"
|
| 33 |
+
sha = None
|
| 34 |
+
try:
|
| 35 |
+
r = requests.get(url, headers=_headers(), timeout=10)
|
| 36 |
+
if r.status_code == 200:
|
| 37 |
+
sha = r.json().get("sha")
|
| 38 |
+
except Exception:
|
| 39 |
+
pass
|
| 40 |
+
body = {"message": f"Update: {len(proxies_data)} proxies", "content": encoded}
|
| 41 |
+
if sha:
|
| 42 |
+
body["sha"] = sha
|
| 43 |
+
r = requests.put(url, json=body, headers=_headers(), timeout=15)
|
| 44 |
+
if r.status_code in (200, 201):
|
| 45 |
+
logger.info(f"GitHub save OK: {len(proxies_data)}")
|
| 46 |
+
return True
|
| 47 |
+
logger.error(f"GitHub save failed: {r.status_code}")
|
| 48 |
+
return False
|
| 49 |
+
except Exception as e:
|
| 50 |
+
logger.error(f"GitHub save error: {e}")
|
| 51 |
+
return False
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def load_from_github():
|
| 55 |
+
try:
|
| 56 |
+
url = f"https://api.github.com/repos/{GITHUB_REPO}/contents/{FILE_PATH}"
|
| 57 |
+
r = requests.get(url, headers=_headers(), timeout=10)
|
| 58 |
+
if r.status_code != 200:
|
| 59 |
+
return []
|
| 60 |
+
content = base64.b64decode(r.json()["content"]).decode()
|
| 61 |
+
proxies = json.loads(content).get("proxies", [])
|
| 62 |
+
logger.info(f"GitHub load OK: {len(proxies)}")
|
| 63 |
+
return proxies
|
| 64 |
+
except Exception as e:
|
| 65 |
+
logger.error(f"GitHub load error: {e}")
|
| 66 |
+
return []
|