annotate pyikb/ikariam.py @ 62:a4c364888197

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