예제 #1
0
    def _get(self, method, auth, **params):
        params.update(self._get_backend_params())
        if 'format' not in params:
            params['format'] = self.response_format
        
        params['api_key'] = self.api_key
        
        for key, value in params.iteritems():
            if isinstance(value, list):
                params[key] = ','.join([item for item in value])

        if method:
            params['method'] = method

        if auth and self.token:
            params['auth_token'] = self.token

        params['api_sig'] = utils.get_api_signature(self.secret_key, **params)
        
        return urllib2.urlopen(utils.REST_API_URL,
            urllib.urlencode(params)).read()
예제 #2
0
파일: base.py 프로젝트: rhymes/flickyou
    def photo_upload(self, filename=None, photo_data=None, data_content_type="image/jpeg", **params):
        """Uploading photos:
            It works outside the normal Flickr API framework because it
            involves sending binary files over the wire.
            
        You can specify either the filename or pass the binary image data.
        The image content type defaults to JPEG.
        
        See: <http://www.flickr.com/services/api/upload.api.html>
        """
        params["api_key"] = self.api_key
        params["auth_token"] = self.token
        params["api_sig"] = utils.get_api_signature(self.secret_key, **params)

        # build data to POST
        # see: http://www.flickr.com/services/api/upload.example.html
        boundary = mimetools.choose_boundary()
        request_body = []

        # required params
        for field in ("api_key", "auth_token", "api_sig"):
            request_body.append("--%s\r\n" % boundary)
            request_body.append('Content-Disposition: form-data; name="%s"\r\n\r\n' % field)
            request_body.append("%s\r\n" % params[field])

        # optional params
        for field in (
            "title",
            "description",
            "tags",
            "is_public",
            "is_friend",
            "is_family",
            "safety_level",
            "content_type",
            "hidden",
        ):
            if field in params:
                request_body.append("--%s\r\n" % boundary)
                request_body.append('Content-Disposition: form-data; name="%s"\r\n\r\n' % field)
                request_body.append("%s\r\n" % params[field])

        # photo field
        request_body.append("--%s\r\n" % boundary)
        request_body.append("Content-Disposition: form-data;" 'name="photo"; filename="%s"\r\n' % filename)
        request_body.append("Content-Type: %s\r\n\r\n" % data_content_type or "image/jpeg")

        if filename and photo_data is None:
            path = os.path.abspath(os.path.normpath(filename))
            f = open(path, "rb")
            photo_data = f.read()
            f.close()

        request_data = []
        request_data.append("".join(request_body).encode("utf-8"))
        request_data.append(photo_data)
        request_data.append(("\r\n--%s--" % boundary).encode("utf-8"))
        request_data = "".join(request_data)

        request = urllib2.Request(utils.UPLOAD_URL)
        request.add_data(request_data)
        request.add_header("Content-Type", "multipart/form-data; boundary=%s" % boundary)

        response = urllib2.urlopen(request)
        photo_data = response.read()

        def _handle_error(photo_data):
            dom = minidom.parseString(photo_data)
            rsp = dom.getElementsByTagName("rsp")[0]
            if rsp.getAttribute("stat") == "fail":
                err = rsp.getElementsByTagName("err")[0]
                code = err.getAttribute("code")
                message = err.getAttribute("msg")
                value = "%s: %s" % (code, message)
                raise BackendError(value)

        def _get_photo_id(photo_data):
            dom = minidom.parseString(photo_data)
            photo_id_tag = dom.getElementsByTagName("photoid")[0]
            return photo_id_tag.firstChild.nodeValue

        _handle_error(photo_data)
        photo_id = _get_photo_id(photo_data)
        return photo_id
예제 #3
0
파일: base.py 프로젝트: rhymes/flickyou
 def _get_authentication_url(self, perms, frob):
     d = dict(api_key=self.api_key, frob=frob, perms=perms)
     d["api_sig"] = utils.get_api_signature(self.secret_key, **d)
     auth_url = "%s?%s" % (utils.AUTH_API_URL, urllib.urlencode(d))
     return auth_url