Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# post - https://medium.com/p/889e37c71c4e
import tweepy
import urllib
from urllib.request import urlretrieve
from urllib.parse import urlparse
import os
import requests
# Twitter keys - https://developer.twitter.com/
API_KEY = "token_here"
API_SECRET_KEY = "token_here"
ACCESS_TOKEN = "token_here"
ACCESS_SECRET = "token_here"
# Authenticate to Twitter
auth = tweepy.OAuthHandler(API_KEY,API_SECRET_KEY)
auth.set_access_token(ACCESS_TOKEN,ACCESS_SECRET)
# Post medium post to twitter
def medium(url):
response = requests.get(url)
posts = response.json()
if posts["status"] == "ok":
posts = posts["items"];
for post in posts:
post_title = post["title"] # get post title
post_url = post["link"] # get post link
post_image = post['thumbnail'] #get post thubnail
if post_image != '': # we are not publishing post without image
a = urlparse(post_image)
file_name = os.path.basename(a.path) # retrive image from url and save it to local
if os.path.exists(file_name) == False and file_name != '': # check for file to avoid multiple tweet of same post
r = requests.get(post_image)
open(file_name, 'wb').write(r.content) # saving image into file
post_title = post_title+" \n "+post_url+" \n @harendraverma2" # Post title and link for tweet
post_result = api.update_status(post_title) # publishing tweet to twitter
print("Done")
medium_url = "https://api.rss2json.com/v1/api.json?rss_url=https://medium.com/feed/@harendraverma21";
medium(medium_url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment