-
Notifications
You must be signed in to change notification settings - Fork 0
/
gitfeed.py
319 lines (267 loc) · 9.91 KB
/
gitfeed.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env python
# Background color for labels
from colorama import Fore, Back, Style, init
from json import loads
from sys import argv, version_info
from datetime import datetime
import requests
import argparse
import time
import os.path
try:
import configparser
except:
from six.moves import configparser
def getArgs(argv=None):
file_path = setConfigurationFiles()
conf = configparser.SafeConfigParser()
conf.read(file_path)
user = conf.get('GitHub Newsfeed', 'user')
max_page = conf.get('GitHub Newsfeed', 'max_page')
quiet = conf.get('GitHub Newsfeed', 'quiet')
no_time_stamp = conf.get('GitHub Newsfeed', 'no_time_stamp')
no_style = conf.get('GitHub Newsfeed', 'no_style')
parser = argparse.ArgumentParser(description='Check your GitHub Newsfeed via the command-line.',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('-u', '--user', default=user,
help='GitHub username for the user to fetch newsfeed for')
parser.add_argument('-p', '--pages', default=max_page,
help='number of newsfeed pages to fetch')
parser.add_argument('-q', '--quiet', default=False,
help='hide comment body in issues & PRs', action='store_true')
parser.add_argument('-nt', '--no-time-stamp', default=False,
help='hide time-stamp of events', action='store_true')
parser.add_argument('-ns', '--no-style', default=False,
help='show plain white text with no colors or style', action='store_true')
return parser.parse_args(argv)
def setConfigurationFiles():
conf = configparser.SafeConfigParser()
home = os.path.expanduser('~')
folder_name = '.gitfeed'
folder_path = os.path.join(home, folder_name)
if not os.path.exists(folder_path):
os.makedirs(folder_path)
file_name = 'gitfeed.ini'
file_path = os.path.join(home, folder_name, file_name)
if not os.path.isfile(file_path):
if version_info > (3,0):
user = input("What's your GitHub username? ")
else:
user = raw_input("What's your GitHub username? ")
print('Writing configuration to ' + file_path)
conf.add_section('GitHub Newsfeed')
conf.set('GitHub Newsfeed', 'user', user)
conf.set('GitHub Newsfeed', 'max_page', '1')
conf.set('GitHub Newsfeed', 'quiet', 'False')
conf.set('GitHub Newsfeed', 'no_time_stamp', 'False')
conf.set('GitHub Newsfeed', 'no_style', 'False')
with open(file_path, 'w') as configfile:
conf.write(configfile)
print('')
return file_path
def removeColor():
Fore.GREEN = ''
Fore.CYAN = ''
Fore.RED = ''
Fore.YELLOW = ''
Fore.MAGENTA = ''
Fore.BLUE = ''
Fore.WHITE = ''
Style.BRIGHT = ''
Back.BLUE = ''
return
def fixEncoding(query):
if version_info > (3, 0):
return query
else:
return query.encode('utf-8')
# review PR
def PRReviewEvent(item, quiet):
user = item['actor']['login']
repo = item['repo']['name']
#commit = item['payload']['comment']['commit_id']
#link = item['payload']['pull_request']['html_url']
#title = item['payload']['pull_request']['title']
number = item['payload']['pull_request']['number']
body = item['payload']['comment']['body']
print(fixEncoding(Fore.CYAN + Style.BRIGHT + '{} reviewed pull request {} on {}'.format(user, number, repo)))
if not quiet:
print(fixEncoding(body))
# open PR, close PR
def PREvent(item, quiet):
user = item['actor']['login']
repo = item['repo']['name']
#link = item['payload']['pull_request']['html_url']
state = item['payload']['pull_request']['state']
number = item['payload']['pull_request']['number']
title = item['payload']['pull_request']['title']
if state == 'open':
print(fixEncoding(Fore.CYAN + '{} opened pull request {} on {}'.format(user, number, repo)))
print(fixEncoding(Style.BRIGHT + fixEncoding(title)))
body = item['payload']['pull_request']['body']
if not quiet:
print(fixEncoding(body))
else:
print(fixEncoding(Fore.CYAN + '{} closed pull request {} on {}'.format(user, number, repo)))
print(fixEncoding(Style.BRIGHT + title))
# comment on issue, PR
def issueCommentEvent(item, quiet):
user = item['actor']['login']
repo = item['repo']['name']
#link = item['payload']['issue']['html_url']
#labels = item['payload']['issue']['labels'] # FIX_ME
#for x in labels:
# print(x['name'])
#state = item['payload']['action']
number = item['payload']['issue']['number']
#title = item['payload']['issue']['title']
try:
if item['payload']['issue']['pull_request']:
group = 'pull request'
except:
group = 'issue'
print(fixEncoding(Fore.CYAN + Style.BRIGHT + '{} commented on {} {} on {}'.format(user, group, number, repo)))
if not quiet:
body = item['payload']['comment']['body']
print(fixEncoding(body))
# open issue, close issue
def issuesEvent(item):
user = item['actor']['login']
repo = item['repo']['name']
#link = item['payload']['issue']['html_url']
state = item['payload']['action']
number = item['payload']['issue']['number']
print(fixEncoding(Fore.RED + Style.BRIGHT + '{} {} issue {} on {}'.format(user, state, number, repo)))
title = item['payload']['issue']['title']
print(fixEncoding(Style.BRIGHT + title))
# comment on a commit
def commitCommentEvent(item, quiet):
user = item['actor']['login']
repo = item['repo']['name']
#link = item['payload']['issue']['html_url']
body = item['payload']['comment']['body']
print(fixEncoding(Fore.CYAN + Style.BRIGHT + '{} commented on {}'.format(user, repo)))
if not quiet:
print(fixEncoding(body))
# starred by following
def watchEvent(item):
user = item['actor']['login']
repo = item['repo']['name']
#link = 'https://github.com/' + item['repo']['name']
print(fixEncoding(Fore.YELLOW + '{} starred {}'.format(user, repo)))
# forked by following
def forkEvent(item):
user = item['actor']['login']
repo = item['repo']['name']
#link = 'https://github.com/' + item['repo']['name']
print(fixEncoding(Fore.GREEN + '{} forked {}'.format(user, repo)))
# delete branch
def deleteEvent(item):
user = item['actor']['login']
repo = item['repo']['name']
#link = 'https://github.com/' + item['repo']['name']
branch = item['payload']['ref']
print(fixEncoding(Fore.RED + '{} deleted branch {} at {}'.format(user, branch, repo)))
# push commits
def pushEvent(item):
user = item['actor']['login']
size = item['payload']['size']
repo = item['repo']['name']
branch = item['payload']['ref'].split('/')[-1]
#link = 'https://github.com/' + item['repo']['name']
print(fixEncoding(Fore.BLUE + '{} pushed {} new commit(s) to {} at {}'.format(user, size, branch, repo)))
# create repo, branch
def createEvent(item):
user = item['actor']['login']
repo = item['repo']['name']
group = item['payload']['ref_type']
#link = 'https://github.com/' + item['repo']['name']
if group == "repository":
print(fixEncoding(Fore.MAGENTA + Style.BRIGHT + '{} created {} {}'.format(user, group, repo)))
else:
branch = item['payload']['ref']
print(fixEncoding(Fore.MAGENTA + Style.BRIGHT + '{} created {} {} at {}'.format(user, group, branch, repo)))
# make public repo
def publicEvent(item):
user = item['actor']['login']
repo = item['repo']['name']
#link = 'https://github.com/' + item['repo']['name']
print(fixEncoding(Fore.MAGENTA + '{} made {} public'.format(user, repo)))
# add collab
def memberEvent(item):
user = item['actor']['login']
action = item['payload']['action']
collab = item['payload']['member']['login']
repo = item['repo']['name']
#link = 'https://github.com/' + item['repo']['name']
print(fixEncoding(Fore.MAGENTA + '{} {} {} as a collaborator to {}'.format(user, action, collab, repo)))
def getTimeDifference(created_at):
created_at = time.strptime(created_at, '%Y-%m-%dT%H:%M:%SZ')
created_at = time.mktime(created_at)
current_time = datetime.utcnow().replace(microsecond=0)
current_time = current_time.isoformat() + 'Z'
current_time = time.strptime(current_time, '%Y-%m-%dT%H:%M:%SZ')
current_time = time.mktime(current_time)
difference = current_time - created_at
days = ('day', int(int(difference) / 86400))
hours = ('hour', int(int(difference) / 3600 % 24))
minutes = ('minute', int(int(difference) / 60 % 60))
seconds = ('second', int(int(difference) % 60))
human_readable = (seconds, minutes, hours, days)
for item in human_readable:
if not item[1] == 0:
if item [1] == 1:
statement = '{} {} ago'.format(item[1], item[0])
else:
statement = '{} {}s ago'.format(item[1], item[0])
return statement
def getPage(user, page, quiet, nt):
url = 'https://api.github.com/users/' + user +'/received_events?page='
response = loads(requests.get(url + str(page)).text)
for item in reversed(response):
if not nt:
created_at = item['created_at']
difference = getTimeDifference(created_at)
#print(Fore.WHITE + Style.NORMAL + Back.BLUE + difference)
print(Fore.WHITE + Back.BLUE + difference)
event = item['type']
if event == "PullRequestReviewCommentEvent": # review PR
PRReviewEvent(item, quiet)
elif event == "PullRequestEvent": # open PR, close PR
PREvent(item, quiet)
elif event == "IssueCommentEvent": # comment on issue/PR
issueCommentEvent(item, quiet)
elif event == "IssuesEvent": # open issue, close issue
issuesEvent(item)
elif event == "CommitCommentEvent":
commitCommentEvent(item, quiet)
elif event == "WatchEvent": # starred
watchEvent(item)
elif event == "ForkEvent": # fork
forkEvent(item)
elif event == "DeleteEvent": # delete branch
deleteEvent(item)
elif event == "PushEvent": # push commits
pushEvent(item)
elif event == "CreateEvent": # make new repo, branch
createEvent(item)
elif event == "PublicEvent": # make repo public
publicEvent(item)
elif event == "MemberEvent": # add collab
memberEvent(item)
print('')
def getPages(user, max_page, quiet, nt):
for page in range(max_page, 0, -1):
getPage(user, page, quiet, nt)
def cli():
init(autoreset=True)
args = getArgs()
user = args.user
max_page = int(args.pages)
quiet = args.quiet
nt = args.no_time_stamp
if args.no_style:
removeColor()
getPages(user, max_page, quiet, nt)
if __name__ == '__main__':
cli()