Image 01 Image 02

twitter action code

Posted on 2nd December 2007 by Taggy
6

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
Responses to.. twitter action code

[...] 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/ [...]



http://theyagar.com posted on December 2nd 2007

test message



kraiger22 » twitter action code posted on December 14th 2007

[...] 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={ … [...]



[...] 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 [...]



Enrique Allen posted on May 23rd 2008

Thanks for sharing, always grateful.

Any suggestions for simply exporting all twitter messages with keyword?



Enrique Allen posted on May 23rd 2008

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



Leave a reply...