Mercurial > eagle-eye
comparison pyikb/ikariam.py @ 60:610bbe1824ef
basic Python login script
author | kevin@localhost.localdomain |
---|---|
date | Tue, 21 Oct 2008 16:14:45 +0800 |
parents | |
children | 1c42ae140ad3 |
comparison
equal
deleted
inserted
replaced
59:b40f87f16263 | 60:610bbe1824ef |
---|---|
1 #!/usr/bin/python | |
2 # -*- coding: utf-8 -*- | |
3 import os,sys,re,string | |
4 import cookielib,urllib2,urllib # for urlencode | |
5 import time | |
6 from sgmllib import SGMLParser | |
7 | |
8 class ContentParser(SGMLParser): | |
9 def __init__(self): | |
10 SGMLParser.__init__(self) | |
11 self.anchor = {'link':'', 'title':''} | |
12 self.anchorlist = [] | |
13 self.liattr={} | |
14 self.inside_elements=['site'] | |
15 self.pat=re.compile('\r|\t|\n') | |
16 | |
17 def start_a(self, attributes): | |
18 """For each anchor tag, pay attention to the href and title attributes.""" | |
19 href, title = '', '' | |
20 for name, value in attributes: | |
21 if name.lower() == 'href': href = value | |
22 if name.lower() == 'title': title = value | |
23 self.anchor['link'] = href | |
24 self.anchor['title'] = title | |
25 self.inside_elements.append('anchor') | |
26 | |
27 def end_a(self): | |
28 self.anchorlist.append(self.anchor) # store the anchor in a list | |
29 self.anchor = {'link':'', 'title':''} # reset the dictionary, | |
30 self.inside_elements.pop() | |
31 | |
32 def handle_data(self, text): | |
33 if self.inside_elements[-1]=='anchor': | |
34 self.anchor['title'] = text | |
35 if self.inside_elements[-1]=='li': | |
36 text=self.pat.sub(' ',text) | |
37 text=string.split(text," ") | |
38 if self.liattcl in self.liattr: | |
39 self.liattr[self.liattcl]=self.liattr[self.liattcl]+text | |
40 else: | |
41 self.liattr[self.liattcl]=text | |
42 | |
43 def start_li(self,attributes): | |
44 self.liattcl='' | |
45 attrs = dict(attributes) | |
46 if attrs.has_key('class'): | |
47 self.liattcl=attrs['class'] | |
48 self.inside_elements.append('li') | |
49 | |
50 def end_li(self): | |
51 if self.inside_elements[-1]=='li': | |
52 self.inside_elements.pop() | |
53 | |
54 | |
55 class connection(object): | |
56 def __init__(self): | |
57 self.page='' | |
58 self.server='s2.ikariam.tw' | |
59 self.baseurl='http://'+self.server | |
60 self.COOKIEFILE = '/tmp/ikcookies.lwp' | |
61 self.cj = cookielib.LWPCookieJar() | |
62 if os.path.isfile(self.COOKIEFILE): | |
63 self.cj.load(self.COOKIEFILE) | |
64 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj)) | |
65 opener.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-TW; rv:1.8.1.12pre) Gecko/20071220 BonEcho/2.0.0.12pre')] | |
66 urllib2.install_opener(opener) | |
67 | |
68 def login(self): | |
69 if not os.path.isfile(self.COOKIEFILE): | |
70 print "create cookie file"+self.COOKIEFILE | |
71 # /index.php?action=loginAvatar&function=login | |
72 params = {"universe":self.server, "name":'username', "password":'passwd'} | |
73 data = urllib.urlencode(params) | |
74 self.page=urllib2.urlopen(self.baseurl+'/index.php?action=loginAvatar&function=login',data).read() | |
75 self.cj.save(self.COOKIEFILE) | |
76 return 1 | |
77 | |
78 def parser(self): | |
79 parser=ContentParser() | |
80 parser.feed(self.page) | |
81 parser.close | |
82 for x in parser.liattr.keys(): | |
83 print x,parser.liattr[x] | |
84 #parser.anchorlist: | |
85 | |
86 def logout(self): | |
87 logout=urllib2.urlopen(self.baseurl+'/index.php?action=loginAvatar&function=logout').read() | |
88 os.remove(self.COOKIEFILE) | |
89 return 1 | |
90 | |
91 def plunder(self): | |
92 '/index.php?view=plunder&destinationCityId=1978' | |
93 | |
94 | |
95 def upgradetest(self): | |
96 urllib2.urlopen(self.baseurl+'/index.php?view=academy&id=117257&position=9').read() | |
97 params = {"action":'CityScreen', \ | |
98 "function":'upgradeBuilding', \ | |
99 "id":'117257',\ | |
100 "position":'9',\ | |
101 "level":'7',\ | |
102 "oldView":'academy'} | |
103 print urllib2.urlopen(self.baseurl+'/index.php?view=townHall&id=117257&position=0#upgrade',urllib.urlencode(params)).read() | |
104 return 1 | |
105 | |
106 def help(): | |
107 print ("Usage: %s [Option] [Channel] [second]") % os.path.basename(sys.argv[0]) | |
108 print ("Option: ") | |
109 helplist=[ | |
110 ("-h","--help","show this usage message."), | |
111 ("-g","--game","Login to the game") | |
112 ] | |
113 helplist.sort() | |
114 for x in helplist: | |
115 print ("\t%2s, %-25s %s" % x) | |
116 | |
117 if __name__=='__main__': | |
118 if len(sys.argv) == 1: | |
119 help() | |
120 sys.exit(2) # common exit code for syntax error | |
121 else: | |
122 arglist=sys.argv[1:] | |
123 if arglist[0] in ('--game','-g'): | |
124 gc=connection() | |
125 gc.login() | |
126 gc.parser() | |
127 gc.logout() | |
128 | |
129 |