forked from grangier/python-goose
-
Notifications
You must be signed in to change notification settings - Fork 3
/
__init__.py
102 lines (87 loc) · 3.42 KB
/
__init__.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# -*- coding: utf-8 -*-
"""\
This is a python port of "Goose" orignialy licensed to Gravity.com
under one or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.
Python port was written by Xavier Grangier for Recrutae
Gravity.com licenses this file
to you under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import os
import platform
from tempfile import mkstemp
from goose.version import version_info, __version__
from goose.configuration import Configuration
from goose.crawler import CrawlCandidate
from goose.crawler import Crawler
from goose.exceptions import *
class Goose(object):
"""\
"""
def __init__(self, config=None):
self.config = config or Configuration()
self.extend_config()
self.initialize()
def extend_config(self):
if isinstance(self.config, dict):
config = Configuration()
for k, v in self.config.items():
if hasattr(config, k):
setattr(config, k, v)
self.config = config
def extract(self, url=None, raw_html=None):
"""\
Main method to extract an article object from a URL,
pass in a url and get back a Article
"""
cc = CrawlCandidate(self.config, url, raw_html)
return self.crawl(cc)
def shutdown_network(self):
pass
def crawl(self, crawl_candiate):
try:
crawler = Crawler(self.config)
article = crawler.crawl(crawl_candiate)
except (UnicodeDecodeError, ValueError), exception:
if self.config.parser_class == 'soupparser':
raise exception
else:
self.config.parser_class = 'soupparser'
article = self.crawl(crawl_candiate)
return article
def initialize(self):
# we don't need to go further if image extractor or
# local_storage is not set
if not self.config.local_storage_path or \
not self.config.enable_image_fetching:
return
# test if config.local_storage_path
# is a directory
if not os.path.isdir(self.config.local_storage_path):
os.makedirs(self.config.local_storage_path)
if not os.path.isdir(self.config.local_storage_path):
raise Exception(self.config.local_storage_path +
" directory does not seem to exist, "
"you need to set this for image processing downloads"
)
# test to write a dummy file to the directory
# to check is directory is writtable
level, path = mkstemp(dir=self.config.local_storage_path)
try:
f = os.fdopen(level, "w")
f.close()
os.remove(path)
except IOError:
raise Exception(self.config.local_storage_path +
" directory is not writeble, "
"you need to set this for image processing downloads"
)