[common extractor] round_str

This commit is contained in:
MaxwellGoblin 2017-07-15 19:24:12 +08:00
parent cff5fb4529
commit 84624d8a8b
2 changed files with 19 additions and 11 deletions

View File

@ -108,6 +108,7 @@ import re
import socket import socket
import sys import sys
import time import time
import math
from urllib import request, parse, error from urllib import request, parse, error
from http import cookiejar from http import cookiejar
from importlib import import_module from importlib import import_module
@ -177,6 +178,11 @@ def general_m3u8_extractor(url, headers={}):
urls.append(seg_url) urls.append(seg_url)
return urls return urls
def round_str(num, cnt):
# workaround for braken builtin round on some android py
fmt_str = '{:.' + str(cnt) + 'f}'
return fmt_str.format(num)
def maybe_print(*s): def maybe_print(*s):
try: print(*s) try: print(*s)
except: pass except: pass
@ -635,7 +641,7 @@ class SimpleProgressBar:
total_pieces_len = len(str(total_pieces)) total_pieces_len = len(str(total_pieces))
# 38 is the size of all statically known size in self.bar # 38 is the size of all statically known size in self.bar
total_str = '%5s' % round(self.total_size / 1048576, 1) total_str = '%5s' % round_str(self.total_size / 1048576, 1)
total_str_width = max(len(total_str), 5) total_str_width = max(len(total_str), 5)
self.bar_size = self.term_size - 27 - 2*total_pieces_len - 2*total_str_width self.bar_size = self.term_size - 27 - 2*total_pieces_len - 2*total_str_width
self.bar = '{:>4}%% ({:>%s}/%sMB) ├{:─<%s}┤[{:>%s}/{:>%s}] {}' % ( self.bar = '{:>4}%% ({:>%s}/%sMB) ├{:─<%s}┤[{:>%s}/{:>%s}] {}' % (
@ -644,11 +650,14 @@ class SimpleProgressBar:
def update(self): def update(self):
self.displayed = True self.displayed = True
bar_size = self.bar_size bar_size = self.bar_size
percent = round(self.received * 100 / self.total_size, 1) percent_str = round_str(self.received * 100 / self.total_size, 1)
if percent >= 100: if self.received == self.total_size:
percent = 100 percent = 100
dots = bar_size * int(percent) // 100 percent_str = '100'
plus = int(percent) - dots // bar_size * 100 else:
percent = math.floor(self.received * 100 / self.total_size)
dots = bar_size * percent // 100
plus = percent - dots // bar_size * 100
if plus > 0.8: if plus > 0.8:
plus = '' plus = ''
elif plus > 0.4: elif plus > 0.4:
@ -656,7 +665,7 @@ class SimpleProgressBar:
else: else:
plus = '' plus = ''
bar = '' * dots + plus bar = '' * dots + plus
bar = self.bar.format(percent, round(self.received / 1048576, 1), bar, self.current_piece, self.total_pieces, self.speed) bar = self.bar.format(percent_str, round_str(self.received / 1048576, 1), bar, self.current_piece, self.total_pieces, self.speed)
sys.stdout.write('\r' + bar) sys.stdout.write('\r' + bar)
sys.stdout.flush() sys.stdout.flush()
@ -991,10 +1000,9 @@ def print_info(site_info, title, type, size, **kwargs):
maybe_print("Title: ", unescape_html(tr(title))) maybe_print("Title: ", unescape_html(tr(title)))
print("Type: ", type_info) print("Type: ", type_info)
if type != 'm3u8': if type != 'm3u8':
print("Size: ", round(size / 1048576, 2), "MiB (" + str(size) + " Bytes)") print("Size: ", round_str(size / 1048576, 2), "MiB (" + str(size) + " Bytes)")
if type == 'm3u8' and 'm3u8_url' in kwargs: if type == 'm3u8' and 'm3u8_url' in kwargs:
print('M3U8 Url: {}'.format(kwargs['m3u8_url'])) print('M3U8 Url: {}'.format(kwargs['m3u8_url']))
print()
def mime_to_container(mime): def mime_to_container(mime):
mapping = { mapping = {

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
from .common import match1, maybe_print, download_urls, get_filename, parse_host, set_proxy, unset_proxy, get_content, dry_run from .common import match1, maybe_print, download_urls, get_filename, parse_host, set_proxy, unset_proxy, get_content, dry_run, round_str
from .common import print_more_compatible as print from .common import print_more_compatible as print
from .util import log from .util import log
from . import json_output from . import json_output
@ -107,7 +107,7 @@ class VideoExtractor():
if 'size' in stream and stream['container'].lower() != 'm3u8': if 'size' in stream and stream['container'].lower() != 'm3u8':
if stream['size'] != float('inf') and stream['size'] != 0: if stream['size'] != float('inf') and stream['size'] != 0:
print(" size: %s MiB (%s bytes)" % (round(stream['size'] / 1048576, 1), stream['size'])) print(" size: %s MiB (%s bytes)" % (round_str(stream['size'] / 1048576, 1), stream['size']))
if 'm3u8_url' in stream: if 'm3u8_url' in stream:
print(" m3u8_url: {}".format(stream['m3u8_url'])) print(" m3u8_url: {}".format(stream['m3u8_url']))
@ -126,7 +126,7 @@ class VideoExtractor():
stream = self.dash_streams[stream_id] stream = self.dash_streams[stream_id]
maybe_print(" - title: %s" % self.title) maybe_print(" - title: %s" % self.title)
print(" size: %s MiB (%s bytes)" % (round(stream['size'] / 1048576, 1), stream['size'])) print(" size: %s MiB (%s bytes)" % (round_str(stream['size'] / 1048576, 1), stream['size']))
print(" url: %s" % self.url) print(" url: %s" % self.url)
print() print()