Fix progress bar line feed and remove pbar option

This commit is contained in:
taizan-hokuto
2020-09-09 22:19:10 +09:00
parent a338f2b782
commit f16c0ee73a
4 changed files with 18 additions and 76 deletions

View File

@@ -1,11 +1,11 @@
import argparse
import os
import sys
import signal
from json.decoder import JSONDecodeError
from pathlib import Path
from .arguments import Arguments
from .progressbar import ProgressBar
from .. exceptions import InvalidVideoIdException, NoContents, PatternUnmatchError
from .. processors.html_archiver import HTMLArchiver
from .. tool.extract.extractor import Extractor
@@ -32,18 +32,12 @@ def main():
'If ID starts with a hyphen (-), enclose the ID in square brackets.')
parser.add_argument('-o', f'--{Arguments.Name.OUTPUT}', type=str,
help='Output directory (end with "/"). default="./"', default='./')
parser.add_argument(f'--{Arguments.Name.PBAR}', action='store_true',
help='Display rich progress bar')
parser.add_argument(f'--{Arguments.Name.SAVE_ERROR_DATA}', action='store_true',
help='Save error data when error occurs(".dat" file)')
parser.add_argument(f'--{Arguments.Name.VERSION}', action='store_true',
help='Show version')
Arguments(parser.parse_args().__dict__)
if Arguments().pbar:
from .progressbar_rich import ProgressBar
else:
from .progressbar_simple import ProgressBar
if Arguments().print_version:
print(f'pytchat v{__version__} © 2019 taizan-hokuto')
return
@@ -52,7 +46,7 @@ def main():
if not Arguments().video_ids:
parser.print_help()
return
for video_id in Arguments().video_ids:
for counter, video_id in enumerate(Arguments().video_ids):
if '[' in video_id:
video_id = video_id.replace('[', '').replace(']', '')
try:
@@ -62,7 +56,9 @@ def main():
else:
raise FileNotFoundError
info = VideoInfo(video_id)
print(f"Extracting...\n"
if len(Arguments().video_ids) > 1:
print(f"\n{'-' * 10} video:{counter + 1} of {len(Arguments().video_ids)} {'-' * 10}")
print(f"\n"
f" video_id: {video_id}\n"
f" channel: {info.get_channel_name()}\n"
f" title: {info.get_title()}")
@@ -70,17 +66,14 @@ def main():
print(f" output path: {path.resolve()}")
duration = info.get_duration()
pbar = ProgressBar(total=(duration * 1000), status="Extracting")
ex = Extractor(video_id,
ex = Extractor(video_id,
callback=pbar._disp,
div=10)
signal.signal(signal.SIGINT, (lambda a, b: cancel(ex, pbar)))
data = ex.extract()
if data == []:
return False
if Arguments().pbar:
pbar.reset("#", "=", total=len(data), status="Rendering ")
else:
pbar.reset("=", "", total=len(data), status="Rendering ")
pbar.reset("#", "=", total=len(data), status="Rendering ")
processor = HTMLArchiver(Arguments().output + video_id + '.html', callback=pbar._disp)
processor.process(
[{'video_id': None,
@@ -88,18 +81,12 @@ def main():
'chatdata': (action["replayChatItemAction"]["actions"][0] for action in data)}]
)
processor.finalize()
if Arguments().pbar:
pbar.reset('#', '#', status='Completed ')
pbar.close()
else:
pbar.close()
print("\nCompleted")
pbar.reset('#', '#', status='Completed ')
pbar.close()
print()
if pbar.is_cancelled():
print("\nThe extraction process has been discontinued.\n")
return False
return True
except InvalidVideoIdException:
print("Invalid Video ID or URL:", video_id)