Exemple #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()
def delete_post(blog_id, post_id):

    service = get_google_service()
    request = service.posts().delete(blogId=blog_id, postId=post_id)

    try:
        response = request.execute()
    except HttpAccessTokenRefreshError as err:
        logging.error(str(err))
        sys.exit()
Exemple #3
0
def get_blogs():

    user_id = "self"

    service = get_google_service()
    request = service.blogs().listByUser(userId=user_id)

    try:
        response = request.execute()
    except HttpAccessTokenRefreshError as err:
        logging.error(str(err))
        sys.exit()

    print("Blogs for User: %s" % user_id)
    print()

    return response.get('items', [])
Exemple #4
0
def get_posts(blog_id):

    service = get_google_service()

    posts = []

    request = service.posts().list(blogId=blog_id)
    while request is not None:

        try:
            response = request.execute()
        except HttpAccessTokenRefreshError as err:
            logging.error(str(err))
            sys.exit()

        posts += response.get('items', [])
        request = service.posts().list_next(request, response)

    return posts