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
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
22from track_anime import add_anime, update_anime, get_all_anime, delete_anime, find_anime
2323
2424access_token = os.environ.get('ANILIST_ACCESS_TOKEN')
2525user_id = os.environ.get('ANILIST_USER_ID')
@@ -166,11 +166,20 @@ except:
166166 progress = 0
167167
168168try:
169 print("Trying to automate")
170 with open("scripts/tmp/id", "w") as id_file:
171 id_file.write(anime_dict[anime_name+f" ({total_episodes} episodes)"])
172 with open("scripts/tmp/anime", "w") as anime_name_file:
173 anime_name_file.write(anime_name)
169 finding_anime = find_anime(get_all_anime(user_config["history_file"]), anilist_id=str(anime_id))
170 print(finding_anime)
171 if finding_anime:
172 print("AUTOMATICALLY BABY")
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)
174183except:
175184 print("Cannot automate")
176185 select_anime(anime_dict)
@@ -186,41 +195,62 @@ run_script("episode_list")
186195binge_watching = False
187196
188197while True:
198
199 mpv_args = []
200
189201 with open("scripts/tmp/episode_list") as ep_list:
190202 temp = ep_list.read()
191203 temp = temp.split()
192204 last_episode = int(temp[-1])
193205
194 if binge_watching:
195 print("binge watching")
196 with open("scripts/tmp/ep_no", "r") as ep_no_file_1:
197 current_ep = int(ep_no_file_1.read())
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)
206 anime_watch_history = get_all_anime(user_config['history_file'])
207
208 # anime_watch_history[]
209 anime_history = find_anime(anime_watch_history, anilist_id=anime_id, allanime_id=get_contents_of("id"))
203210
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
204240 else:
205 print(f"Next episode is here, goshujin sama progress: {current_ep} {type(current_ep)}")
241 print("First time watch")
206242 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:
213243
214 if progress == last_episode:
215 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() == "":
217 progress = 0
218 else:
219 print("Starting last episode of anime")
220 progress = last_episode - 1
244 if progress == last_episode:
245 user_input = input("Do you want to start this anime from beginning? (y/n):")
246 if user_input.lower() == "yes" or user_input.lower() == "y" or user_input.lower() == "":
247 progress = 0
248 else:
249 print("Starting last episode of anime")
250 progress = last_episode - 1
221251
222 watching_ep = progress+1
223 ep_no_file.write(str(watching_ep))
252 watching_ep = progress+1
253 ep_no_file.write(str(watching_ep))
224254
225255 os.system("./scripts/episode_url.sh")
226256
@@ -233,15 +263,15 @@ while True:
233263 links = load_links("scripts/tmp/links")
234264
235265 try:
236 salt = random.randint(0,500)
266 salt = random.randint(0,1500)
237267 print("SALT IS:"+str(salt))
238 start_video(links[0][1], salt)
239 command = """echo '{ "command": ["get_property", "playback-time"] }' | socat - /tmp/mpvsocket"""+str(salt)
268 start_video(links[0][1], salt, mpv_args)
269 connect_mpv_command = """echo '{ "command": ["get_property", "playback-time"] }' | socat - /tmp/mpvsocket"""+str(salt)
240270
241271 while True:
242272 time.sleep(2)
243273
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)
245275 # print(result)
246276 if result.returncode == 0:
247277 output = result.stdout.strip()
@@ -258,15 +288,18 @@ while True:
258288 update_anime(user_config['history_file'], str(anime_id), str(get_contents_of("id")), str(watching_ep), str(playback_time), str(title))
259289 print("Playback time:", playback_time)
260290 else:
261 print("Error:", data["error"])
262 break
263
291 if data['error'] == "property unavailable":
292 pass
293 else:
294 print("Error:", data["error"])
295 break
264296 except json.decoder.JSONDecodeError as e:
265297 print("Error decoding JSON:", e)
266298 break # Exit the loop on unexpected JSON error
267299 else:
268300 killing_error = str(result.stderr)
269301 if killing_error == "property unavailable": # mpv is not started yet
302 print("passing")
270303 pass
271304 else:
272305 print("Error:", killing_error)