-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_api.py
More file actions
79 lines (68 loc) · 2.91 KB
/
github_api.py
File metadata and controls
79 lines (68 loc) · 2.91 KB
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
74
75
76
77
78
79
import os
import json
import requests
from pathlib import Path
from typing import List, Dict, Optional
import threading
class GitHubAPI:
def __init__(self, token: Optional[str] = None):
self.repo = "eden-emulator/Releases"
self.base_url = f"https://api.github.com/repos/{self.repo}"
self.session = requests.Session()
self.token = token
self._setup_session()
def _setup_session(self):
"""Configura los headers de la sesión HTTP."""
headers = {
'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'GitHub-Releases-Downloader-GUI/2.0'
}
if self.token:
headers['Authorization'] = f'token {self.token}'
self.session.headers.update(headers)
def set_repo(self, new_repo: str):
"""Cambia el repositorio y actualiza la URL base."""
self.repo = new_repo
self.base_url = f"https://api.github.com/repos/{self.repo}"
def set_token(self, new_token: str):
"""Configura un nuevo token y actualiza la sesión."""
self.token = new_token
self._setup_session()
def save_token_to_file(self, token: str):
"""Guarda el token en un archivo local."""
token_data = {'github_token': token}
with open('.secret_token.json', 'w') as f:
json.dump(token_data, f, indent=2)
def load_token_from_file(self) -> Optional[str]:
"""Carga el token desde un archivo local."""
token_file = Path(".secret_token.json")
if token_file.exists():
try:
with open(token_file, 'r') as f:
data = json.load(f)
return data.get('github_token')
except:
return None
return None
def get_releases(self) -> List[Dict]:
"""Obtiene una lista de releases del repositorio."""
url = f"{self.base_url}/releases"
response = self.session.get(url, params={'per_page': 20})
response.raise_for_status()
return response.json()
def download_asset(self, asset: Dict, download_path: str, progress_callback=None):
"""Descarga un solo archivo con un callback para el progreso."""
Path(download_path).mkdir(parents=True, exist_ok=True)
file_path = Path(download_path) / asset['name']
url = asset['browser_download_url']
response = self.session.get(url, stream=True)
response.raise_for_status()
total_size = int(response.headers.get('content-length', 0))
downloaded = 0
with open(file_path, 'wb') as f:
for chunk in response.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
downloaded += len(chunk)
if progress_callback and total_size > 0:
progress_callback(downloaded, total_size)