| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- 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', 40),
- ('Label', 29)
- ])
- if picked_tag is not None:
- return items[picked_tag]
- else:
- return None
- def write_tags_to_file(file, item):
- try:
- mp3 = ID3(file)
- mp3.clear()
- 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)
- 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
|