item.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import console
  2. import re
  3. class Item:
  4. def __init__(self, site, title, url, duration=None, size=None, artist=None):
  5. self.site = site
  6. self.title = title
  7. self.artist = artist
  8. self.original_url = url
  9. self.download_url = None
  10. self.url_formatted = False
  11. self.tag_item = None
  12. self.size = size
  13. self.duration = duration
  14. self.bitrate = None
  15. self.bytes = None
  16. self.duration_seconds = None
  17. self.calculate_duration_seconds()
  18. self.calculate_bytes()
  19. self.calculate_bitrate()
  20. def __format__(self, format_spec):
  21. if self[format_spec] is None:
  22. return ''
  23. else:
  24. return self[format_spec]
  25. def format_original_url(self):
  26. if not self.url_formatted:
  27. self.download_url = self.original_url = self.site.format_url(self.original_url)
  28. console.output('Setting original URL to: {0}'.format(self.original_url), console.DBG_INFO)
  29. self.url_formatted = True
  30. def set_download_url(self, url):
  31. console.output('Setting download url to: {0}'.format(url), console.DBG_INFO)
  32. self.download_url = url
  33. def set_bytes(self, bytes):
  34. self.bytes = bytes
  35. def link_tag_item(self, tagitem):
  36. self.tag_item = tagitem
  37. def calculate_duration_seconds(self):
  38. if self.duration is not None:
  39. _split = self.duration.split(':')
  40. minutes = int(_split[0])
  41. seconds = int(_split[1])
  42. self.duration_seconds = (minutes * 60) + seconds
  43. def calculate_bytes(self):
  44. if self.size is not None:
  45. match = re.search('\d+\.?\d+', self.size)
  46. if match is not None:
  47. mbytes = float(match.group())
  48. self.bytes = mbytes * 1048576 # 1024^2 -> MB to B
  49. def calculate_bitrate(self):
  50. if self.duration_seconds is not None and self.bytes is not None:
  51. self.bitrate = self.bytes / self.duration_seconds / 1024 * 8 # bytes / seconds / 1024 * 8