def workflowNames(self):
        statemachines = self.package.getStateMachines()
        workflows = [(utils.cleanName(sm.getName()),
                      sm.getTaggedValue('meta_type', 'Workflow'))
                     for sm in statemachines]
        workflows.sort()

        result = []
        for name, meta_type in workflows:
            item = {}
            item['name'] = name
            item['meta_type'] = meta_type
            result.append(item)

        return result
def addLink(name,
            url,
            mode,
            iconimage,
            plot='Plot Info',
            episode=None,
            date=None,
            show_url=None,
            max_page=None):
    u = sys.argv[0] + "?url=" + urllib.quote_plus(url) + "&mode=" + str(
        mode) + "&name=" + urllib.quote_plus(name)
    ok = True
    liz = xbmcgui.ListItem(utils.cleanName(name),
                           iconImage="DefaultVideo.png",
                           thumbnailImage=iconimage)
    if date and episode:
        labels = {
            "Title": utils.cleanName(name),
            "Plot": utils.cleanName(plot),
            "Season": '0',
            "Episode": str(episode),
            "Aired": utils.convert_airdate(date, 'aired')
        }
        xbmcplugin.setContent(int(sys.argv[1]), 'episodes')
    elif date:
        labels = {
            "Title": utils.cleanName(name),
            "Plot": utils.cleanName(plot),
            "date": utils.convert_airdate(date, 'date')
        }
    else:
        labels = {
            "Title": utils.cleanName(name),
            "plot": utils.cleanName(plot)
        }
    liz.setInfo(type="Video", infoLabels=labels)
    liz.setProperty('IsPlayable', 'true')
    if show_url and max_page:
        RunPlugin = 'RunPlugin({}?mode=11&url={}&max_page={})'.format(
            sys.argv[0], urllib.quote_plus(show_url), str(max_page))
        liz.addContextMenuItems([
            (
                'Jump to page',
                RunPlugin,
            ),
        ])
    ok = xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),
                                     url=u,
                                     listitem=liz)
    return ok
def addLink(name, url, mode, iconimage, plot='Plot Info', episode=None, date=None, show_url=None, max_page=None):
    u = sys.argv[0] + "?url=" + urllib.quote_plus(url) + "&mode=" + str(mode) + "&name=" + urllib.quote_plus(name)
    ok = True
    liz = xbmcgui.ListItem(utils.cleanName(name), iconImage="DefaultVideo.png", thumbnailImage=iconimage)
    if date and episode:
        labels = {"Title": utils.cleanName(name), "Plot" : utils.cleanName(plot), "Season": '0', "Episode": str(episode), "Aired" : utils.convert_airdate(date, 'aired')}
        xbmcplugin.setContent(int(sys.argv[1]), 'episodes')
    elif date:
        labels = {"Title": utils.cleanName(name), "Plot" : utils.cleanName(plot), "date" : utils.convert_airdate(date, 'date')}
    else:
        labels = {"Title": utils.cleanName(name), "plot" : utils.cleanName(plot)}
    liz.setInfo(type="Video", infoLabels=labels)
    liz.setProperty('IsPlayable', 'true')
    if show_url and max_page:
        RunPlugin = 'RunPlugin({}?mode=11&url={}&max_page={})'.format(sys.argv[0], urllib.quote_plus(show_url), str(max_page))
        liz.addContextMenuItems([('Jump to page', RunPlugin,),])
    ok = xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=u, listitem=liz)
    return ok
