DEV Community

Cover image for A Python Program that Tweets Fetched Data from the icanhazdadjoke.com
Vicente G. Reyes
Vicente G. Reyes

Posted on

A Python Program that Tweets Fetched Data from the icanhazdadjoke.com

Cover photo by https://icons8.com/

The purpose of this program was to automate the jokes I have been posting on my facebook account thru twitter. Although I haven't figured out AWS Lambda yet, I believe I'll get there in no time.

This program is run thru IDLE, which, according to Wikipedia, is bundled with the Mac OS X Python since 1.5.2b1.

We start learning with "Getting started with the Twitter API", then we progress to calling the icanhazdadjoke api throughout the article.

In this program, I learned 4 things:

  • How to apply & create the Twitter API keys used in this application

  • How to use Twython to send tweets using Python

  • How to call an API &

  • How to search for the right questions on a search engine to get the right answer

First, we apply for a Twitter Developer account with the help of this. Upon completing the application, we will receive an email confirmation from Twitter to confirm our email address. Then we wait for our application to be approved. We can check the status at developer.twitter.com.

After getting approved for the developer account, we login to developer.twitter.com to get our application registered to get our API Keys.

Review the developer terms and click create.

Click on keys and tokens to view your keys and access tokens then click create under Access token & access token secret.

Coding the program and sending our first tweet

Create an auth.py file on IDLE and paste your keys inside then hit save

consumer_key = 'ENTER_YOUR_CONSUMER_KEY'
consumer_secret = 'ENTER_YOUR_CONSUMER_SECRET'
access_token = 'ENTER_YOUR_ACCESS_TOKEN'
access_token_secret = 'ENTER_YOUR_ACCESS_TOKEN'

Create another file and import twython from Twython and the requests module

from twython import Twython
import requests

Also import the variables from auth.py

from auth import (
    consumer_key,
    consumer_secret,
    access_token,
    access_token_secret
)

save this as twitter.py

Create a connection with the Twitter API using Twython

twitter = Twython(
    consumer_key,
    consumer_secret,
    access_token,
    access_token_secret
)

We'll use the
update_status()
function of Twitter's API to send a tweet

message = "Hello world!"
twitter.update_status(status=message)
print('Tweeted: ' % message)

Now we run the module by using F5 or from tweet.py -> Run -> Run Module.

View from the mobile :

View from the CLI :

View from the browser :

Calling the icanhazdadjoke API

Fortunately, icanhazdadjoke can be called without an authentication by the GET method. Read through their docs here if you want to learn more

url = 'https://icanhazdadjoke.com/'
headers = {'Accept': 'application/json'}
joke_msg = requests.get(url, headers=headers).json().get('joke')
print(joke_msg)

The response is : πŸ™ŒπŸ™ŒπŸ™Œ

Now, we play around the codes.

Designing the Code Structure

We want the output of the icanhazdadjoke tweeted on our twitter account so we use the url variable to call their API. I start with the output, going back to calling the API.

We end up with this code :

url = 'https://icanhazdadjoke.com/'
headers = {'Accept': 'application/json'}
joke_msg = requests.get(url, headers=headers).json().get('joke')
twitter.update_status(status=joke_msg)
print("Tweeted: " + joke_msg)

The output is : 😱😱😱

CLI :

Browser :

Mobile :

πŸ™ŒπŸ˜±πŸ™ŒπŸ˜±πŸ™Œ

Next step here would be to figure out AWS Lambda to automate the tweets twice a day and forget about it.

Top comments (2)

Collapse
 
josheriff profile image
Jose

Hey you import like this:

from Twython import Twython

but the first 'Twython' cannot be capitalize:

must be:

from twython import Twython

Collapse
 
highcenburg profile image
Vicente G. Reyes • Edited

I didn't notice that I typed it wrong here. On my file, it's

from twython import Twython
. Thanks!