Example #1
0
def publish(site):
    type = request.form.get('h')
    new_post_url = API_NEW_POST_URL.format(site.site_id)

    data = {
        'title': request.form.get('name'),
        'content': util.get_complex_content(request.form),
        'excerpt': request.form.get('summary'),
        'slug': request.form.get('slug'),
    }

    files = None
    photo_file = request.files.get('photo')
    if photo_file:
        # TODO support multiple files
        data['format'] = 'image'
        files = {
            'media[0]': (os.path.basename(photo_file.filename), photo_file),
        }

    req = requests.Request('POST', new_post_url, data=util.trim_nulls(data),
                           files=files, headers={
                               'Authorization': 'Bearer ' + site.token,
    })

    req = req.prepare()
    s = requests.Session()
    r = s.send(req)

    if r.status_code // 100 != 2:
        return util.wrap_silo_error_response(r)

    r_data = r.json()
    return util.make_publish_success_response(r_data.get('URL'), data=r_data)
Example #2
0
def publish(site):
    new_post_url = API_NEW_POST_URL.format(site.site_id)

    data = {
        'title': request.form.get('name'),
        'content': util.get_complex_content(request.form),
        'excerpt': request.form.get('summary'),
        'slug': request.form.get('slug'),
    }

    files = None
    photo_files = util.get_possible_array_value(request.files, 'photo')
    photo_urls = util.get_possible_array_value(request.form, 'photo')
    if photo_files or photo_urls:
        data['format'] = 'image'
        if photo_files:
            files = {
                'media[]': [(os.path.basename(photo_file.filename), photo_file)
                            for photo_file in photo_files],
            }
        if photo_urls:
            data['media_urls[]'] = photo_urls

    req = requests.Request('POST',
                           new_post_url,
                           data=util.trim_nulls(data),
                           files=files,
                           headers={'Authorization': 'Bearer ' + site.token})

    req = req.prepare()
    s = requests.Session()
    r = s.send(req)

    if r.status_code // 100 != 2:
        return util.wrap_silo_error_response(r)

    r_data = r.json()
    return util.make_publish_success_response(r_data.get('URL'), data=r_data)
Example #3
0
def publish(site):
    """
    Request:

    POST https://www.googleapis.com/blogger/v3/blogs/6561492933847572094/posts
    {
     "title": "This is a test, beautiful friend",
     "content": "This is some content with <i>html</i>!"
    }

    Response:

    200 OK
    {
     "kind": "blogger#post",
     "id": "8225907794810815386",
     "blog": {
      "id": "6561492933847572094"
     },
     "published": "2015-04-14T20:00:00-07:00",
     "updated": "2015-04-14T20:00:19-07:00",
     "etag": "\"Fgc6PVMaOxmEtPvQq0K7b_sZrRM/dGltZXN0YW1wOiAxNDI5MDY2ODE5MTYwCm9mZnNldDogLTI1MjAwMDAwCg\"",
     "url": "http://nofeathersnofur.blogspot.com/2015/04/this-is-test-beautiful-friend.html",
     "selfLink": "https://www.googleapis.com/blogger/v3/blogs/6561492933847572094/posts/8225907794810815386",
     "title": "This is a test, beautiful friend",
     "content": "This is some content with <i>html</i>!",
     "author": {
      "id": "01975554238474627641",
      "displayName": "Kyle",
      "url": "http://www.blogger.com/profile/01975554238474627641",
      "image": {
       "url": "http://img2.blogblog.com/img/b16-rounded.gif"
      }
     },
     "replies": {
      "totalItems": "0",
      "selfLink": "https://www.googleapis.com/blogger/v3/blogs/6561492933847572094/posts/8225907794810815386/comments"
     },
     "status": "LIVE",
     "readerComments": "ALLOW"
    }
    """
    maybe_refresh_access_token(site.account)

    type = request.form.get('h')
    create_post_url = API_CREATE_POST_URL.format(site.site_id)

    current_app.logger.info('posting to blogger %s', create_post_url)

    post_data = util.trim_nulls({
        'title': request.form.get('name'),
        'content': util.get_complex_content(request.form),
    })

    r = requests.post(create_post_url, headers={
        'Authorization': 'Bearer ' + site.account.token,
        'Content-Type': 'application/json',
    }, data=json.dumps(post_data))

    current_app.logger.info(
        'response from blogger %r, data=%r, headers=%r',
        r, r.content, r.headers)

    if r.status_code // 100 != 2:
        return util.wrap_silo_error_response(r)

    success_data = r.json()
    return util.make_publish_success_response(
        success_data.get('url'), data=success_data)
