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