Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
# post -
import tweepy
import urllib
from urllib.request import urlretrieve
from urllib.parse import urlparse
import os
import requests
# Twitter keys -
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)
# 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
medium_url = "";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment