Skip to content

Commit 9fd973a

Browse files
Edouard SilvestreEdouard Silvestre
authored andcommitted
scrapper github et hugging face
1 parent 1210135 commit 9fd973a

2 files changed

Lines changed: 182 additions & 0 deletions

File tree

scrape_github.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import os
2+
import time
3+
import sqlite3
4+
import requests
5+
from datetime import datetime
6+
7+
REPOS = [
8+
"huggingface/transformers",
9+
"openai/openai-python",
10+
"pytorch/pytorch",
11+
"tensorflow/tensorflow",
12+
"langchain/langchain",
13+
"microsoft/ML-For-Beginners",
14+
"microsoft/AI-For-Beginners",
15+
"karpathy/nn-zero-to-hero",
16+
"Significant-Gravitas/AutoGPT",
17+
"stabilityai/stablediffusion",
18+
]
19+
INTERVAL = 300
20+
DB_FILE = os.path.join(os.path.dirname(__file__), "github_releases.db")
21+
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN")
22+
23+
conn = sqlite3.connect(DB_FILE)
24+
cur = conn.cursor()
25+
cur.execute("""
26+
CREATE TABLE IF NOT EXISTS releases (
27+
id TEXT PRIMARY KEY,
28+
repo TEXT,
29+
tag TEXT,
30+
name TEXT,
31+
published TIMESTAMP,
32+
body TEXT,
33+
html_url TEXT
34+
)
35+
""")
36+
conn.commit()
37+
38+
headers = {"Accept": "application/vnd.github+json"}
39+
if GITHUB_TOKEN:
40+
headers["Authorization"] = f"Bearer {GITHUB_TOKEN}"
41+
42+
def fetch_releases(repo):
43+
url = f"https://api.github.com/repos/{repo}/releases"
44+
r = requests.get(url, headers=headers)
45+
r.raise_for_status()
46+
return r.json()
47+
48+
def save_release(repo, release):
49+
cur.execute("""
50+
INSERT OR IGNORE INTO releases (id, repo, tag, name, published, body, html_url)
51+
VALUES (?, ?, ?, ?, ?, ?, ?)
52+
""", (
53+
release["id"],
54+
repo,
55+
release.get("tag_name", ""),
56+
release.get("name", ""),
57+
release.get("published_at", datetime.utcnow().isoformat()),
58+
release.get("body", ""),
59+
release.get("html_url", "")
60+
))
61+
conn.commit()
62+
63+
def load_seen_ids():
64+
cur.execute("SELECT id FROM releases")
65+
return set(row[0] for row in cur.fetchall())
66+
67+
def main():
68+
print("Initialisation GitHub Releases...")
69+
seen_ids = load_seen_ids()
70+
print(f"{len(seen_ids)} releases déjà enregistrées.")
71+
try:
72+
while True:
73+
for repo in REPOS:
74+
try:
75+
releases = fetch_releases(repo)
76+
for rel in releases:
77+
rel_id = str(rel["id"])
78+
if rel_id not in seen_ids:
79+
print(f"[NOUVELLE RELEASE] {repo}{rel.get('name','(no name)')}")
80+
print(" ->", rel.get("html_url", ""))
81+
save_release(repo, rel)
82+
seen_ids.add(rel_id)
83+
except Exception as e:
84+
print(f"[ERREUR] {repo}: {e}")
85+
86+
print(f"Attente {INTERVAL}s avant prochaine vérification...")
87+
time.sleep(INTERVAL)
88+
except KeyboardInterrupt:
89+
print("Arrêt manuel.")
90+
finally:
91+
conn.close()
92+
93+
if __name__ == "__main__":
94+
main()

scrape_hf.py

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import os
2+
import time
3+
import sqlite3
4+
import requests
5+
from datetime import datetime
6+
7+
INTERVAL = 300
8+
DB_FILE = os.path.join(os.path.dirname(__file__), "huggingface_hub.db")
9+
10+
conn = sqlite3.connect(DB_FILE)
11+
cur = conn.cursor()
12+
cur.execute("""
13+
CREATE TABLE IF NOT EXISTS hubs (
14+
id TEXT PRIMARY KEY,
15+
name TEXT,
16+
author TEXT,
17+
likes INTEGER,
18+
downloads INTEGER,
19+
task TEXT,
20+
last_modified TEXT,
21+
type TEXT,
22+
url TEXT
23+
)
24+
""")
25+
conn.commit()
26+
27+
def fetch_models():
28+
"""Récupère les modèles récents via l’API Hugging Face"""
29+
url = "https://huggingface.co/api/models?sort=lastModified&direction=-1&limit=20"
30+
r = requests.get(url)
31+
r.raise_for_status()
32+
return r.json()
33+
34+
def fetch_datasets():
35+
"""Récupère les datasets récents"""
36+
url = "https://huggingface.co/api/datasets?sort=lastModified&direction=-1&limit=20"
37+
r = requests.get(url)
38+
r.raise_for_status()
39+
return r.json()
40+
41+
def save_item(item, item_type):
42+
cur.execute("""
43+
INSERT OR IGNORE INTO hubs (id, name, author, likes, downloads, task, last_modified, type, url)
44+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
45+
""", (
46+
item.get("id"),
47+
item.get("modelId") or item.get("id"),
48+
item.get("author", ""),
49+
item.get("likes", 0),
50+
item.get("downloads", 0),
51+
", ".join(item.get("pipeline_tag", "") if isinstance(item.get("pipeline_tag"), list) else [item.get("pipeline_tag")]) if item.get("pipeline_tag") else "",
52+
item.get("lastModified", datetime.utcnow().isoformat()),
53+
item_type,
54+
f"https://huggingface.co/{item.get('id')}"
55+
))
56+
conn.commit()
57+
58+
def load_seen_ids():
59+
cur.execute("SELECT id FROM hubs")
60+
return set(row[0] for row in cur.fetchall())
61+
62+
def main():
63+
print("Initialisation Hugging Face Hub...")
64+
seen_ids = load_seen_ids()
65+
print(f"{len(seen_ids)} éléments déjà enregistrés.")
66+
try:
67+
while True:
68+
for item_type, fetch_func in [("model", fetch_models), ("dataset", fetch_datasets)]:
69+
try:
70+
items = fetch_func()
71+
for item in items:
72+
item_id = item.get("id")
73+
if item_id and item_id not in seen_ids:
74+
print(f"[NOUVEAU {item_type.upper()}] {item_id}")
75+
save_item(item, item_type)
76+
seen_ids.add(item_id)
77+
except Exception as e:
78+
print(f"[ERREUR] {item_type}: {e}")
79+
80+
print(f"Attente {INTERVAL}s avant prochaine vérification...")
81+
time.sleep(INTERVAL)
82+
except KeyboardInterrupt:
83+
print("Arrêt manuel.")
84+
finally:
85+
conn.close()
86+
87+
if __name__ == "__main__":
88+
main()

0 commit comments

Comments
 (0)