Feedback on first script -- check for unscraped video files
#1
Star 
Hi there,

I wrote my first script as a learning experience for Python and so far it's been a GREAT tool for learning. Right now it is just designed to be run from a command line or from the interpreter because the results simply get printed on screen.

This script is for XBMC users. It essentially provides the tools to compare the video files that you have in a particular folder with what is stored in the XBMC database, and lets you know if anything is missing. It identifies unscraped files.

Right now, it works as advertised but I am not sure if it will work with a network path i.e., Samba. Will os.walk(//samba/path) work? I also don't have anything built in to identify and/or correct errors.

I would greatly appreciate any feedback and/or testing that people could provide.

It is in a very basic form, only uses two quick GUI elements to allow the user to select first the directory they want to check, and secondly the location of their XBMC database. My future goals with this app are as follows:

-Make a singular GUI interface that looks something like this:

[BOX TO PICK DIRECTORY ]
[BOX TO IDENTIFY XBMC VIDEO DATABASE]

[] .avi
[] .mkv
...etc (checkboxes to select video types to look for)

-Output results to a file
-Identify the OS that you are on and have a default database location
-Eventually turn it into an XBMC addon that will allow you to manually scrape the missing files

Code:
"""Provides functions for comparing the XBMC video database against the
contents of a path.

This module has two primary functions. pathList() scans a directory recursively
looking for video files, dbList() queries the XBMC video database for video
file names.  The two can then be compared, which will reveal which files are
not scraped.

It also contains requestPath() and requestDb() which provide GUIs for selecting
a directory and database path to compare.

"""

__author__ = "Pasquale Ranalli"
__version__ = "0.1"
__maintainer__ = "Pasquale Ranalli"
__email__ = "[email protected]"
__status__ = "Development"

import os
import sqlite3
from Tkinter import Tk
from tkFileDialog import askopenfilename, askdirectory

VIDEOTYPES = ('.avi','.mpg','.mkv','.mpeg','.mp4','.wmv','.iso','.vob',\
                  '.m2v','.ts')

def path_list(path):
    """Takes a path in the form of a string and returns its file contents as a
    list."""
    
    # Walks recursively through the specified path and pulls files of type
    # "videotypes" only.  os.walk places a list of file names, per directory,
    # in the [2] position of a tuple.  This data is extracted and returned.
    #
    # Future revisions will turn each entry of "videotypes" into a series of
    # check boxes.

    filelist = []
    for file in os.walk(path):  
        for items in file[2]:  
            if items.endswith(VIDEOTYPES):    
                filelist.append(items)
            
    return filelist

def db_list(dblocation):
    """ Takes an XBMC video database location, as a string, and returns all of
    the video filenames it contains in the form of a list.

    Standard XBMC Video Database Locations:
        Win XP: C:\\Documents and Settings\\[user]\\Application Data\\XBMC\\
        Win Vista/7: C:\\Users\\[user]\\AppData\\Roaming\\XBMC\\userdata\\Database\\
        Linux: $HOME/.xbmc/userdata/
        
    """

    database = sqlite3.connect(dblocation)
    cursor = database.cursor()
    cursor.execute('SELECT strFilename FROM files')  
    datalist = []
    for row in cursor:  
        datalist.append(row[0])
        
    return datalist

def request_db():
    """Provides a graphical file browser for the user to specify the location
    of their XBMC database.

    """
    
    root = Tk()
    response = askopenfilename\
               (title="Choose your XBMC Video Database (MyVideos34.db)",\
                filetypes=[("allfiles","*"),("dbfiles","*.db*")])
    root.destroy()
    
    return response

def request_path():
    """Provides a graphical directory browser for the user to specify a
    desired folder.

    """
    root = Tk()
    response = askdirectory(title="Select the folder containing \
                                                your video files")
    root.destroy()
    
    return response

if __name__ == '__main__':
    folder = request_path()
    db = request_db()
    filelist = path_list(folder)
    xbmclist = db_list(db)

    # Compares what is in the filelist to what is in the XBMC Database and
    # prints any files missing from the db.
    #
    # Future revision will generate a results file.

    for item in filelist:
        if item not in xbmclist:
            print item
Reply
#2
nice, well written. i assume you have a backaground in coding?
note, for sqlite you have to do this weird thing:
PHP Code:
try:
    
import pysqlite2 as sqlite3
except
:
    
import sqlite3 
because some builds of xbmc use python 2.6, other 2.4. depends on platform
Reply
#3
anarchintosh Wrote:nice, well written. i assume you have a backaground in coding?
note, for sqlite you have to do this weird thing:
PHP Code:
try:
    
import pysqlite2 as sqlite3
except
:
    
import sqlite3 
because some builds of xbmc use python 2.6, other 2.4. depends on platform

Thanks for the response. I sort of have a background in coding, meaning that I got pretty good with Basic and Pascal back in the day but I never really advanced beyond that. I tried learning C and Java a few times, but got lazy. At 31, I've now found my motivation!

I will add that change to the import section, thank you. Right now I am just running it outside of XBMC, can something like this be ran directly through XBMC? I was assuming it would not.
Reply
#4
There is no reason that it would not run in xbmc(just needing a little code changes, like the one above - sqlite3)

You would also be able to drop the code for requesting database path, as it is fairly simple to get that from XBMC.
Reply
#5
giftie Wrote:There is no reason that it would not run in xbmc(just needing a little code changes, like the one above - sqlite3)

You would also be able to drop the code for requesting database path, as it is fairly simple to get that from XBMC.

Very cool, thanks for letting me know. I assume that to get it to run properly in XBMC I'll have to ditch Tkinter and port it to the XBMC GUI packages?

By the way, I did finally test this over a network path and os.walk() works fine. I'm going to set this up to output to a txt file, which will technically make it a basic release. I'd like to then port it to XBMC.

Could you offer a hint on what you mean that it is easy to get the db_path from inside XBMC? I'm assuming there must be something in the xbmc python module that gives you this data? I would love to remove this step and simplify the app for the user.
Reply
#6
os.walk() will only walk a network path if it sees the network path as local. It won't (for instance) walk a smb:// share path inside XBMC, as that's XBMC's internal protocol.

And you can query the database over the json-rpc interface rather than directly via sqlite.

Cheers,
Jonathan
Always read the XBMC online-manual, FAQ and search the forum before posting.
Do not e-mail XBMC-Team members directly asking for support. Read/follow the forum rules.
For troubleshooting and bug reporting please make sure you read this first.


Image
Reply
#7
Just to let you know that someone already implemented this as a plugin. You may be interested...

Missing Movie Plugin
Reply
#8
cuccuizzo Wrote:Just to let you know that someone already implemented this as a plugin. You may be interested...

Missing Movie Plugin

Thanks for posting that, I wasn't aware at all. That will be useful sourcecode to learn from. This is more of a personal project than anything, just to learn python and learn the ins and outs of xbmc.
Reply

Logout Mark Read Team Forum Stats Members Help
Feedback on first script -- check for unscraped video files1