This repository has been archived by the owner on Apr 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
89 lines (66 loc) · 2.9 KB
/
app.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
import json
from typing import Dict, List
from flask import Flask, request, Response, abort, send_file, redirect
from io import BytesIO
import requests
from requests import RequestException
import musicbrainzngs
import api.discogs as discogs
from secrets import USER_AGENT
from locale import *
locale = getdefaultlocale()[0]
app = Flask(__name__)
caa_supported_sizes = [250, 500, 1200]
dc_artist_cache: Dict[str, dict] = {}
musicbrainzngs.set_useragent("Zune", "4.8", "https://github.com/yoshiask/PyZuneImageCatalogServer")
DEFAULT_HEADERS: Dict[str, str] = {
"User-Agent": USER_AGENT
}
import re
@app.after_request
def allow_zunestk_cors(response):
r = request.origin
if r is None:
return response
if re.match(r"https?://(127\.0\.0\.(?:\d*)|localhost(?:\:\d+)?|(?:\w*\.)*zunes\.tk)", r):
response.headers.add('Access-Control-Allow-Origin', r)
response.headers.add('Access-Control-Allow-Credentials', 'true')
response.headers.add('Access-Control-Allow-Headers', 'Content-Type')
response.headers.add('Access-Control-Allow-Headers', 'Cache-Control')
response.headers.add('Access-Control-Allow-Headers', 'X-Requested-With')
response.headers.add('Access-Control-Allow-Headers', 'Authorization')
response.headers.add('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, DELETE')
return response
@app.route("/")
def default():
return Response("Welcome to the Social", mimetype="text/plain")
@app.route(f"/v3.2/<string:locale>/image/<string:mbid>")
def get_image(mbid: str, locale: str):
image_url: str = ""
if mbid.endswith('0' * 12):
dcid: int = int(mbid[:8], 16)
img_idx: int = int(mbid[9:13], 16)
# Get or update cached artist
dc_artist: dict = dc_artist_cache.get(dcid)
if dc_artist is None:
# Artist not in cache
dc_artist = discogs.get_artist_from_dcid(dcid)
dc_artist_cache[dcid] = dc_artist
# Get URL for requested image
image_url = dc_artist["images"][img_idx]["uri"]
else:
# The Cover Art Archive API supports sizes of 250, 500, and 1200
requested_width = request.args.get("width", default=500, type=int)
width = min(caa_supported_sizes, key=lambda x: abs(x - requested_width))
image_url = f"https://coverartarchive.org/release/{mbid}/front-{width}"
# Request the image from the API and forward it to the Zune software
return redirect(image_url, 302)
@app.route(f"/v3.2/<string:locale>/music/artist/<string:mbid>/<string:type>")
def get_artist_image(mbid: str, type: str, locale: str):
dc_artist, mb_artist = discogs.get_artist_from_mbid(mbid)
# Get URL for requested image
image_url: str = dc_artist["images"][0]["uri"]
# Request the image from the API and forward it to the Zune software
return redirect(image_url, 302)
if __name__ == "__main__":
app.run(host="127.0.0.4", port=80)