# coding: utf-8
# getNewTweets.py: get username's new tweets since the last time this
# was run (as recorded in username-lastStatusDate.txt" file) and
# output them as an HTML file.
# 2009-04-09 changed it to track last read tweet's ID, not date
# getNewTweets.py uses the Python twitter interface available at
# http://code.google.com/p/python-twitter/, but the
# http://python-twitter.googlecode.com/files/python-twitter-0.5.tar.gz
# file available there has an out of date version of twitter.py--its
# getFriendsTimeLine method does not recognize the "count" or id
# parameters. After unzipping it, replace the twitter.py file there
# with http://python-twitter.googlecode.com/svn/trunk/twitter.py
# before running setup.py as described on the project home page.
# This used to track where it left off using the date of the last
# tweet, but 2009-04-08 that stopped working, and it's not documented
# as a parameter on
# http://apiwiki.twitter.com/REST+API+Documentation#statuses/friendstimeline,
# so I changed this to track by ID.
# copyright 2009 Bob DuCharme no warranty expressed or implied.
import os
import twitter
import re
username="dummyName"
password="dummyPassword"
def outputHTML(timeline):
print ''
print '
' \
% (username,timeline[0].created_at[0:16],countMsg)
# I tried to do it all with CSS, but a table is so much easier.
print '
'
for s in timeline:
# See
# http://static.unto.net/python-twitter/0.5/doc/twitter.html#Status
# for attributes of s
text = s.text.encode('utf-8')
# convert references to URLs into links
result = URLPattern.search(text)
if result != None:
URL = result.group('URL')
linkedURL = '\g'
text = URLPattern.sub(linkedURL,text)
# convert references to twitter IDs into links
result = userNamePattern.search(text)
if result != None:
uname = result.group('uname')
linkedName = '@\g'
text = userNamePattern.sub(linkedName,text)
# El: Element for element
textEl = ' ' + text + ''
date = s.created_at[0:16]
tweetID = str(s.id)
screenName = s.user.screen_name
userName = s.user.name.encode('utf-8')
userImgEl = ''
userNameEl = '%s' % (screenName,userName,screenName)
dateEl ='%s' \
% (screenName,tweetID,date)
# Make the reply URLs look like this:
# http://twitter.com/home?status=@ldodds%20&in_reply_to_status_id=1347886434&in_reply_to=ldodds
replyURL = "http://twitter.com/home?status=@%s%%20&in_reply_to_status_id=%s&in_reply_to=%s" % (screenName,tweetID,screenName)
replyEl = ' ←' % (replyURL)
# IRTEl: In Reply To Element
IRTToEl = ""
if s.in_reply_to_status_id != None:
IRTScreen_name = s.in_reply_to_screen_name
IRTUser_id = s.in_reply_to_user_id
IRTStatusID = s.in_reply_to_status_id
# model: http://twitter.com/georgebina/status/1350766281
IRTURL = "http://twitter.com/%s/status/%s" % (IRTScreen_name,IRTStatusID)
IRTToEl = '↵'
# Output the table row
print '
%s
' % (userImgEl)
print '
' + userNameEl + ' '
print textEl # I get errors if this isn't output by itself
print ' ' + dateEl + ' ' + replyEl + IRTToEl + '
'
print '
'
print ""
# Save the datetime stamp of the last message for next time we run this.
output = open(fn,'w')
output.write(tweetID)
output.close()
############# main ##############
if username == "dummyName":
print "Reset username and password variables before running this."
exit()
# file storing datetime stamp of last tweet read
homeDir = os.environ["HOME"]
fn = homeDir + "\\" + username + "-lastStatusDate.txt"
# patterns used to convert @username and URL references to links
URLPattern = re.compile("(?Phttps?://[a-zA-Z0-9\.\/\?\&\-\_\=\#\~]+)",re.I)
userNamePattern = re.compile(r"@(?P[a-zA-Z0-9-_]+)")
lastTweetID="0"
if (os.path.isfile(fn)):
fh = open(fn, "r")
lastTweetID = fh.read()
fh.close()
api = twitter.Api(username=username, password=password)
# 200 is the maximum amount you're allowed to return, but ideally
# there haven't been 200 tweets since lastTweetID.
timeline = api.GetFriendsTimeline(user=username,count=200,since_id=lastTweetID)
timeline.reverse() # the whole point of this app
# For the title header.
tweetCount = len(timeline)
if tweetCount == 1:
countMsg = "1 entry"
else:
countMsg = str(tweetCount) + " entries"
if tweetCount == 0:
print "No tweets since last check."
else:
outputHTML(timeline)