-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_search.py
76 lines (68 loc) · 2.2 KB
/
github_search.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
66
67
68
69
70
71
72
73
74
75
76
import argparse
import json
import requests
import logging
# logging.basicConfig(level=logging.DEBUG)
search_url = "https://api.github.com/search/repositories?q={projectname}&sort=stars&order=desc'"
template = {
'uid': 'text',
'title': None,
'arg': None,
'mods': {
'alt': {
'valid': True,
'arg': None,
'subtitle': f'按「Enter键」复制Clone URL到剪贴板',
},
'cmd': {
'valid': True,
'arg': None,
'subtitle': f'按「Enter键」复制SSH URL到剪贴板',
},
}
}
parser = argparse.ArgumentParser()
parser.add_argument("projectname", help="Search a project in GitHub",
type=str)
args = parser.parse_args()
logging.debug(f"args.projectname={args.projectname}")
url = search_url.format(projectname=args.projectname)
logging.debug(f"url={url}")
response = requests.get(url)
assert response.status_code == 200
json_response = json.loads(response.text)
assert "total_count" in json_response
total_count = json_response["total_count"]
logging.debug(f"total_count={total_count}")
assert "items" in json_response
items = json_response["items"]
output_count = total_count if total_count < 10 else 10
logging.debug(f"output_count={output_count}")
output = {"items": list()}
for index in range(output_count):
full_name = items[index]["full_name"]
html_url = items[index]["html_url"]
description = items[index]["description"]
language = items[index]["language"]
stargazers_count = items[index]["stargazers_count"]
ssh_url = items[index]["ssh_url"]
clone_url = items[index]["clone_url"]
item = dict()
item["uid"] = str(index)
item["title"] = full_name
item["subtitle"] = f"{language}/{stargazers_count}🌟/{description}"
item["arg"] = html_url
item["mods"] = {
'alt': {
'valid': True,
'arg': clone_url,
'subtitle': f'Press "Enter" to copy the Clone URL to the clipboard',
},
'cmd': {
'valid': True,
'arg': ssh_url,
'subtitle': f'Press "Enter" to copy the SSH URL to the clipboard',
},
}
output["items"].append(item)
print(json.dumps(output))