This repository was archived by the owner on May 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapi.py
More file actions
214 lines (155 loc) · 6.1 KB
/
api.py
File metadata and controls
214 lines (155 loc) · 6.1 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
import urllib.request
import flask
from flask import request
import parselist
import requests
import yaml
import config
from apscheduler.schedulers.background import BackgroundScheduler
import v3
app = flask.Flask(__name__)
# app.config["DEBUG"] = True
app.config["JSONIFY_PRETTYPRINT_REGULAR"] = True
# Init sentry
#sentry_sdk.init(
# dsn="https://ba10567a81304d18a0f2304269d3ae23@o456896.ingest.sentry.io/5454934",
# integrations=[FlaskIntegration()],
# traces_sample_rate=1.0
#)
# All Packages:
# /v1/<host>/packages
#
# All Packages in category:
# /v1/<host>/packages/category/<category>
#
# Specific Package:
# /v1/<host>/package/<name>
#
# Hosts:
# /v1/hosts
# Get host list
def get_hosts():
global parsed_hosts
hosts_file = requests.get("https://raw.githubusercontent.com/dhtdht020/oscdl-updateserver/master/v1/announcement"
"/repositories.yml").text
parsed_hosts = yaml.load(hosts_file, Loader=yaml.FullLoader)
print("Loaded hosts list")
get_hosts()
# Schedule hosts list for refresh once per 30 minutes
scheduler = BackgroundScheduler()
scheduler.add_job(func=get_hosts, trigger="interval", minutes=30)
scheduler.start()
def get_hostname(host):
if host == "codemii":
return "hbb2.oscwii.org"
else:
return parsed_hosts["repositories"][host]["host"]
def url(host):
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
urllib.request.install_opener(opener)
try:
test_list_url = f"https://{get_hostname(host)}/hbb/homebrew_browser/listv036.txt"
return test_list_url
except KeyError:
test_list_url = f"https://hbb1.oscwii.org/hbb/homebrew_browser/listv036.txt"
return test_list_url
@app.errorhandler(404)
def page_not_found(e):
return "<h1>Error 404: Opened the shop but there were no apps." \
"</h1><p>That doesn't look like our API. Did you follow the documentation?</p>", 404
@app.route('/', methods=['GET'])
def home():
return "<h1>Open Shop Channel API</h1><p>Welcome to the OSC API Public Beta!\n" \
"For documentation, go to our docs. api-docs.oscwii.org</p>"
# Version 2 (Current)
@app.route('/v2/<host>/packages', methods=['GET'], strict_slashes=False)
def v2_packages(host):
test_list_url = url(host)
query = request.args.get("query")
coder = request.args.get("coder")
category = request.args.get("category")
package = request.args.get("package")
f, headers = urllib.request.urlretrieve(test_list_url)
l = parselist.convert_list_file_to_json(f, get_hostname(host))
parser = parselist.hbbjsonparser()
parser.load_json(l)
# Query
if query and coder and category:
return flask.jsonify(parser.query_packages_category_coder(query, coder, category))
if query and coder:
return flask.jsonify(parser.query_packages_coder(query, coder))
if query and category:
return flask.jsonify(parser.query_packages_category(query, category))
if query:
return flask.jsonify(parser.query_packages(query))
# Coder
if coder and category:
return flask.jsonify(parser.get_developer_category(category, coder))
if coder:
return flask.jsonify(parser.get_developer(coder))
# Basic
if category:
return flask.jsonify(parser.get_category(category))
if package:
return flask.jsonify(parser.dictionary(package))
return flask.jsonify(l)
@app.route('/v2/hosts', methods=['GET'], strict_slashes=False)
def v2_api_hosts():
return flask.jsonify(parsed_hosts)
# Version 1 (For backwards compatiblity, do not change!)
@app.route('/v1', methods=['GET'])
def v1():
return "<h1>Open Shop Channel API</h1><p>Oh boy, version 1.\n" \
"For documentation, go to our docs. api-docs.oscwii.org</p>"
@app.route('/v1/hosts', methods=['GET'], strict_slashes=False)
def api_hosts():
return flask.jsonify(parsed_hosts)
@app.route('/v1/<host>/packages', methods=['GET'], strict_slashes=False)
def all_packages(host):
test_list_url = url(host)
f, headers = urllib.request.urlretrieve(test_list_url)
l = parselist.convert_list_file_to_json(f, get_hostname(host))
parser = parselist.hbbjsonparser()
parser.load_json(l)
return flask.jsonify(l)
@app.route('/v1/<host>/package/<app_name>', methods=['GET'], strict_slashes=False)
def one_package(host, app_name):
test_list_url = url(host)
f, headers = urllib.request.urlretrieve(test_list_url)
l = parselist.convert_list_file_to_json(f, get_hostname(host))
parser = parselist.hbbjsonparser()
parser.load_json(l)
return flask.jsonify(parser.dictionary(app_name))
@app.route('/v1/<host>/category/<category>/packages', methods=['GET'], strict_slashes=False)
def category_packages(host, category):
test_list_url = url(host)
f, headers = urllib.request.urlretrieve(test_list_url)
l = parselist.convert_list_file_to_json(f, get_hostname(host))
parser = parselist.hbbjsonparser()
parser.load_json(l)
return flask.jsonify(parser.get_category(category))
@app.route('/v1/<host>/coder/<coder>/packages', methods=['GET'], strict_slashes=False)
def coder_packages(host, coder):
test_list_url = url(host)
f, headers = urllib.request.urlretrieve(test_list_url)
l = parselist.convert_list_file_to_json(f, get_hostname(host))
parser = parselist.hbbjsonparser()
parser.load_json(l)
return flask.jsonify(parser.get_developer(coder))
@app.route('/v1/<host>/category/<category>/coder/<coder>/packages', methods=['GET'], strict_slashes=False)
def coder_packages_categories(host, category, coder):
test_list_url = url(host)
f, headers = urllib.request.urlretrieve(test_list_url)
l = parselist.convert_list_file_to_json(f, get_hostname(host))
parser = parselist.hbbjsonparser()
parser.load_json(l)
return flask.jsonify(parser.get_developer_category(category, coder))
@app.after_request
def after_request(response):
header = response.headers
header['Access-Control-Allow-Origin'] = '*'
return response
app.register_blueprint(v3.v3, url_prefix="/v3")
if __name__ == '__main__':
app.run(port=config.port)