Example #1
0
 def test_urlencode(self):
     assert utils.urlencode({'foo': 'bar',
                             'skipped': (),
                             'a param with space': 'value',
                             'x': 'a value with space'}) == \
         'a+param+with+space=value&foo=bar&x=a+value+with+space'
     assert utils.urlencode({'a': u'foo',
                             'b': u'\u4f60\u597d',
                             'c': '\xe4\xbd\xa0\xe5\xa5\xbd',
                             u'\u4f60\u597d': 'd'}) == \
         'a=foo&b=%E4%BD%A0%E5%A5%BD' + \
         '&c=%E4%BD%A0%E5%A5%BD&%E4%BD%A0%E5%A5%BD=d'
Example #2
0
 def test_urlencode(self):
     assert utils.urlencode({'foo': 'bar',
                             'skipped': (),
                             'a param with space': 'value',
                             'x': 'a value with space'}) == \
         'a+param+with+space=value&foo=bar&x=a+value+with+space'
     assert utils.urlencode({'a': u'foo',
                             'b': u'\u4f60\u597d',
                             'c': '\xe4\xbd\xa0\xe5\xa5\xbd',
                             u'\u4f60\u597d': 'd'}) == \
         'a=foo&b=%E4%BD%A0%E5%A5%BD' + \
         '&c=%E4%BD%A0%E5%A5%BD&%E4%BD%A0%E5%A5%BD=d'
Example #3
0
 def url(self, path="/", attr=None):
     url = self.config["url"] + path
     if attr:
         url += "?"
         i = 0
         for key in attr:
             if i > 0:
                 url += "&"
             val = attr[key]
             url += utils.urlencode(key) + "=" + utils.urlencode(val)
             i += 1
     return url
Example #4
0
 def _wrapper(self, generator="pumblr", group=None, date=None, private=0, tags=None, slug=None, **kw):
     """
 Write API.
 Arguments:
 - `generator`: A short description of the application
 - `group`: Post this to a secondary blog on your account
 - `date`: (optional) - The post date
 - `private`: (optional) 1 or 0. Whether the post is private.
 - `tags`: (optional) Comma-separated list of post tags.
 - `slug`: (optional) A custom string to appear in the post's URL
 \n
     """
     url = "http://www.tumblr.com/api/write"
     if group is not None:
         group = group if "." in group else "%s.tumblr.com" % group
     query = dict(
         email=self._email,
         password=self._password,
         generator=generator,
         group=group,
         date=date,
         private=private,
         tags=tags,
         slug=slug,
     )
     query.update(f(self, **kw))
     if not "type" in query.keys():
         raise PumblrError("post type is needed!")
     self._check_status_code(url, utils.urlencode(query))
Example #5
0
def update_query_param(
    request,
    *remove,
    **add,
):
    return request.path + urlencode(
        _add_query_param(_remove_query_param(request.query, *remove), **add))
Example #6
0
 def openRequestUrl(url, params, files):
     if len(files.get()) == 0:
         try:
             paramsdict = params.get()
             sanitized_params = {}
             for k, v in paramsdict.items():
                 if hasattr(k, 'encode'):
                     key = k.encode('utf-8')
                 elif hasattr(k, 'decode'):
                     key = k.decode('utf-8').encode('utf-8')
                 else:
                     key = str(k).encode('utf-8')
                 if hasattr(v, 'encode'):
                     val = v.encode('utf-8')
                 elif hasattr(k, 'decode'):
                     val = v.decode('utf-8').encode('utf-8')
                 else:
                     val = str(v).encode('utf-8')
                 sanitized_params[key] = val
             f = urllib.urlopen(url, urlencode(sanitized_params))
         #except Exception, e:
         except Exception as e:
             raise KalturaClientException(e, KalturaClientException.ERROR_CONNECTION_FAILED)
     else:
         fullParams = params
         fullParams.update(files)
         datagen, headers = multipart_encode(fullParams.get())
         request = urllib2.Request(url, datagen, headers)
         try:
             f = urllib2.urlopen(request)
         except Exception as e:
             raise KalturaClientException(e, KalturaClientException.ERROR_CONNECTION_FAILED)
     return f
    def authorize(self):
        '''
        Makes an authorization request.
        '''

        if not self._discovered:
            print('Need to discover authorization and token endpoints.')
            return

        headers = {'Content-Type': 'application/x-www-form-urlencoded'}
        payload = {'client_id': self.client_id, 'scope': ' '.join(self.scopes)}
        encoded = urlencode(payload)
        r = requests.request('POST',
                             self._device_auth_endpoint,
                             data=encoded,
                             headers=headers)
        j = r.json()
        r.close()

        if 'error' in j:
            raise RuntimeError(j['error'])

        self._device_code = j['device_code']
        self.user_code = j['user_code']
        self.verification_url = j['verification_url']
        self._interval = j['interval']
        self._code_expires_in = j['expires_in']
        self._authorization_started = True
        message = 'Use code %s at %s to authorize the device.' % (
            self.user_code, self.verification_url)
        print(message)
Example #8
0
 def openRequestUrl(url, params, files):
     if len(files.get()) == 0:
         try:
             paramsdict = params.get()
             sanitized_params = {}
             for k, v in paramsdict.items():
                 if hasattr(k, 'encode'):
                     key = k.encode('utf-8')
                 elif hasattr(k, 'decode'):
                     key = k.decode('utf-8').encode('utf-8')
                 else:
                     key = str(k).encode('utf-8')
                 if hasattr(v, 'encode'):
                     val = v.encode('utf-8')
                 elif hasattr(k, 'decode'):
                     val = v.decode('utf-8').encode('utf-8')
                 else:
                     val = str(v).encode('utf-8')
                 sanitized_params[key] = val
             f = urllib.urlopen(url, urlencode(sanitized_params))
         #except Exception, e:
         except Exception as e:
             raise KalturaClientException(
                 e, KalturaClientException.ERROR_CONNECTION_FAILED)
     else:
         fullParams = params
         fullParams.update(files)
         datagen, headers = multipart_encode(fullParams.get())
         request = urllib2.Request(url, datagen, headers)
         try:
             f = urllib2.urlopen(request)
         except Exception as e:
             raise KalturaClientException(
                 e, KalturaClientException.ERROR_CONNECTION_FAILED)
     return f
    def events(self, limit=5):
        # Calendar id is part of the endpoint iself.
        endpoint = 'https://www.googleapis.com/calendar/v3/calendars/primary/events'
        start_time = today_rfc3339(hours_offset=-1)
        token = self.device_auth.token()
        authorization = 'Bearer %s' % (token)
        headers = {
            'Authorization': authorization,
            'Accept': 'application/json'
        }
        payload = {
            'maxResults': limit,
            'orderBy': 'startTime',
            'singleEvents': 'true',
            'timeMin': start_time,
            'key': API_KEY
        }
        encoded = urlencode(payload)
        full_url = '%s?%s' % (endpoint, encoded)
        r = requests.request('GET', full_url, headers=headers)
        j = r.json()
        r.close()

        if 'error' in j:
            raise RuntimeError(j)

        return self._parse_calendar_events(j)
Example #10
0
 def delete(self, post_id):
     """
     Deleting post
     Arguments:
     - `post_id`: The integer ID of the post you wish to delete.
     """
     url = "http://www.tumblr.com/api/delete"
     query = {"email": self._email, "password": self._password, "post-id": post_id}
     self._check_status_code(url, utils.urlencode(query))
