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

494
adl.py
View File

@ -1,104 +1,30 @@
import os, subprocess, sys, argparse, signal import os, subprocess, sys, argparse, signal
# argument parser
ap = argparse.ArgumentParser()
ap.add_argument("-p", "--player", required=False,
help="Define player used for streaming. Ex: \033[0;36mpyadl -p mpv\033[0m")
ap.add_argument("-i", "--provider", required=False,
help="Define provider used for streaming (check \033[0;36m$anime dl --help\033[0m for providers list)")
ap.add_argument("-s", "--show", required=False,
help='Watch custom show. Ep nr optional, careful with the quotes. Ex: \033[0;36m$adl -s "gegege 2018"\033[0m')
ap.add_argument("-n", "--number", required=False,
help='Specify episode number that will be used with "-s / --show" option. Ex: \033[0;36m$adl -s "gegege 2018" -n "4"\033[0m')
ap.add_argument("-a", "--account", required=False,
help="By default trackma will use account 1. Use '-a 2' for example to change trackma account")
ap.add_argument("-d", "--download", required=False, type=bool, nargs='?', const=True, default=False,
help="Download instead of streaming")
ap.add_argument("-v", "--version", required=False, nargs='?', const=True,
help="Display version and exit")
args = vars(ap.parse_args())
print(args)
# get player
if args["player"]:
player = str(args["player"]) # get player from user
else:
player = "mpv" # default player
# get provider
if args['provider']:
provider = str(args["provider"])
else:
provider = ""
# get show
if args['show']:
show = str(args["show"])
else:
show = ""
# get episode
if args['number']:
if args['number'] and args['show']:
episode = int(args['number'])
else:
print("You need to also specify a show name to use this option")
sys.exit()
else:
episode = 0
# get account
if args['account']:
account = str(int(args["account"]) - 1) # take the account from input
else:
account = "0" # default account
# enable downloading
if args["download"]:
download = True # enable downloading
msg = "downloading" # download message
else:
download = False # specify whether to download or not
msg = "watching" # msg for the watch prompt
# print the version
if args["version"]:
print("Py-adl version 1")
sys.exit()
# required files # required files
dn = os.path.dirname(os.path.realpath(__file__)) # get current directory of the script 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 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 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 = open(DN + "/fzf.txt", "w+") # temp file for fzf
fzf_file_path = dn +"/fzf.txt" # path of the temp file FZF_FILE_PATH = DN +"/fzf.txt" # path of the temp file
print_fzf_path = "python " + dn + "/print_fzf.py" # print the fzf file PRINT_FZF_PATH = "python " + DN + "/print_fzf.py" # print the fzf file
retrieve = True
# setup env variables for better readability of outputs
os.environ['LINES'] = '25'
os.environ['COLUMNS'] = '120'
# exit function # exit function
def exit_adl(): def exit_adl():
fzf_file.close() FZF_FILE.close()
os.remove(fzf_file_path) os.remove(FZF_FILE_PATH)
sys.exit() sys.exit()
def interupt_command(signum, frame): def interupt_command(signum, frame):
exit_adl() exit_adl()
signal.signal(signal.SIGINT, interupt_command)
# colored print # colored print
def color_print(text): def color_print(text):
print("\033[0;36m" + text + " \033[0m") print("\033[0;36m" + text + " \033[0m")
# colored watch primpt # colored watch primpt
def watch_prompt(title, episode): def watch_prompt(title, episode, msg):
print("Now " + msg + " \033[0;34m" + title + "\033[0m, episode \033[0;34m" + str(episode) + " \033[0m") print("Now " + msg + " \033[0;34m" + title + "\033[0m, episode \033[0;34m" + str(episode) + " \033[0m")
# colored input # colored input
@ -106,19 +32,19 @@ def color_prommpt(text):
return input("\033[0;34m" + text + "\033[0m") return input("\033[0;34m" + text + "\033[0m")
# retrieve new list # retrieve new list
def retrieve_list(): def retrieve_list(account):
color_print ("Running trackma retrieve for account " + account + "...") color_print ("Running trackma retrieve for account " + account + "...")
subprocess.call("trackma -a " + account + " retrieve") subprocess.call("trackma -a " + account + " retrieve")
subprocess.call("cls", shell=True) subprocess.call("cls", shell=True)
# retrieve updated list # retrieve updated list
def retrieve_list_update(): def retrieve_list_update(account):
color_print("Running trackma retrieve for account " + account + " to get the updated list...") color_print("Running trackma retrieve for account " + account + " to get the updated list...")
subprocess.call("trackma -a " + account + " retrieve") subprocess.call("trackma -a " + account + " retrieve")
subprocess.call("cls", shell=True) subprocess.call("cls", shell=True)
# load list # load list
def load_list(): def load_list(account):
alist = subprocess.getoutput("trackma -a " + account + " list").splitlines() alist = subprocess.getoutput("trackma -a " + account + " list").splitlines()
alist.pop(0) alist.pop(0)
alist.pop() alist.pop()
@ -143,9 +69,9 @@ def exit_ask():
# check for problematic title # check for problematic title
def check_title(title): def check_title(title):
for problem in problematic_titles: for problem in PROBLEMATIC_TITLES:
if problem.__contains__(title): if problem.__contains__(title):
title = good_title[problematic_titles.index(problem)] title = GOOD_TITLES[PROBLEMATIC_TITLES.index(problem)]
return title return title
# get your title # get your title
@ -160,18 +86,13 @@ def get_episode(choice):
choice = choice[98:100] choice = choice[98:100]
return int(choice) return int(choice)
# get all episodes
def get_all_episodes(choice):
choice = choice[103:105]
return choice
# get score # get score
def get_score(choice): def get_score(choice):
choice = choice[108:110] choice = choice[108:110]
return choice return choice
# watch animes # watch animes
def watch(title, episode): def watch(title, episode, download, provider, player):
cmd = 'anime dl "' + title + '" --episodes ' + episode cmd = 'anime dl "' + title + '" --episodes ' + episode
if not download: if not download:
@ -183,13 +104,13 @@ def watch(title, episode):
subprocess.run(cmd) subprocess.run(cmd)
# next episode # next episode
def next_episode(title,episode): def next_episode(title,episode, msg, download, provider, player):
if not download: if not download:
watch_next = True watch_next = True
while watch_next: while watch_next:
episode = episode + 1 episode = episode + 1
watch_prompt(title, str(episode)) watch_prompt(title, str(episode), msg)
watch(title, str(episode)) watch(title, str(episode), download, provider, player)
while True: while True:
color_print("Current watched episode: " + str(episode)) color_print("Current watched episode: " + str(episode))
yn = color_prommpt("Wanna watch next episode? [Y/n]: ") yn = color_prommpt("Wanna watch next episode? [Y/n]: ")
@ -200,78 +121,78 @@ def next_episode(title,episode):
break break
else: else:
episode = episode + 1 episode = episode + 1
watch_prompt(title, str(episode)) watch_prompt(title, str(episode), msg)
watch(title, str(episode)) watch(title, str(episode), download, provider, player)
# all from last watched # all from last watched
def all_from_last(title,episode): def all_from_last(title,episode, msg, download, provider, player):
watch_prompt(title, str(episode) + " all left episodes") watch_prompt(title, str(episode) + " all left episodes", msg)
watch(title, str(episode + 1) + ':') watch(title, str(episode + 1) + ':', download, provider, player)
# all episode # all episode
def all_episodes(title): def all_episodes(title, msg, download, provider, player):
watch_prompt(title, "all") watch_prompt(title, "all", msg)
watch(title, '1:') watch(title, '1:', download, provider, player)
# watch from custom range # watch from custom range
def custom_episode_range(title): def custom_episode_range(title, msg, download, provider, player):
begginig = color_prommpt("Beggining of interval?: ") begginig = color_prommpt("Beggining of interval?: ")
end = color_prommpt("End of interval?: ") end = color_prommpt("End of interval?: ")
watch_prompt(title, begginig + " to " + end) watch_prompt(title, begginig + " to " + end, msg)
watch(title, begginig + ':' + end) watch(title, begginig + ':' + end, download, provider, player)
# add to last watched m # add to last watched m
def next_plus_n(title, episode, action): def next_plus_n(title, episode, action, msg, download, provider, player):
watch_prompt(title, str(episode + int(action))) watch_prompt(title, str(episode + int(action)), msg)
watch(title, str(episode + int(action)) ) watch(title, str(episode + int(action)), download, provider, player)
# rewatch current episode # rewatch current episode
def rewatch_episode(title, episode): def rewatch_episode(title, episode, msg, download, provider, player):
watch_prompt(title, str(episode)) watch_prompt(title, str(episode), msg)
watch(title, str(episode)) watch(title, str(episode), download, provider, player)
# watch custom episode # watch custom episode
def custom_episode(title): def custom_episode(title, msg, download,provider, player):
episode = color_prommpt("Enter custom episode: ") episode = color_prommpt("Enter custom episode: ")
watch_prompt(title, episode) watch_prompt(title, episode, msg)
watch(title, episode) watch(title, episode, download, provider, player)
# update title # update title
def update_title(title, episode): def update_title(title, episode, account):
color_print("Current episode for " + title + " is " + str(episode)) color_print("Current episode for " + title + " is " + str(episode))
custom = color_prommpt("Enter updated episode number: ") custom = color_prommpt("Enter updated episode number: ")
if custom != "": if custom != "":
subprocess.call('trackma -a ' + account + ' update "' + title + '" ' + custom) subprocess.call('trackma -a ' + account + ' update "' + title + '" ' + custom)
subprocess.call('trackma -a' + account + ' send') subprocess.call('trackma -a' + account + ' send')
retrieve_list_update() retrieve_list_update(account)
else: else:
color_print("Skipping updating...") color_print("Skipping updating...")
# update score # update score
def update_score(title, score): def update_score(title, score, account):
color_print("Current score for " + title + " is " + score) color_print("Current score for " + title + " is " + score)
custom = color_prommpt("Enter updated score: ") custom = color_prommpt("Enter updated score: ")
if custom != "": if custom != "":
subprocess.call('trackma -a ' + account + ' score "' + title + '" ' + custom) subprocess.call('trackma -a ' + account + ' score "' + title + '" ' + custom)
subprocess.call('trackma -a' + account + ' send') subprocess.call('trackma -a' + account + ' send')
retrieve_list_update() retrieve_list_update(account)
else: else:
color_print("Skipping updating...") color_print("Skipping updating...")
# update question # update question
def update_question(title, episode, score): def update_question(title, episode, score, account):
while True: while True:
color_print("Skipping watching episodes. Modifing entry.") color_print("Skipping watching episodes. Modifing entry.")
choice = color_prommpt("Update episode number or update score [E/s]: ") choice = color_prommpt("Update episode number or update score [E/s]: ")
if choice == "e" or choice == "E" or choice == "": if choice == "e" or choice == "E" or choice == "":
update_title(title, episode) update_title(title, episode, account)
break break
elif choice == "s" or choice == "S": elif choice == "s" or choice == "S":
update_score(title, score) update_score(title, score, account)
break break
# ask if you wanna continus watching # ask if you wanna continus watching
def wanna_continu_watch(): def wanna_continu_watch(download):
while True: while True:
if not download: if not download:
yn = color_prommpt("Wanna continue watching? [Y/n]: ") yn = color_prommpt("Wanna continue watching? [Y/n]: ")
@ -283,15 +204,15 @@ def wanna_continu_watch():
return False return False
# ask if you wanna update title meta after watch # ask if you wanna update title meta after watch
def wanna_update_title_after_watch(title, episode, score): def wanna_update_title_after_watch(title, episode, score, download, account):
if not download: if not download:
while True: while True:
yn = color_prommpt("Wanna update episode number or update score of watched anime? [N/e/s]: ") yn = color_prommpt("Wanna update episode number or update score of watched anime? [N/e/s]: ")
if yn == "E" or yn == "e": if yn == "E" or yn == "e":
update_title(title, episode) update_title(title, episode, account)
break break
elif yn == "S" or yn == "s": elif yn == "S" or yn == "s":
update_score(title, score) update_score(title, score, account)
break break
elif yn == "N" or yn == "n" or yn == "": elif yn == "N" or yn == "n" or yn == "":
break break
@ -320,129 +241,208 @@ def choose_episode_specific_show():
color_print(" S - Skip. Exit adl.") color_print(" S - Skip. Exit adl.")
return color_prommpt("Your choice? [A/i/c/s]: ") return color_prommpt("Your choice? [A/i/c/s]: ")
if not show == "" and not episode == 0: def main():
watch(show, str(episode)) signal.signal(signal.SIGINT, interupt_command)
elif not show == "":
while True:
# choose what to do with the choosen anime
action = choose_episode_specific_show()
if action == "a" or action == "A" or action == "":
all_episodes(show)
exit_adl()
# custom range of episodes
elif action == "i" or action == "I":
custom_episode_range(show)
if wanna_continu_watch():
continue
else:
exit_adl()
# watch custom episode
elif action == "c" or action == "C":
custom_episode(show)
if wanna_continu_watch():
continue
else:
exit_adl()
# skip the anime
elif action == "s" or action == "S":
exit_adl()
else:
# main loop
while True:
# retrieving the list on start
if retrieve:
retrieve_list()
retrieve = False
# get the list of anime # setup env variables for better readability of outputs
alist = load_list() os.environ['LINES'] = '25'
os.environ['COLUMNS'] = '120'
# write list to file # argument parser
list2file(alist, fzf_file) ap = argparse.ArgumentParser()
# reload file (I Guess ??) ap.add_argument("-p", "--player", required=False,
fzf_file.seek(0) help="Define player used for streaming. Ex: \033[0;36mpyadl -p mpv\033[0m")
ap.add_argument("-i", "--provider", required=False,
help="Define provider used for streaming (check \033[0;36m$anime dl --help\033[0m for providers list)")
ap.add_argument("-s", "--show", required=False,
help='Watch custom show. Ep nr optional, careful with the quotes. Ex: \033[0;36m$adl -s "gegege 2018"\033[0m')
ap.add_argument("-n", "--number", required=False,
help='Specify episode number that will be used with "-s / --show" option. Ex: \033[0;36m$adl -s "gegege 2018" -n "4"\033[0m')
ap.add_argument("-a", "--account", required=False,
help="By default trackma will use account 1. Use '-a 2' for example to change trackma account")
ap.add_argument("-d", "--download", required=False, type=bool, nargs='?', const=True, default=False,
help="Download instead of streaming")
ap.add_argument("-v", "--version", required=False, nargs='?', const=True,
help="Display version and exit")
# get choice from fzf args = vars(ap.parse_args())
choice = subprocess.getoutput(print_fzf_path + ' | fzf --ansi --reverse --prompt "Choose anime to watch: "')
if choice: # print(args)
# get the title
title = get_title(choice)
# get current episode
episode = get_episode(choice)
# get latest episode
last_episode = get_all_episodes(choice)
# get current score
score = get_score(choice)
# the watch loop # get player
while True: if args["player"]:
# choose what to do with the choosen anime player = str(args["player"]) # get player from user
action = choose_episode() else:
# watch next episode player = "mpv" # default player
if action == "n" or action == "N" or action == "":
next_episode(title, episode) # get provider
wanna_update_title_after_watch(title, episode, score) if args['provider']:
exit_ask() provider = str(args["provider"])
break else:
# watch all left episodes provider = ""
elif action == "l" or action == "L":
all_from_last(title, episode) # get show
wanna_update_title_after_watch(title, episode, score) if args['show']:
exit_ask() show = str(args["show"])
break else:
# watch every episode available show = ""
elif action == "a" or action == "A":
all_episodes(title) # get episode
wanna_update_title_after_watch(title, episode, score) if args['number']:
exit_ask() if args['number'] and args['show']:
break episode = int(args['number'])
# custom range of episodes
elif action == "i" or action == "I":
custom_episode_range(title)
if wanna_continu_watch():
continue
else:
wanna_update_title_after_watch(title, episode, score)
exit_ask()
break
# 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":
next_plus_n(title, episode, action)
if wanna_continu_watch():
continue
else:
wanna_update_title_after_watch(title, episode, score)
exit_ask()
break
# rewatch current episode
elif action == "r" or action == "R":
rewatch_episode(title, episode)
if wanna_continu_watch():
continue
else:
wanna_update_title_after_watch(title, episode, score)
exit_ask()
break
# watch custom episode
elif action == "c" or action == "C":
custom_episode(title)
if wanna_continu_watch():
continue
else:
wanna_update_title_after_watch(title, episode, score)
exit_ask()
break
# update anime meta
elif action == "u" or action == "U":
update_question(title, episode, score)
exit_ask()
break
# skip the anime
elif action == "s" or action == "S":
break
else: else:
exit_ask() print("You need to also specify a show name to use this option")
sys.exit()
else:
episode = 0
exit_adl() # get account
if args['account']:
account = str(int(args["account"]) - 1) # take the account from input
else:
account = "0" # default account
# enable downloading
if args["download"]:
download = True # enable downloading
msg = "downloading" # download message
else:
download = False # specify whether to download or not
msg = "watching" # msg for the watch prompt
# print the version
if args["version"]:
print("Py-adl version 1.1")
sys.exit()
retrieve = True
if not show == "" and not episode == 0:
watch(show, str(episode), download, provider, player)
elif not show == "":
while True:
# choose what to do with the choosen anime
action = choose_episode_specific_show()
if action == "a" or action == "A" or action == "":
all_episodes(show, msg, download, provider, player)
exit_adl()
# custom range of episodes
elif action == "i" or action == "I":
custom_episode_range(show, msg, download, provider, player)
if wanna_continu_watch(download):
continue
else:
exit_adl()
# watch custom episode
elif action == "c" or action == "C":
custom_episode(show, msg, download, provider, player)
if wanna_continu_watch(download):
continue
else:
exit_adl()
# skip the anime
elif action == "s" or action == "S":
exit_adl()
else:
# main loop
while True:
# retrieving the list on start
if retrieve:
retrieve_list(account)
retrieve = False
# get the list of anime
alist = load_list(account)
# write list to file
list2file(alist, FZF_FILE)
# reload file (I Guess ??)
FZF_FILE.seek(0)
# get choice from fzf
choice = subprocess.getoutput(PRINT_FZF_PATH + ' | fzf --ansi --reverse --prompt "Choose anime to watch: "')
if choice:
# get the title
title = get_title(choice)
# get current episode
episode = get_episode(choice)
# get current score
score = get_score(choice)
# the watch loop
while True:
# choose what to do with the choosen anime
action = choose_episode()
# watch next episode
if action == "n" or action == "N" or action == "":
next_episode(title, episode, msg, download,provider,player)
wanna_update_title_after_watch(title, episode, score, download, account)
exit_ask()
break
# watch all left episodes
elif action == "l" or action == "L":
all_from_last(title, episode, msg, download,provider,player)
wanna_update_title_after_watch(title, episode, score, download, account)
exit_ask()
break
# watch every episode available
elif action == "a" or action == "A":
all_episodes(title, msg, download,provider,player)
wanna_update_title_after_watch(title, episode, score, download, account)
exit_ask()
break
# custom range of episodes
elif action == "i" or action == "I":
custom_episode_range(title, msg, download,provider,player)
if wanna_continu_watch(download):
continue
else:
wanna_update_title_after_watch(title, episode, score, download, account)
exit_ask()
break
# 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":
next_plus_n(title, episode, action, msg, download,provider,player)
if wanna_continu_watch(download):
continue
else:
wanna_update_title_after_watch(title, episode, score, download, account)
exit_ask()
break
# rewatch current episode
elif action == "r" or action == "R":
rewatch_episode(title, episode, msg, download,provider,player)
if wanna_continu_watch(download):
continue
else:
wanna_update_title_after_watch(title, episode, score, download, account)
exit_ask()
break
# watch custom episode
elif action == "c" or action == "C":
custom_episode(title, msg, download,provider,player)
if wanna_continu_watch(download):
continue
else:
wanna_update_title_after_watch(title, episode, score, download, account)
exit_ask()
break
# update anime meta
elif action == "u" or action == "U":
update_question(title, episode, score, account)
exit_ask()
break
# skip the anime
elif action == "s" or action == "S":
break
else:
exit_ask()
exit_adl()
if __name__ == "__main__" :
main()