Debian Codegraph @

recipes/gosc_niedzielny.recipe

raw
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3
4 __license__ = 'GPL v3'
5 __copyright__ = '2011, Piotr Kontek, [email protected] 2013-2018, Tomasz Długosz, [email protected]'
6
7 import re
8
9 from lxml import html
10
11 from calibre.web.feeds.news import BasicNewsRecipe
12
13
14 class GN(BasicNewsRecipe):
15 __author__ = 'Piotr Kontek, Tomasz Długosz'
16 title = 'Gość Niedzielny'
17 publisher = 'Wydawnictwo Kurii Metropolitalnej w Katowicach'
18 description = 'Ogólnopolski tygodnik katolicki - fragmenty artykułów z aktualnego numeru'
19 encoding = 'utf-8'
20 no_stylesheets = True
21 language = 'pl'
22 remove_javascript = True
23 ignore_duplicate_articles = {'url'}
24 masthead_url = 'http://gosc.pl/static/images/base/logotypes/100x68/gosc-niedzielny.png'
25
26 def find_last_issue(self):
27 raw = self.index_to_soup('http://gosc.pl/wyszukaj/wydania/3.Gosc-Niedzielny/', raw=True)
28 doc = html.fromstring(raw)
29 page = doc.xpath('//div[@class="search-result release-result"]/div[1]/div[2]/h1//a/@href')
30
31 return page[0]
32
33 def parse_index(self):
34 self.last_issue = self.find_last_issue()
35 soup = self.index_to_soup('http://gosc.pl' + self.last_issue)
36 self.cover_url = 'http://www.gosc.pl' + soup.find('div', attrs={'class': 'fl-w100 release-wp'}).findAll('a')[-4].contents[0]['src']
37 feeds = []
38 # editorial:
39 a = soup.find('div', attrs={'class': 'release-wp-b'})
40 art = a.find('a')
41 articles = [
42 {
43 'title': self.tag_to_string(art),
44 'url': 'http://www.gosc.pl' + art['href'],
45 'description': self.tag_to_string(a.find('p', attrs={'class': 'b lead'})),
46 }
47 ]
48 feeds.append(('Na dobry początek', articles))
49 # columns:
50 for addr in soup.findAll('a', attrs={'href': re.compile(r'kategoria')}):
51 if not addr.span:
52 main_block = self.index_to_soup('http://www.gosc.pl' + addr['href'])
53 articles = list(self.find_articles(main_block))
54 if len(articles) > 0:
55 section = addr.contents[0]
56 feeds.append((section, articles))
57 # not assigned content:
58 page = 1
59 not_assigned = []
60 while True:
61 soup = self.index_to_soup('http://gosc.pl' + self.last_issue.replace('przeglad', 'wszystko') + '/' + str(page))
62 articles = list(self.find_articles(soup))
63 not_assigned.extend(articles)
64 page += 1
65 pages = soup.find('span', attrs={'class': 'pgr_nrs'})
66 if str(page) not in [self.tag_to_string(x) for x in pages.findAll('a')]:
67 break
68
69 feeds.append(('Nieprzypisane', not_assigned))
70 return feeds
71
72 def find_articles(self, main_block):
73 for a in main_block.findAll('div', attrs={'class': ['infoBox']}):
74 art = a.find('a')
75 yield {
76 'title': self.tag_to_string(art),
77 'url': 'http://www.gosc.pl' + art['href'].split('.')[0],
78 'date': self.tag_to_string(a.find('b', attrs={'class': 'time'})).replace('DODANE', ' '),
79 'description': self.tag_to_string(a.find('div', attrs={'class': 'txt'})),
80 }
81
82 def append_page(self, soup, appendtag):
83 chpage = appendtag.find(attrs={'class': 'pgr_nrs'})
84 if chpage:
85 for page in chpage.findAll('a'):
86 soup2 = self.index_to_soup('http://gosc.pl' + page['href'])
87 pagetext = soup2.find(attrs={'class': 'intextAd'})
88 pos = len(appendtag.contents)
89 appendtag.insert(pos, pagetext)
90
91 def preprocess_html(self, soup):
92 self.append_page(soup, soup.body)
93 r = soup.find(attrs={'class': 'lightbox'})
94 if r:
95 r.contents[0]['src'] = r['href']
96 return soup
97
98 def postprocess_html(self, soup, first_fetch):
99 for r in soup.findAll(attrs={'class': 'pgr'}):
100 r.extract()
101 for r in soup.findAll(attrs={'class': 'cm-i-a'}):
102 r.replaceWith('<div style="clear:both"></div>' + r.prettify() + '<div style="clear:both"></div>')
103 return soup
104
105 keep_only_tags = [dict(name='div', attrs={'class': ['cf txt ', 'cf txt att-audio']})]
106
107 remove_tags = [
108 dict(name='p', attrs={'class': 'l l-2 doc-source'}),
109 dict(name='span', attrs={'class': 'wykop'}),
110 dict(
111 name='div', attrs={'class': ['doc_actions', 'cf', 'fr1_cl', 'txt__social-icons', 'txt__tags', 'fb-like fb_iframe_widget', 'jp-audio', 'jp-jplayer']}
112 ),
113 dict(name='div', attrs={'id': 'vote'}),
114 dict(name='a', attrs={'class': 'img_enlarge'}),
115 ]
116
117 extra_css = '''
118 h1 {font-size:150%}
119 p.limiter {font-size:150%; font-weight: bold}
120 p.gn_subtitle {font-weight: bold}
121 span.cm-i-a {text-transform:uppercase;font-size:50%}
122 span.cm-i-p {font-style:italic; font-size:70%;text-align:right}
123 span.gn_brb {color: red; font-weight: bold}
124 div.txt__lead {font-weight: bold; font-size:150%}
125 '''