Esempio n. 1
0
def copy_tv():
    """
    Do the actual TV file copying if not in pretend mode
    """

    global tv_copy_queue
    global args
    global unfound_shows
    global video_file_extensions

    if not len(tv_copy_queue) > 0:
        logging.info( "NO TV FOUND TO COPY" )
    else:
        logging.info( "TV COPY QUEUE IS:" )
        for copy in tv_copy_queue:
            logging.info( str(copy) )


    # First, need to strip files out that have been watched in an ad hoc order...
    playcount_cache = {}  
    new_copy_queue = []

    for copy in tv_copy_queue:        

        # print(str(copy))

        # Only video files have the id stuff at the end, not base files etc...which we always want to copy really...
        tv_show_id = 0
        try:
            tv_show_name = copy[2]
            tv_show_id = int(copy[3])
            tv_show_season = int(copy[4])
            tv_show_episode = int(copy[5])          
        except Exception:
            pass

        # April 2019 - trying to stop files actually watched in kodi from being copied,
        #  but does not work... & also would need to be limited to a-go-go
        #
        if tv_show_id > 0:
            #logging.info("Checking playcount of %s id: %s season: %s episode: %s" % (tv_show_name, str(tv_show_id), str(tv_show_season), str(tv_show_episode))) 

            kodi_playcount = 0
            filename, file_extension = os.path.splitext(copy[0])

            # Grab the playcount from our cache if it is in there...
            try:
                kodi_playcount = playcount_cache[str(tv_show_id) + "-" + str(tv_show_season) + "-" + str(tv_show_episode)]
                # logging.info("Using playcount_cache: " + str(kodi_playcount))
            
            # Otherwise, check this particualr episode is _actually_ unwatched in Kodi's library... 
            #  (if we're dealing with shows being watched in an adhoc order...)
            except KeyError:                    
                result = xbmc.VideoLibrary.GetEpisodes({"tvshowid": tv_show_id, \
                                                        "season": tv_show_season, \
                                                        "properties": ['season', 'episode', 'playcount'], \
                                                        "filter": { "field": "episode", "operator": "is", "value": str(tv_show_episode) } \
                                                        })                
                
                # Episode found in kodi...should be only one
                try:
                    for episode in result["result"]["episodes"]:
                        if episode['season'] == tv_show_season and episode['episode'] == tv_show_episode:
                            #logging.info("Matched: "+ str(episode))
                            playcount_cache[str(tv_show_id) + "-" + str(tv_show_season) + "-" + str(tv_show_episode)] = episode['playcount']
                            kodi_playcount = episode['playcount']
                            break

                # Episode not found in Kodi?  Don't skip it just to be safe...
                except KeyError:                    
                    if file_extension in video_file_extensions:
                        logging.info("%s Season %s episode %s not found in Kodi Library? Copying just to be safe..." % (tv_show_name,tv_show_season,tv_show_episode))

            # One way or another we should have a playcount now, or we've assumed zero...
            if int(kodi_playcount) > 0:
                if file_extension in video_file_extensions:
                    logging.info( "Skipping: " + os.path.basename(copy[0]) + " (Kodi playcount is " + str(kodi_playcount) + ")")
                continue

        # add this file to the new copy queue if it hasn't been watched...
        new_copy_queue.append(copy)
    

    if args.pretend:
        logging.info( "PRETEND MODE - NO ACTUAL COPYING DONE" )
    
    else:
    
        #we're actually copying...
        logging.info( "COPYING TV NOW" )


        #work out if we have enough space
        needed_space = 0
        available_space = utils.get_free_space_gb(user_config['paths']['tv_output_path'])
        for copy in new_copy_queue:
            destin_file = copy[1] + "\\" + os.path.basename(copy[0])
            if not os.path.exists(destin_file):
                needed_space += os.path.getsize(copy[0])

        #convert to GB
        needed_space = needed_space/1024/1024/1024

        logging.info( "TV - available space is " + str(available_space) + " GB")
        logging.info( "TV - needed space is    " + str(needed_space) + " GB")
 
        if needed_space > available_space:
            logging.error("Not enough space!!  Bailing out!")
            sys.exit()

        #make the root output folder if we need to
        if not os.path.exists(user_config['paths']['tv_output_path']):
            try:
                os.mkdir(user_config['paths']['tv_output_path'])
            except Exception as inst:
                logging.error("ERROR - Couldn't make output directory: " + user_config['paths']['tv_output_path'] + format_exc(inst))
                sys.exit()
                      
        # OK NOW FINALLY DO THE ACTUAL TV COPYING

        for copy in new_copy_queue:        

            destin_file = copy[1] + "\\" + os.path.basename(copy[0])

            #make the output folder if it doesn't exist
            if not os.path.exists(copy[1]):
                try:
                    os.makedirs(copy[1])
                except Exception as inst:
                    sys.exit("ERROR - Couldn't make output directory: " + copy[1] + format_exc(inst))


            logging.info( "Copying: " + copy[0] )
            logging.info( "To:      " + destin_file )
            #don't re-copy files if we're re-running the script!

            #TODO - ok we have a newer version maybe?  Might as well copy the better
            #quality version over & remove the lesser one

            if not os.path.exists(destin_file):
                utils.copyFile(copy[0],destin_file)
                logging.info( "Copied:  " + destin_file + "\n" )
            else:
                #check the sizes match in case of interrupted copy
                if not os.path.getsize(copy[0])==os.path.getsize(destin_file):
                    utils.copyFile(copy[0],destin_file)
                    logging.info( "ReCopy:  " + destin_file + "\n" )
                else:
                    logging.info( "Exists:  " + destin_file + "\n" )
 


    ################################################################################

    logging.info( "ANY UNFOUND SHOWS?" )

    if len(unfound_shows) > 0:
        for unfound_show in unfound_shows:
            logging.info("WARNING: I couldn't find this show: " + str(unfound_show) )

    ################################################################################

    # write out a new tv tracker if we're not doing the nuc 
    if args.mode != "agogo":

        logging.info( "WRITING UPDATED TV TRACKER FILE" )

        outname = "results/config." + args.name + ".tv.txt"

        f = open(outname , 'w')
        for output_show in sorted(output_show_list, key=str.lower):
            try:
                oldline = output_show + "|" + str(original_show_list[output_show][0]) + "|" + str(original_show_list[output_show][1])
            except:
                oldline = "Show did not exist in old file"

            newline = output_show + "|" + str(output_show_list[output_show][0]) + "|" + str(output_show_list[output_show][1])
            if oldline!=newline:
                logging.info( "OLD: " + oldline )
                logging.info( "NEW: " + newline )
            f.write(newline + "\n")
        f.close()
