예제 #1
0
파일: upai.py 프로젝트: lfeng/upaily
    def upload(self,  photos, **params):#{{{
        params['format'] = self.format
        params['api_key'] = self.api_key

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

        params['api_sig'] =self.sign(**params)

        req_url = self.api_host + '/api/upload/'

        body = Multipart()
        for arg, value in params.iteritems():
            part = Part({'name': arg}, value)
            body.attach(part)

        if type(photos) != types.ListType:
            photos = [photos]

        for photo in photos:
            filepart = Part({'name': 'photo', 'filename':params['title'].encode('utf8')},
                             photo['data'],
                            'image/'+photo['type'])
            body.attach(filepart)

        request = urllib2.Request(url=req_url)
        request.add_data(str(body))
        (header, value) = body.header()
        request.add_header(header, value)
        #request.add_header('content-length', len(str(body)))
        return json.loads(urllib2.urlopen(request).read())#}}}
예제 #2
0
    def __upload_to_form(self, form_url, filename, callback, **kwargs):
        '''Uploads a photo - can be used to either upload a new photo
        or replace an existing one.

        form_url must be either ``FlickrAPI.flickr_replace_form`` or
        ``FlickrAPI.flickr_upload_form``.
        '''

        if not filename:
            raise IllegalArgumentException("filename must be specified")
        if not self.token_cache.token:
            raise IllegalArgumentException("Authentication is required")

        # Figure out the response format
        format = self.__extract_upload_response_format(kwargs)

        # Update the arguments with the ones the user won't have to supply
        arguments = {'auth_token': self.token_cache.token,
                     'api_key': self.api_key}
        arguments.update(kwargs)

        # Convert to UTF-8 if an argument is an Unicode string
        kwargs = make_utf8(arguments)
        
        if self.secret:
            kwargs["api_sig"] = self.sign(kwargs)
        url = "http://%s%s" % (FlickrAPI.flickr_host, form_url)

        # construct POST data
        body = Multipart()

        for arg, value in kwargs.iteritems():
            part = Part({'name': arg}, value)
            body.attach(part)

        filepart = FilePart({'name': 'photo'}, filename, 'image/jpeg')
        body.attach(filepart)

        return self.__wrap_in_parser(self.__send_multipart, format,
                url, body, callback)
예제 #3
0
infile = 'data.in'
outfile = 'data.out'
cid = getVal("init.info", "diy", "cid")
pid = getVal("init.info", "problem", "pid")
url = 'http://acm.hdu.edu.cn/diy/diy_uploaddata.php?action=upload&cid=' + cid + '&pid=' + pid

body = Multipart()

intext = open(infile, "rb").read()
outtext = open(outfile, "rb").read()

inpart = Part({"name":"inputfile", "filename":infile}, intext, "text/plain")
outpart = Part({"name":"outputfile", "filename":outfile}, outtext, "text/plain")

body.attach(inpart)
body.attach(outpart)

request = Request(url)
request.add_data(str(body))
(header, value) = body.header()
request.add_header(header, value)

cookiejar = CookieJar()
cookieproc = HTTPCookieProcessor(cookiejar)
urlOpener = build_opener(cookieproc)

# login
loginurl = "http://acm.hdu.edu.cn/userloginex.php?action=login&cid=0&notice=0"
loginval = {"username":"******", "userpass":"******", "login":"******"}
urlOpener.open(Request(loginurl, urlencode(loginval)))