Example #11
0
    def prepare_request(self,
                        method,
                        uri,
                        params=None,
                        headers=None,
                        data=None,
                        json=None):
        params = {} if params is None else params
        if not isinstance(params, dict):
            raise TypeError('params should be dict')

        method = method.upper()
        params = encoded_dict(params)
        logger.debug(uri)
        url = '/'.join([
            self.api_server.strip(),
            self.endpoint.strip().strip('/'),
            self.version.strip().strip('/')
        ]) + uri.strip()
        logger.debug(url)
        url_parsed = urlparse(url)
        enc_params = urlencode(params)
        logger.debug(enc_params)
        if url_parsed.query is None or url_parsed.query == '':
            query = enc_params
        else:
            query = '%s&%s' % (url_parsed.query, enc_params)

        real_uri = urlunparse(('', '', url_parsed.path, url_parsed.params,
                               query, url_parsed.fragment))

        real_url = urlunparse(
            (url_parsed.scheme, url_parsed.netloc, url_parsed.path,
             url_parsed.params, query, url_parsed.fragment))

        self.request_data.host = url_parsed.netloc
        self.request_data.uri = real_uri
        self.request_data.method = method
        self.request_data.headers = {
            'Accept': 'application/json; charset=utf-8'
        }
        if headers is not None:
            # headers 是字典
            self.request_data.headers.update(headers)

        if method == 'GET':
            self.request_data.body = ''
        else:
            if json is not None:
                self.request_data.headers[
                    'Content-Type'] = 'application/json; charset=utf-8'
                self.request_data.body = json_util.dumps(json,
                                                         ensure_ascii=False)
            else:
                self.request_data.body = data

        return real_url
def plugin_widgetlisting(pluginpath, sublevel=""):
    """get all nodes in a plugin listing"""
    widgets = []
    if sublevel:
        media_array = kodi_json('Files.GetDirectory', {
            "directory": pluginpath,
            "media": "files"
        })
    else:
        if not getCondVisibility("System.HasAddon(%s)" % pluginpath):
            return []
        media_array = kodi_json('Files.GetDirectory', {
            "directory": "plugin://%s" % pluginpath,
            "media": "files"
        })
    for item in media_array:
        log_msg("skinshortcuts widgets processing: %s" % (item["file"]))
        content = item["file"]
        label = item["label"]
        # extendedinfo has some login-required widgets, skip those
        if ("script.extendedinfo" in pluginpath and not EXTINFO_CREDS
                and ("info=starred" in content or "info=rated" in content
                     or "info=account" in content)):
            continue
        if item.get("filetype", "") == "file":
            continue
        mutils = MetadataUtils()
        media_type = mutils.detect_plugin_content(item["file"])
        del mutils
        if media_type == "empty":
            continue
        if media_type == "folder":
            content = "plugin://script.skin.helper.service?action=widgets&path=%s&sublevel=%s" % (
                urlencode(item["file"]), label)
        # add reload param for widgets
        if "reload=" not in content:
            if "movies" in content:
                reloadstr = "&reload=$INFO[Window(Home).Property(widgetreload-movies)]"
            elif "episodes" in content:
                reloadstr = "&reload=$INFO[Window(Home).Property(widgetreload-episodes)]"
            elif "tvshows" in content:
                reloadstr = "&reload=$INFO[Window(Home).Property(widgetreload-tvshows)]"
            elif "musicvideos" in content:
                reloadstr = "&reload=$INFO[Window(Home).Property(widgetreload-musicvideos)]"
            elif "albums" in content or "songs" in content or "artists" in content:
                reloadstr = "&reload=$INFO[Window(Home).Property(widgetreload-music)]"
            else:
                reloadstr = "&reload=$INFO[Window(Home).Property(widgetreload)]"\
                    "$INFO[Window(Home).Property(widgetreload2)]"
            content = content + reloadstr
        content = content.replace("&limit=100", "&limit=25")
        widgets.append([label, content, media_type])
        if pluginpath == "script.extendedinfo" and not sublevel:
            # some additional entrypoints for extendedinfo...
            widgets += extendedinfo_youtube_widgets()
    return widgets
Example #13
0
 def auth(self, email, password):
     """validate credentials"""
     self._email = email
     self._password = password
     self._authenticated = False
     url = "http://www.tumblr.com/api/authenticate"
     query = dict(email=self._email, password=self._password)
     text = self._check_status_code(url, utils.urlencode(query))
     self._check_we_ll_be_back(text)
     self._authenticated = True
Example #14
0
 def post(self, request, *args, **kwargs):
     """Sends the user to the deletion page with a valid signature."""
     del request, args, kwargs  # unused
     self.enforce_xsrf(self.ACTION_ID)
     action = ('delete', self.params.id)
     path = '/delete?' + utils.urlencode({
         'id': self.params.id,
         'signature': reveal.sign(action),
     })
     return django.shortcuts.redirect(
         self.build_absolute_uri(path, self.env.repo))
Example #15
0
def prepare_form_encoded_body(params):
    """Prepare the Form-Encoded Body.

    Per `section 3.5.2`_ of the spec.

    params: OAuth parameters and data (i.e. POST data).

    .. _`section 3.5.2`: http://tools.ietf.org/html/rfc5849#section-3.5.2

    """
    return urlencode(params)
Example #16
0
def add_kts_mobile_flavor(client, kaltura_id):
    payload = kts_mobile_flavor_payload.copy()
    payload['ks'] = client.getKs()
    settings = properties.load_kaltura_settings().get(kaltura_id)
    url = "%s/api_v3/" % settings['SERVICE_URL']
    data = urlencode(payload)
    req = urllib2.Request(url, data)
    response = urllib2.urlopen(req).read()
    contentxml = ET.fromstring(response)
    flavor_id = contentxml.find('./result/id').text
    return flavor_id
Example #17
0
def add_kts_mobile_flavor(client, kaltura_id):
    payload = kts_mobile_flavor_payload.copy()
    payload['ks'] = client.getKs()
    settings = properties.load_kaltura_settings().get(kaltura_id)
    url = "%s/api_v3/" % settings['SERVICE_URL']
    data = urlencode(payload)
    req = urllib2.Request(url, data)
    response = urllib2.urlopen(req).read()
    contentxml = ET.fromstring(response)
    flavor_id = contentxml.find('./result/id').text
    return flavor_id
Example #18
0
 def dashboard(self, start=0, num=20, type=None, likes=0):
     """
     Dashboard reading.
     Arguments:
     - `start`: The post offset to start from. The default is 0.
     - `num`: The number of posts to return. The default is 20, and the maximum is 50.
     - `type`: The type of posts to return. If unspecified or empty, all types of posts are returned. Must be one of text, quote, photo, link, chat, video, or audio.
     - `likes`: (optional) 1 or 0, default 0. If 1, liked posts will have the liked="true" attribute.
     """
     url = "http://www.tumblr.com/api/dashboard/json"
     query = dict(email=self._email, password=self._password, start=start, num=num, likes=likes, type=type)
     return ApiRead.parse(self._read_json_data(url, utils.urlencode(query)))
Example #19
0
    def getServeUrl(self):
        if len(self.callsQueue) != 1:
            return None

        (url, params, _) = self.getRequestParams()

        # reset state
        self.callsQueue = []
        self.multiRequest = False

        result = '%s&%s' % (url, urlencode(params.get()))
        self.log("Returned url [%s]" % result)
        return result        
Example #20
0
    def getServeUrl(self):
        if len(self.callsQueue) != 1:
            return None

        (url, params, _) = self.getRequestParams()

        # reset state
        self.callsQueue = []
        self.multiRequest = False

        result = '%s&%s' % (url, urlencode(params.get()))
        self.log("Returned url [%s]" % result)
        return result
