-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgithub-stars-backup.py
executable file
·64 lines (53 loc) · 1.93 KB
/
github-stars-backup.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
55
56
57
58
59
60
61
62
63
64
#!/usr/bin/env python3
# Description: download Github starred repositories for a user
# Outputs a JSON file containing name, description, url, homepage, language, number of open issues/stars for each starred repository
# A Personal Access Token must be available in the GITHUB_ACCESS_TOKEN environment variable
import os
import sys
import github
import json
import logging
usage = 'USAGE: {} USERNAME OUTPUT_FILE\nGITHUB_ACCESS_TOKEN must be declared in the environment, see https://github.com/settings/tokens'.format(sys.argv[0])
try:
username = sys.argv[1]
except IndexError:
logging.error("No username specified")
print(usage)
exit(1)
if username == "--help":
print(usage)
exit(0)
try:
outfile = sys.argv[2]
except IndexError:
logging.error("No output file specified")
print(usage)
exit(1)
#############################
g = github.Github(os.environ['GITHUB_ACCESS_TOKEN'])
logging.basicConfig(format='[%(levelname)s]: %(message)s',level=logging.INFO)
stars_backup = {}
index = 1
for starred_repo in g.get_user(username).get_starred():
logging.info(starred_repo)
repo = {}
try:
repo_data = g.get_repo(starred_repo.full_name)
repo['name'] = repo_data.full_name
except github.UnknownObjectException:
logging.warning('repo {} does not exist anymore'.format(starred_repo))
continue
except github.GithubException as e:
logging.warning('something went wrong. {}'.format(str(e)))
continue
repo['description'] = repo_data.description
repo['url'] = 'https://github.com/{}'.format(repo_data.full_name)
repo['homepage'] = repo_data.homepage
repo['language'] = repo_data.language
repo['open_issues'] = repo_data.open_issues
repo['stars'] = repo_data.stargazers_count
stars_backup[repo_data.full_name] = repo
index += 1
stars_json = json.dumps(stars_backup,indent=2)
with open(outfile, 'w+') as outfile:
outfile.write(stars_json)