import console import re class Item: def __init__(self, site, title, url, duration=None, size=None, artist=None): self.site = site self.title = title self.artist = artist self.original_url = url self.download_url = None self.url_formatted = False self.tag_item = None self.size = size self.duration = duration self.bitrate = None self.bytes = None self.duration_seconds = None self.calculate_duration_seconds() self.calculate_bytes() self.calculate_bitrate() def __format__(self, format_spec): if self[format_spec] is None: return '' else: return self[format_spec] def format_original_url(self): if not self.url_formatted: self.download_url = self.original_url = self.site.format_url(self.original_url) console.output('Setting original URL to: {0}'.format(self.original_url), console.DBG_INFO) self.url_formatted = True def set_download_url(self, url): console.output('Setting download url to: {0}'.format(url), console.DBG_INFO) self.download_url = url def set_bytes(self, bytes): self.bytes = bytes def link_tag_item(self, tagitem): self.tag_item = tagitem def calculate_duration_seconds(self): if self.duration is not None: _split = self.duration.split(':') minutes = int(_split[0]) seconds = int(_split[1]) self.duration_seconds = (minutes * 60) + seconds def calculate_bytes(self): if self.size is not None: match = re.search('\d+\.?\d+', self.size) if match is not None: mbytes = float(match.group()) self.bytes = mbytes * 1048576 # 1024^2 -> MB to B def calculate_bitrate(self): if self.duration_seconds is not None and self.bytes is not None: self.bitrate = self.bytes / self.duration_seconds / 1024 * 8 # bytes / seconds / 1024 * 8