Ejemplo n.º 1
0
def UpdateDatabase(DB_CONN, DB_VERSION):
    if (DB_VERSION < 1):
        BuildDatabase(DB_CONN)
        DB_VERSION = dbManager.Db_Version

    if (DB_VERSION < 1.11):
        Database.AlterTable(DB_CONN,
                            TABLE=dbManager.Db_Img,
                            RENAMECOLUMN="Location TO Img_Location")

    if (DB_VERSION < 1.12):
        #SQLite doesn't support Drop Column while altering a table or Insert into a table. this is how we change a db if we need to remove a column
        Database.CreateTable(
            DB_CONN,
            TABLE='TEMP_DB',
            VALUES=[["Program_Id", "INTEGER PRIMARY KEY AUTOINCREMENT"],
                    ["Program_Name", "TEXT UNIQUE NOT NULL"],
                    ["Program_Desctiption", "TEXT"],
                    ["Program_Location", "TEXT NOT NULL"],
                    ["Program_Web_Location", "TEXT NOT NULL"],
                    ["First_Content", "INTEGER"], ["First_Folder", "INTEGER"],
                    ["Num_Content", "INTEGER NOT NULL"]])

        Database.Insert(
            DB_CONN,
            INTO='TEMP_DB',
            SELECT=
            'Program_Id, Program_Name, Program_Desctiption, Program_Location, Program_Web_Location, First_Content, First_Folder, Num_Content',
            FROM=dbManager.Db_Program)

        Database.Drop(DB_CONN, dbManager.Db_Program)

        Database.CreateTable(
            DB_CONN,
            TABLE=dbManager.Db_Program,
            VALUES=[["Program_Id", "INTEGER PRIMARY KEY AUTOINCREMENT"],
                    ["Program_Name", "TEXT UNIQUE NOT NULL"],
                    ["Program_Desctiption", "TEXT"],
                    ["Program_Location", "TEXT NOT NULL"],
                    ["Program_Web_Location", "TEXT NOT NULL"],
                    ["First_Content", "INTEGER"], ["First_Folder", "INTEGER"],
                    ["Num_Content", "INTEGER NOT NULL"]])

        Database.Insert(DB_CONN,
                        INTO=dbManager.Db_Program,
                        SELECT='*',
                        FROM='TEMP_DB')

        Database.Drop(DB_CONN, 'TEMP_DB')

    if (DB_VERSION < 1.13):
        metaFolders.RenameMeta()
        Database.AlterTable(DB_CONN,
                            TABLE=dbManager.Db_Program,
                            ADDCOLUMN="Program_Last_Updated DATETIME")

    Database.Update(DB_CONN,
                    dbManager.Db_YourFlix,
                    SET="Version = %s" % str(dbManager.Db_Version))
