This tutorial is a solution to a problem that I am facing. I need the automation to automatically publish my medium posts to my Twitter handle, so I have created a simple Twitter bot using python to solve my problem.
In this tutorial, I am going to call the medium’s public API to fetch the latest posts published in JSON format using the rss2json.com. Then we have to tweet it using thetweepy python package.
You have to replace username_here it with your username, in my case, it is @harendraverma21. It will provide JSON data of your posts.
Screenshot of JSON response
We will use it letter in this tutorial.
Install packages using pip
pip install tweepy pip install requests
Import into a python file
import tweepy import urllib from urllib.request import urlretrieve from urllib.parse import urlparse import os import requests
You have to visit https://developer.twitter.com/ and create your app to get tokens. This app will require 4 keys —
API_KEY = "token_here" API_SECRET_KEY = "token_here" ACCESS_TOKEN = "token_here" ACCESS_SECRET = "token_here"
You have to replace token_here with your token.
auth = tweepy.OAuthHandler(API_KEY,API_SECRET_KEY) auth.set_access_token(ACCESS_TOKEN,ACCESS_SECRET)
Now we have to retrieve posts from medium public API as mentioned above.
response = requests.get(url) posts = response.json() if posts["status"] == "ok": posts = posts["items"]; for post in posts: post_title = post["title"] post_url = post["link"] post_image = post['thumbnail']
This is the final step to tweet your post on Twitter.
post_result = api.update_status(post_title)
I have provided a basic idea of how you can tweet your posts on Twitter. There are some bugs are in this code such as it will always tweet the latest post multiple times. To overcome the problem I have applied a little trick here. I will save the thumbnail with the post id of the post every time and always check tweet posts that don’t have images in our directory.
Let’s see the full code here:
<iframe src="https://medium.com/media/f2847b52359d3e2aa9930d773956c2dd" frameborder=0></iframe>
I have provided the gist of the code with proper comments which will help you to understand the code.
To automate the process we have to set corn at every x min, to do that I am using crontab to schedule it in my system.
*/10 * * * * python /path_to_script/script.py
This script will run every 10 minutes.
I hope this tutorial will help you solve the problem of tweeting each post to Twitter. It helps me a lot to save time. Feel free to ask anything related to this tutorial.
Thank you for reading this tutorial. Don’t forget to follow and Clap 👏 for this article.
Read more posts 5 Ways to Earn Money From Your Code Top ways to start earning money with your coding skills.python.plainenglish.io Major Differences between Python and R Programming Languages Python vs R: Learn the major differences between Python and R programming languages and get answers to some common…python.plainenglish.io How to Create QR Code Generator and Reader Using Python A guide on creating and reading QR codes in Python with a working example.python.plainenglish.io Top 10 Programming Languages to Learn in 2022 In the year 2022, below is a list of the top programming languages to learn.python.plainenglish.io The Best Python Compiler to Run Code Online Top online compilers for Python programming language.python.plainenglish.io
More content at plainenglish.io
Hello, I am Harendra Kumar Kanojiya - Owner of this website and a Fullstack web developer. I have expertise in full-stack web development using Angular, PHP, Node JS, Python, Laravel, Codeigniter and, Other web technologies. I also love to write blogs on the latest web technology to keep me and others updated. Thank you for reading the articles.