tagging.py 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import console
  2. from mutagen.id3 import ID3, ID3NoHeaderError
  3. from mutagen.id3 import ID3, TIT2, TALB, TPE1, TPE2, COMM, TCOM, TCON, TDRC, APIC
  4. from sites.helper.request import RawRequest
  5. from sites.tags import available
  6. def search_tags(item):
  7. console.output('Searching for id3 tags')
  8. items = []
  9. for site in available:
  10. console.output('Searching {0}'.format(site.url))
  11. _items = site.perform_search('{0}+{1}'.format(item.artist, item.title))
  12. console.output('\tFound {0} results'.format(len(_items)))
  13. items.extend(_items)
  14. if len(items) == 0:
  15. console.output('No matching tags found')
  16. return None
  17. picked_tag = console.option_picker('Pick the most matching tag',
  18. items,
  19. quit=True,
  20. objects=[
  21. '__id__',
  22. 'x.title',
  23. 'x.artist',
  24. 'x.album',
  25. 'x.label'
  26. ],
  27. table=[
  28. ('ID', 2),
  29. ('Title', 50),
  30. ('Artist', 40),
  31. ('Album', 40),
  32. ('Label', 29)
  33. ])
  34. if picked_tag is not None:
  35. return items[picked_tag]
  36. else:
  37. return None
  38. def write_tags_to_file(file, item):
  39. try:
  40. mp3 = ID3(file)
  41. mp3.clear()
  42. except ID3NoHeaderError:
  43. mp3 = ID3()
  44. tagitem = item.tag_item
  45. mp3['TIT2'] = TIT2(encoding=3, text=tagitem.title)
  46. mp3['TALB'] = TALB(encoding=3, text=tagitem.album)
  47. mp3['TPE1'] = TPE1(encoding=3, text=tagitem.artist)
  48. mp3['COMM'] = COMM(encoding=3, text='LABEL:{0};'.format(tagitem.label))
  49. mp3['TCON'] = TCON(encoding=3, text=tagitem.genre)
  50. mp3['APIC'] = APIC(encoding=3,
  51. mime='image/jpeg',
  52. type=3,
  53. desc='Cover',
  54. data=tagitem.cover_image)
  55. mp3.save(file)
  56. def download_artwork(tag):
  57. console.output('Downloading artwork from {0}'.format(tag.cover_url), level=console.DBG_INFO)
  58. cover = RawRequest.get(tag.cover_url)
  59. if len(cover.content) > 0:
  60. tag.set_cover_image(cover.content)
  61. return True
  62. else:
  63. return False