Commit 1ee89b307c

1ee89b307cdd74cb6a1d76fc7babb7a472c901a0

parent: a5fbb4e3fb

Unsigned

Wraient <wraient.911@gmail.com> · 2024-09-22 01:05 UTC

refactor way to get user config and create user config

Layout: unified · split

curd.py +49 −62
@@ -74,7 +74,37 @@ def download_anilist_data(access_token, user_id):
7474
7575 return anilist_user_data
7676
77def load_config():
77default_config = {
78 "player":"mpv",
79 "show_adult_content":False,
80 "history_file":"$HOME/.local/share/curd/curd_history.txt",
81 "subs_language":"english",
82 "sub_or_dub":"sub",
83 "next_episode_prompt":False,
84 "score_on_completion":False,
85 "save_mpv_speed": True,
86 "discord_presence": True,
87 "presence_script_path":"curddiscordpresence.py"
88}
89
90def get_userconfig_value(userconfig:dict, key:str):
91 return userconfig.get(key, default_config.get(key))
92
93def create_default_user_config(default_config):
94 output_string = ""
95 for key, value in default_config.items():
96 # Convert Python True/False to lowercase true/false
97 if isinstance(value, bool):
98 value_str = 'true' if value else 'false'
99 else:
100 value_str = str(value)
101
102 # Write the key=value to the file with no spaces
103 output_string+=f"{key}={value_str}\n"
104
105 return output_string
106
107def load_config() -> dict:
78108 command = """echo $(xdg-user-dir CONFIG)"""
79109
80110 result = subprocess.run(command, shell=True, capture_output=True, text=True)
@@ -97,16 +127,7 @@ def load_config():
97127 pass
98128
99129 with open(config_file_path, 'w') as file:
100 file.write(r"""player="mpv"
101show_adult_content=false
102history_file="$HOME/.local/share/curd/curd_history.txt"
103subs_language="english"
104sub_or_dub="sub"
105score_on_completion=true
106save_mpv_speed=true
107discord_presence=true
108presence_script_path="curddiscordpresence.py"
109""")
130 file.write(create_default_user_config(default_config))
110131
111132 config_dict = {}
112133
@@ -150,23 +171,14 @@ presence_script_path="curddiscordpresence.py"
150171# START OF SCRIPT
151172
152173user_config = load_config() # python dictionary containing all the configs as key value pairs
153
154174anilist_user_data = download_anilist_data(access_token, user_id)
155
156175anime_dict = extract_anime_info(anilist_user_data)[0]
157
158176select_anime(anime_dict)
159
160177anime_name = read_tmp("anime")
161
162178write_to_tmp("query", anime_name)
163
164179run_script("anime_list")
165
166180anime_dict = load_anime_data("scripts/tmp/anime_list")
167
168#clean anime name
169cleaned_text = re.sub(r'\(.*$', '', anime_name).strip()
181cleaned_text = re.sub(r'\(.*$', '', anime_name).strip() # clean anime name
170182
171183try:
172184 result = search_anime_by_title(anilist_user_data, cleaned_text)[0]
@@ -184,7 +196,7 @@ except Exception as e:
184196 exit(1)
185197
186198try:
187 finding_anime = find_anime(get_all_anime(user_config["history_file"]), anilist_id=str(media_id))
199 finding_anime = find_anime(get_all_anime(get_userconfig_value(user_config, "history_file")), anilist_id=str(media_id))
188200 # print(finding_anime)
189201 if finding_anime:
190202 print("Found anime in history")
@@ -203,29 +215,19 @@ except:
203215
204216run_script("episode_list")
205217
206# with open("scripts/tmp/episode_list") as ep_list:
207# temp = ep_list.read()
208# temp = temp.split()
209# last_episode = temp[-1]
210# ep_no = input(f"Enter episode number: (number of episodes: {last_episode})\n")
211
212218binge_watching = False
213
214219mpv_args = []
215
216220last_episode = int(read_tmp("episode_list").split()[-1])
217
218anime_watch_history = get_all_anime(user_config['history_file'])
219# anime_watch_history[]
221anime_watch_history = get_all_anime(get_userconfig_value(user_config, 'history_file'))
220222anime_history = find_anime(anime_watch_history, anilist_id=media_id, allanime_id=get_contents_of("id"))
221223episode_completed = False
222224rewatching = False
225
223226if anime_history: # if it exists in local history
224227 # print(f"came in history {str(progress)}")
225228 if int(anime_history['episode']) != progress+1: # if the upstream progress is ahead
226229 write_to_tmp("ep_no", str(int(progress)+1))
227230 # print(f"Starting anime from upstream {str(progress + 1)}")
228
229231 watching_ep = progress + 1
230232
231233 elif int(anime_history['episode']) == int(progress)+1: # if the upstream progress is NOT ahead
@@ -265,18 +267,13 @@ while True:
265267 salt = random.randint(0,1500)
266268 # print("SALT IS:"+str(salt))
267269 start_video(links[0][1], salt, mpv_args)
268
269270 mpv_socket_path = "/tmp/mpvsocket"+str(salt)
270
271271 connect_mpv_command = """echo '{ "command": ["get_property", "playback-time"] }' | socat - """+mpv_socket_path
272
273272 mpv_pos_command = """echo '{ "command": ["get_property", "time-pos"] }' | socat - """+mpv_socket_path
274
275273 mpv_duration_command = """echo '{ "command": ["get_property", "duration"] }' | socat - """+mpv_socket_path
276274
277275 while True:
278276 time.sleep(2)
279
280277 result = subprocess.run(connect_mpv_command, shell=True, capture_output=True, text=True)
281278 # print(result)
282279 if result.returncode == 0:
@@ -288,18 +285,13 @@ while True:
288285 data = json.loads(output)
289286 if data["error"] == "success":
290287 playback_time = round(int(data["data"]), 2)
291
292 # print(user_config['history_file'])
293
294 update_anime(user_config['history_file'], str(media_id), str(get_contents_of("id")), str(watching_ep), str(playback_time), str(duration), str(title))
288 update_anime(get_userconfig_value(user_config, 'history_file'), str(media_id), str(get_contents_of("id")), str(watching_ep), str(playback_time), str(duration), str(title))
295289 # print("Playback time:", playback_time)
296
297290 watched_percentage = get_percentage_watched(mpv_socket_path)
298291 mpv_playback_speed = get_mpv_playback_speed(mpv_socket_path)
299292 # print("mpv speed", mpv_playback_speed)
300293 # print("watched percentage", watched_percentage)
301294 if watched_percentage > mark_episode_as_completed_at:
302
303295 episode_completed = True
304296 binge_watching = True
305297 # else:
@@ -353,7 +345,7 @@ while True:
353345 if watched_percentage > mark_episode_as_completed_at and not rewatching:
354346 update_anime_progress(access_token, int(media_id), int(watching_ep))
355347 watching_ep = int(watching_ep)+1
356 update_anime(user_config['history_file'], str(media_id), str(get_contents_of("id")), str(watching_ep), "0", str(duration), str(title))
348 update_anime(get_userconfig_value(user_config, 'history_file'), str(media_id), str(get_contents_of("id")), str(watching_ep), "0", str(duration), str(title))
357349
358350 print("bye")
359351 exit(0)
@@ -364,12 +356,12 @@ while True:
364356 current_ep = int(read_tmp("ep_no"))
365357
366358 if current_ep == last_episode:
367 if binge_watching == True and not rewatching and user_config['score_on_completion'] == True: # Completed anime
359 if binge_watching == True and not rewatching and get_userconfig_value(user_config, 'score_on_completion') == True: # Completed anime
368360 anime_rating_by_user = input("Rate this anime:\n")
369361 update_anime_progress(access_token, int(media_id), int(watching_ep))
370362 rate_anime(access_token, media_id, anime_rating_by_user)
371363 print("completed anime.")
372 delete_anime(user_config['history_file'], media_id, get_contents_of("id"))
364 delete_anime(get_userconfig_value(user_config, 'history_file'), media_id, get_contents_of("id"))
373365 exit(0)
374366
375367 else:
@@ -382,8 +374,8 @@ while True:
382374 update_anime_progress(access_token, int(media_id), int(watching_ep))
383375 watching_ep = int(watching_ep)+1
384376 write_to_tmp("ep_no", str(watching_ep))
385 update_anime(user_config['history_file'], str(media_id), str(get_contents_of("id")), str(watching_ep), "0", str(duration), str(title))
386 anime_watch_history = get_all_anime(user_config['history_file'])
377 update_anime(get_userconfig_value(user_config, 'history_file'), str(media_id), str(get_contents_of("id")), str(watching_ep), "0", str(duration), str(title))
378 anime_watch_history = get_all_anime(get_userconfig_value(user_config, 'history_file'))
387379 anime_history = find_anime(anime_watch_history, anilist_id=media_id, allanime_id=get_contents_of("id"))
388380 episode_completed = False
389381 run_script("episode_url")
@@ -391,18 +383,13 @@ while True:
391383 mpv_args = []
392384 watched_percentage = 0
393385
394 if user_config['save_mpv_speed']==True:
386 if get_userconfig_value(user_config, 'save_mpv_speed') ==True:
395387 mpv_args.append(f'--speed={mpv_playback_speed}')
396388
397389 # watching_ep = current_ep+1
398
399 # to_continue_or_not_to_continue = input("Continue? (y/n)\n")
400 # try:
401 # # print("percentage watched",percentage_watched(playback_time, duration), "%")
402 # except:
403 # pass
404 # if to_continue_or_not_to_continue.lower() == "yes" or to_continue_or_not_to_continue.lower() == "y" or to_continue_or_not_to_continue == "":
405 # binge_watching = True
406 # ep_no_file
407 # else:
408 # break
390 if get_userconfig_value(user_config, 'next_episode_prompt') == True:
391 to_continue_or_not_to_continue = input("Continue? (y/n)\n")
392 if to_continue_or_not_to_continue.lower() == "yes" or to_continue_or_not_to_continue.lower() == "y" or to_continue_or_not_to_continue == "":
393 binge_watching = True
394 else:
395 exit(0)
\ No newline at end of file