2021-05-20 20:49:04 -05:00
|
|
|
import json
|
|
|
|
|
|
2022-02-14 23:49:24 -06:00
|
|
|
from typing import Dict, List
|
2022-06-24 04:19:45 -05:00
|
|
|
from flask import Flask, request, Response, abort, send_file, redirect
|
2022-02-14 23:49:24 -06:00
|
|
|
from io import BytesIO
|
|
|
|
|
import requests
|
|
|
|
|
from requests import RequestException
|
2021-02-21 00:30:49 -06:00
|
|
|
|
2021-05-20 20:49:04 -05:00
|
|
|
import musicbrainzngs
|
2022-02-14 23:49:24 -06:00
|
|
|
import api.discogs as discogs
|
|
|
|
|
from secrets import USER_AGENT
|
2021-05-20 20:49:04 -05:00
|
|
|
|
2021-03-20 23:55:33 -05:00
|
|
|
from locale import *
|
2022-02-14 23:49:24 -06:00
|
|
|
|
|
|
|
|
|
2021-03-20 23:55:33 -05:00
|
|
|
locale = getdefaultlocale()[0]
|
|
|
|
|
|
2021-04-17 16:46:34 -05:00
|
|
|
|
2021-02-21 00:30:49 -06:00
|
|
|
app = Flask(__name__)
|
2021-04-19 13:46:23 -05:00
|
|
|
caa_supported_sizes = [250, 500, 1200]
|
2022-02-14 23:49:24 -06:00
|
|
|
dc_artist_cache: Dict[str, dict] = {}
|
2021-02-21 00:30:49 -06:00
|
|
|
|
2021-05-20 20:49:04 -05:00
|
|
|
musicbrainzngs.set_useragent("Zune", "4.8", "https://github.com/yoshiask/PyZuneImageCatalogServer")
|
2022-02-14 23:49:24 -06:00
|
|
|
DEFAULT_HEADERS: Dict[str, str] = {
|
|
|
|
|
"User-Agent": USER_AGENT
|
|
|
|
|
}
|
2021-05-20 20:49:04 -05:00
|
|
|
|
2021-02-21 00:30:49 -06:00
|
|
|
|
2021-05-25 13:30:46 -05:00
|
|
|
import re
|
|
|
|
|
@app.after_request
|
|
|
|
|
def allow_zunestk_cors(response):
|
2021-05-26 10:40:49 -05:00
|
|
|
r = request.origin
|
|
|
|
|
if r is None:
|
|
|
|
|
return response
|
2021-05-25 13:30:46 -05:00
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
2022-02-07 21:20:23 -06:00
|
|
|
@app.route("/")
|
|
|
|
|
def default():
|
|
|
|
|
return Response("Welcome to the Social", mimetype="text/plain")
|
|
|
|
|
|
|
|
|
|
|
2021-03-20 23:55:33 -05:00
|
|
|
@app.route(f"/v3.2/<string:locale>/image/<string:mbid>")
|
|
|
|
|
def get_image(mbid: str, locale: str):
|
2022-02-14 23:49:24 -06:00
|
|
|
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))
|
2022-06-24 04:19:45 -05:00
|
|
|
image_url = f"https://coverartarchive.org/release/{mbid}/front-{width}"
|
2021-03-20 23:55:33 -05:00
|
|
|
|
2021-04-19 13:46:23 -05:00
|
|
|
# Request the image from the API and forward it to the Zune software
|
2022-06-24 04:19:45 -05:00
|
|
|
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)
|
2021-05-20 20:49:04 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2022-06-24 04:19:45 -05:00
|
|
|
app.run(host="127.0.0.4", port=80)
|