-
Notifications
You must be signed in to change notification settings - Fork 10
/
update_gists.py
executable file
·83 lines (67 loc) · 2.71 KB
/
update_gists.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python
# encoding: utf-8
from collections import Counter, OrderedDict
from datetime import datetime
from gist import create_workflow, get_github_token
from github import Github
import itertools
from pprint import pprint as pp
import sys
from workflow import Workflow, web
def create_gist_item(gist):
gist_item = {}
gist_item.update(gist.__dict__['_rawData'])
gist_item['description'] = gist_item.get('description') or ""
gist_item['public'] = gist.public
gist_item['forked'] = len(gist.forks) > 0
gist_item['starred'] = gist.is_starred()
tag_list = set([x.replace("#","") for x in gist_item['description'].split(" ") if x.startswith("#")])
tag_list = filter(len, tag_list)
gist_item['tags'] = list(tag_list)
# Join contents of gist files together for full-text searching.
gist_item['content'] = '\n'.join([x.content for x in gist.files.values() if x.content])
try:
gist_item['language'] = gist_item['files'].values()[0]['language'].replace(" ", "-")
except AttributeError:
gist_item['language'] = ""
return gist_item
def main(wf):
log = wf.logger
# Fetch user token.
gh = Github(login_or_token=get_github_token(wf))
gh_user = gh.get_user()
# Fetch all gists for user.
log.info("Fetching gists...")
gist_set = []
user_gists = list(gh_user.get_gists())
for n, gist in enumerate(user_gists):
log.info(gist)
gist_item = create_gist_item(gist)
gist_set.append(gist_item)
wf.store_data('current_gist', n)
wf.store_data('total_gists', len(user_gists))
# Update cache of gists.
n_starred = len([x for x in gist_set if x['starred']])
n_forked = len([len(x) > 0 for x in gist_set if x['forked']])
n_public = len([x for x in gist_set if x['public']])
n_private = len([x for x in gist_set if not x['public']])
tags = itertools.chain.from_iterable([x['tags'] for x in gist_set])
tag_counts = Counter(tags).most_common()
tag_counts = OrderedDict(tag_counts)
language_counts = Counter([x['language'] for x in gist_set if x['language']]).most_common()
language_counts = OrderedDict(language_counts)
wf.store_data('gists', gist_set)
wf.store_data('n_starred', n_starred)
wf.store_data('n_forked', n_forked)
wf.store_data('n_public', n_public)
wf.store_data('n_private', n_private)
wf.store_data('tag_counts', tag_counts)
wf.store_data('language_counts', language_counts)
# Set last update date/time.
wf.store_data('last_update', datetime.now())
# Reset update status.
wf.store_data('current_gist', None)
wf.store_data('total_gists', None)
if __name__ == '__main__':
wf = create_workflow()
sys.exit(wf.run(main))