Commit f4d5cedeb2
Unsigned
Layout: unified · split
curd.py +67 −41
| @@ -685,15 +685,30 @@ def get_all_anime(database_file): | ||
| 685 | 685 | |
| 686 | 686 | # Function to update or add a new anime entry |
| 687 | 687 | def update_anime(database_file: str, anilist_id:str, allanime_id: str, episode:str, time:str, duration:str, name:str): |
| 688 | anime_list = get_all_anime(database_file) | |
| 689 | updated = False | |
| 688 | try: | |
| 689 | anime_list = get_all_anime(database_file) | |
| 690 | updated = False | |
| 690 | 691 | |
| 691 | # Create a new list to store updated entries | |
| 692 | updated_anime_list = [] | |
| 693 | ||
| 694 | # Check if the anime entry exists and update it | |
| 695 | for anime in anime_list: | |
| 696 | if anime['anilist_id'] == anilist_id and anime['allanime_id'] == allanime_id: | |
| 692 | # Create a new list to store updated entries | |
| 693 | updated_anime_list = [] | |
| 694 | ||
| 695 | # Check if the anime entry exists and update it | |
| 696 | for anime in anime_list: | |
| 697 | if anime['anilist_id'] == anilist_id and anime['allanime_id'] == allanime_id: | |
| 698 | updated_anime_list.append({ | |
| 699 | 'anilist_id': anilist_id, | |
| 700 | 'allanime_id': allanime_id, | |
| 701 | 'episode': episode, | |
| 702 | 'time': time, | |
| 703 | 'duration': duration, | |
| 704 | 'name': name | |
| 705 | }) | |
| 706 | updated = True | |
| 707 | else: | |
| 708 | updated_anime_list.append(anime) | |
| 709 | ||
| 710 | # If no entry was updated, add new entry | |
| 711 | if not updated: | |
| 697 | 712 | updated_anime_list.append({ |
| 698 | 713 | 'anilist_id': anilist_id, |
| 699 | 714 | 'allanime_id': allanime_id, |
| @@ -702,28 +717,16 @@ def update_anime(database_file: str, anilist_id:str, allanime_id: str, episode:s | ||
| 702 | 717 | 'duration': duration, |
| 703 | 718 | 'name': name |
| 704 | 719 | }) |
| 705 | updated = True | |
| 706 | else: | |
| 707 | updated_anime_list.append(anime) | |
| 708 | ||
| 709 | # If no entry was updated, add new entry | |
| 710 | if not updated: | |
| 711 | updated_anime_list.append({ | |
| 712 | 'anilist_id': anilist_id, | |
| 713 | 'allanime_id': allanime_id, | |
| 714 | 'episode': episode, | |
| 715 | 'time': time, | |
| 716 | 'duration': duration, | |
| 717 | 'name': name | |
| 718 | }) | |
| 719 | ||
| 720 | # Write all entries back to the file | |
| 721 | with open(database_file, mode='w', newline='') as file: | |
| 722 | writer = csv.writer(file) | |
| 723 | for anime in updated_anime_list: | |
| 724 | writer.writerow([anime['anilist_id'], anime['allanime_id'], anime['episode'], anime['time'], anime['duration'], anime['name']]) | |
| 725 | ||
| 726 | # print("Updated the file") | |
| 720 | ||
| 721 | # Write all entries back to the file | |
| 722 | with open(database_file, mode='w', newline='') as file: | |
| 723 | writer = csv.writer(file) | |
| 724 | for anime in updated_anime_list: | |
| 725 | writer.writerow([anime['anilist_id'], anime['allanime_id'], anime['episode'], anime['time'], anime['duration'], anime['name']]) | |
| 726 | ||
| 727 | # print("Updated the file") | |
| 728 | except Exception as e: | |
| 729 | print_error(f"This: {e}") | |
| 727 | 730 | |
| 728 | 731 | def find_anime(anime_list, anilist_id=-1, allanime_id=-1): |
| 729 | 732 | for anime in anime_list: |
| @@ -795,10 +798,11 @@ default_config = { | ||
| 795 | 798 | "sub_or_dub":"sub", |
| 796 | 799 | "percentage_to_mark_complete":85, |
| 797 | 800 | "next_episode_prompt": False, |
| 801 | "skip_op": True, | |
| 802 | "skip_ed": True, | |
| 798 | 803 | "score_on_completion": True, |
| 799 | 804 | "save_mpv_speed": True, |
| 800 | "discord_presence": True, | |
| 801 | "presence_script_path":"curddiscordpresence.py" | |
| 805 | "discord_presence": False, | |
| 802 | 806 | } |
| 803 | 807 | |
| 804 | 808 | def convert_seconds_to_minutes(seconds) -> str: |
| @@ -1218,6 +1222,7 @@ else: # if history does not exist | ||
| 1218 | 1222 | write_to_tmp("ep_no", str(watching_ep)) |
| 1219 | 1223 | # print(watching_ep) |
| 1220 | 1224 | # os.system("{current_dir}/scripts/episode_url.sh") |
| 1225 | print("Fetching anime") | |
| 1221 | 1226 | run_script("episode_url") |
| 1222 | 1227 | |
| 1223 | 1228 | # Print the result |
| @@ -1235,7 +1240,14 @@ if get_userconfig_value(user_config, "discord_presence") == True: | ||
| 1235 | 1240 | # print("imported pypresence") |
| 1236 | 1241 | rpc = Presence(discord_client_id) |
| 1237 | 1242 | rpc.connect() |
| 1238 | anime_image_url = get_anime_image(media_id) | |
| 1243 | anime_mal_id, anime_image_url = get_anime_id_and_image(media_id) | |
| 1244 | else: | |
| 1245 | anime_mal_id = get_anime_mal_id(media_id) | |
| 1246 | ||
| 1247 | # default variable declaration | |
| 1248 | watched_percentage = 0 | |
| 1249 | is_paused = False | |
| 1250 | mpv_playback_speed = 1.0 | |
| 1239 | 1251 | |
| 1240 | 1252 | while True: |
| 1241 | 1253 | |
| @@ -1245,9 +1257,17 @@ while True: | ||
| 1245 | 1257 | start_video(links[0][1], salt, mpv_args) |
| 1246 | 1258 | mpv_socket_path = "/tmp/curd/curd_mpvsocket"+str(salt) |
| 1247 | 1259 | connect_mpv_command = """echo '{ "command": ["get_property", "playback-time"] }' | socat - """+mpv_socket_path |
| 1248 | is_paused = False | |
| 1260 | if get_userconfig_value(user_config, "skip_op") == True or get_userconfig_value(user_config, "skip_end"): | |
| 1261 | skip_intervals = get_and_parse_aniskip_data(anime_mal_id, watching_ep) | |
| 1262 | if skip_intervals == None: | |
| 1263 | print("failed to get skip times") | |
| 1264 | ||
| 1249 | 1265 | while True: |
| 1250 | time.sleep(1) | |
| 1266 | # Example usage | |
| 1267 | # anime_id = 28223 | |
| 1268 | # episode = 10 | |
| 1269 | ||
| 1270 | time.sleep(2) | |
| 1251 | 1271 | result = subprocess.run(connect_mpv_command, shell=True, capture_output=True, text=True) |
| 1252 | 1272 | # print(result) |
| 1253 | 1273 | if result.returncode == 0: |
| @@ -1255,16 +1275,12 @@ while True: | ||
| 1255 | 1275 | if not output: # Check if output is empty |
| 1256 | 1276 | print("No data received. Retrying...") |
| 1257 | 1277 | try: |
| 1258 | time.sleep(1) | |
| 1259 | 1278 | data = json.loads(output) |
| 1260 | 1279 | if data["error"] == "success": |
| 1261 | 1280 | playback_time = round(int(data["data"]), 2) |
| 1262 | 1281 | if get_userconfig_value(user_config, "discord_presence") == True: |
| 1263 | mpv_status = get_mpv_paused_status(mpv_socket_path) | |
| 1264 | if mpv_status == True: | |
| 1265 | is_paused = True | |
| 1266 | else: | |
| 1267 | is_paused = False | |
| 1282 | is_paused = get_mpv_paused_status(mpv_socket_path) | |
| 1283 | ||
| 1268 | 1284 | |
| 1269 | 1285 | # print("mpv_status", mpv_status) |
| 1270 | 1286 | # print("is_paused", is_paused) |
| @@ -1284,6 +1300,16 @@ while True: | ||
| 1284 | 1300 | large_image=anime_image_url, |
| 1285 | 1301 | large_text=title, |
| 1286 | 1302 | ) |
| 1303 | if skip_intervals: | |
| 1304 | if get_userconfig_value(user_config, "skip_op") == True: | |
| 1305 | if round(int(playback_time)) > round(int(skip_intervals['op']['start_time'])) and round(int(playback_time)) < round(int(skip_intervals['op']['start_time']))+10: | |
| 1306 | print("skipped opening") | |
| 1307 | seek_mpv(mpv_socket_path, int(skip_intervals['op']['end_time'])) | |
| 1308 | ||
| 1309 | if get_userconfig_value(user_config, "skip_ed") == True: | |
| 1310 | if round(int(playback_time)) > round(int(skip_intervals['ed']['start_time'])) and round(int(playback_time)) < round(int(skip_intervals['ed']['start_time']))+10: | |
| 1311 | print("skipped ending") | |
| 1312 | seek_mpv(mpv_socket_path, int(skip_intervals['ed']['end_time'])) | |
| 1287 | 1313 | |
| 1288 | 1314 | 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)) |
| 1289 | 1315 | # print("Playback time:", playback_time) |