-
Notifications
You must be signed in to change notification settings - Fork 1
/
github_response_parser.py
65 lines (36 loc) · 1.43 KB
/
github_response_parser.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
from github_search_result_item import GithubSearchResultItem
GH_RESULTS_PER_PAGE = 30
GH_MAX_PAGES = 34
GH_DEFAULT_PAGES_COUNT = 2
class GithubResponseParser:
def __init__(self, response):
self.response = response
def get_search_pages_count(self):
response = self.response.json()
# If total results are less than the total results in one page
# Return 2, since page_numbers starts with 1.
# So it needs to do 1 iteration
total_pages = GH_DEFAULT_PAGES_COUNT
if not response or 'total_count' not in response:
return 0
total_count = response['total_count']
if total_count > GH_RESULTS_PER_PAGE:
total_pages = int(total_count / GH_RESULTS_PER_PAGE) + 1
if total_pages > GH_MAX_PAGES:
return GH_MAX_PAGES
return total_pages
def get_search_results(self):
response = self.response.json()
if 'items' not in response:
return None
return [GithubSearchResultItem(item) for item in response['items']]
def get_page_content(self):
response = self.response.json()
if 'content' not in response:
return None
return response['content']
def get_archived_information(self):
response = self.response.json()
if not response or 'archived' not in response:
return None
return response['archived']