/
CurseForgeBadge.py
251 lines (224 loc) · 12.4 KB
/
CurseForgeBadge.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
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
from flask import Flask
from flask import render_template
from flask import request
import CFReader
from badgeCreator import create_badge
app = Flask(__name__)
@app.route('/')
def landing():
return render_template('index.html')
@app.route('/author/<name>.svg')
@app.route('/author/<name>(<l_colour>).svg')
@app.route('/author/<style>_<name>.svg')
@app.route('/author/<style>_<name>(<l_colour>).svg')
@app.route('/author/<style>_<name>_<suffix>.svg')
@app.route('/author/<style>_<name>_<suffix>(<l_colour>).svg')
@app.route('/author/<style>_<name>_<prefix>_<suffix>.svg')
@app.route('/author/<style>_<name>_<prefix>_<suffix>(<l_colour>).svg')
@app.route('/author/<style>_<name>_<suffix>(<l_colour>-<r_colour>-<text_colour>-<shadow_colour>-<logo_colour>).svg')
@app.route('/author/<style>_<name>_<prefix>_<suffix>(<l_colour>-<r_colour>-<text_colour>-<shadow_colour>-<logo_colour>).svg')
def author(name, style='full', suffix=None, prefix=None, l_colour='E04E14', r_colour='2D2D2D', text_colour='fff',
shadow_colour='010101', logo_colour='1C1C1C'):
template = open_template('curseShield.svg', request.args)
replacement = ''
dls = CFReader.get_downloads_author(name)
if style == 'short':
splitted = dls.split(',')
first_number = splitted[0][0]
padding_zeros = '0' * (len(splitted[0]) - 1)
post_fix = ('M+' if len(splitted) > 2 else ('k+' if len(splitted) > 1 else ''))
replacement += first_number + padding_zeros + post_fix
else:
replacement += dls
if prefix:
replacement = prefix + ' ' + replacement
if suffix:
replacement += ' ' + suffix
replacement = replacement.strip()
width = max(len(replacement) * 7 + 12, 40)
replacement, width = apply_text_transforms(replacement, width, request.args)
return create_badge(template, dls=replacement, width=width, totalWidth=(30 + width),
offset=(30.5 + width / 2), l_colour=l_colour, r_colour=r_colour, text_colour=text_colour,
shadow_colour=shadow_colour, logo_colour=logo_colour), 200, {'Content-Type': 'image/svg+xml'}
@app.route('/title/<project>.svg')
@app.route('/title/<project>(<l_colour>).svg')
@app.route('/title/<project>_<prefix>_<suffix>.svg')
@app.route('/title/<project>(<l_colour>).svg')
@app.route('/title/<project>_<prefix>_<suffix>(<l_colour>).svg')
@app.route('/title/<project>(<l_colour>-<r_colour>-<text_colour>-<shadow_colour>-<logo_colour>).svg')
@app.route('/title/<project>_<prefix>_<suffix>(<l_colour>-<r_colour>-<text_colour>-<shadow_colour>-<logo_colour>).svg')
def title(project, suffix=None, prefix=None, l_colour='E04E14', r_colour='2D2D2D', text_colour='fff',
shadow_colour='010101', logo_colour='1C1C1C'):
template = open_template('curseShield.svg', request.args)
replacement = CFReader.get_title(project)
if prefix:
replacement = prefix + ' ' + replacement
if suffix:
replacement += ' ' + suffix
replacement = replacement.strip()
width = len(replacement) * 7 + 12
replacement, width = apply_text_transforms(replacement, width, request.args)
return create_badge(template, dls=replacement, width=width, totalWidth=(30 + width),
offset=(30.5 + width / 2), l_colour=l_colour, r_colour=r_colour, text_colour=text_colour,
shadow_colour=shadow_colour, logo_colour=logo_colour), 200, {'Content-Type': 'image/svg+xml'}
@app.route('/<project>.svg')
@app.route('/<project>(<l_colour>).svg')
@app.route('/<style>_<project>.svg')
@app.route('/<style>_<project>(<l_colour>).svg')
@app.route('/<style>_<project>_<suffix>.svg')
@app.route('/<style>_<project>_<suffix>(<l_colour>).svg')
@app.route('/<style>_<project>_<prefix>_<suffix>.svg')
@app.route('/<style>_<project>_<prefix>_<suffix>(<l_colour>).svg')
@app.route('/<style>_<project>_<suffix>(<l_colour>-<r_colour>-<text_colour>-<shadow_colour>-<logo_colour>).svg')
@app.route('/<style>_<project>_<prefix>_<suffix>(<l_colour>-<r_colour>-<text_colour>-<shadow_colour>-<logo_colour>).svg')
def downloads(project, style='full', suffix=None, prefix=None, l_colour='E04E14', r_colour='2D2D2D', text_colour='fff',
shadow_colour='010101', logo_colour='1C1C1C'):
template = open_template('curseShield.svg', request.args)
replacement = ''
dls = CFReader.get_downloads(project)
if style == 'short':
splitted = dls.split(',')
first_number = splitted[0][0]
padding_zeros = '0' * (len(splitted[0]) - 1)
post_fix = ('M+' if len(splitted) > 2 else ('k+' if len(splitted) > 1 else ''))
replacement += first_number + padding_zeros + post_fix
else:
replacement += dls
if prefix:
replacement = prefix + ' ' + replacement
if suffix:
replacement += ' ' + suffix
replacement = replacement.strip()
width = max(len(replacement) * 7 + 12, 40)
replacement, width = apply_text_transforms(replacement, width, request.args)
return create_badge(template, dls=replacement, width=width, totalWidth=(30 + width),
offset=(30.5 + width / 2), l_colour=l_colour, r_colour=r_colour, text_colour=text_colour,
shadow_colour=shadow_colour, logo_colour=logo_colour), 200, {'Content-Type': 'image/svg+xml'}
@app.route('/versions/<project>.svg')
@app.route('/versions/<project>(<r_colour>).svg')
@app.route('/versions/<project>_<style>.svg')
@app.route('/versions/<project>_<style>(<r_colour>).svg')
@app.route('/versions/<text>_<project>_<style>.svg')
@app.route('/versions/<text>_<project>_<style>(<r_colour>).svg')
@app.route('/versions/<text>_<project>_<style>(<l_colour>-<r_colour>-<text_colour>-<shadow_colour>).svg')
def supported_versions(project, style='all', text='Available for', l_colour='2D2D2D', r_colour='E04E14',
text_colour='fff', shadow_colour='010101'):
template = open_template('shield.svg', request.args)
versions = CFReader.get_versions(project)
versions_text = versions[0] if style == 'latest' else ' | '.join(str(version) for version in versions)
version_width = max(len(versions_text) * 6, 40)
versions_text, version_width = apply_text_transforms(versions_text, version_width, request.args)
text_width = len(text) * 7 + 4
text, text_width = apply_text_transforms(text, text_width, request.args)
return create_badge(template, versions=versions_text, text=text, widthText=text_width, widthVersions=version_width,
totalWidth=(version_width + text_width), offsetText=(text_width / 2),
offsetVersions=(text_width + version_width / 2), l_colour=l_colour, r_colour=r_colour,
text_colour=text_colour, shadow_colour=shadow_colour), 200, {'Content-Type': 'image/svg+xml'}
@app.route('/packs/<project>.svg')
@app.route('/packs/<project>(<l_colour>).svg')
@app.route('/packs/<style>_<project>.svg')
@app.route('/packs/<style>_<project>(<l_colour>).svg')
@app.route('/packs/<style>_<project>_<before>_<after>.svg')
@app.route('/packs/<style>_<project>_<before>_<after>(<l_colour>).svg')
@app.route('/packs/<style>_<project>_<before>_<after>(<l_colour>-<r_colour>-<text_colour>-<shadow_colour>-<logo_colour>).svg')
def packs(project, style='full', before='included in', after='packs', l_colour='E04E14', r_colour='2D2D2D', text_colour='fff',
shadow_colour='010101', logo_colour='1C1C1C'):
template = open_template('curseShield.svg', request.args)
replacement = before + ' '
packs = CFReader.get_dependents(project, 'included')
if style == 'short':
splitted = packs.split(',')
first_number = splitted[0][0]
padding_zeros = '0' * (len(splitted[0]) - 1)
post_fix = ('M+' if len(splitted) > 2 else ('k+' if len(splitted) > 1 else ''))
replacement += first_number + padding_zeros + post_fix
else:
replacement += packs
if after:
replacement += ' ' + after
width = max(len(replacement) * 7 + 12, 40)
replacement, width = apply_text_transforms(replacement, width, request.args)
return create_badge(template, dls=replacement, width=width, totalWidth=(30 + width),
offset=(30.5 + width / 2), l_colour=l_colour, r_colour=r_colour, text_colour=text_colour,
shadow_colour=shadow_colour, logo_colour=logo_colour), 200, {'Content-Type': 'image/svg+xml'}
@app.route('/mods/<project>.svg')
@app.route('/mods/<project>(<l_colour>).svg')
@app.route('/mods/<style>_<project>.svg')
@app.route('/mods/<style>_<project>(<l_colour>).svg')
@app.route('/mods/<style>_<project>_<before>_<after>.svg')
@app.route('/mods/<style>_<project>_<before>_<after>(<l_colour>).svg')
@app.route('/mods/<style>_<project>_<before>_<after>(<l_colour>-<r_colour>-<text_colour>-<shadow_colour>-<logo_colour>).svg')
def mods(project, style='full', before='required for', after='mods', l_colour='E04E14', r_colour='2D2D2D', text_colour='fff',
shadow_colour='010101', logo_colour='1C1C1C'):
template = open_template('curseShield.svg', request.args)
replacement = before + ' '
packs = CFReader.get_dependents(project, 'required')
if style == 'short':
splitted = packs.split(',')
first_number = splitted[0][0]
padding_zeros = '0' * (len(splitted[0]) - 1)
post_fix = ('M+' if len(splitted) > 2 else ('k+' if len(splitted) > 1 else ''))
replacement += first_number + padding_zeros + post_fix
else:
replacement += packs
if after:
if packs == '1' and after == 'mods':
after = 'mod'
replacement += ' ' + after
width = max(len(replacement) * 7 + 12, 40)
replacement, width = apply_text_transforms(replacement, width, request.args)
return create_badge(template, dls=replacement, width=width, totalWidth=(30 + width),
offset=(30.5 + width / 2), l_colour=l_colour, r_colour=r_colour, text_colour=text_colour,
shadow_colour=shadow_colour, logo_colour=logo_colour), 200, {'Content-Type': 'image/svg+xml'}
@app.route('/supported/<project>.svg')
@app.route('/supported/<project>(<l_colour>).svg')
@app.route('/supported/<style>_<project>.svg')
@app.route('/supported/<style>_<project>(<l_colour>).svg')
@app.route('/supported/<style>_<project>_<before>_<after>.svg')
@app.route('/supported/<style>_<project>_<before>_<after>(<l_colour>).svg')
@app.route('/supported/<style>_<project>_<before>_<after>(<l_colour>-<r_colour>-<text_colour>-<shadow_colour>-<logo_colour>).svg')
def supported(project, style='full', before='supported by', after='mods', l_colour='E04E14', r_colour='2D2D2D', text_colour='fff',
shadow_colour='010101', logo_colour='1C1C1C'):
template = open_template('curseShield.svg', request.args)
replacement = before + ' '
required = CFReader.get_dependents(project, 'required')
optional = CFReader.get_dependents(project, 'optional')
total = "{:,}".format(long(required.replace(",", "")) + long(optional.replace(",", "")))
if style == 'short':
splitted = total.split(',')
first_number = splitted[0][0]
padding_zeros = '0' * (len(splitted[0]) - 1)
post_fix = ('M+' if len(splitted) > 2 else ('k+' if len(splitted) > 1 else ''))
replacement += first_number + padding_zeros + post_fix
else:
replacement += total
if after:
if total == '1' and after == 'mods':
after = 'mod'
replacement += ' ' + after
width = max(len(replacement) * 7 + 12, 40)
replacement, width = apply_text_transforms(replacement, width, request.args)
return create_badge(template, dls=replacement, width=width, totalWidth=(30 + width),
offset=(30.5 + width / 2), l_colour=l_colour, r_colour=r_colour, text_colour=text_colour,
shadow_colour=shadow_colour, logo_colour=logo_colour), 200, {'Content-Type': 'image/svg+xml'}
@app.after_request
def add_header(response):
# Image may be cached up to 3 hours
response.cache_control.max_age = 60 * 60 * 3
# Add ETag
response.add_etag()
return response
def open_template(template, args):
uri = 'templates/'
if args.get('badge_style') == 'for_the_badge':
uri += 'for_the_badge_'
elif args.get('badge_style') == 'flat':
uri += 'flat_'
return app.open_resource(uri + template, 'r').read()
def apply_text_transforms(text, width, args):
if args.get('badge_style') == 'for_the_badge':
text = text.upper()
width *= 1.2
return text, width
if __name__ == '__main__':
app.run(host='0.0.0.0', port='80')