def EDIT_DB(db_arg):
    print 'Scrape for DB Activated'
    #print 'Parameter: ' + str(db_arg)
    aborted = 0
    data = db_arg.split(';')
    db_mode = data[0].replace('MODE:', '')
    db_name = data[1].replace('NAME:', '')
    db_url = data[2].replace('URL:', '')
    show_name = db_name
    db_name = db_name.replace(' ', '').lower()
    print 'DB Mode: ' + db_mode
    print 'DB Url:' + db_url
    print 'DB name:' + db_name
    print 'Show name:' + show_name

    # Connect to DB
    conn = sqlite3.connect(local_db)
    conn.text_factory = str
    c = conn.cursor()
    if db_mode == 'UPDATE':
        print 'UPDATE STARTED!!!'
        print 'Show URL: ' + db_url
        # Progress Dialog
        progress = xbmcgui.DialogProgress()
        progress.create('Updating Show', 'Scraping new videos...')
        progress.update(1, "", 'Initializing scrape...', "")
        # Grab Page Content
        content = utils.grab_url(db_url)
        # Grab Show Icon
        show_icon = utils.grab_icon(content)
        # Get max page number
        matchc = re.compile(
            'class="pages">Page (.+?) of (.+?)</span>').findall(content)
        lastp = int(matchc[0][1])

        counter = 0
        video_list = []
        for i in range(1, lastp + 1):
            allready_in_db = 0
            percent = int((100 / lastp) * i)
            message = "Scraping page " + str(i) + " of " + str(lastp)
            progress.update(percent, "", message, "")
            xbmc.sleep(1000)
            url = db_url + 'page/' + str(i)
            page_content = utils.grab_url(url)

            video_info = utils.scrape_data(page_content, 'DB')

            for url, name, thumbnail, plot, airdate in video_info:
                # Test to see if entry is allready in DB
                c.execute('SELECT airdate FROM {} WHERE name="{}"'.format(
                    db_name, name))
                test = c.fetchone()
                if test is None:
                    print 'Episode: "{}" not in DB, adding to update list now!'.format(
                        name)
                    entry = (name, url, thumbnail, plot, airdate)
                    video_list.append(entry)
                    counter = counter + 1
                else:  # Entry available in local DB
                    print 'Episode: "{}" Allready in DB, breaking loop now!'.format(
                        name)
                    print 'Stopping update process but will commit changes (if any) to DB'
                    allready_in_db = 1
                    break

            if progress.iscanceled():
                aborted = True
                break
            if allready_in_db:
                break

        progress.close()
        if aborted:
            print 'Scrape was aborted!'  # DEBUG INFO
            dialog = xbmcgui.Dialog()
            dialog.notification('Update canceled!',
                                'No updates were stored in the DB',
                                xbmcgui.NOTIFICATION_WARNING, 3000)
        else:
            dialog = xbmcgui.Dialog()
            c.execute(
                'UPDATE show_list SET show_icon="{}" WHERE table_name="{}"'.
                format(show_icon, db_name))
            if len(video_list) > 0:
                video_list.reverse()
                c.executemany(
                    'INSERT INTO {} (name, url, thumb, plot, airdate) VALUES (?,?,?,?,?)'
                    .format(db_name), video_list)
                dialog.notification(
                    'Update Successfull',
                    '{} entries added to the DB'.format(str(counter)),
                    xbmcgui.NOTIFICATION_INFO, 3000)
            else:
                dialog.notification('No Updates',
                                    'No updates were found for this show',
                                    xbmcgui.NOTIFICATION_INFO, 3000)
        conn.commit()
        conn.close()

    elif db_mode == 'ADD':
        c.execute(
            'SELECT name FROM sqlite_master WHERE type="table" AND name="{}"'.
            format(db_name))
        test = c.fetchone()
        if test is None:
            print 'Table: "{}" does not exist in DB, i will continue'.format(
                db_name)
            # Progress Dialog
            progress = xbmcgui.DialogProgress()
            progress.create('Adding Show to local Database',
                            'Scraping all videos...')
            progress.update(1, "", 'Initializing scrape...', "")
            # Grab Page Conent
            content = utils.grab_url(db_url)
            # Grab Show Icon
            show_icon = utils.grab_icon(content)
            # Get max page number
            matchc = re.compile(
                'class="pages">Page (.+?) of (.+?)</span>').findall(content)
            lastp = int(matchc[0][1])
            video_list = []
            for i in range(1, lastp + 1):
                percent = int((100 / lastp) * i)
                message = "Scraping page " + str(i) + " of " + str(lastp)
                progress.update(percent, "", message, "")
                xbmc.sleep(1000)
                url = db_url + 'page/' + str(i)  # Modified "/page/" to "page/"
                page_content = utils.grab_url(url)

                video_info = utils.scrape_data(page_content, 'DB')

                for url, name, thumbnail, plot, airdate in video_info:
                    print 'name: ' + utils.cleanName(
                        name
                    )  # + '\nplot: ' + utils.cleanName(plot) # DEBUG INFO
                    entry = (name, url, thumbnail, plot, airdate)
                    video_list.append(entry)
                if progress.iscanceled():
                    aborted = True
                    break

            progress.close()
            dialog = xbmcgui.Dialog()
            if aborted:
                print 'Scrape was aborted!'  # DEBUG INFO
                dialog.notification('Process canceled!',
                                    'The show was not added to the local DB.',
                                    xbmcgui.NOTIFICATION_WARNING, 3000)
            else:
                if len(video_list) > 0:
                    # Turn List of Videos arround to get accurate list based on release date
                    video_list.reverse()
                    # Create DB Tables
                    c.execute(
                        'CREATE TABLE IF NOT EXISTS {} (id INTEGER PRIMARY KEY, name TEXT, url TEXT, thumb TEXT, plot TEXT, airdate TEXT)'
                        .format(db_name))
                    c.execute(
                        'CREATE TABLE IF NOT EXISTS show_list (table_name  TEXT PRIMARY KEY, show_name TEXT, show_url TEXT, show_icon TEXT)'
                    )
                    # Add Content to DB
                    c.execute(
                        'INSERT OR REPLACE INTO show_list VALUES ("{}", "{}", "{}", "{}")'
                        .format(db_name, show_name, db_url, show_icon))
                    c.executemany(
                        'INSERT INTO {} (name, url, thumb, plot, airdate) VALUES (?,?,?,?,?)'
                        .format(db_name), video_list)
                    dialog.notification(
                        'Process Complete!',
                        'The show "{}" was added to the local DB.'.format(
                            show_name), xbmcgui.NOTIFICATION_INFO, 3000)
                    conn.commit()
        else:
            print 'Table: "{}" does exist in DB, i will not continue'.format(
                db_name)
            dialog = xbmcgui.Dialog()
            dialog.ok(
                'Database',
                'Show "{}" is allready in the local DB. Update from there.'.
                format(show_name))
        conn.close()

    elif db_mode == 'REMOVE':
        dialog = xbmcgui.Dialog()
        if dialog.yesno("Remove from local DB",
                        'Dou you really want to remove the show:',
                        '{}'.format(show_name), "", 'No', 'Yes'):
            c.execute('DROP TABLE IF EXISTS "{}"'.format(db_name))
            c.execute(
                'DELETE FROM show_list WHERE table_name="{}"'.format(db_name))
            conn.commit()
        conn.close()
        xbmc.executebuiltin("Container.Refresh")
