瀏覽代碼

Setup new code logic

Deben Oldert 6 年之前
父節點
當前提交
a13f6bec63
共有 4 個文件被更改,包括 68 次插入18 次删除
  1. 0 1
      sites/czne.py
  2. 2 2
      sites/default.py
  3. 20 8
      sites/helper/download.py
  4. 46 7
      sites/helper/item.py

+ 0 - 1
sites/czne.py

@@ -14,7 +14,6 @@ class Czne(DefaultSite):
             .set_container_path('div#dle-content')\
             .set_item_path('article.short-story')\
             .set_title_path('div.short-title')\
-            .set_duration_path('div.news-text div.wpisplayer div.total-time') \
             .set_url_path('div.wpisplayer script:first')
         self.request\
             .add_header('Referer', self.url + '/')\

+ 2 - 2
sites/default.py

@@ -33,12 +33,12 @@ class DefaultSite:
 
     def format_duration(self, field):
         if field is None:
-            return ''
+            return field
         return field.text()
 
     def format_size(self, field):
         if field is None:
-            return ''
+            return field
         return field.text()
 
     def format_url(self, field):

+ 20 - 8
sites/helper/download.py

@@ -36,20 +36,32 @@ def download(item, type='GET', parameters=None, headers=None, cookies=None, stre
 
     full_name = '{0}/{1}'.format(Settings.SaveDir, name)
 
-    size = file.headers.get('content-length')
+    if item.bitrate is None and item.bytes is None:
+        size = file.headers.get('content-length')
+        if size is not None:
+            item.set_bytes(int(size))
+
+            console.output('File size: {0}'.format(console.format_bytes(item.bytes)))
+
+            if item.duration_seconds is not None:
+                item.calculate_bitrate()
+
+                console.output('Bitrate: {0}kbps'.format(int(item.bitrate)))
 
-    savefileprogress(name, full_name, file, size, item)
+            ans = console.ask_input('Continue downloading? [y/n]')
 
+            if ans != 'y':
+                return
 
-def savefileprogress(name, full_name, file, size, item):
-    if size is not None:
-        size = int(size)
-        console.output('Size: {0}'.format(console.format_bytes(size)))
+    savefileprogress(name, full_name, file, item)
+
+
+def savefileprogress(name, full_name, file, item):
     console.output('Saving to: {0}'.format(full_name))
     with open(full_name, 'wb') as f:
         progress = 0
-        if size is not None:
-            bar = console.ProgressBar(total=size)
+        if item.bytes is not None:
+            bar = console.ProgressBar(total=item.bytes)
 
             for chunk in file.iter_content(chunk_size=4096):
                 progress += len(chunk)

+ 46 - 7
sites/helper/item.py

@@ -1,19 +1,35 @@
 import console
+import re
 
 
 class Item:
-    def __init__(self, site, title, url, duration='00:00', size=0, artist=None):
+    def __init__(self, site, title, url, duration=None, size=None, artist=None):
         self.site = site
-        self.original_url = url
-        self.download_url = None
+
         self.title = title
-        self.duration = duration
-        self.size = size
         self.artist = artist
 
+        self.original_url = url
+        self.download_url = None
+        self.url_formatted = False
+
         self.tag_item = None
 
-        self.url_formatted = False
+        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:
@@ -25,5 +41,28 @@ class Item:
         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
+        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