-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathapi_process.py
More file actions
54 lines (47 loc) · 1.58 KB
/
api_process.py
File metadata and controls
54 lines (47 loc) · 1.58 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
#!/usr/bin/env python3
# Takes the raw GitHub API response from
# https://api.github.com/orgs/LinkedIn/repos?page=1&per_page=100
# and produces a new file with only the keys (and associated values) that are
# used for building the website.
#
# usage: python3 api_process.py
import json
import urllib.request
from datetime import datetime, UTC
REQUIRED_KEYS = {
'description',
'fork',
'forks',
'html_url',
'language',
'name',
'size',
'watchers_count',
}
GITHUB_LINKEDIN_REPO_URL = 'https://api.github.com/orgs/LinkedIn/repos'
GITHUB_LINKEDIN_REPO_URL_QUERY = f'{GITHUB_LINKEDIN_REPO_URL}?page={{page}}&per_page=100'
all_repos = []
page = 1
while True:
with urllib.request.urlopen(GITHUB_LINKEDIN_REPO_URL_QUERY.format(page=page)) as response:
print(f'Fetching {GITHUB_LINKEDIN_REPO_URL} page {page}...')
gh_data = json.loads(response.read().decode('utf-8'))
if not gh_data:
break
all_repos.extend(gh_data)
page += 1
filtered_repos = list()
for repo in all_repos:
filtered_repo = dict()
# Skip forked repos, as they are not directly owned by LinkedIn
if repo.get('fork', False):
continue
for k, v in repo.items():
if k in REQUIRED_KEYS:
filtered_repo[k] = v
filtered_repos.append(filtered_repo)
# Write the data out in the desired format.
with open('js/cached-github-api-response.js', 'w+') as f:
f.write(f'// Generated from {GITHUB_LINKEDIN_REPO_URL} on {datetime.now(UTC).date().isoformat()}\n')
f.write('var cachedGithubApiResponse = ')
json.dump(filtered_repos, f)