Example #5
0
    def generateWorkflows(self):
        statemachines = self.package.getStateMachines()
        if not statemachines:
            return

        d = {
            'package': self.package,
            'generator': self,
            'wfgenerator': self,
            'atgenerator': self.atgenerator,
            'builtins': __builtins__,
            'utils' :utils,
        }
        d.update(__builtins__)

        extDir = os.path.join(self.package.getFilePath(), 'Extensions')
        self.atgenerator.makeDir(extDir)

        for sm in statemachines:
            d['statemachine'] = sm
            sm_name = utils.cleanName(sm.getName())

            # Generate workflow script
            log.info("Generating workflow '%s'.", sm_name)
            templ = utils.readTemplate('create_workflow.py')
            scriptpath = os.path.join(extDir, sm_name + '.py')
            filesrc = self.atgenerator.readFile(scriptpath) or ''
            parsedModule = PyModule(filesrc, mode='string')
            d['parsedModule'] = parsedModule
            dtml = HTML(templ, d)
            res = dtml()
            of = self.atgenerator.makeFile(scriptpath)
            of.write(res)
            of.close()

            # Generate workflow transition script, if any
            if sm.getAllTransitionActionNames():
                log.info("Generating workflow script(s).")
                templ = utils.readTemplate('create_workflow_script.py')
                scriptpath = os.path.join(extDir, sm_name + '_scripts.py')
                filesrc = self.atgenerator.readFile(scriptpath) or ''
                parsedModule = PyModule(filesrc, mode='string')
                d['parsedModule'] = parsedModule
                dtml = HTML(templ, d)
                res = dtml()
                of = self.atgenerator.makeFile(scriptpath)
                of.write(res)
                of.close()
            else:
                log.info("Workflow %s has no script(s)." % sm_name)

        del d['statemachine']
        log.debug("Creating InstallWorkflows.py file.")
        templ = utils.readTemplate('InstallWorkflows.py')
        scriptpath = os.path.join(extDir, 'InstallWorkflows.py')
        filesrc = self.atgenerator.readFile(scriptpath) or ''
        parsedModule = PyModule(filesrc, mode='string')
        d['parsedModule'] = parsedModule
        dtml = HTML(templ, d)
        res = dtml()
        of = self.atgenerator.makeFile(scriptpath)
        of.write(res)
        of.close()
    def generateWorkflows(self):
        log.debug("Generating workflows.")
        statemachines = self.package.getStateMachines()
        typemapping=self.typeMapping()
        if not statemachines and not typemapping:
            log.debug("No workflows that agx knows off.")
            return

        d = {
            'package': self.package,
            'generator': self,
            'wfgenerator': self,
            'atgenerator': self.atgenerator,
            'builtins': __builtins__,
            'utils' :utils,
        }
        d.update(__builtins__)

        # we do not create the Extension directory here any longer, since
        # it is just used by workflow scripts (we aim to get
        # rid of them too), so we create it jit when scripts are generated.
        extDir = os.path.join(self.package.getFilePath(), 'Extensions')
        profileDir = os.path.join(self.package.getFilePath(),
                                  'profiles', 'default')
        self.atgenerator.makeDir(profileDir)
        workflowDir = os.path.join(profileDir, 'workflows')
        self.atgenerator.makeDir(workflowDir)

        for sm in statemachines:
            d['info'] = WorkflowInfo(sm)
            d['target_version'] = self.getOption('plone_target_version',
                                          self.package, DEFAULT_TARGET_VERSION)

            # start BBB warning
            smName = utils.cleanName(sm.getName())
            smDir = os.path.join(workflowDir, smName)
            oldFile = os.path.join(extDir, smName + '.py')
            if os.path.exists(oldFile):
                log.warn('Workflow now uses generic setup, please '
                         'remove %s.', oldFile)
            # end BBB warning

            self.atgenerator.makeDir(smDir)
            log.debug("Generated specific workflow's dir '%s'.",
                      smDir)

            # Generate workflow xml
            log.info("Generating workflow '%s'.", smName)
            handleSectionedFile(['profiles', 'definition.xml'],
                            os.path.join(smDir, 'definition.xml'),
                            sectionnames=['FOOT'],
                            templateparams=d)

            self._collectSubscribers(sm)

        # generate wfsubscribers.zcml
        self._generateWorkflowSubscribers()

        oldFile = os.path.join(extDir, 'InstallWorkflows.py')

        # start BBB warning
        if os.path.exists(oldFile):
            log.warn('Workflow now uses generic setup, please '
                     'remove %s.', oldFile)
        # end BBB warning

        log.debug("Creating workflows.xml file.")
        d['workflowNames'] = self.workflowNames()
        d['workflowless'] = self.workflowLessTypes()
        d['typeMapping'] = self.typeMapping()
        d['defaultId'] = self.findDefaultWorkflowId()
        handleSectionedFile(['profiles', 'workflows.xml'],
                            os.path.join(profileDir, 'workflows.xml'),
                            sectionnames=['workflowobjects',
                                          'workflowbindings'],
                            templateparams=d)

        if len(self.extraRoles()) == 0:
            log.debug("Skipping creation of rolemap.xml file.")
            return
        log.debug("Creating rolemap.xml file.")
        d['extraRoles'] = self.extraRoles()

        handleSectionedFile(['profiles', 'rolemap.xml'],
                            os.path.join(profileDir, 'rolemap.xml'),
                            sectionnames=['roles',
                                          'permissions'],
                            templateparams=d)
