|
| 1 | +""" |
| 2 | +Collects GitHub stats from project in pypi_data.json |
| 3 | +""" |
| 4 | +import json |
| 5 | +import sys |
| 6 | +import os |
| 7 | +from threading import Thread |
| 8 | +from github import Github, UnknownObjectException |
| 9 | + |
| 10 | +# Make Python aware of the awesom_config file |
| 11 | +sys.path.append(os.path.dirname(__file__)) |
| 12 | +from awesome_config import * |
| 13 | + |
| 14 | + |
| 15 | +KEY = "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" |
| 16 | + |
| 17 | + |
| 18 | +def get_gh_topics(name, gh_project, results, counter): |
| 19 | + awesome_tags = [] |
| 20 | + awesome_stars = -1 |
| 21 | + try: |
| 22 | + repo = g.get_repo(gh_project) |
| 23 | + awesome_tags = repo.get_topics() |
| 24 | + awesome_stars = repo.stargazers_count |
| 25 | + print(f'{counter}/{len(pypi_data)} {name}: Tags retrieved: {awesome_tags}') |
| 26 | + except UnknownObjectException: |
| 27 | + pass |
| 28 | + |
| 29 | + results[name] = { |
| 30 | + 'tags': awesome_tags, |
| 31 | + 'stars': awesome_stars |
| 32 | + } |
| 33 | + |
| 34 | + |
| 35 | + |
| 36 | +print(f'Reading pypi data from {PYPI_FILE}') |
| 37 | +with open(PYPI_FILE, 'r') as f: |
| 38 | + pypi_data = json.load(f) |
| 39 | + |
| 40 | +gh_projects={} |
| 41 | + |
| 42 | +for name, project in pypi_data.items(): |
| 43 | + # Collect possible github repository URLS |
| 44 | + from contextlib import suppress |
| 45 | + |
| 46 | + gh_urls = [] |
| 47 | + with suppress(KeyError,TypeError): |
| 48 | + gh_urls.append(project['info']['project_urls']['Github']) |
| 49 | + with suppress(KeyError,TypeError): |
| 50 | + gh_urls.append(project['info']['project_urls']['Code']) |
| 51 | + with suppress(KeyError,TypeError): |
| 52 | + gh_urls.append(project['info']['project_urls']['Source Code']) |
| 53 | + with suppress(KeyError,TypeError): |
| 54 | + gh_urls.append(project['info']['project_urls']['Source']) |
| 55 | + with suppress(KeyError,TypeError): |
| 56 | + gh_urls.append(project['info']['project_urls']['Homepage']) |
| 57 | + with suppress(KeyError,TypeError): |
| 58 | + gh_urls.append(project['info']['home_page']) |
| 59 | + |
| 60 | + |
| 61 | + gh_urls_clean = [] |
| 62 | + for url in gh_urls: |
| 63 | + if url is not None and 'github.com' in url and '/issues' not in url: |
| 64 | + url_clean = url.replace('https://github.com/', '').strip('/') |
| 65 | + gh_urls_clean.append(url_clean) |
| 66 | + |
| 67 | + if gh_urls_clean: |
| 68 | + gh_projects[name] = gh_urls_clean[0] |
| 69 | + |
| 70 | +print(f'Github projects found: {len(gh_projects)}/{len(pypi_data)}') |
| 71 | + |
| 72 | +g = Github(KEY) |
| 73 | +counter = 0 |
| 74 | +results = {} |
| 75 | +threads = {} |
| 76 | + |
| 77 | +for name, project in pypi_data.items(): |
| 78 | + |
| 79 | + awesome_tags = [] |
| 80 | + if name in gh_projects: |
| 81 | + gh_project = gh_projects[name] |
| 82 | + threads[name] = Thread(target=get_gh_topics, args=(name, gh_project, results, counter)) |
| 83 | + threads[name].start() |
| 84 | + counter += 1 |
| 85 | + |
| 86 | +print(f'Received {len(results)} tag-results from GitHub') |
| 87 | + |
| 88 | +for name, project in pypi_data.items(): |
| 89 | + project['awesome_stats']['tags'] = [] |
| 90 | + project['awesome_stats']['stars'] = -1 |
| 91 | + if name in results: |
| 92 | + project['awesome_stats']['tags'] = results[name]['tags'] |
| 93 | + project['awesome_stats']['stars'] = results[name]['stars'] |
| 94 | + |
| 95 | +print(f'Storing data into {GH_JSON_FILE}') |
| 96 | +with open(GH_JSON_FILE, 'w') as f: |
| 97 | + json.dump(pypi_data, f, sort_keys=True, indent=4) |
| 98 | + |
| 99 | +print('Done. Exit now!') |
| 100 | + |
| 101 | +# for repo in g.get_user().get_repos(): |
| 102 | +# print(repo.name) |
0 commit comments