# HG changeset patch # User kevin@localhost.localdomain # Date 1224625257 -28800 # Node ID 1c42ae140ad384ad8815b4c3d88f4a8db168a1f5 # Parent a4c364888197c55e05626aff169415b7b9eb9847 add Parser.py and lconf.py. diff -r a4c364888197 -r 1c42ae140ad3 pyikb/Parser.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pyikb/Parser.py Wed Oct 22 05:40:57 2008 +0800 @@ -0,0 +1,51 @@ +#!/usr/bin/python +# -*- coding: utf-8 -*- +import re,string +from sgmllib import SGMLParser + +class ContentParser(SGMLParser): + def __init__(self): + SGMLParser.__init__(self) + self.anchor = {'link':'', 'title':''} + self.anchorlist = [] + self.liattr={} + self.inside_elements=['site'] + self.pat=re.compile('\r|\t|\n') + + def start_a(self, attributes): + """For each anchor tag, pay attention to the href and title attributes.""" + href, title = '', '' + for name, value in attributes: + if name.lower() == 'href': href = value + if name.lower() == 'title': title = value + self.anchor['link'] = href + self.anchor['title'] = title + self.inside_elements.append('anchor') + + def end_a(self): + self.anchorlist.append(self.anchor) # store the anchor in a list + self.anchor = {'link':'', 'title':''} # reset the dictionary, + self.inside_elements.pop() + + def handle_data(self, text): + if self.inside_elements[-1]=='anchor': + self.anchor['title'] = text + if self.inside_elements[-1]=='li': + text=self.pat.sub(' ',text) + text=string.split(text," ") + if self.liattcl in self.liattr: + self.liattr[self.liattcl]=self.liattr[self.liattcl]+text + else: + self.liattr[self.liattcl]=text + + def start_li(self,attributes): + self.liattcl='' + attrs = dict(attributes) + if attrs.has_key('class'): + self.liattcl=attrs['class'] + self.inside_elements.append('li') + + def end_li(self): + if self.inside_elements[-1]=='li': + self.inside_elements.pop() + diff -r a4c364888197 -r 1c42ae140ad3 pyikb/ikariam.py --- a/pyikb/ikariam.py Wed Oct 22 04:04:32 2008 +0800 +++ b/pyikb/ikariam.py Wed Oct 22 05:40:57 2008 +0800 @@ -3,60 +3,14 @@ import os,sys,re,string import cookielib,urllib2,urllib # for urlencode import time -from sgmllib import SGMLParser - -class ContentParser(SGMLParser): - def __init__(self): - SGMLParser.__init__(self) - self.anchor = {'link':'', 'title':''} - self.anchorlist = [] - self.liattr={} - self.inside_elements=['site'] - self.pat=re.compile('\r|\t|\n') - - def start_a(self, attributes): - """For each anchor tag, pay attention to the href and title attributes.""" - href, title = '', '' - for name, value in attributes: - if name.lower() == 'href': href = value - if name.lower() == 'title': title = value - self.anchor['link'] = href - self.anchor['title'] = title - self.inside_elements.append('anchor') - - def end_a(self): - self.anchorlist.append(self.anchor) # store the anchor in a list - self.anchor = {'link':'', 'title':''} # reset the dictionary, - self.inside_elements.pop() - - def handle_data(self, text): - if self.inside_elements[-1]=='anchor': - self.anchor['title'] = text - if self.inside_elements[-1]=='li': - text=self.pat.sub(' ',text) - text=string.split(text," ") - if self.liattcl in self.liattr: - self.liattr[self.liattcl]=self.liattr[self.liattcl]+text - else: - self.liattr[self.liattcl]=text - - def start_li(self,attributes): - self.liattcl='' - attrs = dict(attributes) - if attrs.has_key('class'): - self.liattcl=attrs['class'] - self.inside_elements.append('li') - - def end_li(self): - if self.inside_elements[-1]=='li': - self.inside_elements.pop() - +from lconf import LoadConfigfile +from Parser import ContentParser class connection(object): def __init__(self): self.page='' - self.server='s2.ikariam.tw' - self.baseurl='http://'+self.server + self.confdata=LoadConfigfile().cd + self.baseurl='http://'+self.confdata['server'] self.COOKIEFILE = '/tmp/ikcookies.lwp' self.cj = cookielib.LWPCookieJar() if os.path.isfile(self.COOKIEFILE): @@ -68,8 +22,10 @@ def login(self): if not os.path.isfile(self.COOKIEFILE): print "create cookie file"+self.COOKIEFILE - # /index.php?action=loginAvatar&function=login - params = {"universe":self.server, "name":'username', "password":'passwd'} + params = {"universe":self.confdata['server'], \ + "name":self.confdata['user'], \ + "password":self.confdata['pass']} + data = urllib.urlencode(params) self.page=urllib2.urlopen(self.baseurl+'/index.php?action=loginAvatar&function=login',data).read() self.cj.save(self.COOKIEFILE) diff -r a4c364888197 -r 1c42ae140ad3 pyikb/lconf.py --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pyikb/lconf.py Wed Oct 22 05:40:57 2008 +0800 @@ -0,0 +1,15 @@ +import os,string +class LoadConfigfile(object): + def __init__(self): + profile = os.environ["HOME"]+'/.eagleeye.pm' + self.cd={} + if os.path.isfile(profile): + print "Loading Config file." + cfile=open(profile,'r') + for line in cfile.xreadlines(): + if line[0:3]=='$::': + con=string.split(line[3:-2]) + self.cd[con[0]]=con[2][1:-1] + else: + print "File don't exist." +