Commit fce2f26817
Unsigned
Layout: unified · split
curd.py +192 −129
| @@ -14,15 +14,16 @@ import time | |||
| 14 | from anilist import search_anime_by_title | 14 | from anilist import search_anime_by_title |
| 15 | from anilist import get_user_data, get_anilist_user_id | 15 | from anilist import get_user_data, get_anilist_user_id |
| 16 | from select_link import load_links | 16 | from select_link import load_links |
| 17 | from start_video import start_video, send_command, get_percentage_watched | 17 | from start_video import start_video, send_command, get_percentage_watched, percentage_watched |
| 18 | from select_anime import load_anime_data | 18 | from select_anime import load_anime_data |
| 19 | from select_anime import extract_anime_info | 19 | from select_anime import extract_anime_info |
| 20 | from select_anime import select_anime | 20 | from select_anime import select_anime |
| 21 | 21 | ||
| 22 | from track_anime import add_anime, update_anime, get_all_anime, delete_anime, find_anime | 22 | from track_anime import add_anime, update_anime, get_all_anime, delete_anime, find_anime |
| 23 | 23 | ||
| 24 | access_token = os.environ.get('ANILIST_ACCESS_TOKEN') | 24 | mark_episode_as_completed_at = 85 |
| 25 | 25 | ||
| 26 | access_token = os.environ.get('ANILIST_ACCESS_TOKEN') | ||
| 26 | user_id, user_name = get_anilist_user_id(access_token) | 27 | user_id, user_name = get_anilist_user_id(access_token) |
| 27 | # user_id = os.environ.get('ANILIST_USER_ID') | 28 | # user_id = os.environ.get('ANILIST_USER_ID') |
| 28 | 29 | ||
| @@ -37,9 +38,41 @@ def get_contents_of(tmp_file_name): | |||
| 37 | with open(f"scripts/tmp/{tmp_file_name}", "r") as temp_file: | 38 | with open(f"scripts/tmp/{tmp_file_name}", "r") as temp_file: |
| 38 | return temp_file.read() | 39 | return temp_file.read() |
| 39 | 40 | ||
| 40 | def run_script(script): | 41 | def run_script(script: str): |
| 41 | os.system(f"./scripts/{script}.sh") | 42 | os.system(f"./scripts/{script}.sh") |
| 42 | 43 | ||
| 44 | def write_to_tmp(tmp_filename:str, content:str): | ||
| 45 | try: | ||
| 46 | with open(f"scripts/tmp/{tmp_filename}", "w") as _: | ||
| 47 | _.write(content) | ||
| 48 | return True | ||
| 49 | except: | ||
| 50 | return False | ||
| 51 | |||
| 52 | def read_tmp(tmp_filename:str): | ||
| 53 | try: | ||
| 54 | with open(f"scripts/tmp/{tmp_filename}", "r") as _: | ||
| 55 | content = _.read() | ||
| 56 | return content | ||
| 57 | except: | ||
| 58 | return False | ||
| 59 | |||
| 60 | def download_anilist_data(access_token, user_id): | ||
| 61 | ''' dowlnoad anilist user data''' | ||
| 62 | print("downloading user data") | ||
| 63 | anilist_user_data = get_user_data(access_token, user_id) | ||
| 64 | with open("response.json", "w") as response: | ||
| 65 | response.write(str(anilist_user_data)) | ||
| 66 | try: | ||
| 67 | if anilist_user_data['data'] == None: | ||
| 68 | print("Cannot process user data.") | ||
| 69 | print(anilist_user_data) | ||
| 70 | exit() | ||
| 71 | except: | ||
| 72 | pass | ||
| 73 | |||
| 74 | return anilist_user_data | ||
| 75 | |||
| 43 | def load_config(): | 76 | def load_config(): |
| 44 | command = """echo $(xdg-user-dir CONFIG)""" | 77 | command = """echo $(xdg-user-dir CONFIG)""" |
| 45 | 78 | ||
| @@ -112,51 +145,25 @@ presence_script_path="curddiscordpresence.py" | |||
| 112 | # with open(config_file_path, "r") as config_file: | 145 | # with open(config_file_path, "r") as config_file: |
| 113 | # return config_file.read() | 146 | # return config_file.read() |
| 114 | 147 | ||
| 115 | user_config = load_config() # python dictionary containing all the configs as key value pairs | 148 | # START OF SCRIPT |
| 116 | |||
| 117 | print("downloading user data") | ||
| 118 | anilist_user_data = get_user_data(access_token, user_id) | ||
| 119 | with open("response.json", "w") as response: | ||
| 120 | response.write(str(anilist_user_data)) | ||
| 121 | try: | ||
| 122 | if anilist_user_data['data'] == None: | ||
| 123 | print("Cannot process user data.") | ||
| 124 | print(anilist_user_data) | ||
| 125 | exit() | ||
| 126 | except: | ||
| 127 | pass | ||
| 128 | |||
| 129 | # Searching for anime | ||
| 130 | |||
| 131 | # with open("scripts/tmp/query", "w") as file: | ||
| 132 | # file.write(input("Enter the anime you want:\n")) | ||
| 133 | 149 | ||
| 134 | # os.system("./scripts/anime_list.sh") | 150 | user_config = load_config() # python dictionary containing all the configs as key value pairs |
| 135 | |||
| 136 | # with open('response.json', 'r') as file: | ||
| 137 | # data = json.load(file) | ||
| 138 | 151 | ||
| 139 | # print(data) | 152 | anilist_user_data = download_anilist_data(access_token, user_id) |
| 140 | # print(type(data['data'])) | ||
| 141 | 153 | ||
| 142 | anime_dict = extract_anime_info(anilist_user_data)[0] | 154 | anime_dict = extract_anime_info(anilist_user_data)[0] |
| 143 | # print(anime_dict) | ||
| 144 | # anime_dict = load_anime_data("scripts/tmp/anime_list") | ||
| 145 | # print(anime_dict) | ||
| 146 | # print(f"ANIME DICT = {anime_dict}") | ||
| 147 | 155 | ||
| 148 | select_anime(anime_dict) | 156 | select_anime(anime_dict) |
| 149 | 157 | ||
| 150 | with open("scripts/tmp/anime", "r") as anime: | 158 | anime_name = read_tmp("anime") |
| 151 | anime_name = anime.read() | ||
| 152 | 159 | ||
| 153 | with open("scripts/tmp/query", "w") as query_file: | 160 | write_to_tmp("query", anime_name) |
| 154 | query_file.write(anime_name) | ||
| 155 | 161 | ||
| 156 | run_script("anime_list") | 162 | run_script("anime_list") |
| 157 | 163 | ||
| 158 | anime_dict = load_anime_data("scripts/tmp/anime_list") | 164 | anime_dict = load_anime_data("scripts/tmp/anime_list") |
| 159 | 165 | ||
| 166 | #clean anime name | ||
| 160 | cleaned_text = re.sub(r'\(.*$', '', anime_name).strip() | 167 | cleaned_text = re.sub(r'\(.*$', '', anime_name).strip() |
| 161 | 168 | ||
| 162 | try: | 169 | try: |
| @@ -166,6 +173,10 @@ try: | |||
| 166 | progress = int(result['progress']) | 173 | progress = int(result['progress']) |
| 167 | title = result['english_title'] | 174 | title = result['english_title'] |
| 168 | total_episodes = result['episodes'] | 175 | total_episodes = result['episodes'] |
| 176 | duration = result['duration'] | ||
| 177 | except KeyboardInterrupt: | ||
| 178 | print("bye") | ||
| 179 | exit(0) | ||
| 169 | except: | 180 | except: |
| 170 | print("fucked up") | 181 | print("fucked up") |
| 171 | anime_id = 98460 | 182 | anime_id = 98460 |
| @@ -176,16 +187,15 @@ try: | |||
| 176 | print(finding_anime) | 187 | print(finding_anime) |
| 177 | if finding_anime: | 188 | if finding_anime: |
| 178 | print("AUTOMATICALLY BABY") | 189 | print("AUTOMATICALLY BABY") |
| 179 | with open("scripts/tmp/id", "w") as id_file: | 190 | write_to_tmp("id", finding_anime['allanime_id']) |
| 180 | id_file.write(finding_anime['allanime_id']) | 191 | write_to_tmp("anime", finding_anime['name']) |
| 181 | with open("scripts/tmp/anime", "w") as anime_name_file: | ||
| 182 | anime_name_file.write(finding_anime['name']) | ||
| 183 | else: | 192 | else: |
| 184 | print("Trying to automate") | 193 | print("Trying to automate") |
| 185 | with open("scripts/tmp/id", "w") as id_file: | 194 | write_to_tmp("id", anime_dict[anime_name+f" ({total_episodes} episodes)"]) |
| 186 | id_file.write(anime_dict[anime_name+f" ({total_episodes} episodes)"]) | 195 | write_to_tmp("anime", anime_name) |
| 187 | with open("scripts/tmp/anime", "w") as anime_name_file: | 196 | except KeyboardInterrupt: |
| 188 | anime_name_file.write(anime_name) | 197 | print("bye") |
| 198 | exit(0) | ||
| 189 | except: | 199 | except: |
| 190 | print("Cannot automate") | 200 | print("Cannot automate") |
| 191 | select_anime(anime_dict) | 201 | select_anime(anime_dict) |
| @@ -200,80 +210,56 @@ run_script("episode_list") | |||
| 200 | 210 | ||
| 201 | binge_watching = False | 211 | binge_watching = False |
| 202 | 212 | ||
| 203 | while True: | 213 | mpv_args = [] |
| 204 | 214 | ||
| 205 | mpv_args = [] | 215 | last_episode = int(read_tmp("episode_list").split()[-1]) |
| 206 | 216 | ||
| 207 | with open("scripts/tmp/episode_list") as ep_list: | 217 | anime_watch_history = get_all_anime(user_config['history_file']) |
| 208 | temp = ep_list.read() | 218 | # anime_watch_history[] |
| 209 | temp = temp.split() | 219 | anime_history = find_anime(anime_watch_history, anilist_id=anime_id, allanime_id=get_contents_of("id")) |
| 210 | last_episode = int(temp[-1]) | 220 | episode_completed = False |
| 211 | 221 | if anime_history: # if it exists in local history | |
| 212 | anime_watch_history = get_all_anime(user_config['history_file']) | 222 | print(f"came in history {str(progress)}") |
| 213 | 223 | if int(anime_history['episode']) != progress+1: # if the upstream progress is ahead | |
| 214 | # anime_watch_history[] | 224 | write_to_tmp("ep_no", str(int(progress)+1)) |
| 215 | anime_history = find_anime(anime_watch_history, anilist_id=anime_id, allanime_id=get_contents_of("id")) | 225 | print(f"Starting anime from upstream {str(progress + 1)}") |
| 216 | 226 | ||
| 217 | if anime_history: # if it exists and if the progress is not ahead | 227 | watching_ep = progress + 1 |
| 218 | print(f"came in history {str(progress)}") | 228 | |
| 219 | if int(anime_history['episode']) != progress+1: | 229 | elif int(anime_history['episode']) == int(progress)+1: # if the upstream progress is NOT ahead |
| 220 | with open("scripts/tmp/ep_no", "w") as ep_no_file_2: | 230 | mpv_args.append(f"--start={anime_history['time']}") |
| 221 | ep_no_file_2.write(str(int(progress)+1)) | 231 | write_to_tmp("ep_no", anime_history['episode']) |
| 222 | print(f"Starting anime from upstream {str(progress + 1)}") | 232 | print(f"STARTING ANIME FROM EPISODE {anime_history['episode']} {anime_history['time']}") |
| 223 | 233 | ||
| 224 | watching_ep = progress + 1 | 234 | watching_ep = int(anime_history['episode']) |
| 225 | 235 | else: # if there is no history | |
| 226 | elif int(anime_history['episode']) == int(progress)+1: | 236 | print("no history found") |
| 227 | mpv_args.append(f"--start={anime_history['time']}") | 237 | |
| 228 | with open("scripts/tmp/ep_no", "w") as ep_no_file_2: | 238 | print("First time watch") |
| 229 | ep_no_file_2.write(anime_history['episode']) | 239 | |
| 230 | print(f"STARTING ANIME FROM EPISODE {anime_history['episode']} {anime_history['time']}") | 240 | if progress == last_episode: |
| 231 | 241 | user_input = input("Do you want to start this anime from beginning? (y/n):") | |
| 232 | watching_ep = anime_history['episode'] | 242 | if user_input.lower() == "yes" or user_input.lower() == "y" or user_input.lower() == "": |
| 233 | # if : # if upstream is ahead | 243 | progress = 0 |
| 234 | # pass | ||
| 235 | else: # if there is no history | ||
| 236 | print("no history found") | ||
| 237 | if binge_watching: | ||
| 238 | print("binge watching") | ||
| 239 | with open("scripts/tmp/ep_no", "r") as ep_no_file_1: | ||
| 240 | current_ep = int(ep_no_file_1.read()) | ||
| 241 | if current_ep == last_episode: | ||
| 242 | print("completed anime.") | ||
| 243 | # TODO: Add way to rate the anime | ||
| 244 | delete_anime(user_config['history_file'], anime_id, get_contents_of("id")) | ||
| 245 | exit(0) | ||
| 246 | |||
| 247 | else: | ||
| 248 | print(f"Next episode is here, goshujin sama progress: {current_ep} {type(current_ep)}") | ||
| 249 | with open("scripts/tmp/ep_no", "w") as ep_no_file: | ||
| 250 | ep_no_file.write(str(int(current_ep)+1)) | ||
| 251 | |||
| 252 | watching_ep = current_ep+1 | ||
| 253 | else: | 244 | else: |
| 254 | print("First time watch") | 245 | print("Starting last episode of anime") |
| 255 | with open("scripts/tmp/ep_no", "w") as ep_no_file: | 246 | progress = last_episode - 1 |
| 256 | 247 | ||
| 257 | if progress == last_episode: | 248 | watching_ep = int(progress)+1 |
| 258 | user_input = input("Do you want to start this anime from beginning? (y/n):") | 249 | write_to_tmp("ep_no", str(watching_ep)) |
| 259 | if user_input.lower() == "yes" or user_input.lower() == "y" or user_input.lower() == "": | ||
| 260 | progress = 0 | ||
| 261 | else: | ||
| 262 | print("Starting last episode of anime") | ||
| 263 | progress = last_episode - 1 | ||
| 264 | 250 | ||
| 265 | watching_ep = progress+1 | 251 | # os.system("./scripts/episode_url.sh") |
| 266 | ep_no_file.write(str(watching_ep)) | 252 | run_script("episode_url") |
| 267 | 253 | ||
| 268 | os.system("./scripts/episode_url.sh") | 254 | # Print the result |
| 255 | if anime_id: | ||
| 256 | print(f"Anime ID: {anime_id}") | ||
| 257 | else: | ||
| 258 | print("Anime not found.") | ||
| 269 | 259 | ||
| 270 | # Print the result | 260 | links = load_links("scripts/tmp/links") |
| 271 | if anime_id: | ||
| 272 | print(f"Anime ID: {anime_id}") | ||
| 273 | else: | ||
| 274 | print("Anime not found.") | ||
| 275 | 261 | ||
| 276 | links = load_links("scripts/tmp/links") | 262 | while True: |
| 277 | 263 | ||
| 278 | try: | 264 | try: |
| 279 | salt = random.randint(0,1500) | 265 | salt = random.randint(0,1500) |
| @@ -288,7 +274,6 @@ while True: | |||
| 288 | 274 | ||
| 289 | mpv_duration_command = """echo '{ "command": ["get_property", "duration"] }' | socat - """+mpv_socket_path | 275 | mpv_duration_command = """echo '{ "command": ["get_property", "duration"] }' | socat - """+mpv_socket_path |
| 290 | 276 | ||
| 291 | |||
| 292 | while True: | 277 | while True: |
| 293 | time.sleep(2) | 278 | time.sleep(2) |
| 294 | 279 | ||
| @@ -302,42 +287,120 @@ while True: | |||
| 302 | try: | 287 | try: |
| 303 | data = json.loads(output) | 288 | data = json.loads(output) |
| 304 | if data["error"] == "success": | 289 | if data["error"] == "success": |
| 305 | playback_time = round(int(data["data"])) | 290 | playback_time = round(int(data["data"]), 2) |
| 306 | 291 | ||
| 307 | print(user_config['history_file']) | 292 | # print(user_config['history_file']) |
| 308 | 293 | ||
| 309 | update_anime(user_config['history_file'], str(anime_id), str(get_contents_of("id")), str(watching_ep), str(playback_time), str(title)) | 294 | update_anime(user_config['history_file'], str(anime_id), str(get_contents_of("id")), str(watching_ep), str(playback_time), str(duration), str(title)) |
| 310 | print("Playback time:", playback_time) | 295 | # print("Playback time:", playback_time) |
| 311 | 296 | ||
| 312 | watched_percentage = get_percentage_watched(mpv_socket_path) | 297 | watched_percentage = get_percentage_watched(mpv_socket_path) |
| 313 | print(watched_percentage) | 298 | print("watched percentage", watched_percentage) |
| 314 | if watched_percentage > 85: | 299 | if watched_percentage > mark_episode_as_completed_at: |
| 315 | print("Completed") | 300 | |
| 301 | episode_completed = True | ||
| 302 | binge_watching = True | ||
| 303 | else: | ||
| 304 | print(f"Episode not done: {watched_percentage} {mark_episode_as_completed_at}") | ||
| 316 | # video_duration = subprocess.run(mpv_duration_command, shell=True, capture_output=True, text=True) | 305 | # video_duration = subprocess.run(mpv_duration_command, shell=True, capture_output=True, text=True) |
| 317 | 306 | ||
| 318 | else: | 307 | else: |
| 319 | if data['error'] == "property unavailable": | 308 | if data['error'] == "property unavailable": # Stream has not started yet |
| 320 | pass | 309 | pass |
| 321 | else: | 310 | else: |
| 322 | print("Error:", data["error"]) | 311 | print("Error (330):", data["error"]) |
| 323 | break | 312 | break |
| 324 | except json.decoder.JSONDecodeError as e: | 313 | except json.decoder.JSONDecodeError as e: |
| 325 | print("Error decoding JSON:", e) | 314 | print("Error decoding JSON:", e) |
| 326 | break # Exit the loop on unexpected JSON error | 315 | break # Exit the loop on unexpected JSON error |
| 316 | except KeyboardInterrupt: | ||
| 317 | print("bye") | ||
| 318 | exit(0) | ||
| 319 | except Exception as e: | ||
| 320 | print("some error fucked everything up:\n", e) | ||
| 327 | else: | 321 | else: |
| 328 | killing_error = str(result.stderr) | 322 | killing_error = str(result.stderr) |
| 329 | if killing_error == "property unavailable": # mpv is not started yet | 323 | if killing_error == "property unavailable": # mpv is not started yet |
| 330 | print("passing") | 324 | print("passing") |
| 331 | pass | 325 | pass |
| 332 | else: | 326 | else: # Stream has ended (maybe) |
| 333 | print("Error:", killing_error) | 327 | print("Error (346):", killing_error) |
| 328 | # try: | ||
| 329 | |||
| 330 | # except Exception as e: | ||
| 331 | # print("fuck") | ||
| 332 | # print(f"Exception: {e}") | ||
| 334 | break | 333 | break |
| 335 | except ConnectionRefusedError: | 334 | except ConnectionRefusedError: |
| 336 | print("Player Closed") | 335 | print("Player Closed") |
| 336 | except KeyboardInterrupt: | ||
| 337 | if watched_percentage > mark_episode_as_completed_at: | ||
| 338 | watching_ep = int(watching_ep)+1 | ||
| 339 | |||
| 340 | update_anime(user_config['history_file'], str(anime_id), str(get_contents_of("id")), str(watching_ep), "0", str(duration), str(title)) | ||
| 341 | |||
| 342 | print("bye") | ||
| 343 | exit(0) | ||
| 337 | finally: | 344 | finally: |
| 338 | to_continue_or_not_to_continue = input("Continue? (y/n)\n") | 345 | binge_watching=True |
| 339 | 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 == "": | 346 | |
| 340 | binge_watching = True | 347 | print("binge watching") |
| 348 | current_ep = int(read_tmp("ep_no")) | ||
| 349 | |||
| 350 | if current_ep == last_episode: | ||
| 351 | print("completed anime.") | ||
| 352 | # TODO: Add way to rate the anime | ||
| 353 | delete_anime(user_config['history_file'], anime_id, get_contents_of("id")) | ||
| 354 | exit(0) | ||
| 355 | |||
| 356 | else: | ||
| 357 | print(f"Next episode is here, goshujin sama progress: {current_ep} {type(current_ep)}") | ||
| 358 | # write_to_tmp("ep_no", str(int(current_ep)+1)) | ||
| 359 | |||
| 360 | if watched_percentage > mark_episode_as_completed_at: | ||
| 361 | last_episode = int(read_tmp("episode_list").split()[-1]) | ||
| 362 | |||
| 363 | watching_ep = int(watching_ep)+1 | ||
| 364 | write_to_tmp("ep_no", str(watching_ep)) | ||
| 365 | print("here") | ||
| 366 | update_anime(user_config['history_file'], str(anime_id), str(get_contents_of("id")), str(watching_ep), "0", str(duration), str(title)) | ||
| 367 | print("inside") | ||
| 368 | print(f"Episode done: {type(watching_ep)}") | ||
| 369 | print(f"Episode done: {int(watching_ep)}") | ||
| 370 | |||
| 371 | anime_watch_history = get_all_anime(user_config['history_file']) | ||
| 372 | # anime_watch_history[] | ||
| 373 | anime_history = find_anime(anime_watch_history, anilist_id=anime_id, allanime_id=get_contents_of("id")) | ||
| 374 | episode_completed = False | ||
| 375 | if anime_history: # if it exists in local history | ||
| 376 | print(f"came in history {str(anime_history['episode'])}") | ||
| 377 | print("is ahead") | ||
| 378 | # write_to_tmp("ep_no", str(int(progress)+1)) | ||
| 379 | # print(f"Starting anime from upstream {str(progress + 1)}") | ||
| 380 | |||
| 381 | # watching_ep = progress + 1 | ||
| 382 | |||
| 383 | # elif int(anime_history['episode']) == int(progress)+1: # if the upstream progress is NOT ahead | ||
| 384 | # print("is equal") | ||
| 385 | mpv_args.append(f"--start={anime_history['time']}") | ||
| 386 | # write_to_tmp("ep_no", watching_ep) | ||
| 387 | print(f"STARTING ANIME FROM EPISODE {anime_history['episode']} {anime_history['time']}") | ||
| 388 | watching_ep = int(anime_history['episode']) | ||
| 389 | |||
| 390 | print("watching ep", watching_ep) | ||
| 391 | |||
| 392 | run_script("episode_url") | ||
| 393 | links = load_links("scripts/tmp/links") | ||
| 394 | mpv_args = [] | ||
| 395 | # watching_ep = current_ep+1 | ||
| 396 | |||
| 397 | # to_continue_or_not_to_continue = input("Continue? (y/n)\n") | ||
| 398 | # try: | ||
| 399 | # # print("percentage watched",percentage_watched(playback_time, duration), "%") | ||
| 400 | # except: | ||
| 401 | # pass | ||
| 402 | # 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 == "": | ||
| 403 | # binge_watching = True | ||
| 341 | # ep_no_file | 404 | # ep_no_file |
| 342 | else: | 405 | # else: |
| 343 | break | 406 | # break |