Added everything to a main function and cleaned up the code.

This commit is contained in:
CronyAkatsuki 2021-10-31 21:14:21 +01:00
parent 1a4e7bdaef
commit 3f11eb3f59

566
adl.py
View File

@ -1,5 +1,253 @@
import os, subprocess, sys, argparse, signal import os, subprocess, sys, argparse, signal
# required files
DN = os.path.dirname(os.path.realpath(__file__)) # get current directory of the script
GOOD_TITLES = open(DN + "/good_title.txt").readlines() # the list of good titles
PROBLEMATIC_TITLES = open(DN + "/problem_title.txt").readlines() # list of problematic titles
FZF_FILE = open(DN + "/fzf.txt", "w+") # temp file for fzf
FZF_FILE_PATH = DN +"/fzf.txt" # path of the temp file
PRINT_FZF_PATH = "python " + DN + "/print_fzf.py" # print the fzf file
# exit function
def exit_adl():
FZF_FILE.close()
os.remove(FZF_FILE_PATH)
sys.exit()
def interupt_command(signum, frame):
exit_adl()
# colored print
def color_print(text):
print("\033[0;36m" + text + " \033[0m")
# colored watch primpt
def watch_prompt(title, episode, msg):
print("Now " + msg + " \033[0;34m" + title + "\033[0m, episode \033[0;34m" + str(episode) + " \033[0m")
# colored input
def color_prommpt(text):
return input("\033[0;34m" + text + "\033[0m")
# retrieve new list
def retrieve_list(account):
color_print ("Running trackma retrieve for account " + account + "...")
subprocess.call("trackma -a " + account + " retrieve")
subprocess.call("cls", shell=True)
# retrieve updated list
def retrieve_list_update(account):
color_print("Running trackma retrieve for account " + account + " to get the updated list...")
subprocess.call("trackma -a " + account + " retrieve")
subprocess.call("cls", shell=True)
# load list
def load_list(account):
alist = subprocess.getoutput("trackma -a " + account + " list").splitlines()
alist.pop(0)
alist.pop()
return alist
# write list to a file
def list2file(list, file):
for line in list:
file.write(line)
if not list.index(line) == len(list) - 1 :
file.write("\n")
# exit prompt
def exit_ask():
while True:
subprocess.call("cls", shell=True)
choice = color_prommpt("Want to watch another anime? [Y/n]: ")
if choice == "N" or choice == "n":
exit_adl()
elif choice == "Y" or choice == "y" or choice == "":
return
# check for problematic title
def check_title(title):
for problem in PROBLEMATIC_TITLES:
if problem.__contains__(title):
title = GOOD_TITLES[PROBLEMATIC_TITLES.index(problem)]
return title
# get your title
def get_title(choice):
choice = choice[9:96]
choice = choice.rstrip(".")
title = check_title(choice)
return title
# get episode
def get_episode(choice):
choice = choice[98:100]
return int(choice)
# get score
def get_score(choice):
choice = choice[108:110]
return choice
# watch animes
def watch(title, episode, download, provider, player):
cmd = 'anime dl "' + title + '" --episodes ' + episode
if not download:
cmd += ' --play ' + player
if not provider == "":
cmd += ' --provider ' + provider
subprocess.run(cmd)
# next episode
def next_episode(title,episode, msg, download, provider, player):
if not download:
watch_next = True
while watch_next:
episode = episode + 1
watch_prompt(title, str(episode), msg)
watch(title, str(episode), download, provider, player)
while True:
color_print("Current watched episode: " + str(episode))
yn = color_prommpt("Wanna watch next episode? [Y/n]: ")
if yn == "Y" or yn == "y" or yn == "":
break
elif yn == "N" or yn == "n":
watch_next = False
break
else:
episode = episode + 1
watch_prompt(title, str(episode), msg)
watch(title, str(episode), download, provider, player)
# all from last watched
def all_from_last(title,episode, msg, download, provider, player):
watch_prompt(title, str(episode) + " all left episodes", msg)
watch(title, str(episode + 1) + ':', download, provider, player)
# all episode
def all_episodes(title, msg, download, provider, player):
watch_prompt(title, "all", msg)
watch(title, '1:', download, provider, player)
# watch from custom range
def custom_episode_range(title, msg, download, provider, player):
begginig = color_prommpt("Beggining of interval?: ")
end = color_prommpt("End of interval?: ")
watch_prompt(title, begginig + " to " + end, msg)
watch(title, begginig + ':' + end, download, provider, player)
# add to last watched m
def next_plus_n(title, episode, action, msg, download, provider, player):
watch_prompt(title, str(episode + int(action)), msg)
watch(title, str(episode + int(action)), download, provider, player)
# rewatch current episode
def rewatch_episode(title, episode, msg, download, provider, player):
watch_prompt(title, str(episode), msg)
watch(title, str(episode), download, provider, player)
# watch custom episode
def custom_episode(title, msg, download,provider, player):
episode = color_prommpt("Enter custom episode: ")
watch_prompt(title, episode, msg)
watch(title, episode, download, provider, player)
# update title
def update_title(title, episode, account):
color_print("Current episode for " + title + " is " + str(episode))
custom = color_prommpt("Enter updated episode number: ")
if custom != "":
subprocess.call('trackma -a ' + account + ' update "' + title + '" ' + custom)
subprocess.call('trackma -a' + account + ' send')
retrieve_list_update(account)
else:
color_print("Skipping updating...")
# update score
def update_score(title, score, account):
color_print("Current score for " + title + " is " + score)
custom = color_prommpt("Enter updated score: ")
if custom != "":
subprocess.call('trackma -a ' + account + ' score "' + title + '" ' + custom)
subprocess.call('trackma -a' + account + ' send')
retrieve_list_update(account)
else:
color_print("Skipping updating...")
# update question
def update_question(title, episode, score, account):
while True:
color_print("Skipping watching episodes. Modifing entry.")
choice = color_prommpt("Update episode number or update score [E/s]: ")
if choice == "e" or choice == "E" or choice == "":
update_title(title, episode, account)
break
elif choice == "s" or choice == "S":
update_score(title, score, account)
break
# ask if you wanna continus watching
def wanna_continu_watch(download):
while True:
if not download:
yn = color_prommpt("Wanna continue watching? [Y/n]: ")
else:
yn = color_prommpt("Wanna continue downloading? [Y/n]: ")
if yn == "y" or yn == "Y" or yn == "":
return True
elif yn == "n" or yn == "N":
return False
# ask if you wanna update title meta after watch
def wanna_update_title_after_watch(title, episode, score, download, account):
if not download:
while True:
yn = color_prommpt("Wanna update episode number or update score of watched anime? [N/e/s]: ")
if yn == "E" or yn == "e":
update_title(title, episode, account)
break
elif yn == "S" or yn == "s":
update_score(title, score, account)
break
elif yn == "N" or yn == "n" or yn == "":
break
# choose what to do with episode
def choose_episode():
subprocess.call("cls", shell=True)
color_print("Enter lowercase or uppercase to issue command:")
color_print(" N - Next episode (default, press <ENTER>)")
color_print(" L - from current to Last known:")
color_print(" A - All available, from episode 1")
color_print(" I - custom Interval (range) of episodes")
color_print(" 0-9 - Plus n episodes relative to last seen (type number)")
color_print(" R - Rewatch/redownload current episode in list")
color_print(" C - Custom episode")
color_print(" U - Update entry chosen instead of streaming")
color_print(" S - Skip. Choose another show.")
return color_prommpt("Your choice? [N/l/a/i/0-9/r/c/u/s]: ")
def choose_episode_specific_show():
subprocess.call("cls", shell=True)
color_print("Enter lowercase or uppercase to issue command:")
color_print(" A - All available, from episode 1")
color_print(" I - custom Interval (range) of episodes")
color_print(" C - Custom episode")
color_print(" S - Skip. Exit adl.")
return color_prommpt("Your choice? [A/i/c/s]: ")
def main():
signal.signal(signal.SIGINT, interupt_command)
# setup env variables for better readability of outputs
os.environ['LINES'] = '25'
os.environ['COLUMNS'] = '120'
# argument parser # argument parser
ap = argparse.ArgumentParser() ap = argparse.ArgumentParser()
@ -20,7 +268,7 @@ ap.add_argument("-v", "--version", required=False, nargs='?', const=True,
args = vars(ap.parse_args()) args = vars(ap.parse_args())
print(args) # print(args)
# get player # get player
if args["player"]: if args["player"]:
@ -66,280 +314,31 @@ else:
# print the version # print the version
if args["version"]: if args["version"]:
print("Py-adl version 1") print("Py-adl version 1.1")
sys.exit() sys.exit()
# required files
dn = os.path.dirname(os.path.realpath(__file__)) # get current directory of the script
good_title = open(dn + "/good_title.txt").readlines() # the list of good titles
problematic_titles = open(dn + "/problem_title.txt").readlines() # list of problematic titles
fzf_file = open(dn + "/fzf.txt", "w+") # temp file for fzf
fzf_file_path = dn +"/fzf.txt" # path of the temp file
print_fzf_path = "python " + dn + "/print_fzf.py" # print the fzf file
retrieve = True retrieve = True
# setup env variables for better readability of outputs
os.environ['LINES'] = '25'
os.environ['COLUMNS'] = '120'
# exit function
def exit_adl():
fzf_file.close()
os.remove(fzf_file_path)
sys.exit()
def interupt_command(signum, frame):
exit_adl()
signal.signal(signal.SIGINT, interupt_command)
# colored print
def color_print(text):
print("\033[0;36m" + text + " \033[0m")
# colored watch primpt
def watch_prompt(title, episode):
print("Now " + msg + " \033[0;34m" + title + "\033[0m, episode \033[0;34m" + str(episode) + " \033[0m")
# colored input
def color_prommpt(text):
return input("\033[0;34m" + text + "\033[0m")
# retrieve new list
def retrieve_list():
color_print ("Running trackma retrieve for account " + account + "...")
subprocess.call("trackma -a " + account + " retrieve")
subprocess.call("cls", shell=True)
# retrieve updated list
def retrieve_list_update():
color_print("Running trackma retrieve for account " + account + " to get the updated list...")
subprocess.call("trackma -a " + account + " retrieve")
subprocess.call("cls", shell=True)
# load list
def load_list():
alist = subprocess.getoutput("trackma -a " + account + " list").splitlines()
alist.pop(0)
alist.pop()
return alist
# write list to a file
def list2file(list, file):
for line in list:
file.write(line)
if not list.index(line) == len(list) - 1 :
file.write("\n")
# exit prompt
def exit_ask():
while True:
subprocess.call("cls", shell=True)
choice = color_prommpt("Want to watch another anime? [Y/n]: ")
if choice == "N" or choice == "n":
exit_adl()
elif choice == "Y" or choice == "y" or choice == "":
return
# check for problematic title
def check_title(title):
for problem in problematic_titles:
if problem.__contains__(title):
title = good_title[problematic_titles.index(problem)]
return title
# get your title
def get_title(choice):
choice = choice[9:96]
choice = choice.rstrip(".")
title = check_title(choice)
return title
# get episode
def get_episode(choice):
choice = choice[98:100]
return int(choice)
# get all episodes
def get_all_episodes(choice):
choice = choice[103:105]
return choice
# get score
def get_score(choice):
choice = choice[108:110]
return choice
# watch animes
def watch(title, episode):
cmd = 'anime dl "' + title + '" --episodes ' + episode
if not download:
cmd += ' --play ' + player
if not provider == "":
cmd += ' --provider ' + provider
subprocess.run(cmd)
# next episode
def next_episode(title,episode):
if not download:
watch_next = True
while watch_next:
episode = episode + 1
watch_prompt(title, str(episode))
watch(title, str(episode))
while True:
color_print("Current watched episode: " + str(episode))
yn = color_prommpt("Wanna watch next episode? [Y/n]: ")
if yn == "Y" or yn == "y" or yn == "":
break
elif yn == "N" or yn == "n":
watch_next = False
break
else:
episode = episode + 1
watch_prompt(title, str(episode))
watch(title, str(episode))
# all from last watched
def all_from_last(title,episode):
watch_prompt(title, str(episode) + " all left episodes")
watch(title, str(episode + 1) + ':')
# all episode
def all_episodes(title):
watch_prompt(title, "all")
watch(title, '1:')
# watch from custom range
def custom_episode_range(title):
begginig = color_prommpt("Beggining of interval?: ")
end = color_prommpt("End of interval?: ")
watch_prompt(title, begginig + " to " + end)
watch(title, begginig + ':' + end)
# add to last watched m
def next_plus_n(title, episode, action):
watch_prompt(title, str(episode + int(action)))
watch(title, str(episode + int(action)) )
# rewatch current episode
def rewatch_episode(title, episode):
watch_prompt(title, str(episode))
watch(title, str(episode))
# watch custom episode
def custom_episode(title):
episode = color_prommpt("Enter custom episode: ")
watch_prompt(title, episode)
watch(title, episode)
# update title
def update_title(title, episode):
color_print("Current episode for " + title + " is " + str(episode))
custom = color_prommpt("Enter updated episode number: ")
if custom != "":
subprocess.call('trackma -a ' + account + ' update "' + title + '" ' + custom)
subprocess.call('trackma -a' + account + ' send')
retrieve_list_update()
else:
color_print("Skipping updating...")
# update score
def update_score(title, score):
color_print("Current score for " + title + " is " + score)
custom = color_prommpt("Enter updated score: ")
if custom != "":
subprocess.call('trackma -a ' + account + ' score "' + title + '" ' + custom)
subprocess.call('trackma -a' + account + ' send')
retrieve_list_update()
else:
color_print("Skipping updating...")
# update question
def update_question(title, episode, score):
while True:
color_print("Skipping watching episodes. Modifing entry.")
choice = color_prommpt("Update episode number or update score [E/s]: ")
if choice == "e" or choice == "E" or choice == "":
update_title(title, episode)
break
elif choice == "s" or choice == "S":
update_score(title, score)
break
# ask if you wanna continus watching
def wanna_continu_watch():
while True:
if not download:
yn = color_prommpt("Wanna continue watching? [Y/n]: ")
else:
yn = color_prommpt("Wanna continue downloading? [Y/n]: ")
if yn == "y" or yn == "Y" or yn == "":
return True
elif yn == "n" or yn == "N":
return False
# ask if you wanna update title meta after watch
def wanna_update_title_after_watch(title, episode, score):
if not download:
while True:
yn = color_prommpt("Wanna update episode number or update score of watched anime? [N/e/s]: ")
if yn == "E" or yn == "e":
update_title(title, episode)
break
elif yn == "S" or yn == "s":
update_score(title, score)
break
elif yn == "N" or yn == "n" or yn == "":
break
# choose what to do with episode
def choose_episode():
subprocess.call("cls", shell=True)
color_print("Enter lowercase or uppercase to issue command:")
color_print(" N - Next episode (default, press <ENTER>)")
color_print(" L - from current to Last known:")
color_print(" A - All available, from episode 1")
color_print(" I - custom Interval (range) of episodes")
color_print(" 0-9 - Plus n episodes relative to last seen (type number)")
color_print(" R - Rewatch/redownload current episode in list")
color_print(" C - Custom episode")
color_print(" U - Update entry chosen instead of streaming")
color_print(" S - Skip. Choose another show.")
return color_prommpt("Your choice? [N/l/a/i/0-9/r/c/u/s]: ")
def choose_episode_specific_show():
subprocess.call("cls", shell=True)
color_print("Enter lowercase or uppercase to issue command:")
color_print(" A - All available, from episode 1")
color_print(" I - custom Interval (range) of episodes")
color_print(" C - Custom episode")
color_print(" S - Skip. Exit adl.")
return color_prommpt("Your choice? [A/i/c/s]: ")
if not show == "" and not episode == 0: if not show == "" and not episode == 0:
watch(show, str(episode)) watch(show, str(episode), download, provider, player)
elif not show == "": elif not show == "":
while True: while True:
# choose what to do with the choosen anime # choose what to do with the choosen anime
action = choose_episode_specific_show() action = choose_episode_specific_show()
if action == "a" or action == "A" or action == "": if action == "a" or action == "A" or action == "":
all_episodes(show) all_episodes(show, msg, download, provider, player)
exit_adl() exit_adl()
# custom range of episodes # custom range of episodes
elif action == "i" or action == "I": elif action == "i" or action == "I":
custom_episode_range(show) custom_episode_range(show, msg, download, provider, player)
if wanna_continu_watch(): if wanna_continu_watch(download):
continue continue
else: else:
exit_adl() exit_adl()
# watch custom episode # watch custom episode
elif action == "c" or action == "C": elif action == "c" or action == "C":
custom_episode(show) custom_episode(show, msg, download, provider, player)
if wanna_continu_watch(): if wanna_continu_watch(download):
continue continue
else: else:
exit_adl() exit_adl()
@ -351,28 +350,26 @@ else:
while True: while True:
# retrieving the list on start # retrieving the list on start
if retrieve: if retrieve:
retrieve_list() retrieve_list(account)
retrieve = False retrieve = False
# get the list of anime # get the list of anime
alist = load_list() alist = load_list(account)
# write list to file # write list to file
list2file(alist, fzf_file) list2file(alist, FZF_FILE)
# reload file (I Guess ??) # reload file (I Guess ??)
fzf_file.seek(0) FZF_FILE.seek(0)
# get choice from fzf # get choice from fzf
choice = subprocess.getoutput(print_fzf_path + ' | fzf --ansi --reverse --prompt "Choose anime to watch: "') choice = subprocess.getoutput(PRINT_FZF_PATH + ' | fzf --ansi --reverse --prompt "Choose anime to watch: "')
if choice: if choice:
# get the title # get the title
title = get_title(choice) title = get_title(choice)
# get current episode # get current episode
episode = get_episode(choice) episode = get_episode(choice)
# get latest episode
last_episode = get_all_episodes(choice)
# get current score # get current score
score = get_score(choice) score = get_score(choice)
@ -382,61 +379,61 @@ else:
action = choose_episode() action = choose_episode()
# watch next episode # watch next episode
if action == "n" or action == "N" or action == "": if action == "n" or action == "N" or action == "":
next_episode(title, episode) next_episode(title, episode, msg, download,provider,player)
wanna_update_title_after_watch(title, episode, score) wanna_update_title_after_watch(title, episode, score, download, account)
exit_ask() exit_ask()
break break
# watch all left episodes # watch all left episodes
elif action == "l" or action == "L": elif action == "l" or action == "L":
all_from_last(title, episode) all_from_last(title, episode, msg, download,provider,player)
wanna_update_title_after_watch(title, episode, score) wanna_update_title_after_watch(title, episode, score, download, account)
exit_ask() exit_ask()
break break
# watch every episode available # watch every episode available
elif action == "a" or action == "A": elif action == "a" or action == "A":
all_episodes(title) all_episodes(title, msg, download,provider,player)
wanna_update_title_after_watch(title, episode, score) wanna_update_title_after_watch(title, episode, score, download, account)
exit_ask() exit_ask()
break break
# custom range of episodes # custom range of episodes
elif action == "i" or action == "I": elif action == "i" or action == "I":
custom_episode_range(title) custom_episode_range(title, msg, download,provider,player)
if wanna_continu_watch(): if wanna_continu_watch(download):
continue continue
else: else:
wanna_update_title_after_watch(title, episode, score) wanna_update_title_after_watch(title, episode, score, download, account)
exit_ask() exit_ask()
break break
# something? # something?
elif action == "1" or action == "2" or action == "3" or action == "4" or action == "5" or action == "6" or action == "7" or action == "8" or action == "9": elif action == "1" or action == "2" or action == "3" or action == "4" or action == "5" or action == "6" or action == "7" or action == "8" or action == "9":
next_plus_n(title, episode, action) next_plus_n(title, episode, action, msg, download,provider,player)
if wanna_continu_watch(): if wanna_continu_watch(download):
continue continue
else: else:
wanna_update_title_after_watch(title, episode, score) wanna_update_title_after_watch(title, episode, score, download, account)
exit_ask() exit_ask()
break break
# rewatch current episode # rewatch current episode
elif action == "r" or action == "R": elif action == "r" or action == "R":
rewatch_episode(title, episode) rewatch_episode(title, episode, msg, download,provider,player)
if wanna_continu_watch(): if wanna_continu_watch(download):
continue continue
else: else:
wanna_update_title_after_watch(title, episode, score) wanna_update_title_after_watch(title, episode, score, download, account)
exit_ask() exit_ask()
break break
# watch custom episode # watch custom episode
elif action == "c" or action == "C": elif action == "c" or action == "C":
custom_episode(title) custom_episode(title, msg, download,provider,player)
if wanna_continu_watch(): if wanna_continu_watch(download):
continue continue
else: else:
wanna_update_title_after_watch(title, episode, score) wanna_update_title_after_watch(title, episode, score, download, account)
exit_ask() exit_ask()
break break
# update anime meta # update anime meta
elif action == "u" or action == "U": elif action == "u" or action == "U":
update_question(title, episode, score) update_question(title, episode, score, account)
exit_ask() exit_ask()
break break
# skip the anime # skip the anime
@ -446,3 +443,6 @@ else:
exit_ask() exit_ask()
exit_adl() exit_adl()
if __name__ == "__main__" :
main()