item.py 2.5 KB

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