Commit 1ee89b307c
Unsigned
Layout: unified · split
curd.py +49 −62
| @@ -74,7 +74,37 @@ def download_anilist_data(access_token, user_id): | ||
| 74 | 74 | |
| 75 | 75 | return anilist_user_data |
| 76 | 76 | |
| 77 | def load_config(): | |
| 77 | default_config = { | |
| 78 | "player":"mpv", | |
| 79 | "show_adult_content":False, | |
| 80 | "history_file":"$HOME/.local/share/curd/curd_history.txt", | |
| 81 | "subs_language":"english", | |
| 82 | "sub_or_dub":"sub", | |
| 83 | "next_episode_prompt":False, | |
| 84 | "score_on_completion":False, | |
| 85 | "save_mpv_speed": True, | |
| 86 | "discord_presence": True, | |
| 87 | "presence_script_path":"curddiscordpresence.py" | |
| 88 | } | |
| 89 | ||
| 90 | def get_userconfig_value(userconfig:dict, key:str): | |
| 91 | return userconfig.get(key, default_config.get(key)) | |
| 92 | ||
| 93 | def create_default_user_config(default_config): | |
| 94 | output_string = "" | |
| 95 | for key, value in default_config.items(): | |
| 96 | # Convert Python True/False to lowercase true/false | |
| 97 | if isinstance(value, bool): | |
| 98 | value_str = 'true' if value else 'false' | |
| 99 | else: | |
| 100 | value_str = str(value) | |
| 101 | ||
| 102 | # Write the key=value to the file with no spaces | |
| 103 | output_string+=f"{key}={value_str}\n" | |
| 104 | ||
| 105 | return output_string | |
| 106 | ||
| 107 | def load_config() -> dict: | |
| 78 | 108 | command = """echo $(xdg-user-dir CONFIG)""" |
| 79 | 109 | |
| 80 | 110 | result = subprocess.run(command, shell=True, capture_output=True, text=True) |
| @@ -97,16 +127,7 @@ def load_config(): | ||
| 97 | 127 | pass |
| 98 | 128 | |
| 99 | 129 | with open(config_file_path, 'w') as file: |
| 100 | file.write(r"""player="mpv" | |
| 101 | show_adult_content=false | |
| 102 | history_file="$HOME/.local/share/curd/curd_history.txt" | |
| 103 | subs_language="english" | |
| 104 | sub_or_dub="sub" | |
| 105 | score_on_completion=true | |
| 106 | save_mpv_speed=true | |
| 107 | discord_presence=true | |
| 108 | presence_script_path="curddiscordpresence.py" | |
| 109 | """) | |
| 130 | file.write(create_default_user_config(default_config)) | |
| 110 | 131 | |
| 111 | 132 | config_dict = {} |
| 112 | 133 | |
| @@ -150,23 +171,14 @@ presence_script_path="curddiscordpresence.py" | ||
| 150 | 171 | # START OF SCRIPT |
| 151 | 172 | |
| 152 | 173 | user_config = load_config() # python dictionary containing all the configs as key value pairs |
| 153 | ||
| 154 | 174 | anilist_user_data = download_anilist_data(access_token, user_id) |
| 155 | ||
| 156 | 175 | anime_dict = extract_anime_info(anilist_user_data)[0] |
| 157 | ||
| 158 | 176 | select_anime(anime_dict) |
| 159 | ||
| 160 | 177 | anime_name = read_tmp("anime") |
| 161 | ||
| 162 | 178 | write_to_tmp("query", anime_name) |
| 163 | ||
| 164 | 179 | run_script("anime_list") |
| 165 | ||
| 166 | 180 | anime_dict = load_anime_data("scripts/tmp/anime_list") |
| 167 | ||
| 168 | #clean anime name | |
| 169 | cleaned_text = re.sub(r'\(.*$', '', anime_name).strip() | |
| 181 | cleaned_text = re.sub(r'\(.*$', '', anime_name).strip() # clean anime name | |
| 170 | 182 | |
| 171 | 183 | try: |
| 172 | 184 | result = search_anime_by_title(anilist_user_data, cleaned_text)[0] |
| @@ -184,7 +196,7 @@ except Exception as e: | ||
| 184 | 196 | exit(1) |
| 185 | 197 | |
| 186 | 198 | try: |
| 187 | finding_anime = find_anime(get_all_anime(user_config["history_file"]), anilist_id=str(media_id)) | |
| 199 | finding_anime = find_anime(get_all_anime(get_userconfig_value(user_config, "history_file")), anilist_id=str(media_id)) | |
| 188 | 200 | # print(finding_anime) |
| 189 | 201 | if finding_anime: |
| 190 | 202 | print("Found anime in history") |
| @@ -203,29 +215,19 @@ except: | ||
| 203 | 215 | |
| 204 | 216 | run_script("episode_list") |
| 205 | 217 | |
| 206 | # with open("scripts/tmp/episode_list") as ep_list: | |
| 207 | # temp = ep_list.read() | |
| 208 | # temp = temp.split() | |
| 209 | # last_episode = temp[-1] | |
| 210 | # ep_no = input(f"Enter episode number: (number of episodes: {last_episode})\n") | |
| 211 | ||
| 212 | 218 | binge_watching = False |
| 213 | ||
| 214 | 219 | mpv_args = [] |
| 215 | ||
| 216 | 220 | last_episode = int(read_tmp("episode_list").split()[-1]) |
| 217 | ||
| 218 | anime_watch_history = get_all_anime(user_config['history_file']) | |
| 219 | # anime_watch_history[] | |
| 221 | anime_watch_history = get_all_anime(get_userconfig_value(user_config, 'history_file')) | |
| 220 | 222 | anime_history = find_anime(anime_watch_history, anilist_id=media_id, allanime_id=get_contents_of("id")) |
| 221 | 223 | episode_completed = False |
| 222 | 224 | rewatching = False |
| 225 | ||
| 223 | 226 | if anime_history: # if it exists in local history |
| 224 | 227 | # print(f"came in history {str(progress)}") |
| 225 | 228 | if int(anime_history['episode']) != progress+1: # if the upstream progress is ahead |
| 226 | 229 | write_to_tmp("ep_no", str(int(progress)+1)) |
| 227 | 230 | # print(f"Starting anime from upstream {str(progress + 1)}") |
| 228 | ||
| 229 | 231 | watching_ep = progress + 1 |
| 230 | 232 | |
| 231 | 233 | elif int(anime_history['episode']) == int(progress)+1: # if the upstream progress is NOT ahead |
| @@ -265,18 +267,13 @@ while True: | ||
| 265 | 267 | salt = random.randint(0,1500) |
| 266 | 268 | # print("SALT IS:"+str(salt)) |
| 267 | 269 | start_video(links[0][1], salt, mpv_args) |
| 268 | ||
| 269 | 270 | mpv_socket_path = "/tmp/mpvsocket"+str(salt) |
| 270 | ||
| 271 | 271 | connect_mpv_command = """echo '{ "command": ["get_property", "playback-time"] }' | socat - """+mpv_socket_path |
| 272 | ||
| 273 | 272 | mpv_pos_command = """echo '{ "command": ["get_property", "time-pos"] }' | socat - """+mpv_socket_path |
| 274 | ||
| 275 | 273 | mpv_duration_command = """echo '{ "command": ["get_property", "duration"] }' | socat - """+mpv_socket_path |
| 276 | 274 | |
| 277 | 275 | while True: |
| 278 | 276 | time.sleep(2) |
| 279 | ||
| 280 | 277 | result = subprocess.run(connect_mpv_command, shell=True, capture_output=True, text=True) |
| 281 | 278 | # print(result) |
| 282 | 279 | if result.returncode == 0: |
| @@ -288,18 +285,13 @@ while True: | ||
| 288 | 285 | data = json.loads(output) |
| 289 | 286 | if data["error"] == "success": |
| 290 | 287 | playback_time = round(int(data["data"]), 2) |
| 291 | ||
| 292 | # print(user_config['history_file']) | |
| 293 | ||
| 294 | update_anime(user_config['history_file'], str(media_id), str(get_contents_of("id")), str(watching_ep), str(playback_time), str(duration), str(title)) | |
| 288 | update_anime(get_userconfig_value(user_config, 'history_file'), str(media_id), str(get_contents_of("id")), str(watching_ep), str(playback_time), str(duration), str(title)) | |
| 295 | 289 | # print("Playback time:", playback_time) |
| 296 | ||
| 297 | 290 | watched_percentage = get_percentage_watched(mpv_socket_path) |
| 298 | 291 | mpv_playback_speed = get_mpv_playback_speed(mpv_socket_path) |
| 299 | 292 | # print("mpv speed", mpv_playback_speed) |
| 300 | 293 | # print("watched percentage", watched_percentage) |
| 301 | 294 | if watched_percentage > mark_episode_as_completed_at: |
| 302 | ||
| 303 | 295 | episode_completed = True |
| 304 | 296 | binge_watching = True |
| 305 | 297 | # else: |
| @@ -353,7 +345,7 @@ while True: | ||
| 353 | 345 | if watched_percentage > mark_episode_as_completed_at and not rewatching: |
| 354 | 346 | update_anime_progress(access_token, int(media_id), int(watching_ep)) |
| 355 | 347 | watching_ep = int(watching_ep)+1 |
| 356 | update_anime(user_config['history_file'], str(media_id), str(get_contents_of("id")), str(watching_ep), "0", str(duration), str(title)) | |
| 348 | update_anime(get_userconfig_value(user_config, 'history_file'), str(media_id), str(get_contents_of("id")), str(watching_ep), "0", str(duration), str(title)) | |
| 357 | 349 | |
| 358 | 350 | print("bye") |
| 359 | 351 | exit(0) |
| @@ -364,12 +356,12 @@ while True: | ||
| 364 | 356 | current_ep = int(read_tmp("ep_no")) |
| 365 | 357 | |
| 366 | 358 | if current_ep == last_episode: |
| 367 | if binge_watching == True and not rewatching and user_config['score_on_completion'] == True: # Completed anime | |
| 359 | if binge_watching == True and not rewatching and get_userconfig_value(user_config, 'score_on_completion') == True: # Completed anime | |
| 368 | 360 | anime_rating_by_user = input("Rate this anime:\n") |
| 369 | 361 | update_anime_progress(access_token, int(media_id), int(watching_ep)) |
| 370 | 362 | rate_anime(access_token, media_id, anime_rating_by_user) |
| 371 | 363 | print("completed anime.") |
| 372 | delete_anime(user_config['history_file'], media_id, get_contents_of("id")) | |
| 364 | delete_anime(get_userconfig_value(user_config, 'history_file'), media_id, get_contents_of("id")) | |
| 373 | 365 | exit(0) |
| 374 | 366 | |
| 375 | 367 | else: |
| @@ -382,8 +374,8 @@ while True: | ||
| 382 | 374 | update_anime_progress(access_token, int(media_id), int(watching_ep)) |
| 383 | 375 | watching_ep = int(watching_ep)+1 |
| 384 | 376 | write_to_tmp("ep_no", str(watching_ep)) |
| 385 | update_anime(user_config['history_file'], str(media_id), str(get_contents_of("id")), str(watching_ep), "0", str(duration), str(title)) | |
| 386 | anime_watch_history = get_all_anime(user_config['history_file']) | |
| 377 | update_anime(get_userconfig_value(user_config, 'history_file'), str(media_id), str(get_contents_of("id")), str(watching_ep), "0", str(duration), str(title)) | |
| 378 | anime_watch_history = get_all_anime(get_userconfig_value(user_config, 'history_file')) | |
| 387 | 379 | anime_history = find_anime(anime_watch_history, anilist_id=media_id, allanime_id=get_contents_of("id")) |
| 388 | 380 | episode_completed = False |
| 389 | 381 | run_script("episode_url") |
| @@ -391,18 +383,13 @@ while True: | ||
| 391 | 383 | mpv_args = [] |
| 392 | 384 | watched_percentage = 0 |
| 393 | 385 | |
| 394 | if user_config['save_mpv_speed']==True: | |
| 386 | if get_userconfig_value(user_config, 'save_mpv_speed') ==True: | |
| 395 | 387 | mpv_args.append(f'--speed={mpv_playback_speed}') |
| 396 | 388 | |
| 397 | 389 | # watching_ep = current_ep+1 |
| 398 | ||
| 399 | # to_continue_or_not_to_continue = input("Continue? (y/n)\n") | |
| 400 | # try: | |
| 401 | # # print("percentage watched",percentage_watched(playback_time, duration), "%") | |
| 402 | # except: | |
| 403 | # pass | |
| 404 | # if to_continue_or_not_to_continue.lower() == "yes" or to_continue_or_not_to_continue.lower() == "y" or to_continue_or_not_to_continue == "": | |
| 405 | # binge_watching = True | |
| 406 | # ep_no_file | |
| 407 | # else: | |
| 408 | # break | |
| 390 | if get_userconfig_value(user_config, 'next_episode_prompt') == True: | |
| 391 | to_continue_or_not_to_continue = input("Continue? (y/n)\n") | |
| 392 | if to_continue_or_not_to_continue.lower() == "yes" or to_continue_or_not_to_continue.lower() == "y" or to_continue_or_not_to_continue == "": | |
| 393 | binge_watching = True | |
| 394 | else: | |
| 395 | exit(0) | |
| \ No newline at end of file | ||