twitter action code

i was planning to put this one last week when i released meninblue but thanks to exam i just got time now .

It was basically a bot that would give you the latest commentary of india’s cricket matches on your twitter and it was written in python and using python-twitter .

First the libraries you will need

GNU/Linux or its variants preferred, python,urllib,urllib2,python-twitter and sgmlib and also minidom for xml parsing .

Twitter provides you with a very easy api to work with .posting a message is just two lines.

Here is how i have tried to go about my task .

Choose the main page where you get the list of all ongoing matches and i chose wap version of cricinfo since its easier to parse with minimal html and also their html is a little complex to parse with so many iframes .

i use the sgmllib to parse the html ,the code snippet is below

def get_contents(url):
import urllib, sgmllib


f=urllib.urlopen(”http://ci.plusmo.com/cricket/wap”)
s = f.read()
print “Read”
myparser = MyParser()
myparser.parse(s)
a=myparser.get_hyperlinks()
b=myparser.get_descriptions()
wget_command = “wget -p –output-document=index.html “+url
system(wget_command);
print wget_command
f=open(”$CWD/index.html”) #change the $CWD to what dir u have
filer=f.read()
strnew= filer.replace(’ ‘,’ ‘)
strnew1=strnew.replace(’ ‘,”)
findex=strnew1.find(’<div class=”dat”>’)
findex1=strnew1.find(’</div>’,findex+1,UP_LIMIT)
newindex=strnew1.find(’</div>’,findex1+1,UP_LIMIT_1)
newindex2=strnew1.find(’</div>’,newindex+1,NEW_LIMIT)
finalmesg=strnew1[findex:newindex2]
s=Stripper()
s.feed(finalmesg)
score=s.gettext()
score=score.replace(’<’,”)
score=score.replace(’>’,”)
score=score.replace(’div’,”)
print score
print “SCRE OBTAINED”
check(score)

And now the parser class

class MyParser(sgmllib.SGMLParser):
def parse(self, s):
self.feed(s)
self.close()
def __init__(self, verbose=0):
sgmllib.SGMLParser.__init__(self, verbose)
self.hyperlinks = []
self.descriptions = []
self.inside_a_element = 0


def start_a(self, attributes):
for name, value in attributes:
if name == “href”:
self.hyperlinks.append(value)
self.inside_a_element = 1


def end_a(self):
self.inside_a_element = 0


def handle_data(self, data):
if self.inside_a_element:
self.descriptions.append(data)


def get_hyperlinks(self):
return self.hyperlinks

def get_descriptions(self):

for i in range(len(self.descriptions)):
str=self.descriptions[i]
if str.find(”India”) != -1:
index=i

return self.descriptions

The next stage is to strip all html and get the full content which we can post :-)


class Stripper(sgmllib.SGMLParser):
def __init__(self):
self.data = []
sgmllib.SGMLParser.__init__(self)
def unknown_starttag(self, tag, attrib):
self.data.append(” “)
def unknown_endtag(self, tag):
self.data.append(” “)
def handle_data(self, data):
self.data.append(data)
def gettext(self):
text = string.join(self.data, “”)
return string.join(string.split(text))

And see if the last posted message was the same as the message you have ,if yes then abort posting and wait for a update to be done ( mostly for script when its running while no match is on or after play)


def check(score):
settings={
“username”:”YOURUSERNAME”,
“statusdelimeter1″:”<p class=\”entry-title entry-content\”>”,
“statusdelimeter2″:”</p>”
}



url1=”http://twitter.com/”+settings["username"]
message=”"
print url1
statusdelimeter1=settings["statusdelimeter1"]
statusdelimeter2=settings["statusdelimeter2"]
try:
print “In to the message checkloop”
instream=urllib.urlopen(url1)
for line in instream:
if statusdelimeter1 in line:
message=line[
line.index(statusdelimeter1)+len(statusdelimeter1):line.index(statusdelimeter2)
]


print message
print score
if string.lower(message)==string.lower(score):


print “No change in Score ,May be break or match just got over”
else:
if len(score)>140:
print “LEN Greater than 140,so truncating”
newscore=”
newscore=score[:140]
print newscore
twitter(newscore)

else:
twitter(score)


except IOError, e:
raise “Could not connect.”

And finally when you have the message to be sent ,just use a few lines of twitter library and its done !

def twitter(score):



import sys,os,string
sys.path.append(’$CWD/twitter/’)
sys.path.append(’$CWDsimplejson/’)
import twitter
api=twitter.Api(username=’YOUR_USERNAME’,password=’YOURPASS’)
try:
status=api.PostUpdate(score)
print “POST SUCCESSFULL”
except IOError,e:
raise”Could not Post”

Thats all the code is about :-)

check out 2s birthday update bot :-)

UPDATE: The feed and scores were taken from Plusmo services .

Z Plusmo2 Icon

6 comments ↓

#1 Social Sites News » Blog Archive » twitter action code GERMANY WordPress wordpress on 12.02.07 at 12:26 am

[...] twitter action codeBy TaggyIt was basically a bot that would give you the latest commentary of india's cricket matches on your twitter and it was written in python and using python-twitter . First the libraries you will need. GNU/Linux or its variants preferred, …It’s My Life - http://www.theyagar.com/ [...]

Reply - http://www.theyagar.com/ [...]‘); return false;”>Quote
#2 http://theyagar.com INDIA Linux Mozilla Firefox 2.0.0.10 on 12.02.07 at 11:05 am

test message

#3 kraiger22 » twitter action code WordPress 1.0.2 on 12.14.07 at 10:30 am

[...] Check it out! While looking through the blogosphere we stumbled on an interesting post today.Here’s a quick excerptAnd see if the last posted message was the same as the message you have ,if yes then abort posting and wait for a update to be done ( mostly for script when its running while no match is on or after play). def check(score): settings={ … [...]

#4 A Great List Of Twitter Applications — Largest Social Networking Blog UNITED STATES WordPress 2.3.1 on 02.23.08 at 4:23 pm

[...] action on twitter Check out the code at Twitter Action Code follow all the indian team’s cricket scores from your mobile with plusmo cricket [...]

#5 Enrique Allen UNITED STATES Mac OS X Mozilla Firefox 2.0.0.14 on 05.23.08 at 11:51 am

Thanks for sharing, always grateful.

Any suggestions for simply exporting all twitter messages with keyword?

#6 Enrique Allen UNITED STATES Mac OS X Mozilla Firefox 2.0.0.14 on 05.23.08 at 2:39 pm

Cool just found this: http://bradkellett.com/tweetdumpr/

Reply - http://bradkellett.com/tweetdumpr/‘); return false;”>Quote

Leave a Comment