Esempio n. 2
0
def copy_movies():
    """
    Do the actual movie file copying if not in pretend mode
    """

    global movie_copy_queue                

    ################################################################################
    #The copyQueues contains all the files to be copied and the destination directories - let's copy them


    if not len(movie_copy_queue) > 0:

        logging.info( "NO MOVIES FOUND TO COPY" )
    else:

        logging.info( "MOVIE COPY QUEUE IS:" )
        for copy in movie_copy_queue:
            logging.info( copy )
    ################################################################################
    #and actually do the movie copying...

    if args.pretend:
        logging.info( "PRETEND MODE - NO ACTUAL COPYING DONE" )
    else:

        logging.info( "NOW COPYING MOVIES" )

        #work out if we have enough space
        needed_space = 0
        available_space = utils.get_free_space_gb(user_config['paths']['movie_output_path'])
        for movie in movie_copy_queue:
            output_path = os.path.join(user_config['paths']['movie_output_path'],os.path.basename(movie))
            if not os.path.exists(output_path):
                logging.info(movie)
                needed_space += utils.getSize(movie)

        #convert to GB
        needed_space = needed_space/1024/1024/1024

        logging.info( "Movies - available space is " + str(available_space) + " GB")
        logging.info( "Movies - needed space is    " + str(needed_space) + " GB")

        if needed_space > available_space:
            logging.error("Not enough space!!  Bailing out!")
            sys.exit()

        for movie in movie_copy_queue:
            logging.info( "Copying: " + movie )
            logging.info( "To:    : " + user_config['paths']['movie_output_path'] )
            output_path = os.path.join(user_config['paths']['movie_output_path'],os.path.basename(movie))
            #if the output path exists, we may have only copied to half way or something.
            #check the sizes
            need_to_copy = True
            if os.path.exists(output_path):
                output_path_size = utils.getSize(output_path)
                input_path_size = utils.getSize(movie)
                if output_path_size == input_path_size:
                    need_to_copy = False
                    logging.info( "Exists : " + movie + "\n" )
                else:
                    #get rid of the old folder and start again
                    logging.info( "Deleted : " + output_path)
                    shutil.rmtree(output_path)
            if need_to_copy:
                utils.copyFolder(movie,output_path)
                logging.info( "Copied : " + movie + "\n" )

    ################################################################################
    #we've now considdered all available movies, so write out that as the new list

    logging.info( "WRITING NEW MOVIE TRACKER FILE")

    basename_list = []
    for movie in movies_available:
        basename_list.append(os.path.basename(movie))

    if args.mode!="agogo":
        f = open("results/config." + args.name + ".movies.txt" , 'w')
    else: 
        f = open("results/config.agogo.movies.txt" , 'w')       
    for movie in sorted(basename_list, key=str.lower):
        movie_name = os.path.basename(movie)
        f.write(movie_name + "\n" )
    f.close()
