-
Notifications
You must be signed in to change notification settings - Fork 30
/
import_albums.py
67 lines (54 loc) · 1.91 KB
/
import_albums.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
import re
import sys
import urllib
import urllib2
import config
import pymongo
import pprint
from editing import MusicBrainzClient
import cgi
mb = MusicBrainzClient('lukz_bot', 'mb', 'http://mb.muziq.eu')
opener = urllib2.build_opener()
if config.WWW_USER_AGENT:
opener.addheaders = [('User-Agent', config.WWW_USER_AGENT)]
mongo = pymongo.Connection()
db = mongo.mbot
html_escape_table = {
"&": "&",
'"': """,
"'": "'",
">": ">",
"<": "<",
}
def html_escape(text):
"""Produce entities within text."""
return "".join(html_escape_table.get(c,c) for c in text)
for album in db.albums.find({'status': {'imported': False}})[:1]:
artist_key = 'cdbaby:' + album['artist_cdbaby_id']
if 'type' not in album:
album['type'] = 'album'
album_url = 'http://www.cdbaby.com/cd/' + album['_id'].split(':')[1]
print "adding", album_url
if 'artist_mbid' not in album:
artist = db.artists.find_one(artist_key)
if not artist:
artist_url = 'http://www.cdbaby.com/Artist/' + album['artist_cdbaby_id']
mbid = mb.add_artist({'name': album['artist']}, artist_url)
artist = {'_id': artist_key, 'mbid': mbid}
db.artists.save(artist)
print 'added artist', mbid
album['artist_mbid'] = artist['mbid']
#pprint.pprint(album)
edit_note = album_url
mbid = mb.add_release(album, edit_note)
mb.add_url('release', mbid, 78, album_url)
album['mbid'] = mbid
album['status']['imported'] = True
db.albums.save(album)
print 'added release', mbid
#form = album_to_form(album)
#print '<form action="http://musicbrainz.org/release/add" method="post">'
#for name, value in form.iteritems():
# print '<input type="hidden" name="%s" value="%s" />' % (html_escape(name), html_escape(unicode(value)))
#print '<input type="submit" value="Add Release">'
#print '</form>'