-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex_github.py
More file actions
78 lines (67 loc) · 2.03 KB
/
index_github.py
File metadata and controls
78 lines (67 loc) · 2.03 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
"""GitHub indexer for Lucidia CLI"""
import sqlite3
import json
import os
from pathlib import Path
from datetime import datetime
try:
import requests
except ImportError:
requests = None
DB_PATH = Path.home() / ".blackroad" / "index" / "blackroad.db"
GITHUB_TOKEN = os.environ.get("GITHUB_TOKEN")
ORGS = ["BlackRoad-OS", "blackroadio"]
def get_db():
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
return conn
def fetch_repos(org):
if not requests:
print("pip install requests")
return []
headers = {"Accept": "application/vnd.github.v3+json"}
if GITHUB_TOKEN:
headers["Authorization"] = f"token {GITHUB_TOKEN}"
repos = []
page = 1
while True:
url = f"https://api.github.com/orgs/{org}/repos?per_page=100&page={page}"
resp = requests.get(url, headers=headers)
if resp.status_code != 200:
print(f"Error fetching {org}: {resp.status_code}")
break
data = resp.json()
if not data:
break
repos.extend(data)
page += 1
return repos
def index_github():
conn = get_db()
conn.execute("DELETE FROM resources WHERE type = 'repo'")
total = 0
for org in ORGS:
print(f"Indexing {org}...")
repos = fetch_repos(org)
for repo in repos:
conn.execute('''
INSERT INTO resources (type, name, url, description, metadata)
VALUES (?, ?, ?, ?, ?)
''', (
'repo',
f"{org}/{repo['name']}",
repo['html_url'],
repo.get('description', ''),
json.dumps({
'stars': repo['stargazers_count'],
'language': repo.get('language'),
'updated': repo['updated_at']
})
))
total += len(repos)
print(f" {len(repos)} repos")
conn.commit()
conn.close()
print(f"✓ Indexed {total} repos")
if __name__ == '__main__':
index_github()