From bd883d810c47130b0def7d3cc3096928765e1e8f Mon Sep 17 00:00:00 2001 From: Siarhei Siniak Date: Sun, 7 Jul 2024 08:59:39 +0300 Subject: [PATCH] [~] Refactor --- python/tasks/tiktok/tasks.py | 51 ++++++++++++++++++++++++++++-------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/python/tasks/tiktok/tasks.py b/python/tasks/tiktok/tasks.py index 5744986..fedac5c 100644 --- a/python/tasks/tiktok/tasks.py +++ b/python/tasks/tiktok/tasks.py @@ -132,7 +132,7 @@ def tiktok_video_fetch( method = tiktok_video_fetch_t.method_t(method_str) if method is None: - method = tiktok_video_fetch_t.method_t.tikcdn_io_curl + method = tiktok_video_fetch_t.method_t.pyktok if method == tiktok_video_fetch_t.method_t.pyktok: import pyktok @@ -241,20 +241,43 @@ def tiktok_videos_fetch( return stats +class tiktok_videos_process_t: + @dataclasses_json.dataclass_json + @dataclasses.dataclass + class res_t: + @dataclasses_json.dataclass_json + @dataclasses.dataclass + class stats_t: + saved: int=0 + total: int=0 + skipped: int=0 + error: int=0 + + @dataclasses_json.dataclass_json + @dataclasses.dataclass + class video_t: + meta: Optional[dict[str, Any]]=None + processed_path: Optional[str]=None + + + stats: stats_t=dataclasses.field(default_factory=stats_t) + videos: Iterable[video_t]=dataclasses.field(default_factory=list) + @shared_task() def tiktok_videos_process(meta: Iterable[dict[str, Any]]) -> dict[str, Any]: import tqdm - stats = dict( - saved=0, - total=0, - skipped=0, - error=0, + + res = tiktok_videos_process_t.res_t( + videos=[], ) song = audio_get() for o in tqdm.tqdm(meta): - stats['total'] += 1 + res.stats.total += 1 + res.videos.append(tiktok_videos_process_t.res_t.video_t()) + + res.videos[-1].meta = o path = os.path.join( o['result_dir'], @@ -267,8 +290,11 @@ def tiktok_videos_process(meta: Iterable[dict[str, Any]]) -> dict[str, Any]: processed_path = path_parts[0] + '-proc' + path_parts[1] processed_path_tmp = path_parts[0] + '-proc.tmp' + path_parts[1] + if os.path.exists(processed_path): + res.videos[-1].processed_path = processed_path + if not os.path.exists(path) or os.path.exists(processed_path): - stats['skipped'] += 1 + res.stats.skipped += 1 continue if os.path.exists(processed_path_tmp): @@ -299,16 +325,19 @@ def tiktok_videos_process(meta: Iterable[dict[str, Any]]) -> dict[str, Any]: os.rename(processed_path_tmp, processed_path) - stats['saved'] += 1 + if os.path.exists(processed_path): + res.videos[-1].processed_path = processed_path + + res.stats.saved += 1 except KeyboardInterrupt: break except: logger.error(json.dumps(dict( msg=traceback.format_exc(), ))) - stats['error'] += 1 + res.stats.error += 1 - return stats + return res class audio_get_t: @dataclasses_json.dataclass_json