annotate pyikriam/lazy/www/__init__.py @ 79:9d92e8c12f58

rewrited the code in XPath.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Fri, 24 Oct 2008 21:46:33 +0800
parents a4c364888197
children 60c4b4b78a01
rev   line source
62
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
1 # -*- coding: utf-8 -*-
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
2 #
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
3 # Copyright 2008 Hsin Yi, Chen
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
4 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
5 [Note] the project is not available yet.
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
6
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
7 A web page fetcing tool chain that has a JQuery-like selector and supports chain working.
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
8
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
9 Here is an exmaple can show the the main idea, To restrive a content you want
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
10 in a div box in a web page, and then post and restrive next content in the other
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
11 web page with the param you just maked from the content in first restriving.
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
12 finally, storage the production.
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
13
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
14 def func(s):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
15 return {'msg':s}
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 try:
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
18 c("http://example.tw/").get().find("#id > div") \
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
19 .build_param( func ).post_to("http://example2.com") \
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
20 .save_as('hellow.html')
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
21 except:
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
22 pass
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 more complex example
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 try:
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
27 c("http://example.tw/").retry(4, '5m').get() \
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
28 .find("#id > div"). \
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
29 .build_param( func ).post_to("http://example2.com") \
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
30 .save_as('hellow.html') \
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
31 .end().find("#id2 > img").download('pretty-%s.jpg'). \
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
32 tar_and_zip("pretty_girl.tar.gz")
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
33 except NotFound:
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
34 print "the web page is not found."
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
35 except NoPermissionTosave:
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
36 print "the files can not be save with incorrect permission."
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
37 else:
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
38 print "unknow error."
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
39 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
40 from lazy.www.work import WorkFlow
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
41 from lazy.www.work.fetch import Fetcher, install_opener
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
42 from lazy.www.core import SemiProduct
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 def c(url):
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 connect to a web apge
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
47
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
48 >>> c('http://localhost:8080').get().worker.working_product.content
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
49 'It works!!\\n'
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
50
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
51 >>> c('http://localhost:8080').get().find('//text()')
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
52 'It works!!\\n'
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
53 """
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
54 s= SemiProduct(source=url)
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
55 w = WorkFlow(Fetcher(s))
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
56 return w
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
57
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
58 def lz_install(**kwds):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
59 if('opener' == kwds.get('name')):
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
60 install_opener(kwds.get('cookiefile'))
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 if __name__ == '__main__':
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
63 import doctest
a4c364888197 add ikriam game binding lib python version
hychen@mluna
parents:
diff changeset
64 doctest.testmod()