Python script to check Twitter

Updated article here.

Micro-blogging is all the rage now and lucky for me, the local food trucks use it. There is a local spot that they like to come and they post on Twitter when they will be serving. I do not have a Twitter account, nor do I want one, but I still want to see when a truck is coming without having to religiously check the Twitter page.

In comes my favorite programming language, Python, to save the day! A quick script using and I now automatically get new tweets emailed to me. The epitome of laziness, I know. I used a simple, but popular python Twitter api that is common in most distros, python-twitter.

Its really as simple as:

import twitter
api = twitter.Api()
statuses = api.GetUserTimeline(user)

I then identify any new tweets by their ID and grab their text. I also make sure not to include any annoying retweets with a basic regex.

not re.match("^RT", tweet.text)

text

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
 
#===================================================
# Checks a twitter feed for new posts, if there is
# a new one it will email you.
#
# Requires a curernt python-twitter package with
# GetSearch() support and smtplib.
#===================================================
 
SERVER = 'localhost'
FROM = 'westvalleytruck@arete.com'
TO = ['cpowell@arete.com'] # Must be a list
 
import twitter
import re
import smtplib
 
client = twitter.Api()
 
# Get the last tweet ids we mailed.
try:
    f = open('tc.ini', 'r')
    last_id = f.readline()
    last_search_id = f.readline()
    f.close()
except:
    last_id = 0
    last_search_id = 0
 
# Get the latest tweets
tweet_list = []
new_tweet = False
posts = client.GetUserTimeline("westvalleytruck")
search = client.GetSearch('9200 Oakdale')
 
def check_tweet(tweet, id):
    ''' Checks for new tweets and removes retweets. '''
    global new_tweet
    if tweet.id > id and not re.match("^RT", tweet.text):
        tweet_list.append(tweet.text)
        new_tweet = True
 
# Check tweets and update with new ones
for tweet in posts:
    check_tweet(tweet, last_id)
 
for tweet in search:
    check_tweet(tweet, last_search_id)
 
SUBJECT = "Food Trucks at WVCC"
message = """
From: %s
To: %s
Subject: %s
 
New Message from Food Trucks at WVCC!!
 
%s
""" % (FROM, ', '.join(TO), SUBJECT, 'nn'.join(tweet_list))
 
if new_tweet:
    # Set the last tweet id so we don't repeat any tweets next time
    f = open('tc.ini', 'w')
    f.write(str(posts[0].id) + 'n')
    f.write(str(search[0].id) + 'n')
    f.close()
 
    # Now send email
    server = smtplib.SMTP(SERVER)
    server.sendmail(FROM, TO, message)
    server.quit()

2 thoughts on “Python script to check Twitter

  1. Pingback: UPDATED: Python script to check Twitter

  2. Pingback: UPDATED: Python script to check Twitter | Powell Technical Consulting

Leave a Reply

Your email address will not be published. Required fields are marked *