diff pyikriam/__init__.py @ 246:60c4b4b78a01

code clean
author "Hisn Yi, Chen <ossug.hychen@gmail.com>"
date Mon, 01 Dec 2008 00:25:07 +0800
parents 6ab01f709650
children
line wrap: on
line diff
--- a/pyikriam/__init__.py	Fri Nov 14 02:05:48 2008 +0800
+++ b/pyikriam/__init__.py	Mon Dec 01 00:25:07 2008 +0800
@@ -1,3 +1,94 @@
-from ikariam import Ikariam
+from lazy.www import c
+from lconf import LoadConfigfile
+import cookielib
+import os
+import urllib2
+import urllib
+class Ikariam:
+
+    cities = {}
+
+    def __init__(self, DEBUG=False):
+        self.COOKIEFILE = '/tmp/ikariam.lwp'
+        self.confdata=LoadConfigfile().cd
+        #self.baseurl='http://'+self.confdata['server']
+        if DEBUG:
+            self.debug = True
+            self.baseurl = self.debug_url()
+        else:
+            self.baseurl = 'http://s4.ikariam.tw'
+            self.cj = cookielib.LWPCookieJar()
+            if os.path.isfile(self.COOKIEFILE):
+                self.cj.load(self.COOKIEFILE)
+     
+            opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cj))
+            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')]
+            urllib2.install_opener(opener)
+    
+            self.login()
+
+    def debug_url(self):
+        """
+        get the path of testing datas.
+        >>>
+        
+        @return: file path with scheme 'file://'
+        """
+        return 'file://'+ os.path.abspath(os.path.curdir) + '/tests'
 
-__all__ = ('Ikariam',)
+    def login(self):     
+        print "login to %s...." % self.confdata['server']
+        params = {"universe":self.confdata['server'], \
+        "name":self.confdata['user'], \
+        "password":self.confdata['pass']}
+        ret = c(self.baseurl+'/index.php?action=loginAvatar&function=login').get(params).get_content()
+        self.cj.save(self.COOKIEFILE)
+        
+    def logout(self):
+        print "logut from %s...." % self.confdata['server']
+        c(self.baseurl+'/index.php?action=loginAvatar&function=logout')
+        os.remove(self.COOKIEFILE)
+        
+    def find_number_in(self, html, xpath):    
+        return parse_to_int(self.find_data_in(html, xpath))
+    
+    def find_data_in(self, work, xpath):
+        if work is None or xpath is None:   return
+        return work.find(xpath)
+    
+    def city(self, id):
+	       return self.cities.get(id, IkariamCity(id=id, core=self) )
+    
+class IkariamCity:
+    
+    def __init__(self, id, core ):
+        self.core = core
+        self.id = id
+        self.params = {'view':'city','id':id}
+        self.resources = {'gold':0,'wood':0,'wine':0,'marble':0,'crystal':0,'sulfur':0}
+        
+        # define xpath queries
+        self.xpath = {
+                      'gold':"/html/body[@id='city']/div[@id='container']/div[@id='container2']/div[@id='globalResources']/ul/li[2]/a/span[@id='value_gold']/text()",
+                      'wood':"/html/body[@id='city']/div[@id='container']/div[@id='container2']/div[@id='cityResources']/ul/li[3]/span[@id='value_wood']/text()",
+                      'wine':"/////div[@id='cityResources']/ul/li[4]/span[@id='value_wine']/text()",
+                      'marble':"/////div[@id='cityResources']/ul/li[4]/span[@id='value_wine']/text()",
+                      'crystal':"/////div[@id='cityResources']/ul/li[4]/span[@id='value_wine']/text()",
+                      'sulfur':"/////div[@id='cityResources']/ul/li[4]/span[@id='value_wine']/text()"
+                      }
+        
+    def sync(self):
+        print "pull datas of the city %s" % self.id
+                                
+        if self.core.debug:
+            work = c(self.core.baseurl+'/viewcity.html').get()
+        else:
+            work = c(self.core.baseurl).get(self.params)
+            
+        #print work.find(self.xpath['gold']).get_content()
+        print work.find(self.xpath['wood']).get_content()
+        print work.find(self.xpath['wood']).get_content()
+        
+def parse_to_int( str ):    pass
+   # print str[0]
+   # if str: return int( str.strip().replace(',',''))
\ No newline at end of file