tagging.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. mp3 = ID3(file)
  40. tagitem = item.tag_item
  41. mp3['TIT2'] = TIT2(encoding=3, text=tagitem.title)
  42. mp3['TALB'] = TALB(encoding=3, text=tagitem.album)
  43. mp3['TPE1'] = TPE1(encoding=3, text=tagitem.artist)
  44. mp3['COMM'] = COMM(encoding=3, text='LABEL:{0};'.format(tagitem.label))
  45. mp3['TCON'] = TCON(encoding=3, text=tagitem.genre)
  46. mp3['APIC'] = APIC(encoding=3,
  47. mime='image/jpeg',
  48. type=3,
  49. desc='Cover',
  50. data=tagitem.cover_image)
  51. mp3.save(file)
  52. def download_artwork(tag):
  53. console.output('Downloading artwork from {0}'.format(tag.cover_url), level=console.DBG_INFO)
  54. cover = RawRequest.get(tag.cover_url)
  55. if len(cover.content) > 0:
  56. tag.set_cover_image(cover.content)
  57. return True
  58. else:
  59. return False