@@ -46,100 +46,136 @@ def __init__(self, config, url, raw_html):
4646class Crawler (object ):
4747
4848 def __init__ (self , config ):
49+ # config
4950 self .config = config
5051 # parser
5152 self .parser = self .config .get_parser ()
53+
54+ # article
55+ self .article = Article ()
56+
57+ # init the extractor
58+ self .extractor = self .get_extractor ()
59+
60+ # init the document cleaner
61+ self .cleaner = self .get_cleaner ()
62+
63+ # init the output formatter
64+ self .formatter = self .get_formatter ()
65+
66+ # video extractor
67+ self .video_extractor = self .get_video_extractor ()
68+
69+ # image extrator
70+ self .image_extractor = self .get_image_extractor ()
71+
72+ # TODO : log prefix
5273 self .logPrefix = "crawler:"
5374
5475 def crawl (self , crawl_candidate ):
55- article = Article ()
5676
77+ # parser candidate
5778 parse_candidate = self .get_parse_candidate (crawl_candidate )
79+
80+ # raw html
5881 raw_html = self .get_html (crawl_candidate , parse_candidate )
5982
6083 if raw_html is None :
61- return article
84+ return self . article
6285
86+ # create document
6387 doc = self .get_document (raw_html )
6488
65- extractor = self .get_extractor ()
66- document_cleaner = self .get_document_cleaner ()
67- output_formatter = self .get_output_formatter ()
68-
6989 # article
70- article .final_url = parse_candidate .url
71- article .link_hash = parse_candidate .link_hash
72- article .raw_html = raw_html
73- article .doc = doc
74- article .raw_doc = deepcopy (doc )
75- article .title = extractor .get_title (article )
90+ self .article .final_url = parse_candidate .url
91+ self .article .link_hash = parse_candidate .link_hash
92+ self .article .raw_html = raw_html
93+ self .article .doc = doc
94+ self .article .raw_doc = deepcopy (doc )
7695 # TODO
77- # article.publish_date = config.publishDateExtractor.extract(doc)
78- # article.additional_data = config.get_additionaldata_extractor.extract(doc)
79- article .meta_lang = extractor .get_meta_lang (article )
80- article .meta_favicon = extractor .get_favicon (article )
81- article .meta_description = extractor .get_meta_description (article )
82- article .meta_keywords = extractor .get_meta_keywords (article )
83- article .canonical_link = extractor .get_canonical_link (article )
84- article .domain = extractor .get_domain (article .final_url )
85- article .tags = extractor .extract_tags (article )
86- # # before we do any calcs on the body itself let's clean up the document
87- article .doc = document_cleaner .clean (article )
96+ # self.article.publish_date = config.publishDateExtractor.extract(doc)
97+ # self.article.additional_data = config.get_additionaldata_extractor.extract(doc)
98+ self .article .title = self .extractor .get_title ()
99+ self .article .meta_lang = self .extractor .get_meta_lang ()
100+ self .article .meta_favicon = self .extractor .get_favicon ()
101+ self .article .meta_description = self .extractor .get_meta_description ()
102+ self .article .meta_keywords = self .extractor .get_meta_keywords ()
103+ self .article .canonical_link = self .extractor .get_canonical_link ()
104+ self .article .domain = self .extractor .get_domain ()
105+ self .article .tags = self .extractor .extract_tags ()
106+
107+ # before we do any calcs on the body itself let's clean up the document
108+ self .article .doc = self .cleaner .clean ()
88109
89110 # big stuff
90- article .top_node = extractor .calculate_best_node (article )
91- if article .top_node is not None :
111+ self .article .top_node = self .extractor .calculate_best_node ()
112+
113+ # if we have a top node
114+ # let's process it
115+ if self .article .top_node is not None :
116+
92117 # video handeling
93- video_extractor = self .get_video_extractor ( article )
94- video_extractor . get_videos ()
118+ self .video_extractor . get_videos ( )
119+
95120 # image handeling
96121 if self .config .enable_image_fetching :
97- image_extractor = self .get_image_extractor ( article )
98- article . top_image = image_extractor . get_best_image ( article . raw_doc , article . top_node )
122+ self .get_image ( )
123+
99124 # post cleanup
100- article .top_node = extractor .post_cleanup (article .top_node )
125+ self .article .top_node = self .extractor .post_cleanup ()
126+
101127 # clean_text
102- article .cleaned_text = output_formatter . get_formatted_text (article )
128+ self . article .cleaned_text = self . formatter . get_formatted_text ()
103129
104130 # cleanup tmp file
105- self .relase_resources (article )
131+ self .relase_resources ()
106132
107- return article
133+ # return the article
134+ return self .article
108135
109136 def get_parse_candidate (self , crawl_candidate ):
110137 if crawl_candidate .raw_html :
111138 return RawHelper .get_parsing_candidate (crawl_candidate .url , crawl_candidate .raw_html )
112139 return URLHelper .get_parsing_candidate (crawl_candidate .url )
113140
141+ def get_image (self ):
142+ doc = self .article .raw_doc
143+ top_node = self .article .top_node
144+ self .article .top_image = self .image_extractor .get_best_image (doc , top_node )
145+
114146 def get_html (self , crawl_candidate , parsing_candidate ):
147+ # we got a raw_tml
148+ # no need to fetch remote content
115149 if crawl_candidate .raw_html :
116150 return crawl_candidate .raw_html
151+
117152 # fetch HTML
118- html = HtmlFetcher ().get_html (self .config , parsing_candidate .url )
153+ fetcher = HtmlFetcher (self .config , parsing_candidate .url )
154+ html = fetcher .get_html ()
155+ #html = HtmlFetcher().get_html(self.config, parsing_candidate.url)
119156 return html
120157
121- def get_image_extractor (self , article ):
122- http_client = None
123- return UpgradedImageIExtractor (http_client , article , self .config )
158+ def get_image_extractor (self ):
159+ return UpgradedImageIExtractor (self .config , self .article )
124160
125- def get_video_extractor (self , article ):
126- return VideoExtractor (article , self .config )
161+ def get_video_extractor (self ):
162+ return VideoExtractor (self . config , self .article )
127163
128- def get_output_formatter (self ):
129- return StandardOutputFormatter (self .config )
164+ def get_formatter (self ):
165+ return StandardOutputFormatter (self .config , self . article )
130166
131- def get_document_cleaner (self ):
132- return StandardDocumentCleaner (self .config )
167+ def get_cleaner (self ):
168+ return StandardDocumentCleaner (self .config , self . article )
133169
134170 def get_document (self , raw_html ):
135171 doc = self .parser .fromstring (raw_html )
136172 return doc
137173
138174 def get_extractor (self ):
139- return StandardContentExtractor (self .config )
175+ return StandardContentExtractor (self .config , self . article )
140176
141- def relase_resources (self , article ):
142- path = os .path .join (self .config .local_storage_path , '%s_*' % article .link_hash )
177+ def relase_resources (self ):
178+ path = os .path .join (self .config .local_storage_path , '%s_*' % self . article .link_hash )
143179 for fname in glob .glob (path ):
144180 try :
145181 os .remove (fname )
0 commit comments