#!venv/bin/python3
# media_namer.py - Renames passed media files in a folder (and subfolders) using
#                   OMDB for movies and theTVDB for TV shows

import sys
import os
import logging
import argparse
import random
import subprocess
import shlex

import videoLister
import mff

def main():
    # set up logging
    logger = logging.getLogger(__name__)
    logging.basicConfig(filename='whats_on_tv.log', filemode='w', level=logging.INFO,
                        format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')


    # set up argparse
    parser = argparse.ArgumentParser()
    parser.add_argument('input', help='Input directory to search.')
    # Set up range of choices starting at 15 min in 15 min increments up to 300 min
    parser.add_argument('-t', '--time', help='Viewing time in minutes', type=int, choices=range(15,301)[::15])
    args = parser.parse_args()
    if args.input:
        in_path = args.input
    else:
        parser.print_help()
        sys.exit(1)

    # One time loop to generate a list of available media files in path
    m_list = []
    for item in videoLister.videoDir(in_path):
        logger.debug("Found: {}".format(item)) # Can write really long log files
        m_list.append(item)

    # Randomly select a video to play

    if args.time: # Find a file with a duration shorter than allotted time
        duration = 999 # Fix this, its hacky to get the loop to run...
        while duration > args.time:
            choice = random.choice(m_list)
            m_list.remove(choice) # remove the choice from the list
            m_file = mff.format_info(choice) # get file details
            duration = round(float(m_file['duration']) / 60) # convert to integer minutes
            logger.info("Selected: {}".format(os.path.basename(choice)))
            logger.info("Running time: {} min".format(duration))
    else:
        choice = random.choice(m_list)

    logger.info("Playing: {}".format(os.path.basename(choice)))

    # Launch selected video with MPV in full screen
    play_command = 'mpv "{}" --really-quiet --fs &'.format(choice)
    proc = subprocess.Popen(shlex.split(play_command))
    # use proc.terminate() to kill


if __name__ == '__main__':
    main()

@@ -857,16 +857,110 @@
 %5B::15%5D)%0A
+    parser.add_argument('-n', '--num', help='Number of videos to queue', type=int, default=1)%0A
     args
@@ -1372,17 +1372,66 @@
 to play%0A
-%0A
+    p_list = %5B%5D%0A    for x in range(args.num):%0A    
     if a
@@ -1497,32 +1497,36 @@
 ed time%0A        
+    
 duration = 999 #
@@ -1580,16 +1580,20 @@
         
+    
 while du
@@ -1616,32 +1616,36 @@
 me:%0A            
+ 
+   
 choice = random.
@@ -1659,16 +1659,20 @@
 m_list)%0A
+    
         
@@ -1743,16 +1743,20 @@
         
+   
+ 
 m_file =
@@ -1799,16 +1799,20 @@
 details%0A
+    
         
@@ -1893,16 +1893,20 @@
 minutes%0A
+    
         
@@ -1974,32 +1974,36 @@
 )))%0A            
+    
 logger.info(%22Run
@@ -2039,16 +2039,20 @@
 ation))%0A
+    
     else
@@ -2053,32 +2053,36 @@
   else:%0A        
+  
+  
 choice = random.
@@ -2096,21 +2096,98 @@
 m_list)%0A
-%0A
+            logger.info(%22Selected: %7B%7D%22.format(os.path.basename(choice)))%0A%0A 
     
+   
 logger.i
@@ -2195,15 +2195,25 @@
 fo(%22
-Playing
+Added to playlist
 : %7B%7D
@@ -2243,24 +2243,102 @@
 me(choice)))
+%0A        p_list.append(choice)%0A%0A    logger.info(%22Playlist: %7B%7D%22.format(p_list))
 %0A%0A    # Laun
@@ -2407,12 +2407,10 @@
 mpv 
-%22
 %7B%7D
-%22
  --r
@@ -2436,22 +2436,58 @@
 .format(
-choice
+' '.join('%22%7B%7D%22'.format(p) for p in p_list)
 )%0A    pr

add support for multiple files