-
Notifications
You must be signed in to change notification settings - Fork 2
/
musicbrainz.py
67 lines (58 loc) · 2.93 KB
/
musicbrainz.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
# -*- coding: utf-8 -*-
import urllib2, xmltodict
from unidecode import unidecode
API_REST_MUSICBRAINZ = "http://musicbrainz.org/ws/2/recording/?query="
def retrieveMBID(songName, artistName):
songQuoted = urllib2.quote(songName.replace('/', ' ').encode('utf8'))
try:
file = urllib2.urlopen(API_REST_MUSICBRAINZ + songQuoted)
xml = file.read()
file.close()
doc = xmltodict.parse(xml)
listOfRecordings = doc['metadata']['recording-list']['recording']
listOfMBIDs = []
MBID_found = False
songName = ''.join(e for e in songName if e.isalnum())
artistName = ''.join(e for e in artistName if e.isalnum())
for record in listOfRecordings:
if type(record) == unicode:
retrieved_artist_name = unidecode(listOfRecordings['artist-credit']['name-credit']['artist']['name']).lower()
retrieved_song_name = unidecode(listOfRecordings['title']).lower()
retrieved_artist_name = ''.join(e for e in retrieved_artist_name if e.isalnum())
retrieved_song_name = ''.join(e for e in retrieved_song_name if e.isalnum())
if (((retrieved_artist_name in artistName.lower) or
(artistName in retrieved_artist_name))
and ((retrieved_song_name in songName) or (songName in retrieved_song_name))):
listOfMBIDs.append(listOfRecordings['@id'])
MBID_found = True
break
else:
currentArtistList = record['artist-credit']['name-credit']
if type(currentArtistList) == type([]):
for currentArtist in currentArtistList:
retrieved_artist_name = unidecode(currentArtist['artist']['name']).lower()
retrieved_song_name = unidecode(record['title']).lower()
retrieved_artist_name = ''.join(e for e in retrieved_artist_name if e.isalnum())
retrieved_song_name = ''.join(e for e in retrieved_song_name if e.isalnum())
if (((retrieved_artist_name in artistName.lower()) or
(artistName.lower() in retrieved_artist_name))
and ((retrieved_song_name in songName.lower()) or (songName.lower() in retrieved_song_name))):
listOfMBIDs.append(record['@id'])
MBID_found = True
break
else:
retrieved_artist_name = unidecode(currentArtistList['artist']['name']).lower()
retrieved_song_name = unidecode(record['title']).lower()
retrieved_artist_name = ''.join(e for e in retrieved_artist_name if e.isalnum())
retrieved_song_name = ''.join(e for e in retrieved_song_name if e.isalnum())
if (((retrieved_artist_name in artistName.lower()) or
(artistName.lower() in retrieved_artist_name))
and ((retrieved_song_name in songName.lower()) or (songName.lower() in retrieved_song_name))):
listOfMBIDs.append(record['@id'])
MBID_found = True
break
if MBID_found:
break
return listOfMBIDs
except:
return []