def AddToDefaultChannel(DB_CONN, PROGRAM_ID, NUMBER_OF_CONTENT, ALL_CHANNEL_ID, FILM_CHANNEL_ID, SHOW_CHANNEL_ID):
    _updated = False

    #Add Program to All Channel
    _channelResult = Database.Select(DB_CONN,
        SELECT = 'ChProg_Id, Channel_Id, Program_Id',
        FROM = dbManager.Db_ChProgram,
        WHERE = 'Program_Id = %i AND Channel_Id = %i' % (PROGRAM_ID, ALL_CHANNEL_ID))

    if(_channelResult):
        dbManager.Current_AllChannel.remove(_channelResult)
    else:
        Database.Insert(DB_CONN, 
            INTO = dbManager.Db_ChProgram, 
            ROW = ['Channel_Id, Program_Id'],
            VALUES = [ALL_CHANNEL_ID, PROGRAM_ID])
        _updated = True

    #Add Program to Film Channel
    if(NUMBER_OF_CONTENT == 1):
        _channelResult = Database.Select(DB_CONN,
            SELECT = 'ChProg_Id, Channel_Id, Program_Id',
            FROM = dbManager.Db_ChProgram,
            WHERE = 'Program_Id = %i AND Channel_Id = %i' % (PROGRAM_ID, FILM_CHANNEL_ID))

        if(_channelResult):
            dbManager.Current_FilmsChannel.remove(_channelResult)
        else:
            Database.Insert(DB_CONN, 
                INTO = dbManager.Db_ChProgram, 
                ROW = ['Channel_Id, Program_Id'],
                VALUES = [FILM_CHANNEL_ID, PROGRAM_ID])
            _updated = True

    #Add Program to Show Channel
    elif(NUMBER_OF_CONTENT > 1):
        _channelResult = Database.Select(DB_CONN,
            SELECT = 'ChProg_Id, Channel_Id, Program_Id',
            FROM = dbManager.Db_ChProgram,
            WHERE = 'Program_Id = %i AND Channel_Id = %i' % (PROGRAM_ID, SHOW_CHANNEL_ID))

        if(_channelResult):
            dbManager.Current_ShowsChannel.remove(_channelResult)
        else:
            Database.Insert(DB_CONN, 
                INTO = dbManager.Db_ChProgram, 
                ROW = ['Channel_Id, Program_Id'],
                VALUES = [SHOW_CHANNEL_ID, PROGRAM_ID])
            _updated = True
    
    return _updated
Ejemplo n.º 3
0
def AddProgramToDb(DB_CONN, PROGAM_WEB_LOC, PROGRAM_NAME):
    _programData = Database.Select(DB_CONN,
                                   SELECT='Program_Id',
                                   FROM=dbManager.Db_Program,
                                   WHERE='Program_Web_Location = "%s"' %
                                   PROGAM_WEB_LOC)

    if (_programData):
        DebugLog("Found Program in Location: %s" % PROGAM_WEB_LOC)
        return _programData[0]

    else:
        DebugLog("Adding Program %s" % PROGRAM_NAME)

        _programName = PROGRAM_NAME
        _programWebLoc = PROGAM_WEB_LOC
        _programPhysLoc = os.path.join(dbManager.YF_Html, _programWebLoc[1:])

        _programId = _programId = Database.Insert(
            DB_CONN,
            INTO=dbManager.Db_Program,
            ROW=[
                'Program_Name', 'Program_Location', 'Program_Web_Location',
                'Num_Content'
            ],
            VALUES=[PROGRAM_NAME, _programPhysLoc, _programWebLoc, 0])

        DebugLog("Adding Program %s in Physical Loc: %s - Web Loc: %s" %
                 (_programName, _programPhysLoc, _programWebLoc))

        return _programId
Ejemplo n.º 4
0
def AddContentToDb(DB_CONN, ROOT, FILE_LOC, FILE_NAME):
    _contentData = Database.Select(DB_CONN,
                                   SELECT='Content_Id',
                                   FROM=dbManager.Db_Content,
                                   WHERE='Content_Location = "%s"' % FILE_LOC)

    if (not _contentData):
        _folderLoc = os.path.dirname(FILE_LOC)
        _folderName = os.path.basename(_folderLoc)
        _folderRoot = os.path.dirname(_folderLoc)
        _folderId = None

        DebugLog(
            "ROOT: %s\nFOLDER_LOC: %s\n_folderLoc: %s\n_folderName: %s\n_folderRoot: %s\n"
            % (ROOT, FILE_LOC, _folderLoc, _folderName, _folderRoot))
        _folderData = AddFolderToDb(DB_CONN, _folderRoot, _folderLoc,
                                    _folderName)

        if (_folderData):
            _folderId = _folderData[0]
            _programId = _folderData[1]

            if (debug):
                print("Added Content: %s in Location: %s" %
                      (FILE_NAME, FILE_LOC))

            _fileType = GetVideoType(DB_CONN, FILE_NAME)
            _split = os.path.splitext(FILE_NAME)
            _fileName = _split[0]
            _updateTime = datetime.datetime.now()

            _contentId = Database.Insert(
                DB_CONN,
                INTO=dbManager.Db_Content,
                ROW=[
                    'Folder_Id', 'FileType_Id', 'Content_Name',
                    'Content_Location'
                ],
                VALUES=[_folderId, _fileType, _fileName, FILE_LOC])
            Database.Update(
                DB_CONN,
                dbManager.Db_Program,
                SET='Num_Content = Num_Content + 1, Program_Last_Updated = "%s"'
                % (_updateTime),
                WHERE='Program_Id = %i' % _programId)

            programBuilder.BuildProgram(DB_CONN, _programId)

            DebugLog("Updated and rebuilt Program with Id: %i" % (_programId))

            return _contentId

    else:

        DebugLog("Content: %s already exists" % (FILE_NAME))

        return _contentData[0]
