Commit 77af5a89c6

77af5a89c603faf3f2cd053028deb39fed03949a

parent: a9ca5a3f9c

Unsigned

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

Parse anime history and use it

Layout: unified · split

curd.py +71 −38
@@ -19,7 +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 22from track_anime import add_anime, update_anime, get_all_anime, delete_anime, find_anime
23 23
24access_token = os.environ.get('ANILIST_ACCESS_TOKEN') 24access_token = os.environ.get('ANILIST_ACCESS_TOKEN')
25user_id = os.environ.get('ANILIST_USER_ID') 25user_id = os.environ.get('ANILIST_USER_ID')
@@ -166,11 +166,20 @@ except:
166 progress = 0 166 progress = 0
167 167
168try: 168try:
169 print("Trying to automate") 169 finding_anime = find_anime(get_all_anime(user_config["history_file"]), anilist_id=str(anime_id))
170 with open("scripts/tmp/id", "w") as id_file: 170 print(finding_anime)
171 id_file.write(anime_dict[anime_name+f" ({total_episodes} episodes)"]) 171 if finding_anime:
172 with open("scripts/tmp/anime", "w") as anime_name_file: 172 print("AUTOMATICALLY BABY")
173 anime_name_file.write(anime_name) 173 with open("scripts/tmp/id", "w") as id_file:
174 id_file.write(finding_anime['allanime_id'])
175 with open("scripts/tmp/anime", "w") as anime_name_file:
176 anime_name_file.write(finding_anime['name'])
177 else:
178 print("Trying to automate")
179 with open("scripts/tmp/id", "w") as id_file:
180 id_file.write(anime_dict[anime_name+f" ({total_episodes} episodes)"])
181 with open("scripts/tmp/anime", "w") as anime_name_file:
182 anime_name_file.write(anime_name)
174except: 183except:
175 print("Cannot automate") 184 print("Cannot automate")
176 select_anime(anime_dict) 185 select_anime(anime_dict)
@@ -186,41 +195,62 @@ run_script("episode_list")
186binge_watching = False 195binge_watching = False
187 196
188while True: 197while True:
198
199 mpv_args = []
200
189 with open("scripts/tmp/episode_list") as ep_list: 201 with open("scripts/tmp/episode_list") as ep_list:
190 temp = ep_list.read() 202 temp = ep_list.read()
191 temp = temp.split() 203 temp = temp.split()
192 last_episode = int(temp[-1]) 204 last_episode = int(temp[-1])
193 205
194 if binge_watching: 206 anime_watch_history = get_all_anime(user_config['history_file'])
195 print("binge watching") 207
196 with open("scripts/tmp/ep_no", "r") as ep_no_file_1: 208 # anime_watch_history[]
197 current_ep = int(ep_no_file_1.read()) 209 anime_history = find_anime(anime_watch_history, anilist_id=anime_id, allanime_id=get_contents_of("id"))
198 if current_ep == last_episode:
199 print("completed anime.")
200 # TODO: Add way to rate the anime
201 delete_anime(user_config['history_file'], anime_id, get_contents_of("id"))
202 exit(0)
203 210
211 if anime_history and not (int(anime_history['episode']) < progress): # if it exists and if the progress is not ahead
212 print(f"came in history {str(progress)}")
213 if int(anime_history['episode']) == int(progress)+1:
214 mpv_args.append(f"--start={anime_history['time']}")
215 with open("scripts/tmp/ep_no", "w") as ep_no_file_2:
216 ep_no_file_2.write(anime_history['episode'])
217 print(f"STARTING ANIME FROM EPISODE {anime_history['episode']} {anime_history['time']}")
218
219 watching_ep = anime_history['episode']
220 # if : # if upstream is ahead
221 # pass
222 else: # if there is no history
223 print("no history found")
224 if binge_watching:
225 print("binge watching")
226 with open("scripts/tmp/ep_no", "r") as ep_no_file_1:
227 current_ep = int(ep_no_file_1.read())
228 if current_ep == last_episode:
229 print("completed anime.")
230 # TODO: Add way to rate the anime
231 delete_anime(user_config['history_file'], anime_id, get_contents_of("id"))
232 exit(0)
233
234 else:
235 print(f"Next episode is here, goshujin sama progress: {current_ep} {type(current_ep)}")
236 with open("scripts/tmp/ep_no", "w") as ep_no_file:
237 ep_no_file.write(str(int(current_ep)+1))
238
239 watching_ep = current_ep+1
204 else: 240 else:
205 print(f"Next episode is here, goshujin sama progress: {current_ep} {type(current_ep)}") 241 print("First time watch")
206 with open("scripts/tmp/ep_no", "w") as ep_no_file: 242 with open("scripts/tmp/ep_no", "w") as ep_no_file:
207 ep_no_file.write(str(int(current_ep)+1))
208
209 watching_ep = current_ep+1
210 else:
211 print("First time watch")
212 with open("scripts/tmp/ep_no", "w") as ep_no_file:
213 243
214 if progress == last_episode: 244 if progress == last_episode:
215 user_input = input("Do you want to start this anime from beginning? (y/n):") 245 user_input = input("Do you want to start this anime from beginning? (y/n):")
216 if user_input.lower() == "yes" or user_input.lower() == "y" or user_input.lower() == "": 246 if user_input.lower() == "yes" or user_input.lower() == "y" or user_input.lower() == "":
217 progress = 0 247 progress = 0
218 else: 248 else:
219 print("Starting last episode of anime") 249 print("Starting last episode of anime")
220 progress = last_episode - 1 250 progress = last_episode - 1
221 251
222 watching_ep = progress+1 252 watching_ep = progress+1
223 ep_no_file.write(str(watching_ep)) 253 ep_no_file.write(str(watching_ep))
224 254
225 os.system("./scripts/episode_url.sh") 255 os.system("./scripts/episode_url.sh")
226 256
@@ -233,15 +263,15 @@ while True:
233 links = load_links("scripts/tmp/links") 263 links = load_links("scripts/tmp/links")
234 264
235 try: 265 try:
236 salt = random.randint(0,500) 266 salt = random.randint(0,1500)
237 print("SALT IS:"+str(salt)) 267 print("SALT IS:"+str(salt))
238 start_video(links[0][1], salt) 268 start_video(links[0][1], salt, mpv_args)
239 command = """echo '{ "command": ["get_property", "playback-time"] }' | socat - /tmp/mpvsocket"""+str(salt) 269 connect_mpv_command = """echo '{ "command": ["get_property", "playback-time"] }' | socat - /tmp/mpvsocket"""+str(salt)
240 270
241 while True: 271 while True:
242 time.sleep(2) 272 time.sleep(2)
243 273
244 result = subprocess.run(command, shell=True, capture_output=True, text=True) 274 result = subprocess.run(connect_mpv_command, shell=True, capture_output=True, text=True)
245 # print(result) 275 # print(result)
246 if result.returncode == 0: 276 if result.returncode == 0:
247 output = result.stdout.strip() 277 output = result.stdout.strip()
@@ -258,15 +288,18 @@ while True:
258 update_anime(user_config['history_file'], str(anime_id), str(get_contents_of("id")), str(watching_ep), str(playback_time), str(title)) 288 update_anime(user_config['history_file'], str(anime_id), str(get_contents_of("id")), str(watching_ep), str(playback_time), str(title))
259 print("Playback time:", playback_time) 289 print("Playback time:", playback_time)
260 else: 290 else:
261 print("Error:", data["error"]) 291 if data['error'] == "property unavailable":
262 break 292 pass
263 293 else:
294 print("Error:", data["error"])
295 break
264 except json.decoder.JSONDecodeError as e: 296 except json.decoder.JSONDecodeError as e:
265 print("Error decoding JSON:", e) 297 print("Error decoding JSON:", e)
266 break # Exit the loop on unexpected JSON error 298 break # Exit the loop on unexpected JSON error
267 else: 299 else:
268 killing_error = str(result.stderr) 300 killing_error = str(result.stderr)
269 if killing_error == "property unavailable": # mpv is not started yet 301 if killing_error == "property unavailable": # mpv is not started yet
302 print("passing")
270 pass 303 pass
271 else: 304 else:
272 print("Error:", killing_error) 305 print("Error:", killing_error)