-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.py
64 lines (51 loc) · 1.91 KB
/
github.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
import requests
import re
import base64
import os
import config
with requests.Session() as session:
session.auth = (config.user, config.password)
def get_repos(since=0):
url = 'http://api.github.com/repositories'
data = """{
since: %s
}""" % since
# request = Request(url)
# request.add_header('Authorization', 'token %s' % 'e8265f5a3367135bd923154ab64556c49b1500c9')
response = session.get(url, data=data)
if response.status_code == 403:
print('Problem making request!', response.status_code)
print(response.headers)
matches = re.match(r'<.+?>', response.headers['Link'])
is_next = matches.group(0)[1:-1]
return response.json(), is_next
def get_repo(url):
response = session.get(url)
return response.json()
def get_read_me(url):
# Grabs the readme file associated with the current repository
url += '/readme'
response = session.get(url)
return response.json()
# todo: return array of all commits so we can examine each one
def get_repo_sha(url):
# /repos/:owner/:repo/commits
commits = session.get(url + '/commits').json()
return commits[0]['sha']
def get_file_content(item):
ignore_extensions = ['jpg']
filename, extension = os.path.splitext(item['path'])
if extension in ignore_extensions:
return []
content = session.get(item['url']).json()
lines = content['content'].split('\n')
lines = map(base64.b64decode, lines)
print('path', item['path'])
print('lines', "".join(lines[:5]))
return "".join(lines)
def get_repo_contents(url, sha):
# /repos/:owner/:repo/git/trees/:sha?recursive=1
url += '/git/trees/%s?recursive=1' % sha
# print 'url', url
response = session.get(url)
return response.json()