Ejemplo n.º 5
0
def AddFolderToDb(DB_CONN, ROOT, FOLDER_LOC, FOLDER_NAME):
    if (FOLDER_NAME not in dbManager.ScannerIgnore):
        _folderData = Database.Select(DB_CONN,
                                      SELECT='Folder_Id, Program_Id',
                                      FROM=dbManager.Db_ContFolder,
                                      WHERE='Folder_Location = "%s"' %
                                      FOLDER_LOC)

        if (_folderData):
            DebugLog("Folder %s exists at location %s" %
                     (FOLDER_NAME, FOLDER_LOC))
            return (_folderData[0], _folderData[1])

        elif (FOLDER_NAME not in dbManager.ScannerIgnore):
            _programId = None
            _programId = FindProgram(DB_CONN, ROOT, FOLDER_LOC, FOLDER_NAME)

            if (_programId != -1):
                _folderId = 0

                _folderId = Database.Insert(
                    DB_CONN,
                    INTO=dbManager.Db_ContFolder,
                    ROW=['Folder_Name', 'Folder_Location', 'Program_Id'],
                    VALUES=[FOLDER_NAME, FOLDER_LOC, _programId])

                DebugLog("Adding Folder: %s to Location: %s" %
                         (FOLDER_NAME, FOLDER_LOC))

                return (_folderId, _programId)

            elif (debug):
                raise Exception(
                    "Program not found for Folder: %s in Location: %s" %
                    (FOLDER_NAME, FOLDER_LOC))

        else:
            DebugLog("Folder %s has been ignored at location %s" %
                     (FOLDER_NAME, FOLDER_LOC))
            return None

    DebugLog("Folder %s is being skipped" % (FOLDER_NAME))
def FindFolder(DB_CONN, PHYSICAL_PATH, WEB_PATH, FOLDER_NAME, PROGRAM_ID):
    DebugLog("Checking if Content Folder %s: is in Database" % FOLDER_NAME)

    _searchResult = Database.Select(DB_CONN,
                                    SELECT='Folder_Id, Program_Id',
                                    FROM=dbManager.Db_ContFolder,
                                    WHERE='Folder_Location == "%s"' % WEB_PATH)

    if (FOLDER_NAME not in dbManager.MetaFolder):
        if (_searchResult):
            DebugLog("Folder %s has been Found" % FOLDER_NAME)

            if (PROGRAM_ID == _searchResult[1]):
                DebugLog("Folder %s Information up to date" % FOLDER_NAME)

                dbManager.Current_ContFolder.remove(_searchResult)
                return _searchResult[0]

            else:
                DebugLog("Updating Folder %s with new Program ID: %i" %
                         (FOLDER_NAME, PROGRAM_ID))

                Database.Update(DB_CONN,
                                dbManager.Db_ContFolder,
                                SET='Program_Id = %i' % PROGRAM_ID,
                                WHERE='Folder_Id = %i' % _searchResult[0])

                return _searchResult[0]

        else:
            DebugLog("Adding Folder %s to Database" % FOLDER_NAME)
            _folderId = Database.Insert(
                DB_CONN,
                INTO=dbManager.Db_ContFolder,
                ROW=['Folder_Name', 'Folder_Location', 'Program_Id'],
                VALUES=[FOLDER_NAME, WEB_PATH, PROGRAM_ID])

            return _folderId

    return None
