diff --git a/__pycache__/telegram_wrapper.cpython-312.pyc b/__pycache__/telegram_wrapper.cpython-312.pyc new file mode 100644 index 0000000..a455379 Binary files /dev/null and b/__pycache__/telegram_wrapper.cpython-312.pyc differ diff --git a/cartoons.db b/cartoons.db new file mode 100644 index 0000000..b960b63 Binary files /dev/null and b/cartoons.db differ diff --git a/database/__pycache__/__init__.cpython-312.pyc b/database/__pycache__/__init__.cpython-312.pyc new file mode 100644 index 0000000..579813b Binary files /dev/null and b/database/__pycache__/__init__.cpython-312.pyc differ diff --git a/database/__pycache__/models.cpython-312.pyc b/database/__pycache__/models.cpython-312.pyc new file mode 100644 index 0000000..fd33032 Binary files /dev/null and b/database/__pycache__/models.cpython-312.pyc differ diff --git a/loader/__init__.py b/loader/__init__.py index 13f4f2d..8abd449 100644 --- a/loader/__init__.py +++ b/loader/__init__.py @@ -1,20 +1,33 @@ +import datetime +import sys +import threading +import time +from io import BytesIO +from threading import Lock + import requests def get_futurama(season, episode): - return f'https://s6.streamani.top/video1/IQCFO7Lyp1vm45ai6wpJCQ/1727007181/futurama/{season}/LostFilm/{season}%02d.mp4'%episode + return f'https://s6.streamani.top/video1/IQCFO7Lyp1vm45ai6wpJCQ/1727007181/futurama/{season}/LostFilm/{season}%02d.mp4' % episode + def get_amerika(season, episode): - return f'https://s6.streamani.top/video1/o_oY4tK-92WR9vZiaeuKGw/1727014428/americandad/{season}/TVShows/{season}%02d.mp4'%episode + return f'https://s6.streamani.top/video1/o_oY4tK-92WR9vZiaeuKGw/1727014428/americandad/{season}/TVShows/{season}%02d.mp4' % episode + +def get_simpsons(season, episode): + # return f'https://s2.streamani.top/video1/XvIO8j1e9OWTvOWaduoUbg/1727138157/simpsons/{season}/2x2/{season}%02d.mp4' % episode + # return f'https://s3.streamani.top/video1/uyWDJ_akjg_QQ_dncYGF_Q/1727175508/simpsons/{season}/omskbird/{season}%02d.mp4' % episode + return f'https://s6.streamani.top/video1/50o_QLZGCu54HALjQeXh4w/1727177649/simpsons/{season}/omskbird/{season}%02d.mp4' % episode + + cartoons = { 1: get_futurama, - 2: get_amerika + 2: get_amerika, + 3: get_simpsons } - - - def load_episode(cartoon_id, season, episode): response = requests.get(cartoons[cartoon_id](season, episode), stream=True) @@ -30,6 +43,54 @@ def load_episode(cartoon_id, season, episode): f.write(chunk) print(f"\nВидео сохранено как {filename}") +BYTES_TO_MEGABYTE = 1024 * 1024 + + +def load_episode2(cartoon_id, season, episode): + response = requests.get(cartoons[cartoon_id](season, episode), stream=True) + + assert response.status_code == 200 + + print(f"Идет загрузка {episode} серии...") + total_length = int(response.headers.get('content-length')) + + filename = "C%02dS%02dE%02d.mp4" % (cartoon_id, season, episode) + + total_length_mega = total_length // BYTES_TO_MEGABYTE + + file = b'' + in_second = 0 + second_left = 0 + mb_sec = 0.01 + bytes_sec = 0.001 + + start_downloading = start = time.time() + for chunk in response.iter_content(1024): + file += chunk + in_second += len(chunk) + time_used = (time.time() - start) + second_left += time_used + if second_left > 1: + bytes_sec = in_second / second_left + mb_sec = bytes_sec / BYTES_TO_MEGABYTE + second_left = 0 + in_second = 0 + if time_used: + percentage = int(len(file) / total_length * 100) + sys.stdout.write(f'\r[{"|" * percentage}{" " * (100 - percentage)}]\t' + f'{percentage}%\t' + f'[{len(file) / BYTES_TO_MEGABYTE:.2f}/{total_length_mega}MB]\t' + f'{mb_sec:.2f} MB/S\t' + f'TIME {str(datetime.timedelta(seconds=int(time.time()-start_downloading)))}\t' + f'LEFT {str(datetime.timedelta(seconds=int( + (total_length-len(file)) / bytes_sec + )))}\t') + start = time.time() + with open(f"episodes/{filename}", "wb") as f: + f.write(file) + print(f"\nВидео сохранено как {filename}") + + def load_season(cartoon_id, season_id, episodes=None): print(f"Идет загрузка {season_id} сезона") if episodes: @@ -45,12 +106,69 @@ def load_season(cartoon_id, season_id, episodes=None): try: load_episode(cartoon_id, season_id, episode) except AssertionError: + print("Cannot load", episode) break print("Сезон загружен") -load_season(2, 19, range(6, 26)) + +episode_queue = set() +lock = Lock() + +def add_episode_to_queue(cartoon_id, season, episode): + + with lock: + episode_queue.add((cartoon_id, season, episode)) + +def get_episode_queue(): + while True: + episode = None + with lock: + if episode_queue: + episode = episode_queue.pop() + if episode: + yield episode + time.sleep(2) + +def episode_loader(): + print("+LOADER") + for episode in get_episode_queue(): + try: + load_episode(episode[0], episode[1], episode[2]) + except AssertionError: + print("Cannot load", episode[2]) + break + print("+ENDLOADER") + + +def create_multithread_loader(cartoon_id, season_id, episodes, thread_num): + # for episode in episodes: + # try: + # add_episode_to_queue(cartoon_id, season_id, episode) + # except AssertionError: + # break + + for i in range(thread_num): + threading.Thread(target=episode_loader).start() + + +def seasons_multithread_loader(cartoon_id, seasons): + + + for season in seasons: + print("Сезон %d"%season) + threading.Thread(target=load_season, args=(cartoon_id, season)).start() + +# seasons_multithread_loader(3, range(29, 36)) + +for episode in range(1, 23): + add_episode_to_queue(3, 34, episode) + +for episode in range(1, 19): + add_episode_to_queue(3, 35, episode) + +create_multithread_loader(3, 33, range(12, 23), 15) +# load_season(2, 19, range(21, 30)) # for season in range(16, 20): # print("Сезон %d"%season) # load_season(2, season) -