1 # -*- coding: utf-8 -*-
4 def discogs_albumfind(data
, artist
, album
):
7 # sort results by lowest release id (first version of a release)
8 releases
= sorted(data
.get('results',[]), key
=lambda k
: k
['id'])
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
)
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
)
31 def discogs_albummain(data
):
33 if 'main_release_url' in data
:
34 url
= data
['main_release_url'].rsplit('/', 1)[1]
37 def discogs_albumdetails(data
):
39 albumdata
['album'] = data
['title']
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']
46 for artist
in data
['artists']:
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'])
56 for thumb
in data
['images']:
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