예제 #1
0
    def send_request(self, method=None, parameters=None, files=None):
        if USE_SSL:
            protocol = 'https://'
        else:
            protocol = 'http://'

        url = '%s%s/%s' % (protocol, API_HOST, method)

        data = {
            'api_key': self.api_key,
            'api_secret': self.api_secret,
            'format': self.format
        }

        if parameters:
            data.update(parameters)

        # Local file is provided, use multi-part form
        if files:
            from multipart import Multipart
            form = Multipart()

            for key, value in data.iteritems():
                form.field(key, value)

            for i, file in enumerate(files, 1):
                if hasattr(file, 'read'):
                    if hasattr(file, 'name'):
                        name = os.path.basename(file.name)
                    else:
                        name = 'attachment_%d' % i
                    close_file = False
                else:
                    name = os.path.basename(file)
                    file = open(file, 'r')
                    close_file = True

                try:
                    form.file(name, name, file.read())
                finally:
                    if close_file:
                        file.close()

            (content_type, post_data) = form.get()
            headers = {'Content-Type': content_type}
        else:
            post_data = urllib.urlencode(data)
            headers = {}

        request = urllib2.Request(url, headers=headers, data=post_data)
        response = urllib2.urlopen(request)
        response = response.read()
        response_data = json.loads(response)

        if 'status' in response_data and \
            response_data['status'] == 'failure':
            raise FaceError(response_data['error_code'],
                            response_data['error_message'])

        return response_data
예제 #2
0
파일: uploader.py 프로젝트: epeay/nike4all
    def uploadRun(self, email, password, files):
        '''
        '''
        def file2text(fn):
            fh = open(fn)
            fc = fh.read()
            fh.close()
            return fc

        upload_url = self.url + '/add_run.php'
        success = True
        backupdir = self.getConfigDataFromFile()[2]

        if not self.isLoggedIn:
            self.isLoggedIn = self.webLogin(email, password)

        if self.isLoggedIn:

            for full_path in files:
                self.logger.info("uploading file %s" % full_path)
                path, fname = os.path.split(full_path)

                if self.isNewFile(fname):

                    m = Multipart()
                    m.field('public_run', 'checked')
                    m.field('MAX_FILE_SIZE', '14000000')
                    m.file(
                        'runfile', fname, file2text(full_path),
                        {'Content-Type': 'application/x-www-form-urlencoded'})
                    ct, body = m.get()

                    request = urllib2.Request(upload_url,
                                              headers={'Content-Type': ct},
                                              data=body)
                    reply = ClientCookie.urlopen(request)
                    file_uploaded = 'Runometer: Show Run' in reply.read()

                    if file_uploaded:
                        self.logFileUploaded(fname)
                        shutil.copy(full_path, backupdir)

                    success = success * file_uploaded

                else:
                    self.logger.info(
                        "File %s already uploaded in some previous session" %
                        fname)

        return success
예제 #3
0
    def send_request(self, method=None, parameters=None):
        url = '%s/%s' % (API_URL, method)

        data = {
            'api_key': self.api_key,
            'api_secret': self.api_secret,
            'format': self.format
        }

        if parameters:
            data.update(parameters)
        # raise Exception(url, data)
        # Local file is provided, use multi-part form
        if 'file' in parameters:
            from multipart import Multipart
            form = Multipart()

            for key, value in data.iteritems():

                if key == 'file':
                    file = open(value, 'r')
                    # with open(value, 'r') as file:
                    form.file(os.path.basename(key), os.path.basename(key),
                              file.read())
                else:
                    form.field(key, value)

            (content_type, post_data) = form.get()
            headers = {'Content-Type': content_type}
        else:
            post_data = urllib.urlencode(data)
            headers = {}

        request = urllib2.Request(url, headers=headers, data=post_data)
        response = urllib2.urlopen(request)
        response = response.read()
        response_data = json.loads(response)

        if 'status' in response_data and \
         response_data['status'] == 'failure':
            raise FaceError(response_data['error_code'],
                            response_data['error_message'])

        return response_data
예제 #4
0
def update_theme(email, password, blog_name, file_path):

    cj = cookielib.CookieJar()
    handler = urllib2.HTTPHandler(debuglevel=0)
    opener = urllib2.build_opener(RedirectHandler(), handler,
                                  urllib2.HTTPCookieProcessor(cj))
    urllib2.install_opener(opener)

    login_url = "http://www.tumblr.com/login"
    login_post_data = {'email': email, 'password': password}

    login_req = urllib2.Request(login_url, urllib.urlencode(login_post_data))
    urllib2.urlopen(login_req).read()

    dashboard_req = urllib2.Request("http://www.tumblr.com/customize/%s" %
                                    blog_name)
    custom_html = urllib2.urlopen(dashboard_req).read()

    form_key = re.compile("form_key.*?value=\"(.*?)\"").findall(custom_html)[0]

    customize_url = "http://www.tumblr.com/customize/%s" % blog_name

    m = Multipart()
    m.field("edit_tumblelog[custom_theme]", open(file_path, 'r').read())
    m.field("form_key", form_key)

    ct, body = m.get()

    request = urllib2.Request(
        url=customize_url,
        headers={
            'Content-Type': ct,
            'User-Agent':
            "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.12) Gecko/20080207 Ubuntu/7.10 (gutsy) Firefox/2.0.0.12",
            'Connection': 'keep-alive'
        },
        data=body)
    reply = urllib2.urlopen(request)
    reply.read()