-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.py
More file actions
58 lines (48 loc) · 2.34 KB
/
github.py
File metadata and controls
58 lines (48 loc) · 2.34 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
import math
import requests
from config import GITHUB_USERNAME, GITHUB_API_URL, GITHUB_TOKEN
class GitHubAPIError(Exception):
pass
class GitHubAPI:
def __init__(self):
self.base_url = GITHUB_API_URL.rstrip("/")
self.username = GITHUB_USERNAME
self.session = requests.Session()
self.session.headers.update({"Accept": "application/vnd.github.v3+json"})
if GITHUB_TOKEN:
self.session.headers.update({"Authorization": f"Bearer {GITHUB_TOKEN}"})
def get_recent_followers(self, limit: int = 14):
if not self.username:
raise ValueError("GITHUB_USERNAME configuration is missing.")
from html.parser import HTMLParser
class FollowerParser(HTMLParser):
def __init__(self, target_username):
super().__init__()
self.target_username = target_username
self.followers = []
def handle_starttag(self, tag, attrs):
if tag == 'img':
attrs_dict = dict(attrs)
class_attr = attrs_dict.get('class', '')
if 'avatar' in class_attr and 'avatar-user' in class_attr:
alt = attrs_dict.get('alt', '').replace('@', '')
src = attrs_dict.get('src', '')
if alt and ' ' not in alt and alt.lower() != self.target_username.lower():
if not any(f['login'] == alt for f in self.followers):
self.followers.append({
'login': alt,
'avatar_url': src,
'html_url': f'https://github.com/{alt}'
})
parser = FollowerParser(self.username)
try:
# The GitHub UI natively orders followers by exact follow date
response = self.session.get(
f"https://github.com/{self.username}?tab=followers",
timeout=30
)
response.raise_for_status()
parser.feed(response.text)
return parser.followers[:limit]
except requests.exceptions.RequestException as e:
raise GitHubAPIError(f"GitHub API Error: Failed to fetch followers by follow date - {e}") from e