-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathgithub_search.py
More file actions
148 lines (124 loc) · 5.38 KB
/
github_search.py
File metadata and controls
148 lines (124 loc) · 5.38 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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
"""
This file retrieves Github usernames from the Github API
"""
import argparse
from pathlib import Path
from datetime import datetime
import os
import pandas as pd
from fastcore.foundation import L
from ghapi.all import GhApi, pages
class Service:
"""
Common variables used in functions bundled in Service class.
"""
def __init__(self, api: GhApi):
self.api = api
self.api_service = "github.com"
self.current_date = datetime.today().strftime('%Y-%m-%d')
self.columns = ["service", "date", "user_id"]
def get_complete_query_result(query, query_type, service: Service):
"""Executes a search query and returns the result
Args:
query (string): The query to be executed
query_type (string): Either SEARCH_REPOS to search repositories or
SEARCH_USERS to search users
service (Service): Service object with API connection and metadata vars
Returns:
DataFrame: A dataframe with the results of the query
"""
# do first request to store last page variable in api object
if query_type == "SEARCH_REPOS":
service.api.search.repos(query, per_page=100)
if service.api.last_page() > 0:
query_result = pages(service.api.search.repos,
service.api.last_page(), query)
else:
# if there is only one page, last_page() will return 0.
# This will return nothing, so we need to use 1
query_result = pages(service.api.search.repos, 1, query)
elif query_type == "SEARCH_USERS":
service.api.search.users(query, per_page=100)
if service.api.last_page() > 0:
query_result = pages(service.api.search.users,
service.api.last_page(), query)
else:
# if there is only one page, last_page() will return 0.
# This will return nothing, so we need to use 1
query_result = pages(service.api.search.users, 1, query)
result = L()
for page in query_result:
result.extend(page["items"])
return result
def get_users_from_repos(repos, service: Service):
"""Retrieves the user from repositories data.
Args:
repos (L): Repositories retrieved from Github API
service (Service): Service object with API connection and metadata vars
Returns:
L: list of users
"""
result = L()
for repo in repos:
result.append(
[service.api_service, service.current_date, repo["owner"]["login"]])
return result
def get_users_from_users(users, service: Service):
"""Retrieves the user from users data.
Args:
users (L): Users retrieved from Github API
service (Service): Service object with API connection and metadata vars
Returns:
L: list of users
"""
result = L()
for user in users:
result.append(
[service.api_service, service.current_date, user["login"]])
return result
if __name__ == '__main__':
serv = Service(GhApi(token=os.getenv('GITHUB_TOKEN')))
# Initiate the parser
parser = argparse.ArgumentParser()
# Add arguments to be parsed
parser.add_argument("--topic", "-t", help="set topic search string")
parser.add_argument("--search", "-s", help="set general search string")
# Read arguments from the command line
args = parser.parse_args()
try:
if args.topic:
print(f"Searching topics for {args.topic}...")
topic_repos = get_complete_query_result(f"topic:{args.topic}",
"SEARCH_REPOS", serv)
ids_topic_repos = get_users_from_repos(topic_repos, serv)
pd.DataFrame(ids_topic_repos, columns=serv.columns).to_csv(Path(
"results", "github_search_topic.csv"), index=False)
print("Searching topics done")
else:
print(
"No topic argument provided. If you want to retrieve topics, "
"please provide the argument as --topic")
if args.search:
print(f"Searching repos for {args.search}...")
search_repos = get_complete_query_result(args.search,
"SEARCH_REPOS", serv)
ids_search_repos = get_users_from_repos(search_repos, serv)
pd.DataFrame(ids_search_repos, columns=serv.columns).to_csv(Path(
"results", "github_search_repos.csv"), index=False)
print("Searching repos done")
print(f"Searching users for {args.search}...")
search_users = get_complete_query_result(args.search,
"SEARCH_USERS", serv)
ids_search_users = get_users_from_users(search_users, serv)
pd.DataFrame(ids_search_users, columns=serv.columns).to_csv(Path(
"results", "github_search_users.csv"), index=False)
print("Searching users done")
else:
print(
"No search argument provided. If you want to retrieve general results and "
"users, please provide the argument as --search")
except Exception as e: # pylint: disable=broad-except
print(f"Error occured: {e}")
if "403" in str(e):
print("A HTTP Error 403 indicates that rate limits are reached. "
"Please try again in a few minutes.")