83 lines
1.8 KiB
Python
Executable file
83 lines
1.8 KiB
Python
Executable file
import xmltodict as xxml
|
|
import json
|
|
import musicbrainzngs as mm
|
|
import requests
|
|
from wikidata.client import Client
|
|
import pickle
|
|
|
|
from sys import argv
|
|
|
|
|
|
def wikidata(artistSt):
|
|
location = "/home/user/.mpd/artists.pk"
|
|
alls = {}
|
|
try:
|
|
with open(location, "rb") as f:
|
|
alls = pickle.load(f)
|
|
|
|
if aid in alls:
|
|
return alls[artistSt]
|
|
except:
|
|
pass
|
|
|
|
a = mm.search_artists(artistSt)['artist-list'][0]
|
|
aid = a['id']
|
|
r = requests.get('http://musicbrainz.org/ws/2/artist/'+aid+'?inc=url-rels')
|
|
result = xxml.parse(r.text)
|
|
rel = result['metadata']['artist']['relation-list']['relation']
|
|
almost = list(filter(lambda x: x['@type']=="wikidata",rel))[0]
|
|
url = almost['target']['#text']
|
|
wid = url.split('/')[-1]
|
|
|
|
client = Client()
|
|
entity = client.get(wid)
|
|
imgprop = client.get('P18')
|
|
image = entity[imgprop]
|
|
url = image.image_url
|
|
|
|
alls[artistSt] = url
|
|
|
|
with open(location, "wb") as f:
|
|
pickle.dump(alls, f)
|
|
|
|
return url
|
|
|
|
def cover(albumSt):
|
|
location = "/home/user/.mpd/covers.pk"
|
|
alls = {}
|
|
try:
|
|
with open(location, "rb") as f:
|
|
alls = pickle.load(f)
|
|
|
|
if aid in alls:
|
|
return alls[albumSt]
|
|
except:
|
|
pass
|
|
|
|
s = mm.search_releases(albumSt)
|
|
sid = s['release-list'][0]['id']
|
|
imgs = mm.get_image_list(sid)['images']
|
|
|
|
front = list(filter(lambda x: x['front'], imgs))[0]
|
|
url = front['thumbnails']['small']
|
|
|
|
alls[albumSt] = url
|
|
|
|
with open(location, "wb") as f:
|
|
pickle.dump(alls, f)
|
|
|
|
return url
|
|
|
|
|
|
if __name__ == '__main__':
|
|
mm.set_useragent('asd',5)
|
|
|
|
if len(argv) > 3:
|
|
arg = argv[2:].join(" ")
|
|
else:
|
|
arg = argv[2]
|
|
|
|
if argv[1] == "artist":
|
|
print(wikidata(arg))
|
|
elif argv[1] == "cover":
|
|
print(cover(arg))
|