forked from pastcompute/hacksa2015
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmusicbrainz.py
executable file
·78 lines (69 loc) · 3.55 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
68
69
70
71
72
73
74
75
76
77
#!/usr/bin/python
import sqlite3
import musicbrainzngs
import json
import urllib
import sys
musicbrainzngs.set_useragent("phaze.space hacksa2015 prototype", "0.1")
conn = sqlite3.connect('charts2.db')
c = conn.cursor()
c.execute('CREATE TABLE IF NOT EXISTS musicbrainz_recording (id integer primary key autoincrement,song_id integer, musicbrainz_recording text)')
c.execute('create table if not exists musicbrainz_failure (song_id integer, last_try timestamp)')
c.execute('create table if not exists acoustic(song_id integer, bpm float,key text,danceable text, danceableConf float,gender text,genderConf float,electronic text,electronicConf float);')
conn.commit()
c.execute('select id,name, artist from song where id not in (select song_id from musicbrainz_recording) and id not in (select song_id from musicbrainz_failure where last_try > strftime(\'%s\',\'now\',\'-7 days\') group by song_id having count(song_id) < 5)')
rows=c.fetchall()
for row in rows:
song_id=row[0]
name=row[1]
artist=row[2]
mq = '"%s" AND artist:"%s"' % (name, artist)
print 'searching musicbrainz for ' + repr(artist)+ ' - ' + repr(name)
result = musicbrainzngs.search_recordings(query=mq, limit=10)
first_mbtrackid = None
for y in result['recording-list']:
#if (result['recording-count']result['recording-count'] > 0:
if 'id' in y:
mbtrackid = y['id'] # <-- matches search results from https://musicbrainz.org/search?query=%22Are+You+Gonna+Go+My+Way%22+AND+artist%3A%22Lenny+Kravitz%22+AND+country%3AAU&type=recording&limit=25&method=advanced
if first_mbtrackid is None:
first_mbtrackid=mbtrackid
rec = musicbrainzngs.get_recording_by_id(id=mbtrackid, includes=["tags"])
url ='http://acousticbrainz.org/'+mbtrackid+'/low-level'
print 'fetching from url: ' +url
jsonurl = urllib.urlopen(url)
if jsonurl.getcode() == 200:
print '- has an acoustid entry'
rawJson = json.loads(jsonurl.read())
data= rawJson[u'lowlevel']
rhythm=rawJson[u'rhythm']
beats=rhythm[u'bpm']
tonal=rawJson[u'tonal']
key=tonal[u'chords_key']
scale=tonal[u'chords_scale']
highUrl='http://acousticbrainz.org/'+mbtrackid+'/high-level'
jsonurl2= urllib.urlopen(highUrl)
rawJson2=json.loads(jsonurl2.read())
data2=rawJson2[u'highlevel']
tmp=data2[u'danceability']
danceable=tmp[u'value']
danceConfidence=tmp[u'probability']
tmp=data2[u'gender']
gender=tmp[u'value']
genderConf=tmp[u'probability']
tmp=data2[u'genre_dortmund']
dortmund=tmp[u'value']
dortmundConf=tmp[u'probability']
tmp=data2[u'genre_electronic']
electronic=tmp[u'value']
electronicConf=tmp[u'probability']
c.execute('insert into acoustic(song_id,bpm,key,danceable,danceableConf,gender,genderConf,electronic,electronicConf) values (?,?,?,?,?,?,?,?,?)',(song_id,beats,key+' '+scale,danceable,danceConfidence,gender,genderConf,electronic,electronicConf))
conn.commit()
break;
if first_mbtrackid is not None:
print ' saving mbid '
c.execute('insert into musicbrainz_recording (song_id,musicbrainz_recording) values (?,?)',(song_id,mbtrackid))
conn.commit()
else:
print ' song not found in musicbrainz '
c.execute('insert into musicbrainz_failure (song_id,last_try) values (?,strftime(\'%s\',\'now\'))',(song_id,))
conn.commit()