Esempio n. 3
0
def copy_tv():
    """
    Do the actual TV file copying if not in pretend mode
    """

    global tv_copy_queue
    global args
    global unfound_shows

    if not len(tv_copy_queue) > 0:
        logging.info( "NO TV FOUND TO COPY" )
    else:
        logging.info( "TV COPY QUEUE IS:" )
        for copy in tv_copy_queue:
            logging.info( str(copy) )

    # OK DO THE ACTUAL TV COPYING

    if args.pretend:
        logging.info( "PRETEND MODE - NO ACTUAL COPYING DONE" )
    else:
        #we're actually copying...
        logging.info( "COPYING TV NOW" )


        #work out if we have enough space
        needed_space = 0
        available_space = utils.get_free_space_gb(user_config['paths']['tv_output_path'])
        for copy in tv_copy_queue:
            destin_file = copy[1] + "\\" + os.path.basename(copy[0])
            if not os.path.exists(destin_file):
                needed_space += os.path.getsize(copy[0])

        #convert to GB
        needed_space = needed_space/1024/1024/1024

        logging.info( "TV - available space is " + str(available_space) + " GB")
        logging.info( "TV - needed space is    " + str(needed_space) + " GB")
 
        if needed_space > available_space:
            logging.error("Not enough space!!  Bailing out!")
            sys.exit()

        #make the root output folder if we need to
        if not os.path.exists(user_config['paths']['tv_output_path']):
            try:
                os.mkdir(user_config['paths']['tv_output_path'])
            except Exception as inst:
                logging.error("ERROR - Couldn't make output directory: " + user_config['paths']['tv_output_path'] + format_exc(inst))
                sys.exit()

        for copy in tv_copy_queue:

            destin_file = copy[1] + "\\" + os.path.basename(copy[0])

            #make the output folder if it doesn't exist
            if not os.path.exists(copy[1]):
                try:
                    os.makedirs(copy[1])
                except Exception as inst:
                    sys.exit("ERROR - Couldn't make output directory: " + copy[1] + format_exc(inst))


            logging.info( "Copying: " + copy[0] )
            logging.info( "To:      " + destin_file )
            #don't re-copy files if we're re-running the script!

            #TODO - ok we have a newer version maybe?  Might as well copy the better
            #quality version over & remove the lesser one

            if not os.path.exists(destin_file):
                utils.copyFile(copy[0],destin_file)
                logging.info( "Copied:  " + destin_file + "\n" )
            else:
                #check the sizes match in case of interrupted copy
                if not os.path.getsize(copy[0])==os.path.getsize(destin_file):
                    utils.copyFile(copy[0],destin_file)
                    logging.info( "ReCopy:  " + destin_file + "\n" )
                else:
                    logging.info( "Exists:  " + destin_file + "\n" )
 


    ################################################################################

    logging.info( "ANY UNFOUND SHOWS?" )

    if len(unfound_shows) > 0:
        for unfound_show in unfound_shows:
            logging.info("WARNING: I couldn't find this show: " + str(unfound_show) )

    ################################################################################

    # write out a new tv tracker if we're not doing the nuc 
    if args.mode != "agogo":

        logging.info( "WRITING UPDATED TV TRACKER FILE" )

        outname = "results/config." + args.name + ".tv.txt"

        f = open(outname , 'w')
        for output_show in sorted(output_show_list, key=str.lower):
            try:
                oldline = output_show + "|" + str(original_show_list[output_show][0]) + "|" + str(original_show_list[output_show][1])
            except:
                oldline = "Show did not exist in old file"

            newline = output_show + "|" + str(output_show_list[output_show][0]) + "|" + str(output_show_list[output_show][1])
            if oldline!=newline:
                logging.info( "OLD: " + oldline )
                logging.info( "NEW: " + newline )
            f.write(newline + "\n")
        f.close()