UPDATED: Python script to check Twitter

Ok, so it took all of about 10 minutes for me want to change something on my previous script. The functionality was there, but the plain text emails were horribly ugly. So I re-worked the email to use MIME html messages that are much prettier along with alternate plain text. New code below.

Nice, pretty formatted message.
Nice, pretty formatted message.

Nice, pretty formatted message.

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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/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'
to = 'cpowell@arete.com' # Must be a list
subject = 'Food Trucks at WVCC'
sender = 'westvalleytruck@arete.com'
title = 'New Tweet from the Food Trucks'
 
import twitter
import re
import smtplib
 
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
 
client = twitter.Api()
 
# Get the last tweets 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)
 
# Create message container
msg = MIMEMultipart('alternative')
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = to
 
text = """
From: %s
To: %s
Subject: %s
 
Parsed from:
 
New Message from Food Trucks at WVCC!!
 
%s
""" % (sender, ', '.join(to), subject, 'nn'.join(tweet_list))
 
html = '''
<h2><span style="text-decoration: underline;">%s</span></h2>
<span style="color: #3366cc; font-family: trebuchet ms;"><span style="color: #3366cc; font-family: trebuchet ms;">
      <img style="float: right;" alt="Logo" src="https://twimg0-a.akamaihd.net/profile_images/1598282882/westvalleycorporate.jpg" />
''' % (title)
for tweet in tweet_list:
    html = html + '</span></span>
%s
<span style="color: #3366cc; font-family: trebuchet ms;">' % tweet
html = html + '</span>'
 
part1 = MIMEText(text, 'plain')
part2 = MIMEText(html, 'html')n/y
msg.attach(part1)
msg.attach(part2)
 
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(sender, to, msg.as_string())
    server.quit()
else:
    print 'No new tweets.'

One thought on “UPDATED: Python script to check Twitter

  1. Pingback: Python script to check Twitter

Leave a Reply

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