forked from codelucas/newspaper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
94 lines (76 loc) · 2.61 KB
/
Copy pathapi.py
File metadata and controls
94 lines (76 loc) · 2.61 KB
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
# -*- coding: utf-8 -*-
"""
Ignore the unused imports, this file's purpose is to make visible
anything which a user might need to import from newspaper.
View newspaper/__init__.py for its usage.
"""
__title__ = 'newspaper'
__author__ = 'Lucas Ou-Yang'
__license__ = 'MIT'
__copyright__ = 'Copyright 2014, Lucas Ou-Yang'
import feedparser
from .article import Article
from .configuration import Configuration
from .mthreading import NewsPool
from .settings import POPULAR_URLS, TRENDING_URL
from .source import Source
from .utils import extend_config, get_unicode, print_available_languages
def build(url=u'', dry=False, config=None, **kwargs):
"""Returns a constructed source object without
downloading or parsing the articles
"""
config = config or Configuration()
config = extend_config(config, kwargs)
url = url or ''
s = Source(url, config=config)
if not dry:
s.build()
return s
def build_article(url=u'', config=None, **kwargs):
"""Returns a constructed article object without downloading
or parsing
"""
config = config or Configuration()
config = extend_config(config, kwargs)
url = url or ''
a = Article(url, config=config)
return a
def languages():
"""Returns a list of the supported languages
"""
print_available_languages()
def popular_urls():
"""Returns a list of pre-extracted popular source urls
"""
with open(POPULAR_URLS) as f:
urls = ['http://' + u.strip() for u in f.readlines()]
return urls
def hot():
"""Returns a list of hit terms via google trends
"""
try:
listing = feedparser.parse(TRENDING_URL)['entries']
trends = [item['title'] for item in listing]
return trends
except Exception, e:
print 'ERR hot terms failed!', str(e)
return None
def fulltext(html, language='en'):
"""Takes article HTML string input and outputs the fulltext
Input string is decoded via UnicodeDammit if needed
"""
from .cleaners import DocumentCleaner
from .configuration import Configuration
from .extractors import ContentExtractor
from .outputformatters import OutputFormatter
config = Configuration()
config.language = language
extractor = ContentExtractor(config)
document_cleaner = DocumentCleaner(config)
output_formatter = OutputFormatter(config)
doc = config.get_parser().fromstring(html)
doc = document_cleaner.clean(doc)
top_node = extractor.calculate_best_node(doc)
top_node = extractor.post_cleanup(top_node)
text, article_html = output_formatter.get_formatted(top_node)
return get_unicode(text)