annotate pyikb/ikariam.py @ 172:ba03aa8abdb2

reading Tavern's infomration
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sun, 02 Nov 2008 00:30:02 +0800
parents 1c42ae140ad3
children
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
63
1c42ae140ad3 add Parser.py and lconf.py.
kevin@localhost.localdomain
parents: 60
diff changeset
6 from lconf import LoadConfigfile
1c42ae140ad3 add Parser.py and lconf.py.
kevin@localhost.localdomain
parents: 60
diff changeset
7 from Parser import ContentParser
60
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
8
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
9 class connection(object):
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
10 def __init__(self):
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
11 self.page=''
63
1c42ae140ad3 add Parser.py and lconf.py.
kevin@localhost.localdomain
parents: 60
diff changeset
12 self.confdata=LoadConfigfile().cd
1c42ae140ad3 add Parser.py and lconf.py.
kevin@localhost.localdomain
parents: 60
diff changeset
13 self.baseurl='http://'+self.confdata['server']
60
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
14 self.COOKIEFILE = '/tmp/ikcookies.lwp'
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
15 self.cj = cookielib.LWPCookieJar()
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
16 if os.path.isfile(self.COOKIEFILE):
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
17 self.cj.load(self.COOKIEFILE)
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
18 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
19 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
20 urllib2.install_opener(opener)
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
21
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
22 def login(self):
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
23 if not os.path.isfile(self.COOKIEFILE):
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
24 print "create cookie file"+self.COOKIEFILE
63
1c42ae140ad3 add Parser.py and lconf.py.
kevin@localhost.localdomain
parents: 60
diff changeset
25 params = {"universe":self.confdata['server'], \
1c42ae140ad3 add Parser.py and lconf.py.
kevin@localhost.localdomain
parents: 60
diff changeset
26 "name":self.confdata['user'], \
1c42ae140ad3 add Parser.py and lconf.py.
kevin@localhost.localdomain
parents: 60
diff changeset
27 "password":self.confdata['pass']}
1c42ae140ad3 add Parser.py and lconf.py.
kevin@localhost.localdomain
parents: 60
diff changeset
28
60
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
29 data = urllib.urlencode(params)
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
30 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
31 self.cj.save(self.COOKIEFILE)
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
32 return 1
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
33
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
34 def parser(self):
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
35 parser=ContentParser()
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
36 parser.feed(self.page)
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
37 parser.close
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
38 for x in parser.liattr.keys():
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
39 print x,parser.liattr[x]
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
40 #parser.anchorlist:
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
41
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
42 def logout(self):
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
43 logout=urllib2.urlopen(self.baseurl+'/index.php?action=loginAvatar&function=logout').read()
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
44 os.remove(self.COOKIEFILE)
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
45 return 1
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
46
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
47 def plunder(self):
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
48 '/index.php?view=plunder&destinationCityId=1978'
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
49
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
50
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
51 def upgradetest(self):
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
52 urllib2.urlopen(self.baseurl+'/index.php?view=academy&id=117257&position=9').read()
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
53 params = {"action":'CityScreen', \
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
54 "function":'upgradeBuilding', \
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
55 "id":'117257',\
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
56 "position":'9',\
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
57 "level":'7',\
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
58 "oldView":'academy'}
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
59 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
60 return 1
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
61
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
62 def help():
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
63 print ("Usage: %s [Option] [Channel] [second]") % os.path.basename(sys.argv[0])
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
64 print ("Option: ")
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
65 helplist=[
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
66 ("-h","--help","show this usage message."),
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
67 ("-g","--game","Login to the game")
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
68 ]
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
69 helplist.sort()
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
70 for x in helplist:
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
71 print ("\t%2s, %-25s %s" % x)
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
72
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
73 if __name__=='__main__':
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
74 if len(sys.argv) == 1:
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
75 help()
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
76 sys.exit(2) # common exit code for syntax error
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
77 else:
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
78 arglist=sys.argv[1:]
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
79 if arglist[0] in ('--game','-g'):
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
80 gc=connection()
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
81 gc.login()
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
82 gc.parser()
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
83 gc.logout()
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
84
610bbe1824ef basic Python login script
kevin@localhost.localdomain
parents:
diff changeset
85