Example #21
0
def __request(method, url, params={}, headers={}, auth=None, callback=None):

    # Encode URL
    url_parts = url.split("\\?")
    url = url_parts[0].replace(" ", "%20")
    if len(url_parts) == 2:
        url += "?" + url_parts[1]

    # Lowercase header keys
    headers = dict((k.lower(), v) for k, v in headers.iteritems())
    headers["user-agent"] = USER_AGENT

    data, post_headers = utils.urlencode(params)
    if post_headers is not None:
        headers = dict(headers.items() + post_headers.items())

    headers['Accept-encoding'] = 'gzip'

    if auth is not None:
        if len(auth) == 2:
            user = auth[0]
            password = auth[1]
            encoded_string = base64.b64encode(user + ':' + password)
            headers['Authorization'] = "Basic " + encoded_string

    headers = dict(headers.items() + _defaultheaders.items())

    _unirestResponse = None
    if _httplib == "urlfetch":
        res = urlfetch.fetch(url,
                             payload=data,
                             headers=headers,
                             method=method,
                             deadline=_timeout)
        _unirestResponse = UnirestResponse(res.status_code, res.headers,
                                           res.content)
    else:
        req = urllib2.Request(url, data, headers)
        req.get_method = lambda: method

        try:
            response = urllib2.urlopen(req, timeout=_timeout)
            _unirestResponse = UnirestResponse(response.code, response.headers,
                                               response.read())
        except urllib2.HTTPError, e:
            response = e
            _unirestResponse = UnirestResponse(response.code, response.headers,
                                               response.read())
        except urllib2.URLError, e:
            _unirestResponse = UnirestResponse(0, {}, str(e.reason))
    def check_authorization_complete(self,
                                     sleep_duration_seconds=5,
                                     max_attempts=10):
        '''
        Polls until completion of an authorization request.
        '''

        if not self._authorization_started:
            print('Start an authorization request.')
            return

        headers = {'Content-Type': 'application/x-www-form-urlencoded'}
        payload = {
            'client_id': self.client_id,
            'client_secret': self.client_secret,
            'device_code': self._device_code,
            'grant_type': 'urn:ietf:params:oauth:grant-type:device_code'
        }
        encoded = urlencode(payload)

        current_attempt = 0
        while not self.authorized and current_attempt < max_attempts:
            current_attempt = current_attempt + 1
            r = requests.request('POST',
                                 self._token_endpoint,
                                 data=encoded,
                                 headers=headers)
            j = r.json()
            r.close()
            if 'error' in j:
                if j['error'] == 'authorization_pending':
                    print('Pending authorization. ')
                    time.sleep(sleep_duration_seconds)
                elif j['error'] == 'access_denied':
                    print('Access denied')
                    raise RuntimeError(j['error'])
            else:
                self._access_token = j['access_token']
                self._token_acquired_at = int(time.time())
                self._token_expires_in = j['expires_in']
                self._token_scope = j['scope']
                self._token_type = j['token_type']
                self._refresh_token = j['refresh_token']
                print('Completed authorization')
                self._authorization_completed = True
                saved = self.save()
                if not saved:
                    print('Unable to save auth state.')
Example #23
0
def add_flavor_to_default_conversion_profile(client, flavor_id, kaltura_id):
    def_cp = client.conversionProfile.getDefault()
    settings = properties.load_kaltura_settings().get(kaltura_id)
    cp_id = def_cp.id
    cp_flavorParamsIds = def_cp.flavorParamsIds + u',{}'.format(flavor_id)
    url = "{}/api_v3/?service=conversionProfile&action=update".format(
        settings['SERVICE_URL'])
    payload = {}
    payload['ks'] = client.getKs()
    payload['id'] = cp_id
    payload['conversionProfile:flavorParamsIds'] = cp_flavorParamsIds
    data = urlencode(payload)
    req = urllib2.Request(url, data)
    response = urllib2.urlopen(req).read()
    if "<error>" in response:
        raise Exception(response)
Example #24
0
    def _formulate_get(self, path, params=None):
        """Return URL and headers for a GET request.

        This is similar to _formulate_change, except parameters are encoded
        into the URL.

        :param path: Path to the object to issue a GET on.
        :param params: Optional dict of parameter values.
        :return: A tuple: URL and headers for the request.
        """
        url = self._make_url(path)
        if params is not None and len(params) > 0:
            url += "?" + urlencode(params.items())
        headers = {}
        self.auth.sign_request(url, headers)
        return url, headers
Example #25
0
    def get_sitting_urls(assembly_id, div_id, sessionurl):
        root = get.webpage(get.htmltree(sessionurl))
        js_calls = [parse_js_call(j) for j in root.xpath('.//a/@href')]

        params = match_name_codes(js_calls, filter='mainsearch2', type='sessions')
        nsittings = len(params)
        params['j'] = str(nsittings)

        urls = []
        for i in range(nsittings):
            params['SES_NUM'] = params['SES_NUM%s' % i]
            url = '%s&%s' % (sessionurl, urlencode(params))
            # TODO: generalize me
            url = url.replace('con_search2', 'con_search3')
            urls.append({'session_name': params['SES_NUM'], 'url': url})
        return urls
Example #26
0
def add_flavor_to_default_conversion_profile(client, flavor_id, kaltura_id):
    def_cp = client.conversionProfile.getDefault()
    settings = properties.load_kaltura_settings().get(kaltura_id)
    cp_id = def_cp.id
    cp_flavorParamsIds = def_cp.flavorParamsIds + u',{}'.format(flavor_id)
    url = "{}/api_v3/?service=conversionProfile&action=update".format(
        settings['SERVICE_URL'])
    payload = {}
    payload['ks'] = client.getKs()
    payload['id'] = cp_id
    payload['conversionProfile:flavorParamsIds'] = cp_flavorParamsIds
    data = urlencode(payload)
    req = urllib2.Request(url, data)
    response = urllib2.urlopen(req).read()
    if "<error>" in response:
        raise Exception(response)
Example #27
0
    def build_absolute_path(self, path=None, repo=None, params=None):
        """Builds an absolute path, including the path prefix if required.

        Django's HttpRequest objects have a similar function, but we implement
        our own so that we can handle path prefixes correctly when they're in
        use.

        Args:
            path (str, optional): A path beginning with a slash (may include a
                query string), e.g., '/abc?x=y'. If the path argument is not
                specified or is None, the current request's path will be used.
            repo (str, optional): A repo ID. If specified, the path will be
                considered relative to the repo's route. If this is specified,
                path must also be specified.
            params (list, optional): A list of tuples of query param keys and
                values to add to the path.

        Returns:
            str: An absolute path, including the sitewide OPTIONAL_PATH_PREFIX
            if it was used with the original request (e.g.,
            '/personfinder/abc?x=y'). Does not preserve query parameters from
            the original request.
        """
        if path is None:
            assert not repo
            # request.path will already include the path prefix if it's being
            # used.
            return self.request.path
        assert path[0] == '/'
        if repo:
            path = '/%s%s' % (repo, path)
        if self._request_is_for_prefixed_path():
            res = '/%s%s' % (site_settings.OPTIONAL_PATH_PREFIX, path)
        else:
            res = path
        if params:
            url_parts = list(urlparse.urlparse(res))
            url_params = dict(urlparse.parse_qsl(url_parts[4]))
            for key, value in params:
                if value is None:
                    if key in url_params:
                        del (url_params[key])
                else:
                    url_params[key] = value
            url_parts[4] = utils.urlencode(url_params)
            res = urlparse.urlunparse(url_parts)
        return res
