예제 #1
0
def create_post(blog_id, title, text="", url="", image_url=""):

    shortened_url = shorten_link(url) if url else ""

    url_html = "<a href='{shortened_url}'>{url}</a><br/>".format(url=url, shortened_url=shortened_url) if url else ""
    image_html = "<img style='width: 100%; height: auto;' src='{image_url}'></img><br/>".format(image_url=image_url) if image_url else ""
    text_html = "<p>" + text + "</p>" + "<br/>" if text else ""
    content = url_html + image_html + text_html

    body = {
        "blog": {
            "id": blog_id
        },
        "kind": "blogger#post",
        "author": {
            "id": "self"
        },
        "title": title,
        "content": content
    }

    service = get_google_service()
    request = service.posts().insert(blogId=blog_id, body=body, isDraft=False, fetchImages=True, fetchBody=True)

    try:
        response = request.execute()
    except HttpAccessTokenRefreshError as err:
        logging.error(str(err))
        sys.exit()
예제 #2
0
def create_post(title, text="", url="", image_url=""):

    shortened_url = shorten_link(url) if url else ""

    url_html = "<a href='{shortened_url}'>{url}</a><br/>".format(url=url, shortened_url=shortened_url) if url else ""
    image_html = "<img style='width: 100%; height: auto;' src='{image_url}'></img><br/>".format(image_url=image_url) if image_url else ""
    text_html = "<p>" + text + "</p>" + "<br/>" if text else ""
    content = url_html + image_html + text_html

    # https://developer.wordpress.com/docs/api/1.1/post/sites/%24site/posts/new/
    # http://python-wordpress-xmlrpc.readthedocs.org/en/latest/examples/posts.html

    post = WordPressPost()
    post.title = title
    post.content = content
    post.author = "testaccount"
    post.post_status = "publish"
    # post.terms_names = {
    #   'post_tag': ['test', 'firstpost'],
    #   'category': ['Introductions', 'Tests']
    # }

    try:
        redditsync.wordpress.wp.call(NewPost(post))
    except Exception as err:
        logging.error(str(err))
        sys.exit()