-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGitHubData.py
More file actions
60 lines (48 loc) · 1.82 KB
/
GitHubData.py
File metadata and controls
60 lines (48 loc) · 1.82 KB
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
import json
import datetime
import urllib.request as urllib2
import socks
import time
from SocksProxy import SocksiPyHandler
class GitHubData:
def __init__(self, proxy_hostname, proxy_port):
self._proxy_hostname = proxy_hostname
self._proxy_port = proxy_port
self.initConnection()
def initConnection(self):
self._connection = urllib2.build_opener(SocksiPyHandler(socks.PROXY_TYPE_SOCKS5, self._proxy_hostname, self._proxy_port))
def getGitHubDataTry(self, request):
response = self._connection.open(fullurl='https://api.github.com' + request)
headers = response.getheaders()
body = response.read().decode()
status = [item[1] for item in headers if item[0] == 'Status']
if len(status) == 0 or status[0] == '403 Forbidden':
print(headers)
print(body)
print(request)
rate_limit_reset_time = int([item[1] for item in headers if item[0] == 'X-RateLimit-Reset'][0])
print(datetime.datetime.utcfromtimestamp(rate_limit_reset_time))
raise BaseException('403 Forbidden')
if len(status) == 0 or not status[0] == '200 OK':
print(headers)
print(body)
print(request)
raise 'Bad Header Status'
try:
return json.loads(s=body)
except:
print(headers)
print(body)
print(request)
raise 'Cant parse json'
def getGitHubData(self, request):
tries = 30
while tries > 0:
try:
return self.getGitHubDataTry(request)
except:
tries -= 1
time.sleep(10)
print('Try refetch data {}'.format(request))
self.initConnection()
raise BaseException('Cant fetch data')