def ScanProgramMetaFolder(DB_CONN, PROGRAM_ID, PROGRAM_LOC, PROGRAM_WEB_LOC):
    _programMetaLoc = os.path.join(PROGRAM_LOC, dbManager.MetaFolder)
    _programWebLoc = os.path.join(PROGRAM_WEB_LOC, dbManager.MetaFolder)
    _updated = False
    
    DebugLog("Scanning %s for MetaData" % _programMetaLoc)

    if(os.path.exists(_programMetaLoc)):
        for _item in os.listdir(_programMetaLoc):
            DebugLog("Found Meta Data %s" % _item)

            _physicalLoc = os.path.join(_programMetaLoc, _item)
            _webLoc = os.path.join(_programWebLoc, _item)
            _fileType = GetPhotoType(DB_CONN, _physicalLoc)
            _imageType = GetImageType(DB_CONN, "Default")

            DebugLog("%s File type is %i" % (_item, _fileType))

            if(_fileType != -1):
                _searchResult = Database.Select(DB_CONN,
                    SELECT = 'ProgImg_Id',
                    FROM = dbManager.Db_Img,
                    WHERE = 'Img_Location = "%s"' % _webLoc)

                if(_searchResult):
                    DebugLog("%s Exists in Database" % (_item))
                    dbManager.Current_MetaImages.remove(_searchResult)
                
                else:
                    DebugLog("%s is being added to Database" % (_item))
                    Database.Insert(DB_CONN,
                        INTO = dbManager.Db_Img,
                        ROW = ['Program_Id', 'Image_Type', 'File_Type', 'Img_Location'],
                        VALUES = [PROGRAM_ID, _imageType, _fileType, _webLoc])
                    _updated = True
    else:
        DebugLog("Meta Data Folder Does not Exist for Program with ID = %i" % PROGRAM_ID)

    return _updated
def AddProgram(DB_CONN, ITEM, PHYSICAL_ROOT, WEB_ROOT):

    DebugLog("Adding Program %s to Database" % ITEM)

    _physicalLoc = os.path.join(PHYSICAL_ROOT, ITEM)
    _webLoc = os.path.join(WEB_ROOT, ITEM)

    if (ITEM not in dbManager.ScannerIgnore and os.path.isdir(_physicalLoc)):
        _programId = -1
        _searchResult = Database.Select(DB_CONN,
                                        SELECT='Program_Id',
                                        FROM=dbManager.Db_Program,
                                        WHERE='Program_Location == "%s"' %
                                        _physicalLoc)

        if (_searchResult):
            _programId = _searchResult[0]
            dbManager.Current_Program.remove(_searchResult)

        else:
            DebugLog("Adding Program at %s" % _physicalLoc)

            _programId = Database.Insert(
                DB_CONN,
                INTO=dbManager.Db_Program,
                ROW=[
                    'Program_Name', 'Program_Location', 'Program_Web_Location',
                    'Num_Content'
                ],
                VALUES=[ITEM, _physicalLoc, _webLoc, 0])

        if (_programId == -1):
            raise Exception("ERROR: No Program Id Created")

        ContentManager.FindContent(DB_CONN, PHYSICAL_ROOT, WEB_ROOT, ITEM,
                                   _programId)
