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

Python Developer | Audio Editor | Technical Writer | OSS Contributor | Tag Moderator @ @ThePracticalDEV | Valorant TonyPoppins #881488
Search for a command to run...

Python Developer | Audio Editor | Technical Writer | OSS Contributor | Tag Moderator @ @ThePracticalDEV | Valorant TonyPoppins #881488
No comments yet. Be the first to comment.
A no-fluff deployment runbook for getting a Cookiecutter Django project live on DigitalOcean using Docker and Traefik. Covers the full path from droplet provisioning to a working production deployment

If youâre switching between networks with different trust levelsâhome, cafĂ©, coworking spaceâyou probably donât want your VPN behavior to be static. This guide walks through a clean, system-level way

Why this script exists If you've ever watched your free disk space quietly shrink over a few weeks of active development, you know the feeling: yesterday you had plenty of headroom, today your IDE is

A message from a colleague dropped into my inbox one morning: Hi Ice, now that we pushed the changes sa site, we need to scour the site for mentions of Level 1, Level 2, Level 1/2 and change them acc

Backing up your WordPress site is one of the most important maintenance tasks you can do as a site owner. While plugins like UpdraftPlus or Jetpack make this easy, knowing how to do it manually via SS

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








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'
from twython import Twython import requests
from auth import (
consumer_key,
consumer_secret,
access_token,
access_token_secret
)
save this as twitter.py
twitter = Twython(
consumer_key,
consumer_secret,
access_token,
access_token_secret
)
update_status()function of Twitter's API to send a tweet
message = "Hello world!"
twitter.update_status(status=message)
print('Tweeted: ' % message)
View from the mobile :

View from the CLI :

View from the browser :

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 : :raised_hands::raised_hands::raised_hands:

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 : :scream::scream::scream:
CLI :

Browser :

Mobile :

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