Example #4
0
def publish(site):
    auth = OAuth1(
        client_key=current_app.config['TUMBLR_CLIENT_KEY'],
        client_secret=current_app.config['TUMBLR_CLIENT_SECRET'],
        resource_owner_key=site.account.token,
        resource_owner_secret=site.account.token_secret)

    create_post_url = CREATE_POST_URL.format(site.domain)
    photo_url = util.get_first(util.get_possible_array_value(request.form, 'photo'))
    photo_file = util.get_first(util.get_possible_array_value(request.files, 'photo'))

    if photo_url:
        data = util.trim_nulls({
            'type': 'photo',
            'slug': request.form.get('slug'),
            'caption': request.form.get('content[html]') or
            request.form.get('content') or request.form.get('name') or
            request.form.get('summary'),
            'source': photo_url
        })
        r = requests.post(create_post_url, data=data, auth=auth)

    elif photo_file:
        # tumblr signs multipart in a weird way. first sign the request as if
        # it's application/x-www-form-urlencoded, then recreate the request as
        # multipart but use the signed headers from before. Mostly cribbed from
        # https://github.com/tumblr/pytumblr/blob/\
        # 20e7e38ba6f0734335deee64d4cae45fa8a2ce90/pytumblr/request.py#L101

        # The API documentation and some of the code samples gave me the
        # impression that you could also send files just as part of the
        # form-encoded data but I couldnit make it work
        # https://www.tumblr.com/docs/en/api/v2#pphoto-posts
        # https://gist.github.com/codingjester/1649885#file-upload-php-L56
        data = util.trim_nulls({
            'type': 'photo',
            'slug': request.form.get('slug'),
            'caption': request.form.get('content[html]') or
            request.form.get('content') or request.form.get('name') or
            request.form.get('summary'),
        })
        fake_req = requests.Request('POST', create_post_url, data=data)
        fake_req = fake_req.prepare()
        auth(fake_req)

        real_headers = dict(fake_req.headers)

        # manually strip these, requests will recalculate them for us
        del real_headers['Content-Type']
        del real_headers['Content-Length']

        current_app.logger.info(
            'uploading photo to tumblr %s, headers=%r',
            create_post_url, real_headers)
        r = requests.post(create_post_url, data=data, files={
            'data': photo_file,
        }, headers=real_headers)

    else:
        data = util.trim_nulls({
            # one of: text, photo, quote, link, chat, audio, video
            'type': 'text',
            'slug': request.form.get('slug'),
            'title': request.form.get('name'),
            'body': util.get_complex_content(request.form),
        })
        current_app.logger.info(
            'posting to tumblr %s, data=%r', create_post_url, data)
        r = requests.post(create_post_url, data=data, auth=auth)

    current_app.logger.info(
        'response from tumblr %r, data=%r, headers=%r',
        r, r.content, r.headers)

    if r.status_code // 100 != 2:
        current_app.logger.warn(
            'Tumblr publish failed with response %s', r.text)
        return util.wrap_silo_error_response(r)

    location = None
    if 'Location' in r.headers:
        location = r.headers['Location']
    else:
        # only get back the id, look up the url
        post_id = r.json().get('response').get('id')
        r = requests.get(FETCH_POST_URL.format(site.domain), params={
            'api_key': current_app.config['TUMBLR_CLIENT_KEY'],
            'id': post_id,
        })
        if r.status_code // 100 == 2:
            posts = r.json().get('response', {}).get('posts', [])
            if posts:
                location = posts[0].get('post_url')

    return util.make_publish_success_response(location)
Example #5
0
def publish(site):
    auth = OAuth1(
        client_key=current_app.config["TUMBLR_CLIENT_KEY"],
        client_secret=current_app.config["TUMBLR_CLIENT_SECRET"],
        resource_owner_key=site.account.token,
        resource_owner_secret=site.account.token_secret,
    )

    type = request.form.get("h")

    create_post_url = CREATE_POST_URL.format(site.domain)
    photo_file = request.files.get("photo")
    if photo_file:
        # tumblr signs multipart in a weird way. first sign the request as if
        # it's application/x-www-form-urlencoded, then recreate the request as
        # multipart but use the signed headers from before. Mostly cribbed from
        # https://github.com/tumblr/pytumblr/blob/\
        # 20e7e38ba6f0734335deee64d4cae45fa8a2ce90/pytumblr/request.py#L101

        # The API documentation and some of the code samples gave me the
        # impression that you could also send files just as part of the
        # form-encoded data but I couldnit make it work
        # https://www.tumblr.com/docs/en/api/v2#pphoto-posts
        # https://gist.github.com/codingjester/1649885#file-upload-php-L56
        data = util.trim_nulls(
            {
                "type": "photo",
                "slug": request.form.get("slug"),
                "caption": request.form.get("content[html]")
                or request.form.get("content")
                or request.form.get("name")
                or request.form.get("summary"),
            }
        )
        fake_req = requests.Request("POST", create_post_url, data=data)
        fake_req = fake_req.prepare()
        auth(fake_req)

        real_headers = dict(fake_req.headers)

        # manually strip these, requests will recalculate them for us
        del real_headers["Content-Type"]
        del real_headers["Content-Length"]

        current_app.logger.info("uploading photo to tumblr %s, headers=%r", create_post_url, real_headers)
        r = requests.post(create_post_url, data=data, files={"data": photo_file}, headers=real_headers)

    else:
        data = util.trim_nulls(
            {
                # one of: text, photo, quote, link, chat, audio, video
                "type": "text",
                "slug": request.form.get("slug"),
                "title": request.form.get("name"),
                "body": util.get_complex_content(request.form),
            }
        )
        current_app.logger.info("posting to tumblr %s, data=%r", create_post_url, data)
        r = requests.post(create_post_url, data=data, auth=auth)

    current_app.logger.info("response from tumblr %r, data=%r, headers=%r", r, r.content, r.headers)

    if r.status_code // 100 != 2:
        current_app.logger.warn("Tumblr publish failed with response %s", r.text)
        return util.wrap_silo_error_response(r)

    location = None
    if "Location" in r.headers:
        location = r.headers["Location"]
    else:
        # only get back the id, look up the url
        post_id = r.json().get("response").get("id")
        r = requests.get(
            FETCH_POST_URL.format(site.domain),
            params={"api_key": current_app.config["TUMBLR_CLIENT_KEY"], "id": post_id},
        )
        if r.status_code // 100 == 2:
            posts = r.json().get("response", {}).get("posts", [])
            if posts:
                location = posts[0].get("post_url")

    return util.make_publish_success_response(location)
