-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontribution-spy.py
86 lines (65 loc) · 2.44 KB
/
contribution-spy.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
#!/usr/bin/env python
import datetime
import os.path
import time
from random import randint
from HTMLParser import HTMLParser
from requests import get
URL = "https://github.com/users/%s/contributions"
log_file = "contributions.csv"
class CustomHTMLParser(HTMLParser):
def __init__(self):
HTMLParser.__init__(self)
self.rects = []
def handle_starttag(self, tag, attrs):
if tag == "rect":
self.rects.append(attrs)
# noinspection SpellCheckingInspection
def main():
# Get username to check from file
with open("usernames.txt") as f:
username = f.readline()
# If username has length 0, do not run
if len(username) == 0:
return
contribs_prev = None
date_prev = None
while True:
# Get number of contributions
r = get(URL % (username.strip()))
parser = CustomHTMLParser()
parser.feed(r.text)
d = dict(parser.rects[-1])
number = int(d["data-count"])
date = d["data-date"]
# If this is the 1st loop, set the previous contributions to current
if contribs_prev is None or date_prev is None:
contribs_prev = number
date_prev = date
elif date != date_prev:
# The date changed, reset the number of contributions
contribs_prev = 0
date_prev = date
# Get current time and difference of contributions
current_date = datetime.datetime.now()
contribs_diff = number - contribs_prev
# If log file does not exist, create it with headers
if not os.path.isfile(log_file):
# Create log file with headers
with open(log_file, "w") as f:
f.write("curr_time,ghb_date,contribs,diff\n")
# Write to log file if there was a change in contributions
if contribs_diff > 0:
with open(log_file, "a") as f:
f.write(str(current_date) + "," + date + "," + str(number) +
"," + str(contribs_diff) + "\n")
# Save new "previous" number of contributions
contribs_prev = number
# Print change to console
print(str(current_date) + " -> There was a change of: " +
str(contribs_diff) + " contributions!")
# Wait 150-500 seconds at random to simulate superhuman behavior
wait_time = randint(150, 500)
time.sleep(wait_time)
if __name__ == '__main__':
main()