Example #28
0
    def build_absolute_path(self, path=None, repo=None, params=None):
        """Builds an absolute path, including the path prefix if required.

        Django's HttpRequest objects have a similar function, but we implement
        our own so that we can handle path prefixes correctly when they're in
        use.

        Args:
            path (str, optional): A path beginning with a slash (may include a
                query string), e.g., '/abc?x=y'. If the path argument is not
                specified or is None, the current request's path will be used.
            repo (str, optional): A repo ID. If specified, the path will be
                considered relative to the repo's route. If this is specified,
                path must also be specified.
            params (list, optional): A list of tuples of query param keys and
                values to add to the path.

        Returns:
            str: An absolute path, including the sitewide OPTIONAL_PATH_PREFIX
            if it was used with the original request (e.g.,
            '/personfinder/abc?x=y'). Does not preserve query parameters from
            the original request.
        """
        if path is None:
            assert not repo
            # request.path will already include the path prefix if it's being
            # used.
            return self.request.path
        assert path[0] == '/'
        if repo:
            path = '/%s%s' % (repo, path)
        if self._request_is_for_prefixed_path():
            res = '/%s%s' % (site_settings.OPTIONAL_PATH_PREFIX, path)
        else:
            res = path
        if params:
            url_parts = list(urlparse.urlparse(res))
            url_params = dict(urlparse.parse_qsl(url_parts[4]))
            for key, value in params:
                if value is None:
                    if key in url_params:
                        del(url_params[key])
                else:
                    url_params[key] = value
            url_parts[4] = utils.urlencode(url_params)
            res = urlparse.urlunparse(url_parts)
        return res
    def token(self, force_refresh=False):
        '''
        Fetches a valid access token.
        '''

        if not self._authorization_completed:
            print('Complete an authorization request')
            return

        buffer = 10 * 60 * -1  # 10 min in seconds
        now = int(time.time())
        is_valid = now < (self._token_acquired_at + self._token_expires_in +
                          buffer)
        if not is_valid or force_refresh:
            print('Token expired. Refreshing access tokens.')
            headers = {'Content-Type': 'application/x-www-form-urlencoded'}
            payload = {
                'client_id': self.client_id,
                'client_secret': self.client_secret,
                'refresh_token': self._refresh_token,
                'grant_type': 'refresh_token'
            }
            encoded = urlencode(payload)
            r = requests.request('POST',
                                 self._token_endpoint,
                                 data=encoded,
                                 headers=headers)
            status_code = r.status_code
            j = r.json()
            r.close()

            if status_code == 400:
                print('Unable to refresh tokens.')
                raise (RuntimeError('Unable to refresh tokens.'))

            print('Updated access tokens.')
            self._access_token = j['access_token']
            self._token_acquired_at = int(time.time())
            self._token_expires_in = j['expires_in']
            self._token_scope = j['scope']
            self._token_type = j['token_type']
            saved = self.save()
            if not saved:
                print('Unable to store auth state.')

        return self._access_token
Example #30
0
    def prepare_request(self, method, uri, params=None, headers=None, data=None, json=None):
        params = {} if params is None else params
        if not isinstance(params, dict):
            raise TypeError('params should be dict')

        method = method.upper()
        params = encoded_dict(params)
        logger.debug(uri)
        url = '/'.join([self.api_server.strip(), self.endpoint.strip().strip('/'),
                        self.version.strip().strip('/')]) + uri.strip()
        logger.debug(url)
        url_parsed = urlparse(url)
        enc_params = urlencode(params)
        logger.debug(enc_params)
        if url_parsed.query is None or url_parsed.query == '':
            query = enc_params
        else:
            query = '%s&%s' % (url_parsed.query, enc_params)

        real_uri = urlunparse(('', '', url_parsed.path, url_parsed.params,
                               query, url_parsed.fragment))

        real_url = urlunparse((url_parsed.scheme, url_parsed.netloc, url_parsed.path,
                               url_parsed.params,
                               query, url_parsed.fragment))

        self.request_data.host = url_parsed.netloc
        self.request_data.uri = real_uri
        self.request_data.method = method
        self.request_data.headers = {
            'Accept': 'application/json; charset=utf-8'
        }
        if headers is not None:
            # headers 是字典
            self.request_data.headers.update(headers)

        if method == 'GET':
            self.request_data.body = ''
        else:
            if json is not None:
                self.request_data.headers['Content-Type'] = 'application/json; charset=utf-8'
                self.request_data.body = json_util.dumps(json, ensure_ascii=False)
            else:
                self.request_data.body = data

        return real_url
Example #31
0
def __request(method, url, params={}, headers={}, auth=None, callback=None):

    # Encode URL
    url_parts = url.split("\\?")
    url = url_parts[0].replace(" ", "%20")
    if len(url_parts) == 2:
        url += "?" + url_parts[1]

    # Lowercase header keys
    headers = dict((k.lower(), v) for k, v in headers.iteritems())
    headers["user-agent"] = USER_AGENT

    data, post_headers = utils.urlencode(params)
    if post_headers is not None:
        headers = dict(headers.items() + post_headers.items())

    headers['Accept-encoding'] = 'gzip'

    if auth is not None:
        if len(auth) == 2:
            user = auth[0]
            password = auth[1]
            encoded_string = base64.b64encode(user + ':' + password)
            headers['Authorization'] = "Basic " + encoded_string

    headers = dict(headers.items() + _defaultheaders.items())

    _unirestResponse = None
    if _httplib == "urlfetch":
        res = urlfetch.fetch(url, payload=data, headers=headers, method=method, deadline=_timeout)
        _unirestResponse = UnirestResponse(res.status_code,
                                           res.headers,
                                           res.content)
    else:
        req = urllib2.Request(url, data, headers)
        req.get_method = lambda: method
        try:
            response = urllib2.urlopen(req, timeout=_timeout)
        except urllib2.HTTPError, e:
            response = e

        _unirestResponse = UnirestResponse(response.code,
                                           response.url,
                                           response.headers,
                                           response.read())
Example #32
0
 def execute_request(self, path, method = 'GET', **params):
     params = urlencode(params)
     if method in ['GET', 'DELETE']:
         req_url = "?".join([path, params])
         body = None
     else:
         req_url = path
         body = params.encode("utf8")
     req_url = "/".join([self.api_url, req_url])
     if self.token:
         self.headers['Authorization'] = "bearer:%s" % self.token
     func = getattr(requests, method.lower())
     response = func(req_url, data = body, headers = self.headers, verify = False)
     status = {}
     status['status'] = response.status_code
     content = response.text
     data = loads(content)
     return status, data
Example #33
0
    def get_sitting_urls(assembly_id, div_id, sessionurl):
        root = get.webpage(get.htmltree(sessionurl))
        js_calls = [parse_js_call(j) for j in root.xpath('.//a/@href')]

        params = match_name_codes(js_calls,
                                  filter='mainsearch2',
                                  type='sessions')
        nsittings = len(params)
        params['j'] = str(nsittings)

        urls = []
        for i in range(nsittings):
            params['SES_NUM'] = params['SES_NUM%s' % i]
            url = '%s&%s' % (sessionurl, urlencode(params))
            # TODO: generalize me
            url = url.replace('con_search2', 'con_search3')
            urls.append({'session_name': params['SES_NUM'], 'url': url})
        return urls
Example #34
0
 def do_find(self):
     req = requests.post("https://libertyvf.tv/v2/recherche/",
                         "categorie=films&mot_search=" +
                         utils.urlencode(self.title),
                         headers=LiberttVfFinder.HEAD)
     dom = etree.HTML(req.content)
     out = []
     for elem in (dom.xpath('//div[@class="bloc-generale"]')):
         if elem.findall('h2'):
             f = Film("libertyvf.tv")
             f.title = elem.findall('h2/a')[0].text[13:]
             f.url = elem.findall('h2/a')[0].get("href")
             f.year = int(
                 elem.xpath(
                     ".//a[contains(@href, 'https://libertyvf.tv/films/annee/')]"
                 )[0].text[8:])
             out.append(f)
     return out
Example #35
0
def prepare_request_uri_query(params, url):
    """Prepare the Request URI Query.

    Per `section 3.5.3`_ of the spec.

    params: OAuth parameters and data (i.e. POST data).
    url: The request url. Query components will be removed.

    .. _`section 3.5.3`: http://tools.ietf.org/html/rfc5849#section-3.5.3

    """
    # convert dict to list of tuples
    if isinstance(params, dict):
        params = params.items()

    # append OAuth params to the existing set of query components
    sch, net, path, par, query, fra = urlparse(url)
    queryparams = parse_qsl(query, True)
    queryparams.extend(params)
    queryparams.sort(key=lambda i: i[0].startswith("oauth_"))
    query = urlencode(queryparams)
    return urlunparse((sch, net, path, par, query, fra))
