-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
74 lines (63 loc) · 2.67 KB
/
main.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
from ulauncher.api.client.Extension import Extension
from ulauncher.api.client.EventListener import EventListener
from ulauncher.api.shared.event import KeywordQueryEvent
from ulauncher.api.shared.item.ExtensionResultItem import ExtensionResultItem
from ulauncher.api.shared.action.RenderResultListAction import RenderResultListAction
from ulauncher.api.shared.action.HideWindowAction import HideWindowAction
from ulauncher.api.shared.action.OpenUrlAction import OpenUrlAction
import requests
import json
import os
import time
import protondb_api
api_url = 'https://api.steampowered.com/ISteamApps/GetAppList/v0002/?format=json'
steamjson = os.path.join(os.path.dirname(__file__), 'steamapi.json')
PDB = 'https://www.protondb.com/app/'
class Extension(Extension):
def __init__(self):
super(Extension, self).__init__()
self.subscribe(KeywordQueryEvent, KeywordQueryEventListener())
class KeywordQueryEventListener(EventListener):
def download_api(self):
"""Download API json file."""
data = requests.get(api_url)
with open(steamjson, 'wb')as file:
file.write(data.content)
def steam_api_check(self):
"""Check if api json file exists or is outdated."""
try:
age = int(time.time() - os.path.getmtime(steamjson))
if age > 43200:
self.download_api()
except FileNotFoundError:
self.download_api()
def on_event(self, event, extension):
"""Query ProtonDB."""
self.steam_api_check()
num = int(extension.preferences['search_results'])
appid = extension.preferences['appid']
keyword = event.get_keyword() or str()
query = event.get_argument() or str()
if len(query.strip()) == 0:
return RenderResultListAction([
ExtensionResultItem(icon='icon.svg',
name='Enter a search term',
on_enter=HideWindowAction())
])
else:
if keyword == appid:
data = protondb_api.get_data_appid(query, num)
else:
data = protondb_api.get_data(query, num)
items = []
if data.strip() != '':
jdata = json.loads(data)
for i in jdata:
items.append(ExtensionResultItem(
icon='icon.svg',
name=f"[{i['pdb']}] {i['name']} ({i['appid']})",
description=f"{PDB}{i['appid']}",
on_enter=OpenUrlAction(f"{PDB}{i['appid']}")))
return RenderResultListAction(items)
if __name__ == '__main__':
Extension().run()