Exemple #1
0
    def _pack_image(filename, contentname, max_size=1024, **params):
        """Pack image from file into multipart-formdata post body"""
        # image must be less than 700kb in size
        try:
            if os.path.getsize(filename) > (max_size * 1024):
                raise QWeiboError('File is too big, must be less than 700kb.')
        except os.error:
            raise QWeiboError('Unable to access file')

        # image must be gif, jpeg, or png
        file_type = mimetypes.guess_type(filename)
        if file_type is None:
            raise QWeiboError('Could not determine file type')
        file_type = file_type[0]
        if file_type.split('/')[0] != 'image':
            raise QWeiboError('Invalid file type for image: %s' % file_type)

        # build the mulitpart-formdata body
        BOUNDARY = 'QqWeIbObYaNdElF----'  # qqweibo by andelf
        body = []
        for key, val in params.items():
            if val is not None:
                body.append('--' + BOUNDARY)
                body.append('Content-Disposition: form-data; name="%s"' % key)
                body.append('Content-Type: text/plain; charset=UTF-8')
                body.append('Content-Transfer-Encoding: 8bit')
                body.append('')
                val = convert_to_utf8_bytes(val)
                body.append(val)
        fp = open(filename, 'rb')
        body.append('--' + BOUNDARY)
        body.append(
            'Content-Disposition: form-data; name="%s"; filename="%s"' %
            (contentname, filename.encode('utf-8')))
        body.append('Content-Type: %s' % file_type)
        body.append('Content-Transfer-Encoding: binary')
        body.append('')
        body.append(fp.read())
        body.append('--%s--' % BOUNDARY)
        body.append('')
        fp.close()
        body.append('--%s--' % BOUNDARY)
        body.append('')
        # fix py3k
        for i in range(len(body)):
            body[i] = convert_to_utf8_bytes(body[i])
        body = b'\r\n'.join(body)
        # build headers
        headers = {
            'Content-Type': 'multipart/form-data; boundary=%s' % BOUNDARY,
            'Content-Length': len(body)
        }

        return headers, body
Exemple #2
0
    def _pack_image(filename, contentname, max_size=1024, **params):
        """Pack image from file into multipart-formdata post body"""
        # image must be less than 700kb in size
        try:
            if os.path.getsize(filename) > (max_size * 1024):
                raise QWeiboError('File is too big, must be less than 700kb.')
        except os.error:
            raise QWeiboError('Unable to access file')

        # image must be gif, jpeg, or png
        file_type = mimetypes.guess_type(filename)
        if file_type is None:
            raise QWeiboError('Could not determine file type')
        file_type = file_type[0]
        if file_type.split('/')[0] != 'image':
            raise QWeiboError('Invalid file type for image: %s' % file_type)

        # build the mulitpart-formdata body
        BOUNDARY = 'QqWeIbObYaNdElF----'  # qqweibo by andelf
        body = []
        for key, val in params.items():
            if val is not None:
                body.append('--' + BOUNDARY)
                body.append('Content-Disposition: form-data; name="%s"' % key)
                body.append('Content-Type: text/plain; charset=UTF-8')
                body.append('Content-Transfer-Encoding: 8bit')
                body.append('')
                val = convert_to_utf8_bytes(val)
                body.append(val)
        fp = open(filename, 'rb')
        body.append('--' + BOUNDARY)
        body.append('Content-Disposition: form-data; name="%s"; filename="%s"' % (contentname, filename.encode('utf-8')))
        body.append('Content-Type: %s' % file_type)
        body.append('Content-Transfer-Encoding: binary')
        body.append('')
        body.append(fp.read())
        body.append('--%s--' % BOUNDARY)
        body.append('')
        fp.close()
        body.append('--%s--' % BOUNDARY)
        body.append('')
        # fix py3k
        for i in range(len(body)):
            body[i] = convert_to_utf8_bytes(body[i])
        body = b'\r\n'.join(body)
        # build headers
        headers = {
            'Content-Type': 'multipart/form-data; boundary=%s' % BOUNDARY,
            'Content-Length': len(body)
        }

        return headers, body
Exemple #3
0
    def authorize_request(self, url, method, headers, parameters):
        query = dict(parameters)
        if "oauth_consumer_key" not in query:
            query["oauth_consumer_key"] = self._api_key
        if "access_token" not in query:
            query["access_token"] = self.access_token
        if "openid" not in query:
            query["openid"] = self.openid
        if "scope" not in query:
            query["scope"] = "all"
        if "clientip" not in query:
            query["clientip"] = "127.0.0.1"
        query["oauth_version"] = "2.a"

        query = query.items()
        query = [(str(k), convert_to_utf8_bytes(v)) for k, v in query]
        query.sort()
        if method == 'POST':
            return url, query
        elif method == 'GET':
            params = '&'.join(("%s=%s" % kv) for kv in query)
            if '?' in url:
                return "%s&%s" % (url, params), query
            else:
                return "%s?%s" % (url, params), query
Exemple #4
0
    def authorize_request(self, url, method, headers, parameters):
        query = dict(parameters)
        if "oauth_consumer_key" not in query:
            query["oauth_consumer_key"] = self.apiKey
        if "access_token" not in query:
            query["access_token"] = self.accessToken
        if "openid" not in query:
            query["openid"] = self.openid
        if "scope" not in query:
            query["scope"] = "all"
        if "clientip" not in query:
            query["clientip"] = "127.0.0.1"
        query["oauth_version"] = "2.a"

        query = query.items()
        query = [(str(k), convert_to_utf8_bytes(v)) for k,v in query]
        query.sort()
        if method == 'POST':
            return url, query
        elif method == 'GET':
            print query
            params = '&'.join(("%s=%s" % kv) for kv in query)
            if '?' in url:
                return "%s&%s" % (url, params), query
            else:
                return "%s?%s" % (url, params), query