-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag.py
69 lines (51 loc) · 2.13 KB
/
tag.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
import acoustid
import musicbrainzngs
import eyed3
import json
import time
from os import listdir, path, rename
from os.path import isfile, join, dirname
"""
Fingerprint search with https://acoustid.org
Album info search with https://musicbrainz.org/
"""
BASE_DIR = dirname(dirname(__file__))
INBOX_DIR = path.join(BASE_DIR, 'in')
STORE_DIR = path.join(BASE_DIR, 'store')
COVER_DIR = path.join(BASE_DIR, 'covers')
APIKEY = "******"
BRAINZ_USER = "******"
BRAINZ_PASS = "******"
def tag_file(song_file):
musicbrainzngs.auth(BRAINZ_USER, BRAINZ_PASS)
musicbrainzngs.set_useragent("Auto tag script", "0.1", "http://localhost")
#song = path.join(MP3_DIR, '3.mp3')
print song_file
for score, recording_id, title, artist in acoustid.match(APIKEY, song_file):
# Get song data
result_data = musicbrainzngs.get_recording_by_id(recording_id, includes=['artists','releases'])
title = result_data['recording']['title']
artist = result_data['recording']['artist-credit-phrase']
print "%s - %s" % (title, artist)
# Set ID3 tags
audiofile = eyed3.load(song_file)
audiofile.tag.artist = unicode(artist)
audiofile.tag.title = unicode(title)
# Get Cover Art
if result_data['recording']['release-count']:
try:
imagedata = musicbrainzngs.get_image_front(result_data['recording']['release-list'][0]['id'])
print audiofile.tag.images.set(eyed3.id3.frames.ImageFrame.FRONT_COVER, imagedata, 'image/jpeg')
cover = open(path.join(COVER_DIR, result_data['recording']['release-list'][0]['title'] + '.jpg'), "w+")
cover.write(imagedata)
print "---"
except musicbrainzngs.musicbrainz.ResponseError:
pass
audiofile.tag.save()
if __name__== "__main__":
onlyfiles = [ f for f in listdir(INBOX_DIR) if isfile(join(INBOX_DIR,f)) ]
for f in onlyfiles:
if path.splitext(f)[1] == ".mp3":
print f
tag_file(path.join(INBOX_DIR, f))
rename(path.join(INBOX_DIR,f), path.join(STORE_DIR, "%s.mp3" % time.time()))