Example #36
0
def get_session_urls(assembly_id, div_id, listurl):
    def searchform(root, num=''):
        return root.xpath('.//form[@name="searchform%s"]/@action' % num)[0]

    root = get.webpage(get.htmltree(listurl))
    js_calls = [parse_js_call(j) for j in root.xpath('.//a/@href')]

    params = match_name_codes(js_calls, filter='mainsearch', type='committees')
    nsessions = len(params)/2
    params['i'] = str(nsessions)
    params['div'] = str(div_id)
    params['DAE_NUM'] = str(assembly_id)

    urls = []
    for i in range(nsessions):
        params['COMM_NAME'] = params['COMM_NAME%s' % i]
        params['COMM_CODE'] = params['COMM_CODE%s' % i]
        urls.append(\
            {'committee': params['COMM_NAME'],
             'url': '%s/content/%s?%s' %\
                    (BASEURL, searchform(root)[:-2], urlencode(params))})
    return urls
Example #37
0
def get_session_urls(assembly_id, div_id, listurl):
    def searchform(root, num=''):
        return root.xpath('.//form[@name="searchform%s"]/@action' % num)[0]

    root = get.webpage(get.htmltree(listurl))
    js_calls = [parse_js_call(j) for j in root.xpath('.//a/@href')]

    params = match_name_codes(js_calls, filter='mainsearch', type='committees')
    nsessions = len(params) / 2
    params['i'] = str(nsessions)
    params['div'] = str(div_id)
    params['DAE_NUM'] = str(assembly_id)

    urls = []
    for i in range(nsessions):
        params['COMM_NAME'] = params['COMM_NAME%s' % i]
        params['COMM_CODE'] = params['COMM_CODE%s' % i]
        urls.append(\
            {'committee': params['COMM_NAME'],
             'url': '%s/content/%s?%s' %\
                    (BASEURL, searchform(root)[:-2], urlencode(params))})
    return urls
Example #38
0
 def get(self):
     self.require_user()
     nickname = self.request.get('nickname', '')
     next = self.request.get('next', '')
     duration = utils.describe_delta(
         datetime.timedelta(0, int(self.request.get('duration', '0'))))
     if not nickname:
         self.render('templates/register.html', next=next, duration=duration,
                     nickname=self.user.nickname().split('@')[0])
     else:
         # Then proceed to the OAuth authorization page.
         parameters = {
             'scope': latitude.LatitudeOAuthClient.SCOPE,
             'domain': model.Config.get('oauth_consumer_key'),
             'granularity': 'best',
             'location': 'current'
         }
         callback_url = self.request.host_url + '/_oauth_callback?' + \
             utils.urlencode(nickname=nickname, next=next)
         oauth_webapp.redirect_to_authorization_page(
             self, latitude.LatitudeOAuthClient(utils.oauth_consumer),
             callback_url, parameters)
Example #39
0
 def reblog(self, post_id, reblog_key, comment=None, reblog_as=None, group=None):
     """
     Reblogging post.
     Arguments:
     - `post_id`: The integer ID of the post to reblog.
     - `reblog_key`: The corresponding reblog_key value from the post's read data.
     - `comment`: (optional) Text, HTML, or Markdown string (see format) of the commentary added to the reblog.
     - `reblog_as`: (optional) Reblog as a different format from the original post.
     - `group`: (optional) Post this to a secondary blog on your account.
     """
     if group is not None:
         group = group if "." in group else "%s.tumblr.com" % group
     url = "http://www.tumblr.com/api/reblog"
     query = {
         "email": self._email,
         "password": self._password,
         "post-id": post_id,
         "reblog-key": reblog_key,
         "group": group,
         "comment": comment,
         "as": reblog_as,
     }
     self._check_status_code(url, utils.urlencode(query))
Example #40
0
    def _formulate_change(self, path, params, as_json=False):
        """Return URL, headers, and body for a non-GET request.

        This is similar to _formulate_get, except parameters are encoded as
        a multipart form body.

        :param path: Path to the object to issue a GET on.
        :param params: A dict of parameter values.
        :param as_json: Encode params as application/json instead of
            multipart/form-data. Only use this if you know the API already
            supports JSON requests.
        :return: A tuple: URL, headers, and body for the request.
        """
        url = self._make_url(path)
        if 'op' in params:
            params = dict(params)
            op = params.pop('op')
            url += '?' + urlencode([('op', op)])
        if as_json:
            body, headers = encode_json_data(params)
        else:
            body, headers = encode_multipart_data(params, {})
        self.auth.sign_request(url, headers)
        return url, headers, body
Example #41
0
    def do_find(self):
        req = requests.post(
            "https://www.zone-warez.com/index.php?do=search",
            "do=search&subaction=search&search_start=0&full_search=1&result_from=1&story="
            + utils.urlencode(self.title) +
            "&titleonly=0&searchuser=&replyless=0&replylimit=0&searchdate=0&beforeafter=after&sortby=date&resorder=desc&showposts=1&catlist%5B%5D=2&user_hash=636b4560bb73a51b506836d8293f5770506a68d0",
            headers=ZoneTelechargementFinder.HEAD)
        print(req.content)
        dom = etree.HTML(req.content)
        out = []
        for elem in (dom.xpath('//div[@class="mov"]')):
            try:
                f = Film("https://www.zone-warez.com/")
                f.year = int(elem.findall('.//div[@class="mov-m"]/a')[0].text)
                f.langue = elem.findall(
                    './/span[@class="langue"]/b')[0].text.strip()
                f.title = elem.findall('a[@class="mov-t nowrap"]')[0].get(
                    "title")
                f.url = elem.findall('a[@class="mov-t nowrap"]')[0].get("href")
                out.append(f)
            except:
                pass

        return out
