Commit b4d27eaa5e

b4d27eaa5e0fdb85948ffe9cbeaa258c6a4fa9f8

parent: abb4ae56c3

Unsigned

Wraient <wraient.911@gmail.com> · 2024-09-03 19:10 UTC

Implemented:
track anime watching in history file
verbose error handling for no config file
Error handling for/and history file
Remove unneccesary comments
variable to find which episode is being watched

Layout: unified · split

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