Bläddra i källkod

Rewrite Item class to enable Empty object

Deben Oldert 6 år sedan
förälder
incheckning
5309522108
4 ändrade filer med 64 tillägg och 28 borttagningar
  1. 2 1
      console.py
  2. 3 2
      sites/default.py
  3. 50 18
      sites/helper/item.py
  4. 9 7
      sites/helper/structure.py

+ 2 - 1
console.py

@@ -68,7 +68,8 @@ def option_picker(question, options, objects=None, selected=None, quit=False, ta
                 _row += '[{{{name}:{size}.{size}}}]'.format(name=str(objects[j]), size=str(table[j][1]))
                 # _row += '[{' + str(objects[j]) + ':' + str(table[j][1]) + '.' + str(table[j][1]) + '}] '
 
-            output(_row.format(__id__=str(i), x=option).strip())
+            output(_row.format(__id__=str(i), x=option))
+            # output(_row.format(__id__=str(i), x=dict(map(lambda s: s if s is not None else '', option.__dict__.items()))).strip())
 
         else:
             if i == selected:

+ 3 - 2
sites/default.py

@@ -2,6 +2,7 @@ import console
 from sites.helper.request import Request
 from sites.helper.query import Query
 from sites.helper.structure import Structure
+from sites.helper.item import Empty
 
 import sites.helper.download as Download
 
@@ -33,12 +34,12 @@ class DefaultSite:
 
     def format_duration(self, field):
         if field is None:
-            return field
+            return Empty
         return field.text()
 
     def format_size(self, field):
         if field is None:
-            return field
+            return Empty
         return field.text()
 
     def format_url(self, field):

+ 50 - 18
sites/helper/item.py

@@ -2,34 +2,45 @@ import console
 import re
 
 
+class EmptyAttr:
+    def __str__(self):
+        return ''
+
+    def __format__(self, format_spec):
+        return ''.__format__(format_spec)
+
+    def __getattr__(self, item):
+        return None
+
+    def __getitem__(self, item):
+        return None
+
+
+Empty = EmptyAttr()
+
+
 class Item:
-    def __init__(self, site, title, url, duration=None, size=None, artist=None):
+    def __init__(self, site):
         self.site = site
 
-        self.title = title
-        self.artist = artist
+        self.title = Empty
+        self.artist = Empty
 
-        self.original_url = url
+        self.original_url = Empty
         self.download_url = None
         self.url_formatted = False
 
         self.tag_item = None
 
-        self.size = size
-        self.duration = duration
-        self.bitrate = None
+        self.size = Empty
+        self.duration = Empty
+        self.bitrate = Empty
         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]
+        # self.calculate_duration_seconds()
+        # self.calculate_bytes()
+        # self.calculate_bitrate()
 
     def format_original_url(self):
         if not self.url_formatted:
@@ -37,6 +48,26 @@ class Item:
             console.output('Setting original URL to: {0}'.format(self.original_url), console.DBG_INFO)
             self.url_formatted = True
 
+    def set_title(self, title):
+        self.title = title
+
+    def set_artist(self, artist):
+        self.artist = artist
+
+    def set_original_url(self, url):
+        self.original_url = url
+
+    def set_duration_string(self, duration):
+        self.duration = duration
+        self.calculate_duration_seconds()
+
+    def set_size_string(self, size):
+        self.size = size
+        self.calculate_bytes()
+
+        self.calculate_bitrate()
+
+
     def set_download_url(self, url):
         console.output('Setting download url to: {0}'.format(url), console.DBG_INFO)
         self.download_url = url
@@ -48,7 +79,7 @@ class Item:
         self.tag_item = tagitem
 
     def calculate_duration_seconds(self):
-        if self.duration is not None:
+        if self.duration is not Empty:
             _split = self.duration.split(':')
             minutes = int(_split[0])
             seconds = int(_split[1])
@@ -56,7 +87,7 @@ class Item:
             self.duration_seconds = (minutes * 60) + seconds
 
     def calculate_bytes(self):
-        if self.size is not None:
+        if self.size is not Empty:
             match = re.search('\d+\.?\d+', self.size)
 
             if match is not None:
@@ -66,3 +97,4 @@ class Item:
     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
+

+ 9 - 7
sites/helper/structure.py

@@ -1,6 +1,6 @@
 from pyquery import PyQuery
 
-from sites.helper.item import Item
+from sites.helper.item import Item, Empty
 
 
 class Structure:
@@ -79,11 +79,13 @@ class Structure:
             size = result.find(self.item_size_path) if self.item_size_path is not None else None
 
             if title is not None and url is not None:
-                items.append(Item(self.site,
-                                  self.site.format_title(title),
-                                  url,
-                                  self.site.format_duration(duration),
-                                  self.site.format_size(size),
-                                  self.site.format_artist(title)))
+                item = Item(self.site)
+                item.set_title(self.site.format_title(title))
+                item.set_artist(self.site.format_artist(title))
+                item.set_original_url(url)
+                item.set_duration_string(self.site.format_duration(duration))
+                item.set_size_string(self.site.format_size(size))
+
+                items.append(item)
 
         return items