changeset 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 c20912354d1d
files pyikriam/__init__.py pyikriam/example.py pyikriam/lazy/__init__.py pyikriam/lazy/www/README pyikriam/lazy/www/__init__.py pyikriam/lazy/www/core/__init__.py pyikriam/lazy/www/core/utils.py pyikriam/lazy/www/work/__init__.py pyikriam/lazy/www/work/fetch.py pyikriam/lazy/www/work/find.py
diffstat 9 files changed, 319 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pyikriam/__init__.py	Wed Oct 22 04:04:32 2008 +0800
@@ -0,0 +1,57 @@
+from lazy.www import c
+import cookielib
+import os
+import urllib2
+import urllib
+class Ikariam:
+
+    cities = {}
+
+    def __init__(self, server, username, password):
+        self.COOKIEFILE = '/tmp/ikariam.lwp'
+        self.server=server
+        self.baseurl='http://'+self.server
+
+        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(username, password)
+
+    def login(self,username,password):     
+	print "login to %s...." % self.server
+        params = {"universe":self.server, "name":username, "password":password}
+        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.server
+        c(self.baseurl+'/index.php?action=loginAvatar&function=logout')
+        os.remove(self.COOKIEFILE)
+        
+    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}
+        
+    def sync(self):
+	print "pull datas of the city %s" % self.id
+        xpath_globalinfo = "/html/body[@id='city']/div[@id='container']/div[@id='container2']/div[@id='globalResources']/ul"
+
+        xpath_gold = xpath_globalinfo + "/li[2]/a/span[@id='value_gold']/text()"
+        self.gold = c(self.core.baseurl).get(self.params).find(xpath_gold).get_content()[0]
+        
+if __name__ == '__main__':
+    i = Ikariam('hychen','pwdyouknow')
+    city = i.city(117261)
+    city.sync()
+    print city.gold
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pyikriam/example.py	Wed Oct 22 04:04:32 2008 +0800
@@ -0,0 +1,6 @@
+from __init__ import Ikariam
+
+i = Ikariam(server='s1.ikariam.tw', username='hychen', password='')
+city = i.city(117261)
+city.sync()
+print 'gold is'+city.gold
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pyikriam/lazy/www/README	Wed Oct 22 04:04:32 2008 +0800
@@ -0,0 +1,18 @@
+Requirements:
+
+	lxml - python libxml binding
+
+	it needs to installing the following packages before install lxml.
+
+    *  libxml 2.6.21 or later. It can be found here: http://xmlsoft.org/downloads.html
+    *  libxslt 1.1.15 or later. It can be found here: http://xmlsoft.org/XSLT/downloads.html
+		
+	If you use Ubuntu, here is what you need to do.
+	
+	$ apt-get install libxml2-dev libxslt1-dev
+	$ eazy_install lxml
+
+Example:
+
+	product = c('http://www.google.com.tw').find("/foo/bar/").working_prodcut
+	print product.content
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pyikriam/lazy/www/__init__.py	Wed Oct 22 04:04:32 2008 +0800
@@ -0,0 +1,64 @@
+# -*- coding: utf-8 -*-
+#
+# Copyright 2008 Hsin Yi, Chen
+"""
+    [Note] the project is not available yet.
+
+    A web page fetcing tool chain that has a JQuery-like selector and supports chain working.
+    
+    Here is an exmaple can show the the main idea, To restrive a content you want
+    in a div box in a web page, and then post and restrive next content in the other
+    web page with the param you just maked from the content in first restriving.
+    finally, storage the production.
+    
+    def func(s):
+        return {'msg':s}
+    
+    try:
+        c("http://example.tw/").get().find("#id > div") \
+            .build_param( func ).post_to("http://example2.com") \
+            .save_as('hellow.html')
+    except:
+        pass
+        
+    more complex example
+        
+    try:
+        c("http://example.tw/").retry(4, '5m').get() \
+            .find("#id > div"). \
+            .build_param( func ).post_to("http://example2.com") \
+            .save_as('hellow.html') \
+            .end().find("#id2 > img").download('pretty-%s.jpg'). \
+            tar_and_zip("pretty_girl.tar.gz")
+    except NotFound:
+        print "the web page is not found."
+    except NoPermissionTosave:
+        print "the files can not be save with incorrect permission."
+    else:
+        print "unknow error."
+"""
+from lazy.www.work import WorkFlow
+from lazy.www.work.fetch import Fetcher, install_opener
+from lazy.www.core import SemiProduct
+
+def c(url):
+    """
+    connect to a web apge
+    
+    >>> c('http://localhost:8080').get().worker.working_product.content
+    'It works!!\\n'
+    
+    >>> c('http://localhost:8080').get().find('//text()')
+    'It works!!\\n'    
+    """
+    s= SemiProduct(source=url)    
+    w = WorkFlow(Fetcher(s))
+    return w
+
+def lz_install(**kwds):
+    if('opener' == kwds.get('name')):
+       install_opener(kwds.get('cookiefile'))
+
+if __name__ == '__main__':
+    import doctest
+    doctest.testmod()
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pyikriam/lazy/www/core/__init__.py	Wed Oct 22 04:04:32 2008 +0800
@@ -0,0 +1,12 @@
+                          
+class SemiProduct:
+
+    last_work = None
+    source = None
+    content = None
+    
+    def __init__(self, **kwds):
+        self.source = kwds.get('source','')        
+        
+    def __str__(self):        
+        return self.content
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pyikriam/lazy/www/core/utils.py	Wed Oct 22 04:04:32 2008 +0800
@@ -0,0 +1,4 @@
+
+def mix_in(py_class, mixin_class):
+    if mixin_class not in py_class.__bases__:
+        py_class.__bases__ += (mixin_class,)
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pyikriam/lazy/www/work/__init__.py	Wed Oct 22 04:04:32 2008 +0800
@@ -0,0 +1,52 @@
+from lazy.www.work.fetch import Fetcher
+from lazy.www.work.find import Finder
+from lazy.www.core import SemiProduct
+class WorkFlow:
+    
+    serial_number = 0
+    working_product = None
+    worker = None
+    
+    def __init__(self, worker):
+        self.set_worker(worker)
+
+    def set_worker(self, worker):
+        self.worker = worker
+        if self.worker.working_product is None:
+            self.working_product = SemiProduct()
+        else:
+            self.working_product = self.worker.working_product
+    
+    def get_content(self):
+        return self.working_product.content
+     
+    def change_worker(self, new_worker):
+        self.serial_number += 1
+        self.worker = new_worker
+        
+    def is_fetcher(self, obj):
+        if  obj is not None:    return True
+    
+    def get(self, data = {} ):
+        if not self.is_fetcher(self.worker) :
+            self.change_worker( Fetcher(self.working_product) )
+        
+        self.working_product.content = self.worker.get(data)
+        return self
+            
+    def post(self, data = {} ):
+        if not self.is_fetcher(self.worker):
+            self.change_worker( Fetcher(self.working_product) )
+        
+        self.working_product.content = self.worker.post(data)
+        return self
+    
+    def is_finder(self, obj):
+        if obj is not None: return True
+    
+    def find(self, express):
+        #if not self.is_finder(self.worker):
+        self.worker = Finder(self.working_product)
+        self.working_product.content = self.worker.find(express)
+        
+        return self
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pyikriam/lazy/www/work/fetch.py	Wed Oct 22 04:04:32 2008 +0800
@@ -0,0 +1,83 @@
+import urllib2
+import urllib
+import cookielib
+import os
+
+def install_opener(cookiefile):
+    COOKIEFILE = cookiefile
+    cj = cookielib.LWPCookieJar()
+    if os.path.isfile(COOKIEFILE):
+        cj.load(COOKIEFILE)
+    else:
+        cj.save(cookiefile)
+        opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(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)
+
+class Fetcher:
+    
+    opener = None
+    
+    working_product = None
+    
+    """
+    A Semi Production Decoration for content fetching.
+    
+    handles content restriving.
+    
+    >>> o = Fetcher( SemiProduct(source="http://localhost:8080") )
+    >>> o.get().working_product.content
+    'It works!!\\n'
+    """
+    def __init__(self, working_product):
+        self.working_product = working_product
+        
+    def get(self, data = {}):
+        """        
+        send datas via http get method.
+        """        
+        res = urllib2.urlopen(self.working_product.source, urllib.urlencode(data))
+        return res.read()
+    
+    def post(self, data = {} ):
+        """
+        send datas via http post method.
+        
+        >>> o = Fetcher( SemiProduct(source="http://localhost:8080") )
+        >>> o.post({'a':'b'}).working_product.content
+        'It works!!\\n'
+        """        
+        res = urllib2.urlopen(self.working_product.source, urllib.urlencode(data))
+        return res.read()    
+
+    def refer(self, refer_url):
+        """
+        refer getter/setter.
+
+        >>> o = Fetcher( SemiProduct(source="http://localhost:8080") )
+        >>> o.refer('http://www.example.com')        
+        """
+        raise NotImplementedError
+
+    def retry(self, count = 0, intval = 0, timeout = 0):
+        """
+        retry to fetch the content.
+
+        >>> o = Fetcher( SemiProduct(source="http://localhost:8080") )
+        >>> o.retry(4)        
+        """        
+        raise NotImplementedError
+    
+class Retry:
+    
+    """
+     A Fetcher Decoration for retry goal.
+     
+     
+    """
+    def __init__(self, fetcher):
+        raise NotImplementedError
+    
+if __name__ == '__main__':
+    import doctest
+    doctest.testmod()
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/pyikriam/lazy/www/work/find.py	Wed Oct 22 04:04:32 2008 +0800
@@ -0,0 +1,23 @@
+from lxml import etree
+from cStringIO import StringIO
+
+class Finder:
+
+    dom_tree = None
+    xpath = None
+
+    def __init__(self, working_product):
+        self.working_prodcut = working_product
+
+        self.encoding = 'utf8'
+        parser = etree.HTMLParser(encoding=self.encoding)
+        self.dom_tree = etree.parse(StringIO(self.working_prodcut.content), parser)
+    
+    def find(self, express , callback = None):
+        xpath = self.dom_tree.xpath(express)
+        
+        if callback is None:
+            ret = xpath
+        else:
+            ret = self.callback(xpath)
+        return ret