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
1919from select_anime import extract_anime_info
2020from select_anime import select_anime
2121
22from track_anime import add_anime, update_anime, get_all_anime, delete_anime
2223
2324access_token = os.environ.get('ANILIST_ACCESS_TOKEN')
2425user_id = os.environ.get('ANILIST_USER_ID')
@@ -26,6 +27,10 @@ user_id = os.environ.get('ANILIST_USER_ID')
2627# print(access_token)
2728# print(user_id)
2829
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
2934def run_script(script):
3035 os.system(f"./scripts/{script}.sh")
3136
@@ -45,8 +50,10 @@ def load_config():
4550 print("Creating config")
4651
4752 try:
53 print("making folder")
4854 os.makedirs(folder_name)
4955 except:
56 print("error making folder")
5057 pass
5158
5259 with open(config_file_path, 'w') as file:
@@ -59,7 +66,7 @@ score_on_completion=true
5966discord_presence=true
6067presence_script_path="curddiscordpresence.py"
6168""")
62
69
6370 config_dict = {}
6471
6572 with open(config_file_path, 'r') as file:
@@ -83,6 +90,16 @@ presence_script_path="curddiscordpresence.py"
8390
8491 # Store in the dictionary
8592 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
86103 return config_dict
87104
88105 # else:
@@ -91,8 +108,6 @@ presence_script_path="curddiscordpresence.py"
91108
92109user_config = load_config() # python dictionary containing all the configs as key value pairs
93110
94print(user_config)
95
96111print("downloading user data")
97112anilist_user_data = get_user_data(access_token, user_id)
98113with open("response.json", "w") as response:
@@ -160,10 +175,6 @@ except:
160175 print("Cannot automate")
161176 select_anime(anime_dict)
162177
163# Remove everything from the last '(' including the parentheses
164
165
166# os.system("./scripts/episode_list.sh")
167178run_script("episode_list")
168179
169180# with open("scripts/tmp/episode_list") as ep_list:
@@ -186,14 +197,16 @@ while True:
186197 current_ep = int(ep_no_file_1.read())
187198 if current_ep == last_episode:
188199 print("completed anime.")
189
190200 # TODO: Add way to rate the anime
201 delete_anime(user_config['history_file'], anime_id, get_contents_of("id"))
191202 exit(0)
192203
193204 else:
194205 print(f"Next episode is here, goshujin sama progress: {current_ep} {type(current_ep)}")
195206 with open("scripts/tmp/ep_no", "w") as ep_no_file:
196207 ep_no_file.write(str(int(current_ep)+1))
208
209 watching_ep = current_ep+1
197210 else:
198211 print("First time watch")
199212 with open("scripts/tmp/ep_no", "w") as ep_no_file:
@@ -205,10 +218,9 @@ while True:
205218 else:
206219 print("Starting last episode of anime")
207220 progress = last_episode - 1
208 # else:
209221
210 # print(progress)
211 ep_no_file.write(str(progress+1))
222 watching_ep = progress+1
223 ep_no_file.write(str(watching_ep))
212224
213225 os.system("./scripts/episode_url.sh")
214226
@@ -218,9 +230,6 @@ while True:
218230 else:
219231 print("Anime not found.")
220232
221 # with open("scripts/tmp/link", 'r') as links:
222 # link = links.read()
223
224233 links = load_links("scripts/tmp/links")
225234
226235 try:
@@ -243,6 +252,10 @@ while True:
243252 data = json.loads(output)
244253 if data["error"] == "success":
245254 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))
246259 print("Playback time:", playback_time)
247260 else:
248261 print("Error:", data["error"])
@@ -252,9 +265,12 @@ while True:
252265 print("Error decoding JSON:", e)
253266 break # Exit the loop on unexpected JSON error
254267 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
258274 except ConnectionRefusedError:
259275 print("Player Closed")
260276 finally: