Automate Blog Post creation using Blogger APIs and Python

This article will discuss the following topics,

  • Blogger's REST APIs.
  • Google's OAuth 2.0 playground.
  • Accessing spreadsheet in Python.
  • Creating blogger posts via Python.

An experiment to automate blog post creation when you have structured data.

I was searching for IFSC codes for some bank transactions and came across Reserve Bank's website, which has a list of Excel files containing Bank Codes, Contact Info and related details of each bank's branch. This time I will be looking towards Bharat co-operative bank of Mumbai. Here is how the corresponding data looks like.


In the above picture, each row represents lots of details about each branch of the Bank. Let's think of creating a list of blog posts where each post gives IFSC codes of a particular branch along with address and contact details.

As the first step, I have created a blog in blogger. In the Blogger dashboard, please also note the parameter called blog ID which is a kind of unique identifier to the blog. It is needed while making REST calls to Blogger.


Now, let's go to Google Developer's website to check out some Blogger APIs and their authentication.
We can authorize Blogger APIs in two ways.
  1. By an API key.
  2. By OAuth 2.0 token.
Using API key is the easiest way, but is restricted to read-only access. For Create, Edit, Delete operations we need to depend on OAuth 2.0 method. We will begin with API key and then move on to OAuth 2.0 authentication. API key can be generated from the developer's site itself.


Let's have a quick demo using a rest client. I will be using Advanced REST Client(ARC). I'm using the API for fetching details of posts in the blog. In the url, we must add Blog Id and as a query param we need to add API key. On the API call, we received 200 response code and the response contains details about blog posts like post title, post contents etc.


Now we can check on the API used to create blog post. As said earlier we need to obtain auth 2.0 token for the API. Here is the API for post creation.


For our experiment, we can depend on Google's OAuth 2.0 playground. It's a place where we can generate an OAuth token for development purposes. It has a list of APIs from Google, but we will stick with Blogger API. Authorize the API and exchange authorization code for OAuth token.




Let's create a post via rest client using our OAuth 2.0 token. As the header parameters add OAuth token with keyword 'Bearer '. In the request body, add Blog Id, Post Title, and Post Contents. Do the POST operation and you could see the 200 OK response along with the details we sent.


Refresh the blog, so we could see the new post we just made.


Moving on to real codes, the major modules in the project are an HTML template, Python code for reading Excel and posting to Blogger.
  • In the HTML template everything written inside double curly braces such as bank name, branch name, IFSC code will be replaced by values from excel sheet.
  • Using xlrd library we will open the file, and then iterate from first to final row of the sheet and collect details.
  • The core part is creating and publishing a blog post. The POST URL, Body, Headers are constructed from details - OAuth token received as an argument, Blog Id, modified HTML template by excel data.

The code can be found on this Github Repository - PythonicWad
After executing the Python code, You can see the list of posts being published in the Blog.


Blog for Reference - IFSC Codes of BHARAT COOPERATIVE BANK MUMBAI
If you have any suggestions/corrections please let me know from Comment Section. Thanks

Comments

Post a Comment

Popular posts from this blog

Setting up Python Flask server on internet via Port forwarding

The beginning of Data Quest

Creation of Orphan Process in Linux using Python

How to remove TWRP from Android Phone - Nubia Z11 miniS