Example #42
0
def get_url(**kwargs):
    return '{0}?{1}'.format(_url, utils.urlencode(kwargs))
    def getcast(self):
        """helper to get all cast for a given media item"""
        db_id = None
        all_cast = []
        all_cast_names = list()
        cache_str = ""
        download_thumbs = self.params.get("downloadthumbs", "") == "true"
        extended_cast_action = self.params.get("castaction", "") == "extendedinfo"
        tmdb = Tmdb()
        movie = self.params.get("movie")
        tvshow = self.params.get("tvshow")
        episode = self.params.get("episode")
        movieset = self.params.get("movieset")

        try:  # try to parse db_id
            if movieset:
                cache_str = "movieset.castcache-%s-%s" % (self.params["movieset"], download_thumbs)
                db_id = int(movieset)
            elif tvshow:
                cache_str = "tvshow.castcache-%s-%s" % (self.params["tvshow"], download_thumbs)
                db_id = int(tvshow)
            elif movie:
                cache_str = "movie.castcache-%s-%s" % (self.params["movie"], download_thumbs)
                db_id = int(movie)
            elif episode:
                cache_str = "episode.castcache-%s-%s" % (self.params["episode"], download_thumbs)
                db_id = int(episode)
        except Exception:
            pass

        cachedata = self.cache.get(cache_str)
        if cachedata:
            # get data from cache
            all_cast = cachedata
        else:
            # retrieve data from json api...
            if movie and db_id:
                all_cast = self.kodi_db.movie(db_id)["cast"]
            elif movie and not db_id:
                filters = [{"operator": "is", "field": "title", "value": movie}]
                result = self.kodi_db.movies(filters=filters)
                all_cast = result[0]["cast"] if result else []
            elif tvshow and db_id:
                all_cast = self.kodi_db.tvshow(db_id)["cast"]
            elif tvshow and not db_id:
                filters = [{"operator": "is", "field": "title", "value": tvshow}]
                result = self.kodi_db.tvshows(filters=filters)
                all_cast = result[0]["cast"] if result else []
            elif episode and db_id:
                all_cast = self.kodi_db.episode(db_id)["cast"]
            elif episode and not db_id:
                filters = [{"operator": "is", "field": "title", "value": episode}]
                result = self.kodi_db.episodes(filters=filters)
                all_cast = result[0]["cast"] if result else []
            elif movieset:
                if not db_id:
                    for item in self.kodi_db.moviesets():
                        if item["title"].lower() == movieset.lower():
                            db_id = item["setid"]
                if db_id:
                    json_result = self.kodi_db.movieset(db_id, include_set_movies_fields=["cast"])
                    if "movies" in json_result:
                        for movie in json_result["movies"]:
                            all_cast += movie["cast"]

            # optional: download missing actor thumbs
            if all_cast and download_thumbs:
                for cast in all_cast:
                    if cast.get("thumbnail"):
                        cast["thumbnail"] = get_clean_image(cast.get("thumbnail"))
                    if not cast.get("thumbnail"):
                        artwork = tmdb.get_actor(cast["name"])
                        cast["thumbnail"] = artwork.get("thumb", "")
            # lookup tmdb if item is requested that is not in local db
            if not all_cast:
                tmdbdetails = {}
                if movie and not db_id:
                    tmdbdetails = tmdb.search_movie(movie)
                elif tvshow and not db_id:
                    tmdbdetails = tmdb.search_tvshow(tvshow)
                if tmdbdetails.get("cast"):
                    all_cast = tmdbdetails["cast"]
            # save to cache
            self.cache.set(cache_str, all_cast)

        # process listing with the results...
        for cast in all_cast:
            if cast.get("name") not in all_cast_names:
                liz = xbmcgui.ListItem(label=cast.get("name"), label2=cast.get("role"), iconImage=cast.get("thumbnail"))
                if extended_cast_action:
                    url = "RunScript(script.extendedinfo,info=extendedactorinfo,name=%s)" % cast.get("name")
                    url = "plugin://script.skin.helper.service/?action=launch&path=%s" % url
                    is_folder = False
                else:
                    url = "RunScript(script.skin.helper.service,action=getcastmedia,name=%s)" % cast.get("name")
                    url = "plugin://script.skin.helper.service/?action=launch&path=%s" % urlencode(url)
                    is_folder = False
                all_cast_names.append(cast.get("name"))
                liz.setThumbnailImage(cast.get("thumbnail"))
                xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]), url=url, listitem=liz, isFolder=is_folder)
        xbmcplugin.endOfDirectory(int(sys.argv[1]))
Example #44
0
    def update_backgrounds(self):
        '''update all our provided backgrounds'''

        # conditional background
        self.win.setProperty("SkinHelper.ConditionalBackground",
                             get_cond_background())

        # movies backgrounds
        if xbmc.getCondVisibility("Library.HasContent(movies)"):
            # random/all movies
            self.set_background("SkinHelper.AllMoviesBackground",
                                "videodb://movies/titles/",
                                label=32010)
            # in progress movies
            self.set_background(
                "SkinHelper.InProgressMoviesBackground",
                "videodb://movies/titles/?xsp=%s" % urlencode(
                    '{"limit":50,"order":{"direction":"ascending","method":"random"},'
                    '"rules":{"and":[{"field":"inprogress","operator":"true","value":[]}]},"type":"movies"}'
                ),
                label=32012)
            # recent movies
            self.set_background("SkinHelper.RecentMoviesBackground",
                                "videodb://recentlyaddedmovies/",
                                label=32011)
            # unwatched movies
            self.set_background(
                "SkinHelper.UnwatchedMoviesBackground",
                "videodb://movies/titles/?xsp=%s" % urlencode(
                    '{"limit":50,"order":{"direction":"ascending","method":"random"},'
                    '"rules":{"and":[{"field":"playcount","operator":"is","value":0}]},"type":"movies"}'
                ),
                label=32013)

        # tvshows backgrounds
        if xbmc.getCondVisibility("Library.HasContent(tvshows)"):
            # random/all tvshows
            self.set_background("SkinHelper.AllTvShowsBackground",
                                "videodb://tvshows/titles/",
                                label=32014)
            # in progress tv shows
            self.set_background(
                "SkinHelper.InProgressShowsBackground",
                "videodb://tvshows/titles/?xsp=%s" % urlencode(
                    '{"limit":50,"order":{"direction":"ascending","method":"random"},'
                    '"rules":{"and":[{"field":"inprogress","operator":"true","value":[]}]},"type":"tvshows"}'
                ),
                label=32016)
            # recent episodes
            self.set_background("SkinHelper.RecentEpisodesBackground",
                                "videodb://recentlyaddedepisodes/",
                                label=32015)

        # all musicvideos
        if xbmc.getCondVisibility("Library.HasContent(musicvideos)"):
            self.set_background("SkinHelper.AllMusicVideosBackground",
                                "videodb://musicvideos/titles",
                                label=32018)

        # all music
        if xbmc.getCondVisibility("Library.HasContent(music)"):
            # music artists
            self.set_background("SkinHelper.AllMusicBackground",
                                "musicdb://artists/",
                                label=32019)
            # recent albums
            self.set_background("SkinHelper.RecentMusicBackground",
                                "musicdb://recentlyaddedalbums/",
                                label=32023)
            # random songs
            self.set_background("SkinHelper.AllMusicSongsBackground",
                                "musicdb://songs/",
                                label=32022)

        # tmdb backgrounds (extendedinfo)
        if xbmc.getCondVisibility("System.HasAddon(script.extendedinfo)"):
            self.set_background(
                "SkinHelper.TopRatedMovies",
                "plugin://script.extendedinfo/?info=topratedmovies",
                label=32020)
            self.set_background(
                "SkinHelper.TopRatedShows",
                "plugin://script.extendedinfo/?info=topratedtvshows",
                label=32021)

        # pictures background
        self.set_background("SkinHelper.PicturesBackground",
                            "pictures",
                            label=32017)

        # pvr background
        if xbmc.getCondVisibility("PVR.HasTvChannels"):
            self.set_background("SkinHelper.PvrBackground", "pvr", label=32024)

        # smartshortcuts backgrounds
        for node in self.smartshortcuts.get_smartshortcuts_nodes():
            self.set_background(node[0], node[1], label=node[2])

        # global backgrounds
        self.set_global_background("SkinHelper.GlobalFanartBackground", [
            "SkinHelper.AllMoviesBackground",
            "SkinHelper.AllTvShowsBackground",
            "SkinHelper.AllMusicVideosBackground",
            "SkinHelper.AllMusicBackground"
        ],
                                   label=32009)
        self.set_global_background("SkinHelper.AllVideosBackground", [
            "SkinHelper.AllMoviesBackground",
            "SkinHelper.AllTvShowsBackground",
            "SkinHelper.AllMusicVideosBackground"
        ],
                                   label=32025)
        self.set_global_background("SkinHelper.AllVideosBackground2", [
            "SkinHelper.AllMoviesBackground", "SkinHelper.AllTvShowsBackground"
        ],
                                   label=32026)
        self.set_global_background("SkinHelper.RecentVideosBackground", [
            "SkinHelper.RecentMoviesBackground",
            "SkinHelper.RecentEpisodesBackground"
        ],
                                   label=32027)
        self.set_global_background("SkinHelper.InProgressVideosBackground", [
            "SkinHelper.InProgressMoviesBackground",
            "SkinHelper.InProgressShowsBackground"
        ],
                                   label=32028)
