|
|
@@ -0,0 +1,79 @@
|
|
|
+import console
|
|
|
+from mutagen.id3 import ID3, ID3NoHeaderError
|
|
|
+from mutagen.id3 import ID3, TIT2, TALB, TPE1, TPE2, COMM, TCOM, TCON, TDRC, APIC
|
|
|
+
|
|
|
+from sites.helper.request import RawRequest
|
|
|
+from sites.tags import available
|
|
|
+
|
|
|
+
|
|
|
+def search_tags(item):
|
|
|
+ console.output('Searching for id3 tags')
|
|
|
+
|
|
|
+ items = []
|
|
|
+
|
|
|
+ for site in available:
|
|
|
+ console.output('Searching {0}'.format(site.url))
|
|
|
+ _items = site.perform_search('{0}+{1}'.format(item.artist, item.title))
|
|
|
+ console.output('\tFound {0} results'.format(len(_items)))
|
|
|
+
|
|
|
+ items.extend(_items)
|
|
|
+
|
|
|
+ if len(items) == 0:
|
|
|
+ console.output('No matching tags found')
|
|
|
+ return None
|
|
|
+
|
|
|
+ picked_tag = console.option_picker('Pick the most matching tag',
|
|
|
+ items,
|
|
|
+ quit=True,
|
|
|
+ objects=[
|
|
|
+ '__id__',
|
|
|
+ 'x.title',
|
|
|
+ 'x.artist',
|
|
|
+ 'x.album',
|
|
|
+ 'x.label'
|
|
|
+ ],
|
|
|
+ table=[
|
|
|
+ ('ID', 2),
|
|
|
+ ('Title', 50),
|
|
|
+ ('Artist', 40),
|
|
|
+ ('Album', 50),
|
|
|
+ ('Label', 19)
|
|
|
+ ])
|
|
|
+ if picked_tag is not None:
|
|
|
+ return items[picked_tag]
|
|
|
+ else:
|
|
|
+ return None
|
|
|
+
|
|
|
+
|
|
|
+def write_tags_to_file(file, item):
|
|
|
+ try:
|
|
|
+ mp3 = ID3(file.name)
|
|
|
+ except ID3NoHeaderError:
|
|
|
+ mp3 = ID3()
|
|
|
+
|
|
|
+ tagitem = item.tag_item
|
|
|
+
|
|
|
+ mp3['TIT2'] = TIT2(encoding=3, text=tagitem.title)
|
|
|
+ mp3['TALB'] = TALB(encoding=3, text=tagitem.album)
|
|
|
+ mp3['TPE1'] = TPE1(encoding=3, text=tagitem.artist)
|
|
|
+ mp3['COMM'] = COMM(encoding=3, text='LABEL:{0};'.format(tagitem.label))
|
|
|
+ mp3['TCON'] = TCON(encoding=3, text=tagitem.genre)
|
|
|
+
|
|
|
+ mp3['APIC'] = APIC(encoding=3,
|
|
|
+ mime='image/jpeg',
|
|
|
+ type=3,
|
|
|
+ desc='Cover',
|
|
|
+ data=tagitem.cover_image)
|
|
|
+
|
|
|
+ mp3.save(file.name)
|
|
|
+
|
|
|
+
|
|
|
+def download_artwork(tag):
|
|
|
+ console.output('Downloading artwork from {0}'.format(tag.cover_url), level=console.DBG_INFO)
|
|
|
+ cover = RawRequest.get(tag.cover_url)
|
|
|
+
|
|
|
+ if len(cover.content) > 0:
|
|
|
+ tag.set_cover_image(cover.content)
|
|
|
+ return True
|
|
|
+ else:
|
|
|
+ return False
|