Debian Codegraph @

recipes/nytimes_tech.recipe

raw
1 #!/usr/bin/env python
2
3 from calibre.web.feeds.news import BasicNewsRecipe
4
5 use_wayback_machine = False
6
7
8 class NytTech(BasicNewsRecipe):
9 title = 'New York Times Technology Beat'
10 __author__ = 'unkn0wn'
11 description = 'The latest in technology - Gadgetwise'
12 oldest_article = 14 # days
13 encoding = 'utf-8'
14 use_embedded_content = False
15 language = 'en_US'
16 remove_empty_feeds = True
17 resolve_internal_links = True
18 ignore_duplicate_articles = {'title', 'url'}
19 masthead_url = 'https://static01.nytimes.com/newsgraphics/2015-12-23-masthead-2016/b15c3d81d3d7b59065fff9a3f3afe85aa2e2dff5/_assets/nyt-logo.png'
20
21 feeds = [
22 'https://rss.nytimes.com/services/xml/rss/nyt/Technology.xml',
23 ]
24
25 recipe_specific_options = {
26 'days': {
27 'short': 'Oldest article to download from this news source. In days ',
28 'long': 'For example, 0.5, gives you articles from the past 12 hours',
29 'default': str(oldest_article),
30 },
31 'comp': {'short': 'Compress News Images?', 'long': 'enter yes', 'default': 'no'},
32 'rev': {
33 'short': 'Reverse the order of articles in each feed?',
34 'long': 'enter yes',
35 'default': 'no',
36 },
37 'res': {
38 'short': ('For hi-res images, select a resolution from the following\noptions: popup, jumbo, mobileMasterAt3x, superJumbo'),
39 'long': (
40 'This is useful for non e-ink devices, and for a lower file size\nthan '
41 'the default, use mediumThreeByTwo440, mediumThreeByTwo225, articleInline.'
42 ),
43 },
44 }
45
46 def __init__(self, *args, **kwargs):
47 BasicNewsRecipe.__init__(self, *args, **kwargs)
48 d = self.recipe_specific_options.get('days')
49 if d and isinstance(d, str):
50 self.oldest_article = float(d)
51 r = self.recipe_specific_options.get('rev')
52 if r and isinstance(r, str):
53 if r.lower() == 'yes':
54 self.reverse_article_order = True
55 c = self.recipe_specific_options.get('comp')
56 if c and isinstance(c, str):
57 if c.lower() == 'yes':
58 self.compress_news_images = True
59
60 extra_css = '''
61 .byl, .time { font-size:small; color:#202020; }
62 .cap { font-size:small; text-align:center; }
63 .cred { font-style:italic; font-size:small; }
64 em, blockquote { color: #202020; }
65 .sc { font-variant: small-caps; }
66 .lbl { font-size:small; color:#404040; }
67 img { display:block; margin:0 auto; }
68 '''
69
70 @property
71 def nyt_parser(self):
72 ans = getattr(self, '_nyt_parser', None)
73 if ans is None:
74 from calibre.live import load_module
75
76 self._nyt_parser = ans = load_module('calibre.web.site_parsers.nytimes')
77 return ans
78
79 def get_nyt_page(self, url, skip_wayback=False):
80 if use_wayback_machine and not skip_wayback:
81 from calibre import browser
82
83 return self.nyt_parser.download_url(url, browser())
84 return self.index_to_soup(url, raw=True)
85
86 articles_are_obfuscated = use_wayback_machine
87
88 if use_wayback_machine:
89
90 def get_obfuscated_article(self, url):
91 from calibre.ptempfile import PersistentTemporaryFile
92
93 with PersistentTemporaryFile() as tf:
94 tf.write(self.get_nyt_page(url))
95 return tf.name
96
97 def preprocess_raw_html(self, raw_html, url):
98 return self.nyt_parser.extract_html(self.index_to_soup(raw_html), url)
99
100 def get_browser(self, *args, **kwargs):
101 kwargs['user_agent'] = 'User-Agent: Mozilla/5.0 (compatible; archive.org_bot; Wayback Machine Live Record; +http://archive.org/details/archive.org_bot)'
102 br = BasicNewsRecipe.get_browser(self, *args, **kwargs)
103 return br
104
105 def preprocess_html(self, soup):
106 w = self.recipe_specific_options.get('res')
107 if w and isinstance(w, str):
108 res = '-' + w
109 for img in soup.findAll('img', attrs={'src': True}):
110 if '-article' in img['src']:
111 ext = img['src'].split('?')[0].split('.')[-1]
112 img['src'] = img['src'].rsplit('-article', 1)[0] + res + '.' + ext
113 for c in soup.findAll('div', attrs={'class': 'cap'}):
114 for p in c.findAll(['p', 'div']):
115 p.name = 'span'
116 return soup