Example #6
0
def publish(site):
    auth = OAuth1(client_key=current_app.config['TUMBLR_CLIENT_KEY'],
                  client_secret=current_app.config['TUMBLR_CLIENT_SECRET'],
                  resource_owner_key=site.account.token,
                  resource_owner_secret=site.account.token_secret)

    create_post_url = CREATE_POST_URL.format(site.domain)
    photo_url = util.get_first(
        util.get_possible_array_value(request.form, 'photo'))
    photo_file = util.get_first(
        util.get_possible_array_value(request.files, 'photo'))

    if photo_url:
        data = util.trim_nulls({
            'type':
            'photo',
            'slug':
            request.form.get('slug'),
            'caption':
            request.form.get('content[html]') or request.form.get('content')
            or request.form.get('name') or request.form.get('summary'),
            'source':
            photo_url
        })
        r = requests.post(create_post_url, data=data, auth=auth)

    elif photo_file:
        # tumblr signs multipart in a weird way. first sign the request as if
        # it's application/x-www-form-urlencoded, then recreate the request as
        # multipart but use the signed headers from before. Mostly cribbed from
        # https://github.com/tumblr/pytumblr/blob/\
        # 20e7e38ba6f0734335deee64d4cae45fa8a2ce90/pytumblr/request.py#L101

        # The API documentation and some of the code samples gave me the
        # impression that you could also send files just as part of the
        # form-encoded data but I couldnit make it work
        # https://www.tumblr.com/docs/en/api/v2#pphoto-posts
        # https://gist.github.com/codingjester/1649885#file-upload-php-L56
        data = util.trim_nulls({
            'type':
            'photo',
            'slug':
            request.form.get('slug'),
            'caption':
            request.form.get('content[html]') or request.form.get('content')
            or request.form.get('name') or request.form.get('summary'),
        })
        fake_req = requests.Request('POST', create_post_url, data=data)
        fake_req = fake_req.prepare()
        auth(fake_req)

        real_headers = dict(fake_req.headers)

        # manually strip these, requests will recalculate them for us
        del real_headers['Content-Type']
        del real_headers['Content-Length']

        current_app.logger.info('uploading photo to tumblr %s, headers=%r',
                                create_post_url, real_headers)
        r = requests.post(create_post_url,
                          data=data,
                          files={
                              'data': photo_file,
                          },
                          headers=real_headers)

    else:
        data = util.trim_nulls({
            # one of: text, photo, quote, link, chat, audio, video
            'type': 'text',
            'slug': request.form.get('slug'),
            'title': request.form.get('name'),
            'body': util.get_complex_content(request.form),
        })
        current_app.logger.info('posting to tumblr %s, data=%r',
                                create_post_url, data)
        r = requests.post(create_post_url, data=data, auth=auth)

    current_app.logger.info('response from tumblr %r, data=%r, headers=%r', r,
                            r.content, r.headers)

    if r.status_code // 100 != 2:
        current_app.logger.warn('Tumblr publish failed with response %s',
                                r.text)
        return util.wrap_silo_error_response(r)

    location = None
    if 'Location' in r.headers:
        location = r.headers['Location']
    else:
        # only get back the id, look up the url
        post_id = r.json().get('response').get('id')
        r = requests.get(FETCH_POST_URL.format(site.domain),
                         params={
                             'api_key':
                             current_app.config['TUMBLR_CLIENT_KEY'],
                             'id': post_id,
                         })
        if r.status_code // 100 == 2:
            posts = r.json().get('response', {}).get('posts', [])
            if posts:
                location = posts[0].get('post_url')

    return util.make_publish_success_response(location)