HCE Project Python language Distributed Tasks Manager Application, Distributed Crawler Application and client API bindings.  2.0.0-chaika
Hierarchical Cluster Engine Python language binding
ftest_dc_fetchers.py
Go to the documentation of this file.
1 """
2 HCE project, Python bindings, Crawler application.
3 Fetcher module tests.
4 
5 @package: dc
6 @file ftest_dc_fetchers.py
7 @author bgv <bgv.hce@gmail.com>
8 @link: http://hierarchical-cluster-engine.com/
9 @copyright: Copyright &copy; 2015 IOIX Ukraine
10 @license: http://hierarchical-cluster-engine.com/license/
11 @since: 1.4.3
12 """
13 
14 import logging
15 import os
16 import json
17 import jsonpickle
18 
19 from app.Utils import varDump
20 from dc_crawler.Fetcher import BaseFetcher
21 
22 
23 # create logger
24 logger = logging.getLogger('ftest_dc_fetchers')
25 logger.setLevel(logging.DEBUG)
26 # create console handler and set level to debug
27 ch = logging.StreamHandler()
28 ch.setLevel(logging.DEBUG)
29 # create formatter
30 formatter = logging.Formatter('%(asctime)s - %(thread)ld - %(threadName)s - %(module)s - %(funcName)s - %(levelname)s - %(message)s')
31 # add formatter to ch
32 ch.setFormatter(formatter)
33 # add ch to logger
34 logger.addHandler(ch)
35 
36 
37 if __name__ == "__main__":
38  res = None
39 
40  print "CurDir:\n", os.path.dirname(os.path.realpath(__file__))
41  loadHeaders = False
42  headersDict = {}
43 
44  if loadHeaders:
45  hdrs = None
46  with open("../../ini/crawler-task_headers.txt", 'r') as f:
47  hdrs = ''.join(f.readlines())
48  for header in hdrs.splitlines():
49  if not header:
50  continue
51  try:
52  key, value = header[:header.index(':')].strip(), header[header.index(':') + len(':'):].strip()
53  except Exception:
54  print "header error:%s", header
55  os.abort()
56  headersDict[key] = value
57 
58  print "headersDict:\n", varDump(headersDict)
59 
60  #url = 'http://127.0.0.1/'
61  #url = 'http://127.0.0.1/index0.html'
62  #url = 'about:blank'
63  url = 'https://www.google.co.jp/search?q=&gws_rd=cr###window.IFRAME_KWSRC="http://127.0.0.1/keywords_big.txt";'
64  #url = 'https://www.google.com/search?q=test&gws_rd=cr'
65  httpTimeout = 60000
66  tm = int(httpTimeout) / 1000.0
67  if isinstance(httpTimeout, float):
68  tm += float('0' + str(httpTimeout).strip()[str(httpTimeout).strip().find('.'):])
69  allowRedirects = 1
70  proxies = None
71  authorization = None
72  postData = None
73  process_content_types = ["text/html"]
74  maxResourceSize = 1024 * 1024
75  maxHttpRedirects = 3
76  fetchType = BaseFetcher.TYP_NORMAL
77  localFilters = None
78  urlObjDepth = 0
79 
80  #Dynamic fetcher test
81  headersDict = {'--disable-web-security':'', '--allow-running-insecure-content':''}
82  macroCode = {"name":"tests",
83  "sets":[{"name":"set1", "items":['', '', '', ''], "repeat":1, "delay":0}],
84  "result_type":2,
85  "result_content_type":"text/json",
86  "result_fetcher_type":1}
87 
88  macroCode['sets'][0]['items'][0] = '1'
89  macroCode['sets'][0]['items'][1] = \
90  "\
91  var s=window.document.createElement('script');\
92  s.src='http://127.0.0.1/macro_test4.js';\
93  s.type='text/javascript';\
94  window.document.head.appendChild(s);\
95  return [window.jQuery===undefined, window.MACRO_PREPARE===undefined, window.MACRO_COLLECT===undefined];\
96  "
97  macroCode['sets'][0]['items'][2] = '50'
98  macroCode['sets'][0]['items'][3] = \
99  "\
100  if(window.MACRO_COLLECT===undefined){\
101  return [window.jQuery===undefined, window.MACRO_COLLECT===undefined];\
102  }else{\
103  return [window.jQuery===undefined, window.MACRO_COLLECT([window.IFRAME_NAME, window.IFRAME_URLS])];\
104  }\
105  "
106  fetchType = BaseFetcher.TYP_DYNAMIC
107  #change current dir for webdriver executable run with path ./
108  os.chdir("../../bin/")
109 
110  try:
111  #Test of NORMAL (request lib based) fetcher
112  res = BaseFetcher.get_fetcher(fetchType).open(url, timeout=tm, headers=headersDict,
113  allow_redirects=allowRedirects, proxies=proxies,
114  auth=authorization, data=postData, log=logger,
115  allowed_content_types=process_content_types,
116  max_resource_size=maxResourceSize,
117  max_redirects=maxHttpRedirects,
118  filters=localFilters, depth=urlObjDepth, macro=macroCode)
119 
120  except Exception, err:
121  #logger.debug("Exception:\n%s", varDump(err))
122  print "Exception:\n", varDump(err)
123 
124  #rd = varDump(res)
125  rd = json.dumps(json.loads(jsonpickle.encode(res)), indent=2)
126  #logger.debug("Result:\n%s", varDump(res))
127  print "Result:\n", rd
128 
129 
def varDump(obj, stringify=True, strTypeMaxLen=256, strTypeCutSuffix='...', stringifyType=1, ignoreErrors=False, objectsHash=None, depth=0, indent=2, ensure_ascii=False, maxDepth=10)
Definition: Utils.py:410
Definition: join.py:1