def EDIT_DB(db_arg):
    print 'Scrape for DB Activated'
    #print 'Parameter: ' + str(db_arg)
    aborted = 0
    data = db_arg.split(';')
    db_mode = data[0].replace('MODE:', '')
    db_name = data[1].replace('NAME:', '')
    db_url = data[2].replace('URL:', '')
    show_name = db_name
    db_name = db_name.replace(' ', '').lower()
    print 'DB Mode: ' + db_mode
    print 'DB Url:' + db_url
    print 'DB name:' + db_name
    print 'Show name:' + show_name

    # Connect to DB
    conn = sqlite3.connect(local_db)
    conn.text_factory = str
    c = conn.cursor()
    if db_mode == 'UPDATE':
        print 'UPDATE STARTED!!!'
        print 'Show URL: '+ db_url
        # Progress Dialog
        progress = xbmcgui.DialogProgress()
        progress.create('Updating Show', 'Scraping new videos...')
        progress.update( 1, "", 'Initializing scrape...', "" )
        # Grab Page Content
        content = utils.grab_url(db_url)
        # Grab Show Icon
        show_icon = utils.grab_icon(content)
        # Get max page number
        matchc = re.compile('class="pages">Page (.+?) of (.+?)</span>').findall(content)
        lastp = int(matchc[0][1])

        counter = 0
        video_list = []
        for i in range(1, lastp + 1):
            allready_in_db = 0
            percent = int( (100 / lastp) * i)
            message = "Scraping page " + str(i) + " of " + str(lastp)
            progress.update( percent, "", message, "" )
            xbmc.sleep( 1000 )
            url = db_url + 'page/' + str(i)
            page_content = utils.grab_url(url)

            video_info = utils.scrape_data(page_content, 'DB')

            for url, name, thumbnail, plot, airdate in video_info:
                # Test to see if entry is allready in DB
                c.execute('SELECT airdate FROM {} WHERE name="{}"'.format(db_name, name))
                test = c.fetchone()
                if test is None:
                    print 'Episode: "{}" not in DB, adding to update list now!'.format(name)
                    entry = (name, url, thumbnail, plot, airdate)
                    video_list.append(entry)
                    counter = counter + 1
                else: # Entry available in local DB
                    print 'Episode: "{}" Allready in DB, breaking loop now!'.format(name)
                    print 'Stopping update process but will commit changes (if any) to DB'
                    allready_in_db = 1
                    break

            if progress.iscanceled():
                aborted = True
                break
            if allready_in_db:
                break

        progress.close()
        if aborted:
            print 'Scrape was aborted!' # DEBUG INFO
            dialog = xbmcgui.Dialog()
            dialog.notification('Update canceled!', 'No updates were stored in the DB', xbmcgui.NOTIFICATION_WARNING, 3000)
        else:
            dialog = xbmcgui.Dialog()
            c.execute('UPDATE show_list SET show_icon="{}" WHERE table_name="{}"'.format(show_icon, db_name))
            if len(video_list) > 0:
                video_list.reverse()
                c.executemany('INSERT INTO {} (name, url, thumb, plot, airdate) VALUES (?,?,?,?,?)'.format(db_name), video_list)
                dialog.notification('Update Successfull', '{} entries added to the DB'.format(str(counter)), xbmcgui.NOTIFICATION_INFO, 3000)
            else:
                dialog.notification('No Updates', 'No updates were found for this show', xbmcgui.NOTIFICATION_INFO, 3000)
        conn.commit()
        conn.close()

    elif db_mode == 'ADD':
        c.execute('SELECT name FROM sqlite_master WHERE type="table" AND name="{}"'.format(db_name))
        test = c.fetchone()
        if test is None:
            print 'Table: "{}" does not exist in DB, i will continue'.format(db_name)
            # Progress Dialog
            progress = xbmcgui.DialogProgress()
            progress.create('Adding Show to local Database', 'Scraping all videos...')
            progress.update( 1, "", 'Initializing scrape...', "" )
            # Grab Page Conent
            content = utils.grab_url(db_url)
            # Grab Show Icon
            show_icon = utils.grab_icon(content)
            # Get max page number
            matchc = re.compile('class="pages">Page (.+?) of (.+?)</span>').findall(content)
            lastp = int(matchc[0][1])
            video_list = []
            for i in range(1, lastp + 1):
                percent = int( (100 / lastp) * i)
                message = "Scraping page " + str(i) + " of " + str(lastp)
                progress.update( percent, "", message, "" )
                xbmc.sleep( 1000 )
                url = db_url + 'page/' + str(i) # Modified "/page/" to "page/"
                page_content = utils.grab_url(url)

                video_info = utils.scrape_data(page_content, 'DB')

                for url, name, thumbnail, plot, airdate in video_info:
                    print 'name: ' + utils.cleanName(name) # + '\nplot: ' + utils.cleanName(plot) # DEBUG INFO
                    entry = (name, url, thumbnail, plot, airdate)
                    video_list.append(entry)
                if progress.iscanceled():
                    aborted = True
                    break

            progress.close()
            dialog = xbmcgui.Dialog()
            if aborted:
                print 'Scrape was aborted!' # DEBUG INFO
                dialog.notification('Process canceled!', 'The show was not added to the local DB.', xbmcgui.NOTIFICATION_WARNING, 3000)
            else:
                if len(video_list) > 0:
                    # Turn List of Videos arround to get accurate list based on release date
                    video_list.reverse()
                    # Create DB Tables
                    c.execute('CREATE TABLE IF NOT EXISTS {} (id INTEGER PRIMARY KEY, name TEXT, url TEXT, thumb TEXT, plot TEXT, airdate TEXT)'.format(db_name))
                    c.execute('CREATE TABLE IF NOT EXISTS show_list (table_name  TEXT PRIMARY KEY, show_name TEXT, show_url TEXT, show_icon TEXT)')
                    # Add Content to DB
                    c.execute('INSERT OR REPLACE INTO show_list VALUES ("{}", "{}", "{}", "{}")'.format(db_name, show_name, db_url, show_icon))
                    c.executemany('INSERT INTO {} (name, url, thumb, plot, airdate) VALUES (?,?,?,?,?)'.format(db_name), video_list)
                    dialog.notification('Process Complete!',
                                        'The show "{}" was added to the local DB.'.format(show_name),
                                        xbmcgui.NOTIFICATION_INFO, 3000)
                    conn.commit()
        else:
            print 'Table: "{}" does exist in DB, i will not continue'.format(db_name)
            dialog = xbmcgui.Dialog()
            dialog.ok('Database', 'Show "{}" is allready in the local DB. Update from there.'.format(show_name))
        conn.close()

    elif db_mode == 'REMOVE':
        dialog = xbmcgui.Dialog()
        if dialog.yesno("Remove from local DB", 'Dou you really want to remove the show:','{}'.format(show_name), "",'No','Yes'):
            c.execute('DROP TABLE IF EXISTS "{}"'.format(db_name))
            c.execute('DELETE FROM show_list WHERE table_name="{}"'.format(db_name))
            conn.commit()
        conn.close()
        xbmc.executebuiltin("Container.Refresh")