Example #45
0
def __request(method, url, params={}, headers={}, auth=None, callback=None):

    # Encode URL
    url_parts = url.split("\\?")
    url = url_parts[0].replace(" ", "%20")
    if len(url_parts) == 2:
        url += "?" + url_parts[1]

    # Lowercase header keys
    headers = dict((k.lower(), v) for k, v in headers.iteritems())
    headers["user-agent"] = USER_AGENT

    data, post_headers = utils.urlencode(params)
    if post_headers is not None:
        headers = dict(headers.items() + post_headers.items())

    headers['Accept-encoding'] = 'gzip'

    if auth is not None:
        if len(auth) == 2:
            user = auth[0]
            password = auth[1]
            encoded_string = base64.b64encode(user + ':' + password)
            headers['Authorization'] = "Basic " + encoded_string

    headers = dict(headers.items() + _defaultheaders.items())

    _unirestResponse = None
    if _httplib == "urlfetch":
        res = urlfetch.fetch(url, payload=data, headers=headers, method=method, deadline=_timeout)
        _unirestResponse = UnirestResponse(res.status_code,
                                           res.headers,
                                           res.content)
    else:
        # Retry if there's a connection failure.
        response = None
        error = None
        for i in range(0, 3):
            req = urllib2.Request(url, data, headers)
            req.get_method = lambda: method
            try:
                # context = ssl._create_unverified_context()
                response = urllib2.urlopen(req, timeout=_timeout)
            except urllib2.HTTPError as e:
                response = e
            except urllib2.URLError as e:
                error = e
                continue
            except socket.error as e:
                error = e
                continue
            except Exception as e:
                error = e
                break

            break

        if response is not None:
            _unirestResponse = UnirestResponse(response.code,
                                           response.headers,
                                           response.read())
        else:
            _unirestResponse = error

    if callback is None or callback == {}:
        return _unirestResponse
    else:
        callback(_unirestResponse)
Example #46
0
 def execute_initiate(self, request, **kwargs):
     return self.services[kwargs["service"]].oauth_initiate(config.url + "/authorization/" + kwargs["service"] + "/callback/?from=" + urlencode(request.referrer or config.url))
Example #47
0
def add_query_param(request, **params):
    return request.path + urlencode(_add_query_param(request.query, **params))
Example #48
0
def remove_query_param(request, *params):
    return request.path + urlencode(_remove_query_param(
        request.query, *params))
    def getcast(self):
        '''helper to get all cast for a given media item'''
        db_id = None
        all_cast = []
        all_cast_names = list()
        cache_str = ""
        download_thumbs = self.params.get("downloadthumbs", "") == "true"
        extended_cast_action = self.params.get("castaction",
                                               "") == "extendedinfo"
        movie = self.params.get("movie")
        tvshow = self.params.get("tvshow")
        episode = self.params.get("episode")
        movieset = self.params.get("movieset")

        try:  # try to parse db_id
            if movieset:
                cache_str = "movieset.castcache-%s-%s" % (
                    self.params["movieset"], download_thumbs)
                db_id = int(movieset)
            elif tvshow:
                cache_str = "tvshow.castcache-%s-%s" % (self.params["tvshow"],
                                                        download_thumbs)
                db_id = int(tvshow)
            elif movie:
                cache_str = "movie.castcache-%s-%s" % (self.params["movie"],
                                                       download_thumbs)
                db_id = int(movie)
            elif episode:
                cache_str = "episode.castcache-%s-%s" % (
                    self.params["episode"], download_thumbs)
                db_id = int(episode)
        except Exception:
            pass

        cachedata = self.cache.get(cache_str)
        if cachedata:
            # get data from cache
            all_cast = cachedata
        else:
            # retrieve data from json api...
            if movie and db_id:
                all_cast = self.mutils.kodidb.movie(db_id)["cast"]
            elif movie and not db_id:
                filters = [{
                    "operator": "is",
                    "field": "title",
                    "value": movie
                }]
                result = self.mutils.kodidb.movies(filters=filters)
                all_cast = result[0]["cast"] if result else []
            elif tvshow and db_id:
                all_cast = self.mutils.kodidb.tvshow(db_id)["cast"]
            elif tvshow and not db_id:
                filters = [{
                    "operator": "is",
                    "field": "title",
                    "value": tvshow
                }]
                result = self.mutils.kodidb.tvshows(filters=filters)
                all_cast = result[0]["cast"] if result else []
            elif episode and db_id:
                all_cast = self.mutils.kodidb.episode(db_id)["cast"]
            elif episode and not db_id:
                filters = [{
                    "operator": "is",
                    "field": "title",
                    "value": episode
                }]
                result = self.mutils.kodidb.episodes(filters=filters)
                all_cast = result[0]["cast"] if result else []
            elif movieset:
                if not db_id:
                    for item in self.mutils.kodidb.moviesets():
                        if item["title"].lower() == movieset.lower():
                            db_id = item["setid"]
                if db_id:
                    json_result = self.mutils.kodidb.movieset(
                        db_id, include_set_movies_fields=["cast"])
                    if "movies" in json_result:
                        for movie in json_result['movies']:
                            all_cast += movie['cast']

            # optional: download missing actor thumbs
            if all_cast and download_thumbs:
                for cast in all_cast:
                    if cast.get("thumbnail"):
                        cast["thumbnail"] = self.mutils.get_clean_image(
                            cast.get("thumbnail"))
                    if not cast.get("thumbnail"):
                        artwork = self.mutils.tmdb.get_actor(cast["name"])
                        cast["thumbnail"] = artwork.get("thumb", "")
            # lookup tmdb if item is requested that is not in local db
            if not all_cast:
                tmdbdetails = {}
                if movie and not db_id:
                    tmdbdetails = self.mutils.tmdb.search_movie(movie)
                elif tvshow and not db_id:
                    tmdbdetails = self.mutils.tmdb.search_tvshow(tvshow)
                if tmdbdetails.get("cast"):
                    all_cast = tmdbdetails["cast"]
            # save to cache
            self.cache.set(cache_str, all_cast)

        # process listing with the results...
        for cast in all_cast:
            if cast.get("name") not in all_cast_names:
                liz = xbmcgui.ListItem(label=cast.get("name"),
                                       label2=cast.get("role"),
                                       iconImage=cast.get("thumbnail"))
                if extended_cast_action:
                    url = "RunScript(script.extendedinfo,info=extendedactorinfo,name=%s)" % cast.get(
                        "name")
                    url = "plugin://script.skin.helper.service/?action=launch&path=%s" % url
                    is_folder = False
                else:
                    url = "RunScript(script.skin.helper.service,action=getcastmedia,name=%s)" % cast.get(
                        "name")
                    url = "plugin://script.skin.helper.service/?action=launch&path=%s" % urlencode(
                        url)
                    is_folder = False
                all_cast_names.append(cast.get("name"))
                liz.setThumbnailImage(cast.get("thumbnail"))
                xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),
                                            url=url,
                                            listitem=liz,
                                            isFolder=is_folder)
        xbmcplugin.endOfDirectory(int(sys.argv[1]))
Example #50
0
 def oauth_initiate(self, callback_url):
     return redirect("http://api.vk.com/oauth/authorize?client_id=" + self.client_id + "&redirect_uri=" + urlencode(callback_url) + "&display=page")
Example #51
0
    def oauth_callback(self, request):
        if request.args.get("error") is not None:
            return False

        # TODO: import vkontakte
        access_token = simplejson.loads(urllib2.urlopen(urllib2.Request("https://api.vkontakte.ru/oauth/access_token?client_id=" + self.client_id + "&client_secret=" + self.client_secret + "&code=" + urlencode(request.args.get("code")))).read())
        user = simplejson.loads(urllib2.urlopen(urllib2.Request("https://api.vkontakte.ru/method/getProfiles?uid=" + str(access_token["user_id"]) + "&access_token=" + access_token["access_token"] + "&fields=uid,first_name,last_name,nickname,screen_name,photo")).read())["response"][0]
        
        return (int(user["uid"]), dict(user, access_token=access_token["access_token"]))