Ejemplo n.º 9
0
def BuildDatabase(DB_CONN):
    #Checks to see if Database exists and if it does drop it
    listOfDb = dbManager.Db_Old + dbManager.Db_List

    for db in listOfDb:
        if (Database.Select(DB_CONN,
                            SELECT="name",
                            FROM="sqlite_master",
                            WHERE="type='table' AND name='%s'" % db) != None):
            Database.Drop(DB_CONN, db)

    #Building YourFlix Table and Setting Version
    Database.CreateTable(DB_CONN,
                         TABLE=dbManager.Db_YourFlix,
                         VALUES=[["Version", "REAL NOT NULL"]])

    Database.Insert(DB_CONN,
                    INTO=dbManager.Db_YourFlix,
                    VALUES=[dbManager.Db_Version])

    #Building Channel Table
    Database.CreateTable(
        DB_CONN,
        TABLE=dbManager.Db_Channel,
        VALUES=[["Channel_Id", "INTEGER PRIMARY KEY AUTOINCREMENT"],
                ["Channel_Name", "TEXT UNIQUE NOT NULL"],
                ["Channel_Desctiption", "TEXT"]])

    for _channelName in dbManager.DefaultChannels:
        Database.Insert(DB_CONN,
                        INTO=dbManager.Db_Channel,
                        ROW=["Channel_Name"],
                        VALUES=[_channelName])

    #Building Channel Program Table
    Database.CreateTable(
        DB_CONN,
        TABLE=dbManager.Db_ChProgram,
        VALUES=[["ChProg_Id", "INTEGER PRIMARY KEY AUTOINCREMENT"],
                ["Channel_Id", "INTEGER"], ["Program_Id", "INTEGER"]])

    #Building Program Table
    Database.CreateTable(
        DB_CONN,
        TABLE=dbManager.Db_Program,
        VALUES=[["Program_Id", "INTEGER PRIMARY KEY AUTOINCREMENT"],
                ["Program_Name", "TEXT UNIQUE NOT NULL"],
                ["Program_Desctiption", "TEXT"],
                ["Program_Location", "TEXT NOT NULL"],
                ["Program_Web_Location", "TEXT NOT NULL"],
                ["First_Content", "INTEGER"], ["First_Folder", "INTEGER"],
                ["Num_Content", "INTEGER NOT NULL"],
                ["Program_Last_Updated", "DATETIME"]])

    #Building Program Image Table
    Database.CreateTable(
        DB_CONN,
        TABLE=dbManager.Db_Img,
        VALUES=[["ProgImg_Id", "INTEGER PRIMARY KEY AUTOINCREMENT"],
                ["Program_Id", "INTEGER NOT NULL"],
                ["Image_Type", "INTEGER NOT NULL"],
                ["File_Type", "INTEGER NOT NULL"],
                ["Img_Location", "TEXT UNIQUE NOT NULL"]])

    #Building Image Type Table
    Database.CreateTable(
        DB_CONN,
        TABLE=dbManager.Db_ImageType,
        VALUES=[["ImgType_Id", "INTEGER PRIMARY KEY AUTOINCREMENT"],
                ["Image_Type", "TEXT UNIQUE NOT NULL"]])

    Database.Insert(DB_CONN,
                    INTO=dbManager.Db_ImageType,
                    ROW=["Image_Type"],
                    VALUES=['Default'])

    #Building FileType Table and Setting Set Media Types
    Database.CreateTable(
        DB_CONN,
        TABLE=dbManager.Db_File,
        VALUES=[["FileType_Id", "INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL"],
                ["FileType", "TEXT NOT NULL"],
                ["FileType_Extention", "TEXT UNIQUE NOT NULL"]])

    #adding Folder to DB
    Database.Insert(DB_CONN,
                    INTO=dbManager.Db_File,
                    ROW=["FileType", "FileType_Extention"],
                    VALUES=[dbManager.FolderType, ''])

    #Adding Supported Images
    for _type in dbManager.SupportedImg:
        fileExtention = "." + _type.lower()
        Database.Insert(DB_CONN,
                        INTO=dbManager.Db_File,
                        ROW=["FileType", "FileType_Extention"],
                        VALUES=[dbManager.ImageType, fileExtention])

    #Adding Supported Videos
    for _type in dbManager.SupportedVideos:
        fileExtention = "." + _type.lower()
        Database.Insert(DB_CONN,
                        INTO=dbManager.Db_File,
                        ROW=["FileType", "FileType_Extention"],
                        VALUES=[dbManager.VideoType, fileExtention])

    #Building Program Folder Table
    Database.CreateTable(
        DB_CONN,
        TABLE=dbManager.Db_ContFolder,
        VALUES=[["Folder_Id", "INTEGER PRIMARY KEY AUTOINCREMENT"],
                ["Folder_Name", "TEXT NOT NULL"],
                ["Folder_Location", "TEXT NOT NULL"],
                ["Program_Id", "INTEGER NOT NULL"]])

    #Building Content Table
    Database.CreateTable(
        DB_CONN,
        TABLE=dbManager.Db_Content,
        VALUES=[["Content_Id", "INTEGER PRIMARY KEY AUTOINCREMENT"],
                ["Folder_Id", "INTEGER NOT NULL"],
                ["FileType_Id", "INTEGER NOT NULL"],
                ["Content_Name", "TEXT NOT NULL"],
                ["Content_Location", "TEXT UNIQUE NOT NULL"]])
