Python API Example with Wallabag Web Application

python and wallabag

Wallabag

Many a times it happens that we need to create API to post data to some web application using python framework.
To over come this problem of sending data to application from outside of it, using API, I am going to show how you can do this for Wallabag Web Application. Wallabag is Read It Later type of application, where you can save website links, and then read later.

Thus we will look here how to write API script that can send information to Wallabag web based application.

To do this we need access to Wallabag. It is open open source project (MIT license) so you can download and install as self hosted service.

Collecting Information

Once we installed Wallabag or got access to it, we will collect information needed for authorization.
Go to Wallabag application, then API Client Management Tab and create the client.
Note client id, client secret.
See below screenshot for references.

Python API Example Script

Now we can go to python IDE and write the script as below. Here https://mysite.com is base url, wallabag is an installation folder where we installed application.


import requests


# below 5 variables have to be set

USERNAME = 'xxxxxxxx'
PASSWORD = 'xxxxxxxx'
CLIENTID = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx'
SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
HOST = 'https://mysite.com/wallabag/web'    


gettoken = {'username': USERNAME, 'password': PASSWORD, 'client_id': CLIENTID, 'client_secret': SECRET, 'grant_type': 'password'}
print (gettoken)
r = requests.post('{}/oauth/v2/token'.format(HOST), gettoken)
print (r.content)

access = r.json().get('access_token')


url = 'https://visited_site.com' # URL of the article
# should the article be already read? 0 or 1 for archive
# should the article be added as favorited? 0 or 1 for starred

article = {'url': url, 'archive': 0 , 'starred': 0, 'access_token': access}
r = requests.post('{}/api/entries.json'.format(HOST), article)


"""
output:
{'username': 'xxxxxxxxx', 'password': 'xxxxxxxxxx', 'grant_type': 'password', 'client_id': 'xxxxxxxxxxxxxxx', 'client_secret': 'xxxxxxxxxxx'}
b'{"access_token":"xxxxxxxxxxxxxx","expires_in":3600,"token_type":"bearer","scope":null,"refresh_token":"xxxxxxxxxxxxxx"}'
"""



Troubleshooting

I found useful to include print (r.content) in case something goes wrong. It can help see what is returned by sever.
Also it helped me looking at log which is located at /var/logs/prod.log under yoursite.com/wallabag. In case something is going wrong it might have some clue in the log.

Conclusion

We looked at python api example of how to integrate python script with Wallabag web application and send data using Wallabag API and python requests library.

References

wallabag api example
Wallabag

Web API to Save to Pocket App and Instapaper App

As we surf the web we find a lot of information that we might use later. We use different applications (Pocket app, Instapaper, Diigo, Evernote or other apps) to save links or notes what we find.

While many of the above applications have a lot of great features there still a lot of opportunities to automate some processes using web API that many of applications provide now.

This will allow to extend application functionality and eliminate some manual processes.

For Example: You have about 20 links that you want to send to pocket like application.
Another example: When you add link to one application you may be want also save link or note to Pocket app or to Instapaper application.
Or may be you want automatically (through script) extract links from some web sites and save them to your Pocket app.

In today post we will look at few examples that allow you start to do this. We will check how to use Pocket API and Instapaper API with python programming.

API for Pocket App

pocket API Pocket, previously known as Read It Later, is an application and service for managing a reading list of articles from the Internet. It is available on many different devices including web browsers. (Wikipedia)
There is great post[1] that is showing how to set up API for it. This post has detailed screenshots how to get all the needed identification information for successful login.

In summary you need get online consumer key for your api application then obtain token code via script. Then you can access the link that will include token and do authorization of application. After this you can use API for sending links.

Below is the summary python script to send the link to Pocket app including previous steps:

import requests

# can be any for link
redirect_link = "google.com"
consumer_key="xxxxxxxx"
# obtain consume key online
#connect to pocket API to get token code
pocket_api = requests.post('https://getpocket.com/v3/oauth/request',
         data = {'consumer_key':consumer_key,
                 'redirect_uri':redirect_link})

pocket_api.status_code       #if 200, it means all ok.

print(pocket_api.headers)               
print (pocket_api.text)

#remove 'code='
token= pocket_api.text[5:]
print (token)
url="https://getpocket.com/auth/authorize?request_token=" + token + "&redirect_uri=" + redirect_link 

import webbrowser
webbrowser.open_new(url) # opens in default browser
#click on Authorize button in webbrowser

# Once authoration done you can post as below (need uncomment code below)  
"""
pocket_add = requests.post('https://getpocket.com/v3/add',
       data= {'url': 'https://getpocket.com/developer/apps/new',
              'consumer_key':consumer_key,
              'access_token': token})
print (pocket_add.status_code)

"""

API for Instapaper

Instapaper is a bookmarking service owned by Pinterest. It allows web content to be saved so it can be “read later” on a different device, such as an e-reader, smartphone, tablet. (Wikipedia)
Below is the code example how to send link to Instapaper. The code example is based on the script that I found on Internet [2]

import urllib, sys

def error(msg):
sys.exit(msg)

def main():
api = 'https://www.instapaper.com/api/add'

params = urllib.parse.urlencode({
'username' : "actual_user_name",
'password' : "actual_password",
'url' : "https://www.actual_url",
'title' : "actual_title",
'selection' : "description"

}).encode("utf-8")

r = urllib.request.urlopen(api, params)

status = r.getcode()

if status == 201:

print('%s saved as %s' % (r.headers['Content-Location'], r.headers['X-Instapaper-Title']))
elif status == 400:
error('Status 400: Bad request or exceeded the rate limit. Probably missing a required parameter, such as url.')
elif status == 403:
error('Status 403: Invalid username or password.')
elif status == 500:
error('Status 500: The service encountered an error. Please try again later')

if __name__ == '__main__':
main()

References
1. Add Pocket API using Python – Tutorial
2. Instapaper