Example #52
0
def get_entry(media_id, kaltura_id, client=None, width=120, height=120):
    settings = properties.load_kaltura_settings().get(kaltura_id)
    error = False
    error_message = None
    entry = None
    try:
        entry = client.media.get(media_id)
    except Exception as inst:
        error_message = str(inst)
        error = True
    entryData = {}
    if error:
        entryData['success'] = False
        entryData['message'] = error_message
    else:
        entryData['success'] = True
        typelist = {
            KalturaMediaType.VIDEO: 'VIDEO',
            KalturaMediaType.IMAGE: 'IMAGE',
            KalturaMediaType.AUDIO: 'AUDIO',
            KalturaMediaType.LIVE_STREAM_FLASH: 'LIVE_STREAM_FLASH',
            KalturaMediaType.LIVE_STREAM_WINDOWS_MEDIA:
                'LIVE_STREAM_WINDOWS_MEDIA',
            KalturaMediaType.LIVE_STREAM_REAL_MEDIA: 'LIVE_STREAM_REAL_MEDIA',
            KalturaMediaType.LIVE_STREAM_QUICKTIME: 'LIVE_STREAM_QUICKTIME'
        }
        entryData['media_type'] = typelist[entry.getMediaType().getValue()]
        # Urls/Accessors
        entryData['url'] = entry.getDataUrl()
        entryData['thumbnail_url_old'] = entry.getThumbnailUrl()
        url = "%s/api_v3/?service=thumbasset&action=list" % settings[
            'SERVICE_URL']
        data = urlencode({"filter:entryIdEqual": media_id, "action": "list"})
        req = urllib2.Request(url, data)
        content = urllib2.urlopen(req).read()
        entryData['ks'] = client.getKs()
        contentxml = ET.fromstring(content)
        thumb_id = ''
        for item in contentxml.findall('./result/objects/item'):
            tags = item.find('tags').text
            if tags and 'default_thumb' in tags:
                thumb_id = item.find('id').text
        thumbnail_url = "%s/p/%s/thumbnail/entry_id/%s" % (
            settings['SERVICE_URL'], settings['PARTNER_ID'], media_id)
        entryData['thumbnail_url'] = thumbnail_url + \
                                     "/width/%s/height/%s?%s" % (
                                     width, height, int(time.time()))
        entryData['download_url'] = entry.getDownloadUrl()
        entryData['thumb_id'] = thumb_id
        # ViewData
        entryData['plays'] = entry.getPlays()
        entryData['views'] = entry.getViews()
        entryData['rank'] = (entry.getRank(), entry.getTotalRank())
        # Video Properties
        entryData['width'] = entry.getWidth()
        entryData['height'] = entry.getHeight()
        entryData['duration'] = entry.getDuration()
        entryData['created'] = entry.getCreatedAt()
        entryData['updated'] = entry.getUpdatedAt()
        # Video Data
        entryData['name'] = entry.getName()
        entryData['description'] = entry.getDescription()
        entryData['tags'] = entry.getTags()
        # Search Text
        entryData['searchtext'] = entry.getSearchText()
        # Control
        entryData['startDate'] = entry.getStartDate()
        entryData['endDate'] = entry.getEndDate()
        # entryData['status'] = entry.getStatus()
    return entryData
Example #53
0
 def read(self, name, start=0, num=20, type=None, id=None, search=None, tagged=None):
     """
     Reading Tumblr data.
     Arguments:
     - `name`: username
     - `start`: The post offset to start from. The default is 0.
     - `num`: The number of posts to return. The default is 20, and the maximum is 50.
     - `type`: (optional) The type of posts to return. If unspecified or empty, all types of posts are returned. Must be one of text, quote, photo, link, chat, video, or audio.
     - `id`: A specific post ID to return. Use instead of start, num, or type.
     - `search`: Search for posts with this query.
     - `tagged`: Return posts with this tag in reverse-chronological order (newest first).
     """
     if id is not None:
         query = dict(id=id)
     else:
         query = dict(start=start, num=num, type=type, search=search, tagged=tagged)
     url = "http://%s/api/read/json?%s" % ((name if "." in name else "%s.tumblr.com" % name), utils.urlencode(query))
     # url = "http://%s.tumblr.com/api/read/json?%s" % (name, utils.urlencode(query))
     return ApiRead.parse(self._read_json_data(url))
Example #54
0
 def _like_unlike(self, post_id, reblog_key, like):
     url = "http://www.tumblr.com/api/%s" % ("like" if like else "unlike")
     query = {"email": self._email, "password": self._password, "post-id": post_id, "reblog-key": reblog_key}
     self._check_status_code(url, utils.urlencode(query))
Example #55
0
def get_entry(media_id, kaltura_id, client=None, width=120, height=120):
    settings = properties.load_kaltura_settings().get(kaltura_id)
    error = False
    error_message = None
    entry = None
    try:
        entry = client.media.get(media_id)
    except Exception as inst:
        error_message = str(inst)
        error = True
    entryData = {}
    if error:
        entryData['success'] = False
        entryData['message'] = error_message
    else:
        entryData['success'] = True
        typelist = {
            KalturaMediaType.VIDEO: 'VIDEO',
            KalturaMediaType.IMAGE: 'IMAGE',
            KalturaMediaType.AUDIO: 'AUDIO',
            KalturaMediaType.LIVE_STREAM_FLASH: 'LIVE_STREAM_FLASH',
            KalturaMediaType.LIVE_STREAM_WINDOWS_MEDIA:
            'LIVE_STREAM_WINDOWS_MEDIA',
            KalturaMediaType.LIVE_STREAM_REAL_MEDIA: 'LIVE_STREAM_REAL_MEDIA',
            KalturaMediaType.LIVE_STREAM_QUICKTIME: 'LIVE_STREAM_QUICKTIME'
        }
        entryData['media_type'] = typelist[entry.getMediaType().getValue()]
        # Urls/Accessors
        entryData['url'] = entry.getDataUrl()
        entryData['thumbnail_url_old'] = entry.getThumbnailUrl()
        url = "%s/api_v3/?service=thumbasset&action=list" % settings[
            'SERVICE_URL']
        data = urlencode({"filter:entryIdEqual": media_id, "action": "list"})
        req = urllib2.Request(url, data)
        content = urllib2.urlopen(req).read()
        entryData['ks'] = client.getKs()
        contentxml = ET.fromstring(content)
        thumb_id = ''
        for item in contentxml.findall('./result/objects/item'):
            tags = item.find('tags').text
            if tags and 'default_thumb' in tags:
                thumb_id = item.find('id').text
        thumbnail_url = "%s/p/%s/thumbnail/entry_id/%s" % (
            settings['SERVICE_URL'], settings['PARTNER_ID'], media_id)
        entryData['thumbnail_url'] = thumbnail_url + \
                                     "/width/%s/height/%s?%s" % (
                                     width, height, int(time.time()))
        entryData['download_url'] = entry.getDownloadUrl()
        entryData['thumb_id'] = thumb_id
        # ViewData
        entryData['plays'] = entry.getPlays()
        entryData['views'] = entry.getViews()
        entryData['rank'] = (entry.getRank(), entry.getTotalRank())
        # Video Properties
        entryData['width'] = entry.getWidth()
        entryData['height'] = entry.getHeight()
        entryData['duration'] = entry.getDuration()
        entryData['created'] = entry.getCreatedAt()
        entryData['updated'] = entry.getUpdatedAt()
        # Video Data
        entryData['name'] = entry.getName()
        entryData['description'] = entry.getDescription()
        entryData['tags'] = entry.getTags()
        # Search Text
        entryData['searchtext'] = entry.getSearchText()
        # Control
        entryData['startDate'] = entry.getStartDate()
        entryData['endDate'] = entry.getEndDate()
        # entryData['status'] = entry.getStatus()
    return entryData
Example #56
0
 def oauth_initiate(self, callback_url):
     return redirect("%(homepage)s/api/auth/?api_key=%(api)s&cb=%(callback_url)s" % {
         "homepage"      : self.network.homepage,
         "api"           : self.api_key,
         "callback_url"  : urlencode(callback_url),
     })