File size: 2,755 Bytes
94b42f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import json
import logging
import requests
logger = logging.getLogger(__name__)

def pull_from_gist(filename: str) -> dict:
    """Pull a specific JSON file from the GitHub Gist."""
    from config import GIST_ID, GIST_TOKEN
    if not GIST_ID or not GIST_TOKEN:
        return None

    try:
        url = f"https://api.github.com/gists/{GIST_ID}"
        headers = {"Authorization": f"token {GIST_TOKEN}"}
        resp = requests.get(url, headers=headers, timeout=10)
        
        if resp.status_code == 200:
            gist_data = resp.json()
            files = gist_data.get("files", {})
            if filename in files:
                content = files[filename].get("content", "{}")
                logger.info(f"Successfully pulled {filename} from Gist")
                return json.loads(content)
        elif resp.status_code == 401:
            logger.error(f"Gist Pull Failed ({filename}): [401 Unauthorized] Check your GIST_TOKEN.")
        elif resp.status_code != 404:
            logger.error(f"Failed to pull {filename} from Gist (Status {resp.status_code})")
    except Exception as e:
        logger.error(f"Error pulling {filename} from Gist: {e}")
    
    return None


def push_to_gist(filename: str, data_dict: dict):
    """Push a dictionary to a specific file in the GitHub Gist."""
    from config import GIST_ID, GIST_TOKEN
    if not GIST_ID or not GIST_TOKEN:
        return

    try:
        url = f"https://api.github.com/gists/{GIST_ID}"
        headers = {"Authorization": f"token {GIST_TOKEN}"}
        
        content = json.dumps(data_dict, indent=2)
        payload = {
            "files": {
                filename: {
                    "content": content
                }
            }
        }
        
        resp = requests.patch(url, headers=headers, json=payload, timeout=10)
        
        if resp.status_code == 200:
            logger.info(f"Successfully pushed {filename} to Gist")
        elif resp.status_code == 401:
            logger.error(f"Gist Push Failed ({filename}): [401 Unauthorized] Check your GIST_TOKEN.")
        else:
            logger.error(f"Failed to push {filename} to Gist (Status {resp.status_code})")
    except Exception as e:
        logger.error(f"Error pushing {filename} to Gist: {e}")


# ── Legacy Wrappers (to avoid breaking other files) ───────────────────────────
def pull_history_from_gist() -> dict:
    from config import HISTORY_FILE_NAME
    return pull_from_gist(HISTORY_FILE_NAME)

def push_history_to_gist(history_dict: dict):
    from config import HISTORY_FILE_NAME
    push_to_gist(HISTORY_FILE_NAME, history_dict)