Commit b4d27eaa5e
Unsigned
Layout: unified · split
curd.py +33 −17
| @@ -19,6 +19,7 @@ from select_anime import load_anime_data | ||
| 19 | 19 | from select_anime import extract_anime_info |
| 20 | 20 | from select_anime import select_anime |
| 21 | 21 | |
| 22 | from track_anime import add_anime, update_anime, get_all_anime, delete_anime | |
| 22 | 23 | |
| 23 | 24 | access_token = os.environ.get('ANILIST_ACCESS_TOKEN') |
| 24 | 25 | user_id = os.environ.get('ANILIST_USER_ID') |
| @@ -26,6 +27,10 @@ user_id = os.environ.get('ANILIST_USER_ID') | ||
| 26 | 27 | # print(access_token) |
| 27 | 28 | # print(user_id) |
| 28 | 29 | |
| 30 | def get_contents_of(tmp_file_name): | |
| 31 | with open(f"scripts/tmp/{tmp_file_name}", "r") as temp_file: | |
| 32 | return temp_file.read() | |
| 33 | ||
| 29 | 34 | def run_script(script): |
| 30 | 35 | os.system(f"./scripts/{script}.sh") |
| 31 | 36 | |
| @@ -45,8 +50,10 @@ def load_config(): | ||
| 45 | 50 | print("Creating config") |
| 46 | 51 | |
| 47 | 52 | try: |
| 53 | print("making folder") | |
| 48 | 54 | os.makedirs(folder_name) |
| 49 | 55 | except: |
| 56 | print("error making folder") | |
| 50 | 57 | pass |
| 51 | 58 | |
| 52 | 59 | with open(config_file_path, 'w') as file: |
| @@ -59,7 +66,7 @@ score_on_completion=true | ||
| 59 | 66 | discord_presence=true |
| 60 | 67 | presence_script_path="curddiscordpresence.py" |
| 61 | 68 | """) |
| 62 | ||
| 69 | ||
| 63 | 70 | config_dict = {} |
| 64 | 71 | |
| 65 | 72 | with open(config_file_path, 'r') as file: |
| @@ -83,6 +90,16 @@ presence_script_path="curddiscordpresence.py" | ||
| 83 | 90 | |
| 84 | 91 | # Store in the dictionary |
| 85 | 92 | config_dict[key] = value |
| 93 | ||
| 94 | if not os.path.exists(config_dict['history_file']): | |
| 95 | try: | |
| 96 | os.makedirs(os.path.dirname(config_dict['history_file'])) | |
| 97 | except: | |
| 98 | pass | |
| 99 | print("creating history file") | |
| 100 | with open(config_dict["history_file"], "w") as history_file: | |
| 101 | history_file.write("") | |
| 102 | ||
| 86 | 103 | return config_dict |
| 87 | 104 | |
| 88 | 105 | # else: |
| @@ -91,8 +108,6 @@ presence_script_path="curddiscordpresence.py" | ||
| 91 | 108 | |
| 92 | 109 | user_config = load_config() # python dictionary containing all the configs as key value pairs |
| 93 | 110 | |
| 94 | print(user_config) | |
| 95 | ||
| 96 | 111 | print("downloading user data") |
| 97 | 112 | anilist_user_data = get_user_data(access_token, user_id) |
| 98 | 113 | with open("response.json", "w") as response: |
| @@ -160,10 +175,6 @@ except: | ||
| 160 | 175 | print("Cannot automate") |
| 161 | 176 | select_anime(anime_dict) |
| 162 | 177 | |
| 163 | # Remove everything from the last '(' including the parentheses | |
| 164 | ||
| 165 | ||
| 166 | # os.system("./scripts/episode_list.sh") | |
| 167 | 178 | run_script("episode_list") |
| 168 | 179 | |
| 169 | 180 | # with open("scripts/tmp/episode_list") as ep_list: |
| @@ -186,14 +197,16 @@ while True: | ||
| 186 | 197 | current_ep = int(ep_no_file_1.read()) |
| 187 | 198 | if current_ep == last_episode: |
| 188 | 199 | print("completed anime.") |
| 189 | ||
| 190 | 200 | # TODO: Add way to rate the anime |
| 201 | delete_anime(user_config['history_file'], anime_id, get_contents_of("id")) | |
| 191 | 202 | exit(0) |
| 192 | 203 | |
| 193 | 204 | else: |
| 194 | 205 | print(f"Next episode is here, goshujin sama progress: {current_ep} {type(current_ep)}") |
| 195 | 206 | with open("scripts/tmp/ep_no", "w") as ep_no_file: |
| 196 | 207 | ep_no_file.write(str(int(current_ep)+1)) |
| 208 | ||
| 209 | watching_ep = current_ep+1 | |
| 197 | 210 | else: |
| 198 | 211 | print("First time watch") |
| 199 | 212 | with open("scripts/tmp/ep_no", "w") as ep_no_file: |
| @@ -205,10 +218,9 @@ while True: | ||
| 205 | 218 | else: |
| 206 | 219 | print("Starting last episode of anime") |
| 207 | 220 | progress = last_episode - 1 |
| 208 | # else: | |
| 209 | 221 | |
| 210 | # print(progress) | |
| 211 | ep_no_file.write(str(progress+1)) | |
| 222 | watching_ep = progress+1 | |
| 223 | ep_no_file.write(str(watching_ep)) | |
| 212 | 224 | |
| 213 | 225 | os.system("./scripts/episode_url.sh") |
| 214 | 226 | |
| @@ -218,9 +230,6 @@ while True: | ||
| 218 | 230 | else: |
| 219 | 231 | print("Anime not found.") |
| 220 | 232 | |
| 221 | # with open("scripts/tmp/link", 'r') as links: | |
| 222 | # link = links.read() | |
| 223 | ||
| 224 | 233 | links = load_links("scripts/tmp/links") |
| 225 | 234 | |
| 226 | 235 | try: |
| @@ -243,6 +252,10 @@ while True: | ||
| 243 | 252 | data = json.loads(output) |
| 244 | 253 | if data["error"] == "success": |
| 245 | 254 | playback_time = round(int(data["data"])) |
| 255 | ||
| 256 | print(user_config['history_file']) | |
| 257 | ||
| 258 | update_anime(user_config['history_file'], str(anime_id), str(get_contents_of("id")), str(watching_ep), str(playback_time), str(title)) | |
| 246 | 259 | print("Playback time:", playback_time) |
| 247 | 260 | else: |
| 248 | 261 | print("Error:", data["error"]) |
| @@ -252,9 +265,12 @@ while True: | ||
| 252 | 265 | print("Error decoding JSON:", e) |
| 253 | 266 | break # Exit the loop on unexpected JSON error |
| 254 | 267 | else: |
| 255 | print("Error:", result.stderr) | |
| 256 | break | |
| 257 | ||
| 268 | killing_error = str(result.stderr) | |
| 269 | if killing_error == "property unavailable": # mpv is not started yet | |
| 270 | pass | |
| 271 | else: | |
| 272 | print("Error:", killing_error) | |
| 273 | break | |
| 258 | 274 | except ConnectionRefusedError: |
| 259 | 275 | print("Player Closed") |
| 260 | 276 | finally: |