Пример #1
0
    def getUserScript(self, script_route, **kwargs):

        klass = self

        class UserscriptHandler(RequestHandler):

            def get(self, random, route):

                bookmarklet_host = Env.setting('bookmarklet_host')
                loc = bookmarklet_host if bookmarklet_host else "{0}://{1}".format(self.request.protocol, self.request.headers.get('X-Forwarded-Host') or self.request.headers.get('host'))

                params = {
                    'includes': fireEvent('userscript.get_includes', merge = True),
                    'excludes': fireEvent('userscript.get_excludes', merge = True),
                    'version': klass.getVersion(),
                    'api': '%suserscript/' % Env.get('api_base'),
                    'host': loc,
                }

                script = klass.renderTemplate(__file__, 'template.js_tmpl', **params)
                klass.createFile(os.path.join(Env.get('cache_dir'), 'whatpotato.user.js'), script)

                self.redirect(Env.get('api_base') + 'file.cache/whatpotato.user.js')

        Env.get('app').add_handlers(".*$", [('%s%s' % (Env.get('api_base'), script_route), UserscriptHandler)])
Пример #2
0
    def __init__(self):

        if Env.get('desktop'):
            self.updater = DesktopUpdater()
        elif os.path.isdir(os.path.join(Env.get('app_dir'), '.git')):
            git_default = 'git'
            git_command = self.conf('git_command', default = git_default)
            git_command = git_command if git_command != git_default and (os.path.isfile(git_command) or re.match('^[a-zA-Z0-9_/\.\-]+$', git_command)) else git_default
            self.updater = GitUpdater(git_command)
        else:
            self.updater = SourceUpdater()

        addEvent('app.load', self.logVersion, priority = 10000)
        addEvent('app.load', self.setCrons)
        addEvent('updater.info', self.info)

        addApiView('updater.info', self.info, docs = {
            'desc': 'Get updater information',
            'return': {
                'type': 'object',
                'example': """{
        'last_check': "last checked for update",
        'update_version': "available update version or empty",
        'version': current_cp_version
}"""}
        })
        addApiView('updater.update', self.doUpdateView)
        addApiView('updater.check', self.checkView, docs = {
            'desc': 'Check for available update',
            'return': {'type': 'see updater.info'}
        })

        addEvent('setting.save.updater.enabled.after', self.setCrons)
Пример #3
0
    def getCache(self, cache_key, url = None, **kwargs):

        use_cache = not len(kwargs.get('data', {})) > 0 and not kwargs.get('files')

        if use_cache:
            cache_key_md5 = md5(cache_key)
            cache = Env.get('cache').get(cache_key_md5)
            if cache:
                if not Env.get('dev'): log.debug('Getting cache %s', cache_key)
                return cache

        if url:
            try:

                cache_timeout = 300
                if 'cache_timeout' in kwargs:
                    cache_timeout = kwargs.get('cache_timeout')
                    del kwargs['cache_timeout']

                data = self.urlopen(url, **kwargs)
                if data and cache_timeout > 0 and use_cache:
                    self.setCache(cache_key, data, timeout = cache_timeout)
                return data
            except:
                if not kwargs.get('show_error', True):
                    raise

                log.debug('Failed getting cache: %s', (traceback.format_exc(0)))
                return ''
Пример #4
0
 def getCredentials(self, **kwargs):
     try:
         oauth_token = kwargs.get('oauth')
     except:
         return 'redirect', Env.get('web_base') + 'settings/downloaders/'
     log.debug('oauth_token is: %s', oauth_token)
     self.conf('oauth_token', value = oauth_token);
     return 'redirect', Env.get('web_base') + 'settings/downloaders/'
Пример #5
0
def page_not_found(rh):
    index_url = Env.get('web_base')
    url = rh.request.uri[len(index_url):]

    if url[:3] != 'api':
        r = index_url + '#' + url.lstrip('/')
        rh.redirect(r)
    else:
        if not Env.get('dev'):
            time.sleep(0.1)

        rh.set_status(404)
        rh.write('Wrong API key used')
Пример #6
0
    def register(self):
        if self.registered:
            return
        try:

            hostname = self.conf("hostname")
            password = self.conf("password")
            port = self.conf("port")

            self.growl = notifier.GrowlNotifier(
                applicationName=Env.get("appname"),
                notifications=["Updates"],
                defaultNotifications=["Updates"],
                applicationIcon=self.getNotificationImage("medium"),
                hostname=hostname if hostname else "localhost",
                password=password if password else None,
                port=port if port else 23053,
            )
            self.growl.register()
            self.registered = True
        except Exception as e:
            if "timed out" in str(e):
                self.registered = True
            else:
                log.error("Failed register of growl: %s", traceback.format_exc())