This repository has been archived by the owner on Apr 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
github.py
74 lines (60 loc) · 1.54 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
65
66
67
68
69
70
71
72
73
74
"""
Search GitHub repos
"""
from albertv0 import *
from os import path
import requests
import json
__iid__ = 'PythonInterface/v0.1'
__prettyname__ = 'GitHub Repos'
__version__ = '1.0'
__trigger__ = 'gh '
__author__ = 'Angelo Gazzola'
__dependencies__ = []
__icon__ = path.dirname(__file__) + '/icons/GitHub.png'
REQUEST_HEADERS = {
'User-Agent': (
'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko)'
' Chrome/62.0.3202.62 Safari/537.36'
)
}
session = requests.Session()
session.trust_env = False
def to_item(repo):
description = repo["description"]
if description and len(description) > 40:
description = description[:40] + "..."
subtext = "{} ({} issues - {} forks)".format(
description,
repo["open_issues"],
repo["forks_count"]
)
return Item(
id=str(repo['id']),
text=repo['full_name'],
icon=__icon__,
subtext=subtext,
actions=[
UrlAction('View on Github', repo['html_url']),
ClipAction('Copy clone url', repo['clone_url']),
]
)
def search(query):
response = session.get("https://api.github.com/search/repositories",
headers=REQUEST_HEADERS,
params={
"q": query,
}
)
if response.json().get('items'):
repos = sorted(
response.json()['items'],
key=(lambda el: int(el["stargazers_count"]))
)
return [to_item(repo) for repo in repos]
return []
def handleQuery(query):
if query.isTriggered and len(query.string) > 0:
items = search(query.string)
return items
return [Item(icon=__icon__, text='GitHub repos')]