zhimin-z commited on
Commit
43dd07e
·
1 Parent(s): 260fd60
Files changed (1) hide show
  1. msr.py +74 -0
msr.py ADDED
@@ -0,0 +1,74 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import json
3
+ import time
4
+ import re
5
+ import requests
6
+ from bs4 import BeautifulSoup
7
+ import dotenv
8
+
9
+ dotenv.load_dotenv(override=True)
10
+
11
+ GITHUB_TOKEN = os.getenv('GITHUB_TOKEN')
12
+ HEADERS = {
13
+ 'Authorization': f'token {GITHUB_TOKEN}' if GITHUB_TOKEN else '',
14
+ 'Accept': 'application/vnd.github.v3+json',
15
+ }
16
+
17
+
18
+ def get_app_info(app_slug):
19
+ """Get GitHub App info, returns None if app doesn't exist or has no description."""
20
+ response = requests.get(f'https://api.github.com/apps/{app_slug}', headers=HEADERS)
21
+ if response.status_code == 200:
22
+ data = response.json()
23
+ if data.get('description'):
24
+ return {
25
+ 'name': data.get('name'),
26
+ 'website': f'https://api.github.com/apps/{app_slug}',
27
+ 'developer': data.get('owner', {}).get('login'),
28
+ 'status': 'active',
29
+ 'created_at': data.get('created_at'),
30
+ }
31
+ return None
32
+
33
+
34
+ def scrape_marketplace_apps(category):
35
+ """Scrape GitHub Marketplace for app slugs."""
36
+ apps, page = set(), 1
37
+ while True:
38
+ response = requests.get(
39
+ f'https://github.com/marketplace?type=apps&category={category}&page={page}',
40
+ headers={'User-Agent': 'Mozilla/5.0'}
41
+ )
42
+ if response.status_code != 200:
43
+ break
44
+ soup = BeautifulSoup(response.text, 'html.parser')
45
+ page_apps = {
46
+ m.group(1) for link in soup.select('a[href*="/marketplace/"]')
47
+ if (m := re.match(r'^/marketplace/([^/?#]+)$', link.get('href', '')))
48
+ and m.group(1) not in {'actions', 'apps', 'category', 'verified'}
49
+ }
50
+ if not page_apps or not soup.select_one('a[rel="next"]'):
51
+ apps.update(page_apps)
52
+ break
53
+ apps.update(page_apps)
54
+ page += 1
55
+ time.sleep(1)
56
+ return apps
57
+
58
+
59
+ if __name__ == '__main__':
60
+ """Remove JSON files for bots that no longer exist."""
61
+ for filename in os.listdir('.'):
62
+ if filename.endswith('.json'):
63
+ with open(filename) as f:
64
+ data = json.load(f)
65
+ if data.get('name') and not get_app_info(data['name']):
66
+ os.remove(filename)
67
+
68
+ """Save bot info to JSON file."""
69
+ for slug in scrape_marketplace_apps('ai-assisted'):
70
+ if app_info := get_app_info(slug):
71
+ """Save bot info to JSON file."""
72
+ with open(f'{slug}[bot].json', 'w', encoding='utf-8') as f:
73
+ json.dump(app_info, f, indent=4, ensure_ascii=False)
74
+ time.sleep(0.5)