annotate pyikriam/lazy/www/work/fetch.py @ 68:4ba1e981716d

merged kevint and hychen's work.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Wed, 22 Oct 2008 06:24:39 +0800
parents a4c364888197
children 60c4b4b78a01
rev   line source
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
1 import urllib2
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
2 import urllib
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
3 import cookielib
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
4 import os
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
5
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
6 def install_opener(cookiefile):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
7 COOKIEFILE = cookiefile
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
8 cj = cookielib.LWPCookieJar()
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
9 if os.path.isfile(COOKIEFILE):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
10 cj.load(COOKIEFILE)
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
11 else:
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
12 cj.save(cookiefile)
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
13 opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
14 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')]
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
15 urllib2.install_opener(opener)
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
16
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
17 class Fetcher:
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
18
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
19 opener = None
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
20
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
21 working_product = None
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
22
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
23 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
24 A Semi Production Decoration for content fetching.
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
25
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
26 handles content restriving.
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
27
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
28 >>> o = Fetcher( SemiProduct(source="http://localhost:8080") )
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
29 >>> o.get().working_product.content
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
30 'It works!!\\n'
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
31 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
32 def __init__(self, working_product):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
33 self.working_product = working_product
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
34
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
35 def get(self, data = {}):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
36 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
37 send datas via http get method.
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
38 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
39 res = urllib2.urlopen(self.working_product.source, urllib.urlencode(data))
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
40 return res.read()
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
41
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
42 def post(self, data = {} ):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
43 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
44 send datas via http post method.
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
45
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
46 >>> o = Fetcher( SemiProduct(source="http://localhost:8080") )
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
47 >>> o.post({'a':'b'}).working_product.content
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
48 'It works!!\\n'
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
49 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
50 res = urllib2.urlopen(self.working_product.source, urllib.urlencode(data))
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
51 return res.read()
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
52
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
53 def refer(self, refer_url):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
54 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
55 refer getter/setter.
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
56
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
57 >>> o = Fetcher( SemiProduct(source="http://localhost:8080") )
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
58 >>> o.refer('http://www.example.com')
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
59 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
60 raise NotImplementedError
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
61
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
62 def retry(self, count = 0, intval = 0, timeout = 0):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
63 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
64 retry to fetch the content.
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
65
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
66 >>> o = Fetcher( SemiProduct(source="http://localhost:8080") )
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
67 >>> o.retry(4)
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
68 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
69 raise NotImplementedError
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
70
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
71 class Retry:
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
72
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
73 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
74 A Fetcher Decoration for retry goal.
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
75
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
76
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
77 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
78 def __init__(self, fetcher):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
79 raise NotImplementedError
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
80
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
81 if __name__ == '__main__':
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
82 import doctest
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
83 doctest.testmod()