Uh oh!
There was an error while loading. Please reload this page.
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
Latest commit
214 lines (155 loc) · 6.1 KB
/
Copy pathapi.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
importurllib.request
importflask
fromflaskimportrequest
importparselist
importrequests
importyaml
importconfig
fromapscheduler.schedulers.backgroundimportBackgroundScheduler
importv3
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
defget_hosts():
globalparsed_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()
defget_hostname(host):
ifhost=="codemii":
return"hbb2.oscwii.org"
else:
returnparsed_hosts["repositories"][host]["host"]
defurl(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"
returntest_list_url
exceptKeyError:
test_list_url=f"https://hbb1.oscwii.org/hbb/homebrew_browser/listv036.txt"
returntest_list_url
@app.errorhandler(404)
defpage_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'])
defhome():
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)
defv2_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
ifqueryandcoderandcategory:
returnflask.jsonify(parser.query_packages_category_coder(query, coder, category))
ifqueryandcoder:
returnflask.jsonify(parser.query_packages_coder(query, coder))
ifqueryandcategory:
returnflask.jsonify(parser.query_packages_category(query, category))
ifquery:
returnflask.jsonify(parser.query_packages(query))
# Coder
ifcoderandcategory:
returnflask.jsonify(parser.get_developer_category(category, coder))
ifcoder:
returnflask.jsonify(parser.get_developer(coder))
# Basic
ifcategory:
returnflask.jsonify(parser.get_category(category))
ifpackage:
returnflask.jsonify(parser.dictionary(package))
returnflask.jsonify(l)
@app.route('/v2/hosts', methods=['GET'], strict_slashes=False)
defv2_api_hosts():
returnflask.jsonify(parsed_hosts)
# Version 1 (For backwards compatiblity, do not change!)
@app.route('/v1', methods=['GET'])
defv1():
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)
defapi_hosts():
returnflask.jsonify(parsed_hosts)
@app.route('/v1/<host>/packages', methods=['GET'], strict_slashes=False)
defall_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)
returnflask.jsonify(l)
@app.route('/v1/<host>/package/<app_name>', methods=['GET'], strict_slashes=False)
defone_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)
returnflask.jsonify(parser.dictionary(app_name))
@app.route('/v1/<host>/category/<category>/packages', methods=['GET'], strict_slashes=False)
defcategory_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)
returnflask.jsonify(parser.get_category(category))
@app.route('/v1/<host>/coder/<coder>/packages', methods=['GET'], strict_slashes=False)
defcoder_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)
returnflask.jsonify(parser.get_developer(coder))
@app.route('/v1/<host>/category/<category>/coder/<coder>/packages', methods=['GET'], strict_slashes=False)
defcoder_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)
returnflask.jsonify(parser.get_developer_category(category, coder))
@app.after_request
defafter_request(response):
header=response.headers
header['Access-Control-Allow-Origin'] ='*'
returnresponse
app.register_blueprint(v3.v3, url_prefix="/v3")
if__name__=='__main__':
app.run(port=config.port)