[FileItem] Fix mimetype content lookup
[xbmc.git] / addons / metadata.generic.albums / lib / discogs.py
blob448c3547645f57e2d4fce50d36eda3e099b2394b
1 # -*- coding: utf-8 -*-
2 import difflib
4 def discogs_albumfind(data, artist, album):
5 albums = []
6 masters = []
7 # sort results by lowest release id (first version of a release)
8 releases = sorted(data.get('results',[]), key=lambda k: k['id'])
9 for item in releases:
10 masterid = item['master_id']
11 # we are not interested in multiple versions that belong to the same master release
12 if masterid not in masters:
13 masters.append(masterid)
14 albumdata = {}
15 albumdata['artist'] = item['title'].split(' - ',1)[0]
16 albumdata['album'] = item['title'].split(' - ',1)[1]
17 albumdata['artist_description'] = item['title'].split(' - ',1)[0]
18 albumdata['year'] = str(item.get('year', ''))
19 albumdata['label'] = item['label'][0]
20 albumdata['thumb'] = item['thumb']
21 albumdata['dcalbumid'] = item['id']
22 # discogs does not provide relevance, use our own
23 artistmatch = difflib.SequenceMatcher(None, artist.lower(), albumdata['artist'].lower()).ratio()
24 albummatch = difflib.SequenceMatcher(None, album.lower(), albumdata['album'].lower()).ratio()
25 if artistmatch > 0.90 and albummatch > 0.90:
26 score = round(((artistmatch + albummatch) / 2), 2)
27 albumdata['relevance'] = str(score)
28 albums.append(albumdata)
29 return albums
31 def discogs_albummain(data):
32 if data:
33 if 'main_release_url' in data:
34 url = data['main_release_url'].rsplit('/', 1)[1]
35 return url
37 def discogs_albumdetails(data):
38 albumdata = {}
39 albumdata['album'] = data['title']
40 if 'styles' in data:
41 albumdata['styles'] = ' / '.join(data['styles'])
42 albumdata['genres'] = ' / '.join(data['genres'])
43 albumdata['year'] = str(data['year'])
44 albumdata['label'] = data['labels'][0]['name']
45 artists = []
46 for artist in data['artists']:
47 artistdata = {}
48 artistdata['artist'] = artist['name']
49 artists.append(artistdata)
50 albumdata['artist'] = artists
51 albumdata['artist_description'] = data['artists_sort']
52 albumdata['rating'] = str(int((float(data['community']['rating']['average']) * 2) + 0.5))
53 albumdata['votes'] = str(data['community']['rating']['count'])
54 if 'images' in data:
55 thumbs = []
56 for thumb in data['images']:
57 thumbdata = {}
58 thumbdata['image'] = thumb['uri']
59 thumbdata['preview'] = thumb['uri150']
60 # not accurate: discogs can provide any art type, there is no indication if it is an album front cover (thumb)
61 thumbdata['aspect'] = 'thumb'
62 thumbs.append(thumbdata)
63 albumdata['thumb'] = thumbs
64 return albumdata