Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
This script reads pre-formatted tweets from a text file and posts them at defined intervals. It uses the Twython library (wrapper for Twitter API). More information at
import datetime
import time
import re
import os
from twython import Twython
import sys
import traceback
import httplib, urllib #used in the Pushover code
#sys.stdout = open('twitter.log', 'a') #Outputs to file instead of Standard Output.
# Path to Dropbox Sync Folder
folderPath = '/home/USERNAME/Dropbox/Scripts/Twitter/'
folderAttach = '/home/USERNAME/Dropbox/Scripts/Twitter/attach/'
schFile = 'TweetSchedule.txt'
#Posts only if current time is within 59 min of predefined post time in text file.
#The script has been setup in crontab to run every hour.
postInterval = 3540
# Twitter Credentials
def pushover(msg):
conn = httplib.HTTPSConnection("")
conn.request("POST", "/1/messages.json",
"token": "'INCLUDE_YOURS_HERE'",
"message": msg,
}), { "Content-type": "application/x-www-form-urlencoded" })
def postTweets(tweetText, tweetAttach):
twitter = Twython(App_Key, App_Secret, Oauth_Token, Oauth_Token_Secret)
if tweetAttach=='':
print 'Successfully tweeted!'
completeAttPath = folderAttach+tweetAttach
attachment=open(completeAttPath, 'rb')
response = twitter.upload_media(media=attachment)
twitter.update_status(media_ids=[response['media_id']], status=tweetText)
print 'Successfully tweeted!'
er = traceback.format_exc()
print 'Warning: tweet -> '+tweetText+'<- could not be tweeted'
print 'Error:\n'+er
msg = 'Tweet: '+tweetText+'\nError: '+er[-180:]
def isItTime(freq): #receives string in the form XXX@XX:XX@XX:XX or XXXX-XX-XX@XX:XX@XX:XX per day
now =
postFlag = False
days = freq.split(",")
for day in days:
rightDay = False
times = day.split("@")
# Determining if it is the correct date/week day:
# ---
#takes 1st item in the list - which is the date/week day - and determines which type of date it is:
if re.match('^[0-9]{4}-[0-9]{2}-[0-9]{2}$',times[0]):
print 'Processing date in xxxx-xx-xx format'
if times[0]==now.strftime("%Y-%m-%d"):
rightDay = True
print 'It\'s the right day'
elif re.match('^[a-zA-Z]{3}$',times[0]):
print 'Processing date in XXX format'
if times[0]==now.strftime("%a"): #Weekday's as local's abbreviated name: Mon, Tue, etc.
rightDay = True
print 'It\'s the right day'
print times[0]+' -> Wrong date format!'
# Determining if it is the correct time:
# ---
if rightDay:
for time in times[1:]: #doesn't iterate over the 1st element which is the day
if re.match('^[0-9]{1,2}:[0-9]{2}',time):
nowStr = now.strftime("%H:%M") #Converts datetime object to string (so that it can be converted to a time object below)
postTime = datetime.datetime.strptime(time,'%H:%M') #Converts the string into a time object
nowTime = datetime.datetime.strptime(nowStr,'%H:%M')
diffTime = nowTime - postTime
diffTimeSeconds = diffTime.total_seconds()
if diffTimeSeconds>=0 and diffTimeSeconds<postInterval:
postFlag = True
print 'It\'s the right time'
print time+' -> Wrong time format!'
return postFlag
#Main Script
schTweets = open(folderPath+schFile,'rU') #IMPORTANT: rU Opens the file with Universal Newline Support, so \n and/or \r is recognized as a new line.
tweetList = schTweets.readlines()
print '\n%s Running script...' %"%Y-%m-%d %H:%M:%S")
for tweet in tweetList:
tweet = tweet.strip('\n')
tStatus, tText, tAttach, tTimes = tweet.split('|')
if tStatus=='a':
print 'Checking time interval '+tTimes
if isItTime(tTimes):
print 'Trying to post...'
postTweet(tText, tAttach)
print 'Not right time to tweet'
except ValueError:
print 'Incorrect line format. There might not be a | character in the line'

This comment has been minimized.

Copy link
Owner Author

contradirony commented Mar 26, 2017

Differences from original are:

  • for attaching image to tweet, update_status_with_media is deprecated - updated with upload_media instead
  • readability formatting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.