item.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. from helper 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. def __eq__(self, other):
  13. if other is None or other is Empty:
  14. return True
  15. return False
  16. Empty = EmptyAttr()
  17. class Item:
  18. def __init__(self, site):
  19. self.site = site
  20. self.title = Empty
  21. self.artist = Empty
  22. self.original_url = Empty
  23. self.download_url = None
  24. self.url_formatted = False
  25. self.tag_item = None
  26. self.size = Empty
  27. self.duration = Empty
  28. self.bitrate = Empty
  29. self.bytes = None
  30. self.duration_seconds = None
  31. self.score = 0.0
  32. # self.calculate_duration_seconds()
  33. # self.calculate_bytes()
  34. # self.calculate_bitrate()
  35. def format_original_url(self):
  36. if not self.url_formatted:
  37. self.download_url = self.original_url = self.site.format_url(self.original_url)
  38. console.output('Setting original URL to: {0}'.format(self.original_url), console.DBG_INFO)
  39. self.url_formatted = True
  40. def set_title(self, title):
  41. self.title = title
  42. def set_artist(self, artist):
  43. self.artist = artist
  44. def set_original_url(self, url):
  45. self.original_url = url
  46. def set_duration_string(self, duration):
  47. self.duration = duration
  48. self.calculate_duration_seconds()
  49. def set_size_string(self, size):
  50. self.size = size
  51. self.calculate_bytes()
  52. self.calculate_bitrate()
  53. def set_download_url(self, url):
  54. console.output('Setting download url to: {0}'.format(url), console.DBG_INFO)
  55. self.download_url = url
  56. def set_bytes(self, bytes):
  57. self.bytes = bytes
  58. def link_tag_item(self, tagitem):
  59. self.tag_item = tagitem
  60. def calculate_duration_seconds(self):
  61. if self.duration is not Empty:
  62. _split = self.duration.split(':')
  63. minutes = int(_split[0])
  64. seconds = int(_split[1])
  65. self.duration_seconds = (minutes * 60) + seconds
  66. def calculate_bytes(self):
  67. if self.size is not Empty:
  68. match = re.search('\d+\.?\d+', self.size)
  69. if match is not None:
  70. mbytes = float(match.group())
  71. self.bytes = mbytes * 1048576 # 1024^2 -> MB to B
  72. def calculate_bitrate(self):
  73. if self.duration_seconds is not None and self.bytes is not None:
  74. self.bitrate = self.bytes / self.duration_seconds / 1024 * 8 # bytes / seconds / 1024 * 8
  75. def increment_score(self, by=1):
  76. self.score += by
  77. def decrement_score(self, by=1):
  78. self.score -= by