def FindContent(DB_CONN, PHYSICAL_ROOT, WEB_ROOT, FOLDER_NAME, PROGRAM_ID):
    _currentPhysicalFolder = os.path.join(PHYSICAL_ROOT, FOLDER_NAME)
    _foundOrAddedContent = 0

    if os.path.isdir(_currentPhysicalFolder):
        DebugLog("Checking DIR %s" % _currentPhysicalFolder)

        _currentWebFolder = os.path.join(WEB_ROOT, FOLDER_NAME)
        _currentFolderId = -1

        for _item in os.listdir(_currentPhysicalFolder):
            DebugLog("Found %s" % _item)

            if (_item not in dbManager.ScannerIgnore):
                _physicalContent = os.path.join(_currentPhysicalFolder, _item)
                _webContent = os.path.join(_currentWebFolder, _item)

                if (os.path.isdir(_physicalContent)):
                    _foundOrAddedContent += FindContent(
                        DB_CONN, _currentPhysicalFolder, _currentWebFolder,
                        _item, PROGRAM_ID)

                else:

                    #Make Sure it has the same select data as the Pull data
                    _searchResult = Database.Select(
                        DB_CONN,
                        SELECT='Content_Id, Folder_Id',
                        FROM=dbManager.Db_Content,
                        WHERE='Content_Location == "%s"' % _webContent)

                    if (_currentFolderId == -1):
                        _currentFolderId = FindFolder(DB_CONN, PHYSICAL_ROOT,
                                                      _currentWebFolder,
                                                      FOLDER_NAME, PROGRAM_ID)

                    if (_currentFolderId):
                        if (_searchResult):
                            DebugLog("Content: %s is already in DB" % _item)
                            dbManager.Current_Content.remove(_searchResult)
                            _foundOrAddedContent += 1

                        else:
                            _fileType = GetVideoType(DB_CONN, _physicalContent)
                            _split = os.path.splitext(_item)
                            _fileName = _split[0]
                            _updateTime = datetime.datetime.now()

                            if (_fileType != -1):
                                DebugLog("Adding %s to Database" % _item)

                                Database.Insert(DB_CONN,
                                                INTO=dbManager.Db_Content,
                                                ROW=[
                                                    'Folder_Id', 'FileType_Id',
                                                    'Content_Name',
                                                    'Content_Location'
                                                ],
                                                VALUES=[
                                                    _currentFolderId,
                                                    _fileType, _fileName,
                                                    _webContent
                                                ])

                                Database.Update(
                                    DB_CONN,
                                    dbManager.Db_Program,
                                    SET=
                                    'Num_Content = Num_Content + 1, Program_Last_Updated = "%s"'
                                    % (_updateTime),
                                    WHERE='Program_Id = %i' % PROGRAM_ID)
                                _foundOrAddedContent += 1

    return _foundOrAddedContent