Example #1
0
 def _api_multipart(self, a, b, mime):
     url = self._urlreplace(a, b)
     purl = urlparse.urlparse(url)
     
     data = email.mime.multipart.MIMEMultipart("form-data")
     for m in mime:
         data.attach(m)
     
     multipart = data.as_string()
     boundary = data.get_boundary()
     ctype, multipart = multipart.split("\n\n", 1)
     
     if purl.scheme == "https":
         c = httplib.HTTPSConnection(purl.netloc)
     else:
         c = httplib.HTTPConnection(purl.netloc)
     
     oauth_header = self.oauth.oauth_header(
         url, "POST", secret = self.oauth.asecret)
     
     # multipart/form-data: \n -> \r\n
     multipart_data = list()
     multipart_data.append("--" + boundary)
     for m in multipart.split("--" + boundary):
         if m == "" or m == "--":
             continue
         
         header, body = m.split("\n\n", 1)
         
         for h in header.strip().split("\n"):
             multipart_data.append(h.strip())
         
         multipart_data.append("")
         multipart_data.append(body[:-1])
         multipart_data.append("--" + boundary)
     multipart_data[-1] += "--"
     
     multipart = "\r\n".join(multipart_data)
     
     c.putrequest("POST", purl.path)        
     c.putheader("Authorization", oauth_header)
     c.putheader("Content-Length", str(len(multipart)))
     c.putheader("Content-Type", 
                 'multipart/form-data; boundary="%s"' % boundary)
     c.endheaders()
     c.send(multipart)
     
     response = c.getresponse().read()
     return self._convert(response)
Example #2
0
    def upload(self, filename, message=""):
        # img
        image = open(filename)
        imgdata = image.read()
        image.close()
        mimeimg = email.mime.image.MIMEImage(
            imgdata, _encoder=email.encoders.encode_noop)
        mimeimg.add_header("Content-Disposition",
                           "form-data",
                           name="media",
                           filename="image")
        mimeimg.add_header("Content-Length", str(len(imgdata)))

        # key
        mimekey = email.mime.text.MIMEText(self.apikey)
        mimekey.add_header("Content-Disposition", "form-data", name="key")

        # message
        mimemsg = email.mime.text.MIMEText(message)
        mimemsg.set_charset("utf-8")
        mimemsg.add_header("Content-Disposition", "form-data", name="message")

        data = email.mime.multipart.MIMEMultipart("form-data")
        data.attach(mimekey)
        data.attach(mimemsg)
        data.attach(mimeimg)

        multipart = data.as_string()
        boundary = data.get_boundary()
        ctype, multipart = multipart.split("\n\n", 1)

        c = httplib.HTTPConnection(self.host)
        c.putrequest("POST", self.url)

        c.putheader("Content-Length", str(len(multipart)))
        c.putheader("Content-Type",
                    'multipart/form-data; boundary="%s"' % boundary)

        oauth_header = self.oauth.oauth_header(self.verify_credentials_url,
                                               secret=self.oauth.asecret,
                                               realm="http://api.twitter.com/")

        c.putheader("X-Auth-Service-Provider", self.verify_credentials_url)
        c.putheader("X-Verify-Credentials-Authorization", oauth_header)

        c.endheaders()
        c.send(multipart)

        response = c.getresponse().read()
        return json.loads(response)
Example #3
0
 def upload(self, filename, message = ""):        
     # img
     image = open(filename)
     imgdata = image.read()
     image.close()
     mimeimg = email.mime.image.MIMEImage(imgdata, _encoder = email.encoders.encode_noop)
     mimeimg.add_header("Content-Disposition", "form-data", name = "media", filename = "image")
     mimeimg.add_header("Content-Length", str(len(imgdata)))
     
     # key
     mimekey = email.mime.text.MIMEText(self.apikey)
     mimekey.add_header("Content-Disposition", "form-data", name = "key")
     
     # message
     mimemsg = email.mime.text.MIMEText(message)
     mimemsg.set_charset("utf-8")
     mimemsg.add_header("Content-Disposition", "form-data", name = "message")
     
     data = email.mime.multipart.MIMEMultipart("form-data")
     data.attach(mimekey)
     data.attach(mimemsg)
     data.attach(mimeimg)
     
     multipart = data.as_string()
     boundary = data.get_boundary()
     ctype, multipart = multipart.split("\n\n", 1)
     
     c = httplib.HTTPConnection(self.host)
     c.putrequest("POST", self.url)
     
     c.putheader("Content-Length", str(len(multipart)))
     c.putheader("Content-Type", 
                 'multipart/form-data; boundary="%s"' % boundary)
     
     oauth_header = self.oauth.oauth_header(
         self.verify_credentials_url, 
         secret = self.oauth.asecret,
         realm = "http://api.twitter.com/")
     
     c.putheader("X-Auth-Service-Provider", self.verify_credentials_url)
     c.putheader("X-Verify-Credentials-Authorization", oauth_header)
     
     c.endheaders()
     c.send(multipart)
     
     response = c.getresponse().read()
     return json.loads(response)