-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsync_github_stars.py
60 lines (52 loc) · 1.98 KB
/
sync_github_stars.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
#!/usr/bin/env python
""" Fed up with GitHub stars being essentially useless, I decided to give them
some purpose.
Run this script occasionally (via cron or similar) to keep a local and
up-to-date copy of your starred repos (and even delete repos which
have been unstarred!).
Three variables may or may not need configuring to run this script in your
environment, they are ``github_username``, ``starred_dir`` and ``cleanup``.
Their purpose should be somewhat obvious.
"""
from __future__ import print_function
import getpass
import os
import subprocess
import requests
GITHUB_USERNAME = getpass.getuser()
STARRED_DIR = os.path.expanduser('~/Source/Starred')
DELETE_UNSTARRED = True
def main(user, dest, cleanup):
"""
``user`` - GitHub username
``dest`` - Destination folder for Starred repos
``cleanup`` - Delete repos that have been Unstarred
"""
url = 'https://api.github.com/users/{}/starred'.format(user)
starred_repos = {}
page = 1
while True:
data = requests.get(url, params=dict(per_page=100, page=page)).json()
if not data:
break
for repo in data:
repo_dir = repo['full_name'].replace('/', '_')
starred_repos[repo_dir] = repo['clone_url']
page += 1
for (repo_name, repo_url) in starred_repos.iteritems():
workdir = os.path.join(dest, repo_name)
if os.path.isdir(workdir):
cmd = ['git', '-C', workdir, 'pull']
else:
cmd = ['git', 'clone', repo_url, workdir]
print("Processing {}".format(repo_name))
subprocess.call(cmd)
if cleanup:
for repo in os.listdir(dest):
repo_path = os.path.join(dest, repo)
if not os.path.isdir(repo_path) or repo.startswith('.'):
continue
if repo not in starred_repos:
subprocess.call(['rm', '-r', repo_path])
if __name__ == '__main__':
main(GITHUB_USERNAME, STARRED_DIR, DELETE_UNSTARRED)