libGrouphug.py:
import string import httplib import sgmlStripper
def getRandomGrouphug():
connection = httplib.HTTPConnection("grouphug.us") connection.request("GET","/random") response = connection.getresponse() data = response.read()
startPos = string.find(data,'<td class=\"conf-text\">') + 22 data = data[startPos:] endPos = string.find(data,'</td>') data = data[:endPos]
stripper = sgmlStripper.Stripper()
##warning, two different strip functions here
#Note, this is a strip function from sgmlStripper to strip tags
hug = stripper.strip(data)
#this is the strip from string to strip beginning and trailing whitespace
hug = string.strip(hug)
return hug;
if __name__ == '__main__': print getRandomGrouphug()
ljPoster.py:
import time import urllib import urllib2
class ljPoster: """class to create an object that can log into and post on Livejournal.com and other sites which use the same code such as Deadjournal.com"""
def __init__(self,username,password,server="http://www.livejournal.com"): """constructor requires username and password"""
self.username = username
self.password = password
self.server = server
def post(self,subject,body): """Post to livejournal"""
#subject may only be 255 chars
subject = subject[:255]
#lets get the date in the tuple since LJ.com wants the local date or they won't post
dateTuple = time.localtime()
#lets create a dict
self.ljRequest = { 'user':self.username, 'password':self.password, 'mode':"postevent", 'lineendings':'\n', 'subject':subject, 'event':body, 'year':dateTuple[0], 'mon':dateTuple[1], 'day':dateTuple[2], 'hour':dateTuple[3], 'min':dateTuple[4] } #doing the actual posting
data = urllib.urlencode(self.ljRequest)
post = urllib2.urlopen("http://www.livejournal.com/interface/flat",data) return post
if __name__ == "__main__": """user should never invoke this as __main__ ..."""
print "This is a library"
sgmlStripper.py:
#SGML stripper class, inherited from SGMLParser
import sgmllib
class Stripper(sgmllib.SGMLParser):
def __init__(self): sgmllib.SGMLParser.__init__(self)
def strip(self, some_html): """Given a SGML/HTML string, strip tags"""
self.theString = ""
self.feed(some_html)
self.close()
return self.theString
def handle_data(self, data): """Given a string, append to Stripper object's current data"""
self.theString += data
adaptiveSystems.py:
import libGrouphug import ljPoster import time import random
"""Create a livejournal robot"""
#paste your LJ login info here
myLivejournal = ljPoster.ljPoster("AdaptiveSystems","mypassword")
while (1): myLivejournal.post(" ",libGrouphug.getRandomGrouphug())
|