-
-
Save thegreyd/a0e6344e2cc6d54ba28423a53a20dafb to your computer and use it in GitHub Desktop.
Python3 version - Extracting info from github
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# gitabel Python3 version | |
# the world's smallest project management tool | |
# reports relabelling times in github (time in seconds since epoch) | |
# thanks to dr parnin | |
# todo: | |
# - ensure events sorted by time | |
# - add issue id | |
# - add person handle | |
""" | |
You will need to add your authorization token in the code. | |
Here is how you do it. | |
1) In terminal run the following command | |
curl -i -u <your_username> -d '{"scopes": ["repo", "user"], "note": "OpenSciences"}' https://api.github.com/authorizations | |
2) Enter ur password on prompt. You will get a JSON response. | |
In that response there will be a key called "token" . | |
Copy the value for that key and paste it on line marked "token" in the attached source code. | |
3) Run the python file. | |
python gitable.py | |
""" | |
import urllib.request, urllib.error, urllib.parse | |
import json | |
import re,datetime | |
import sys | |
class L(): | |
"Anonymous container" | |
def __init__(i,**fields) : | |
i.override(fields) | |
def override(i,d): i.__dict__.update(d); return i | |
def __repr__(i): | |
d = i.__dict__ | |
name = i.__class__.__name__ | |
return name+'{'+' '.join([':%s %s' % (k,pretty(d[k])) | |
for k in i.show()])+ '}' | |
def show(i): | |
lst = [str(k)+" : "+str(v) for k,v in i.__dict__.items() if v != None] | |
return ',\t'.join(map(str,lst)) | |
def secs(d0): | |
d = datetime.datetime(*list(map(int, re.split('[^\d]', d0)[:-1]))) | |
epoch = datetime.datetime.utcfromtimestamp(0) | |
delta = d - epoch | |
return delta.total_seconds() | |
def dump1(u,issues): | |
token = "your auth_token" # <=== | |
request = urllib.request.Request(u, headers={"Authorization" : "token "+token}) | |
v = urllib.request.urlopen(request).read() | |
w = json.loads(v.decode()) | |
if not w: return False | |
for event in w: | |
issue_id = event['issue']['number'] | |
if not event.get('label'): continue | |
created_at = secs(event['created_at']) | |
action = event['event'] | |
label_name = event['label']['name'] | |
user = event['actor']['login'] | |
milestone = event['issue']['milestone'] | |
if milestone != None : milestone = milestone['title'] | |
eventObj = L(when=created_at, | |
action = action, | |
what = label_name, | |
user = user, | |
milestone = milestone) | |
all_events = issues.get(issue_id) | |
if not all_events: all_events = [] | |
all_events.append(eventObj) | |
issues[issue_id] = all_events | |
return True | |
def dump(u,issues): | |
try: | |
return dump1(u, issues) | |
except Exception as e: | |
print(e) | |
print("Contact TA") | |
return False | |
def launchDump(): | |
page = 1 | |
issues = dict() | |
while(True): | |
doNext = dump('https://api.github.com/repos/opensciences/opensciences.github.io/issues/events?page=' + str(page), issues) | |
print("page "+ str(page)) | |
page += 1 | |
if not doNext : break | |
for issue, events in issues.items(): | |
print("ISSUE " + str(issue)) | |
for event in events: print(event.show()) | |
print('') | |
launchDump() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment