2021-03-31 14:51:33 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
""" curses interface to lunch moviesort and tvsort """
|
|
|
|
|
|
|
|
import curses
|
2021-04-23 10:15:30 +00:00
|
|
|
import json
|
2021-03-31 14:51:33 +00:00
|
|
|
import logging
|
2021-04-02 04:12:10 +00:00
|
|
|
import sys
|
|
|
|
from os import path
|
2021-03-31 14:51:33 +00:00
|
|
|
from time import sleep
|
|
|
|
|
|
|
|
import src.tvsort as tvsort
|
|
|
|
import src.tvsort_id as tvsort_id
|
|
|
|
import src.moviesort as moviesort
|
2021-04-04 06:29:42 +00:00
|
|
|
import src.db_export as db_export
|
2021-04-16 07:16:27 +00:00
|
|
|
import src.trailers as trailers
|
2021-04-17 08:12:19 +00:00
|
|
|
import src.id_fix as id_fix
|
2021-03-31 14:51:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_config():
|
|
|
|
""" read out config file and return config dict """
|
2021-04-02 04:12:10 +00:00
|
|
|
# build path
|
|
|
|
root_folder = path.dirname(sys.argv[0])
|
2021-04-04 06:29:42 +00:00
|
|
|
if root_folder == '/sbin':
|
|
|
|
# running interactive
|
2021-04-23 10:15:30 +00:00
|
|
|
config_path = 'config.json'
|
2021-04-04 06:29:42 +00:00
|
|
|
else:
|
2021-04-23 10:15:30 +00:00
|
|
|
config_path = path.join(root_folder, 'config.json')
|
2021-03-31 14:51:33 +00:00
|
|
|
# parse
|
2021-04-23 10:15:30 +00:00
|
|
|
with open(config_path, 'r') as config_file:
|
|
|
|
data = config_file.read()
|
|
|
|
config = json.loads(data)
|
2021-03-31 14:51:33 +00:00
|
|
|
return config
|
|
|
|
|
|
|
|
|
|
|
|
def get_pending_all(config):
|
|
|
|
""" figure out what needs to be done """
|
2021-04-23 10:15:30 +00:00
|
|
|
movie_downpath = config['media']['movie_downpath']
|
|
|
|
tv_downpath = config['media']['tv_downpath']
|
2021-03-31 14:51:33 +00:00
|
|
|
# call subfunction to collect pending
|
2021-04-23 10:15:30 +00:00
|
|
|
pending_movie = moviesort.get_pending(movie_downpath)
|
|
|
|
pending_tv = tvsort.get_pending(tv_downpath)
|
2021-04-16 07:18:55 +00:00
|
|
|
pending_trailer = len(trailers.get_pending(config))
|
2021-04-17 08:12:19 +00:00
|
|
|
pending_movie_fix = len(id_fix.get_pending(config))
|
|
|
|
pending_total = pending_movie + pending_tv + pending_trailer + pending_movie_fix
|
2021-03-31 14:51:33 +00:00
|
|
|
# build dict
|
|
|
|
pending = {}
|
|
|
|
pending['movies'] = pending_movie
|
|
|
|
pending['tv'] = pending_tv
|
2021-04-16 07:16:27 +00:00
|
|
|
pending['trailer'] = pending_trailer
|
2021-04-17 08:12:19 +00:00
|
|
|
pending['movie_fix'] = pending_movie_fix
|
2021-03-31 14:51:33 +00:00
|
|
|
pending['total'] = pending_total
|
|
|
|
return pending
|
|
|
|
|
|
|
|
|
2021-04-23 10:15:30 +00:00
|
|
|
def print_menu(stdscr, current_row_idx, menu, pending):
|
2021-03-31 14:51:33 +00:00
|
|
|
""" print menu with populated pending count """
|
|
|
|
# build stdscr
|
|
|
|
h, w = stdscr.getmaxyx()
|
|
|
|
longest = len(max(menu))
|
|
|
|
x = w // 2 - longest
|
|
|
|
stdscr.clear()
|
|
|
|
# loop through menu items
|
|
|
|
for idx, row in enumerate(menu):
|
|
|
|
# menu items count
|
|
|
|
if row == 'All':
|
|
|
|
pending_count = pending['total']
|
|
|
|
elif row == 'Movies':
|
|
|
|
pending_count = pending['movies']
|
|
|
|
elif row == 'TV shows':
|
|
|
|
pending_count = pending['tv']
|
2021-04-16 07:16:27 +00:00
|
|
|
elif row == 'Trailer download':
|
|
|
|
pending_count = pending['trailer']
|
2021-04-17 08:12:19 +00:00
|
|
|
elif row == 'Fix Movie Names':
|
|
|
|
pending_count = pending['movie_fix']
|
2021-03-31 14:51:33 +00:00
|
|
|
else:
|
2021-04-16 07:16:27 +00:00
|
|
|
pending_count = ' '
|
2021-03-31 14:51:33 +00:00
|
|
|
# center whole
|
|
|
|
y = h // 2 - len(menu) + idx
|
|
|
|
# print string to menu
|
|
|
|
text = f'[{pending_count}] {row}'
|
|
|
|
if idx == current_row_idx:
|
|
|
|
stdscr.attron(curses.color_pair(1))
|
|
|
|
stdscr.addstr(y, x, text)
|
|
|
|
stdscr.attroff(curses.color_pair(1))
|
|
|
|
else:
|
|
|
|
stdscr.addstr(y, x, text)
|
|
|
|
# load
|
|
|
|
stdscr.refresh()
|
|
|
|
|
|
|
|
|
|
|
|
def sel_handler(menu_item, config):
|
|
|
|
""" lunch scripts from here based on selection """
|
|
|
|
if menu_item == 'All':
|
|
|
|
moviesort.main(config)
|
|
|
|
tvsort.main(config, tvsort_id)
|
2021-04-04 08:06:49 +00:00
|
|
|
db_export.main(config)
|
2021-04-17 08:53:59 +00:00
|
|
|
trailers.main(config)
|
|
|
|
id_fix.main(config)
|
2021-03-31 14:51:33 +00:00
|
|
|
elif menu_item == 'Movies':
|
|
|
|
moviesort.main(config)
|
|
|
|
elif menu_item == 'TV shows':
|
|
|
|
tvsort.main(config, tvsort_id)
|
2021-04-04 06:29:42 +00:00
|
|
|
elif menu_item == 'DB export':
|
|
|
|
db_export.main(config)
|
2021-04-16 07:16:27 +00:00
|
|
|
elif menu_item == 'Trailer download':
|
|
|
|
trailers.main(config)
|
2021-04-17 08:12:19 +00:00
|
|
|
elif menu_item == 'Fix Movie Names':
|
|
|
|
id_fix.main(config)
|
2021-03-31 14:51:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def curses_main(stdscr, menu, config):
|
|
|
|
""" curses main to desplay and restart the menu """
|
|
|
|
curses.curs_set(0)
|
|
|
|
curses.init_pair(1, curses.COLOR_BLUE, curses.COLOR_WHITE)
|
|
|
|
current_row_idx = 0
|
2021-04-16 08:42:36 +00:00
|
|
|
pending = get_pending_all(config)
|
2021-04-23 10:15:30 +00:00
|
|
|
print_menu(stdscr, current_row_idx, menu, pending)
|
2021-03-31 14:51:33 +00:00
|
|
|
# endless loop
|
|
|
|
while True:
|
|
|
|
# wait for exit signal
|
|
|
|
try:
|
|
|
|
key = stdscr.getch()
|
|
|
|
stdscr.clear()
|
|
|
|
# react to kee press
|
|
|
|
if key == curses.KEY_UP and current_row_idx > 0:
|
|
|
|
current_row_idx -= 1
|
|
|
|
elif key == curses.KEY_DOWN and current_row_idx < len(menu) - 1:
|
|
|
|
current_row_idx += 1
|
|
|
|
elif key == curses.KEY_ENTER or key in [10, 13]:
|
|
|
|
menu_item = menu[current_row_idx]
|
|
|
|
stdscr.addstr(0, 0, f'start task: {menu_item}')
|
|
|
|
stdscr.refresh()
|
|
|
|
sleep(1)
|
|
|
|
# exit curses and do something
|
|
|
|
return menu_item
|
|
|
|
# print
|
2021-04-23 10:15:30 +00:00
|
|
|
print_menu(stdscr, current_row_idx, menu, pending)
|
2021-03-31 14:51:33 +00:00
|
|
|
stdscr.refresh()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
# clean exit on ctrl + c
|
|
|
|
return 'Exit'
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
""" main wraps the curses menu """
|
|
|
|
# setup
|
2021-04-17 08:15:09 +00:00
|
|
|
menu = ['All', 'Movies', 'TV shows', 'DB export', 'Trailer download', 'Fix Movie Names', 'Exit']
|
2021-03-31 14:51:33 +00:00
|
|
|
config = get_config()
|
2021-04-23 10:15:30 +00:00
|
|
|
log_folder = config['media']['log_folder']
|
|
|
|
log_file = path.join(log_folder, 'rename.log')
|
2021-04-04 06:29:42 +00:00
|
|
|
logging.basicConfig(filename=log_file,level=logging.INFO,format='%(asctime)s:%(message)s')
|
2021-03-31 14:51:33 +00:00
|
|
|
# endless loop
|
|
|
|
while True:
|
|
|
|
pending = get_pending_all(config)
|
|
|
|
if not pending:
|
|
|
|
return
|
|
|
|
menu_item = curses.wrapper(curses_main, menu, config)
|
|
|
|
if menu_item == 'Exit':
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
sel_handler(menu_item, config)
|
|
|
|
sleep(3)
|
|
|
|
|
|
|
|
|
|
|
|
# start here
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|