-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
54 lines (38 loc) · 1.36 KB
/
main.py
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
import requests
import os
import logging
import subprocess
import re
import settings
_GITHUB_BASE_URL = 'https://api.github.com'
_REPO_DIR = settings.get('repo_dir', os.getcwd())
_REPO_CNT = settings.get('repo_count', 10)
_QUERY_STRING = settings.get('repo_query_string')
_TOKEN = settings.get('github_token', required=False)
def main():
logging.warning('Starting')
page_num = 1
visited_repo_cnt = 1
while True:
if visited_repo_cnt > _REPO_CNT:
break
headers = {'Authorization': f'token {_TOKEN}'} if _TOKEN else None
r = requests.get(f'{_GITHUB_BASE_URL}/search/repositories?'
f'q={_QUERY_STRING}&page={page_num}&per_page=1',
headers=headers)
data = r.json()
items = data['items']
for item in items:
url = item['clone_url']
repo_name = re.sub('/', '---', item['full_name'])
args = ['git', 'clone', url, os.path.join(_REPO_DIR, repo_name)]
p = subprocess.Popen(args, stdout=subprocess.PIPE)
p.communicate()
logging.warning(f'Visited repo={repo_name} [{visited_repo_cnt}/{_REPO_CNT}]')
visited_repo_cnt += 1
if visited_repo_cnt > _REPO_CNT:
break
page_num += 1
logging.warning('Done')
if __name__ == '__main__':
main()