~ubuntu-branches/debian/sid/calibre/sid

« back to all changes in this revision

Viewing changes to recipes/gosc_full.recipe

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2014-02-27 07:48:06 UTC
  • mto: This revision was merged to the branch mainline in revision 74.
  • Revision ID: package-import@ubuntu.com-20140227074806-64wdebb3ptosxhhx
Tags: upstream-1.25.0+dfsg
Import upstream version 1.25.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
from __future__ import unicode_literals
 
3
 
 
4
__license__   = 'GPL v3'
 
5
__copyright__ = '2011, Piotr Kontek, piotr.kontek@gmail.com \
 
6
                 2013-2014, Tomasz Długosz, tomek3d@gmail.com'
 
7
 
 
8
from calibre.web.feeds.news import BasicNewsRecipe
 
9
import re, time
 
10
from lxml import html
 
11
 
 
12
class GN(BasicNewsRecipe):
 
13
    __author__ = 'Piotr Kontek, Tomasz Długosz'
 
14
    title = u'Gość Niedzielny - pełny numer'
 
15
    publisher = 'Wydawnictwo Kurii Metropolitalnej w Katowicach'
 
16
    description = 'Ogólnopolski tygodnik katolicki - pełny numer sprzed 4 tygodni'
 
17
    encoding = 'utf-8'
 
18
    no_stylesheets = True
 
19
    language = 'pl'
 
20
    remove_javascript = True
 
21
 
 
22
    def find_last_issue(self):
 
23
        raw = self.index_to_soup('http://gosc.pl/wyszukaj/wydania/3.Gosc-Niedzielny/', raw=True)
 
24
        doc = html.fromstring(raw)
 
25
        page = doc.xpath('//div[@class="c"]//div[@class="search-result"]/div[1]/div[2]/h1//a/@href')
 
26
 
 
27
        if time.strftime("%w") in ['3','4']:
 
28
            return page[5]
 
29
        else:
 
30
            return page[4]
 
31
 
 
32
    def parse_index(self):
 
33
        soup = self.index_to_soup('http://gosc.pl' + self.find_last_issue())
 
34
        self.cover_url = 'http://www.gosc.pl' + soup.find('div',attrs={'class':'fl-w100 release-wp'}).findAll('a')[-4].contents[0]['src']
 
35
        feeds = []
 
36
        enlisted = []
 
37
        # editorial:
 
38
        a = soup.find('div',attrs={'class':'release-wp-b'})
 
39
        art = a.find('a')
 
40
        articles = [
 
41
                    {'title' : self.tag_to_string(art),
 
42
                     'url' : 'http://www.gosc.pl' + art['href'],
 
43
                     'description' : self.tag_to_string(a.find('p',attrs={'class':'b lead'}))
 
44
                    }]
 
45
        feeds.append((u'Wstępniak',articles))
 
46
        enlisted.append(articles[0].get('url'))
 
47
        # columns:
 
48
        for addr in soup.findAll('a',attrs={'href':re.compile('kategoria')}):
 
49
            if addr.string != u'wszystkie artyku\u0142y z tej kategorii \xbb':
 
50
                main_block = self.index_to_soup('http://www.gosc.pl' + addr['href'])
 
51
                articles = list(self.find_articles(main_block))
 
52
                if len(articles) > 0:
 
53
                    section = addr.string
 
54
                    feeds.append((section, articles))
 
55
                    enlisted.extend(list(article.get('url') for article in articles))
 
56
        # not assigned content:
 
57
        page = 1
 
58
        not_assigned = []
 
59
        while True:
 
60
            soup = self.index_to_soup('http://gosc.pl' + self.find_last_issue().replace('przeglad','wszystko') + '/' + str(page))
 
61
            articles = list(self.find_articles(soup))
 
62
            not_assigned.extend([x for x in articles if x.get('url') not in enlisted])
 
63
            page+=1
 
64
            pages = soup.find('span', attrs={'class':'pgr_nrs'})
 
65
            if str(page) not in [self.tag_to_string(x)[1] for x in pages.findAll('a')]:
 
66
                break
 
67
 
 
68
        feeds.insert(1,(u'Nieprzypisane', not_assigned))
 
69
        return feeds
 
70
 
 
71
    def find_articles(self, main_block):
 
72
        for a in main_block.findAll('div', attrs={'class':['prev_doc2', 'sr-document']}):
 
73
            art = a.find('a')
 
74
            yield {
 
75
                'title' : self.tag_to_string(art),
 
76
                'url' : 'http://www.gosc.pl' + art['href'],
 
77
                'date' : self.tag_to_string(a.find('p', attrs={'class':'sr-date'})),
 
78
                'description' : self.tag_to_string(a.find('p', attrs={'class':'sr-lead'}))
 
79
            }
 
80
 
 
81
    def append_page(self, soup, appendtag):
 
82
        chpage= appendtag.find(attrs={'class':'pgr_nrs'})
 
83
        if chpage:
 
84
            for page in chpage.findAll('a'):
 
85
                soup2 = self.index_to_soup('http://gosc.pl' + page['href'])
 
86
                pagetext = soup2.find(attrs={'class':'intextAd'})
 
87
                pos = len(appendtag.contents)
 
88
                appendtag.insert(pos, pagetext)
 
89
 
 
90
    def preprocess_html(self, soup):
 
91
        self.append_page(soup, soup.body)
 
92
        r = soup.find(attrs={'class':'lightbox'})
 
93
        if r:
 
94
            r.contents[0]['src'] = r['href']
 
95
        return soup
 
96
 
 
97
    def postprocess_html(self, soup, first_fetch):
 
98
        for r in soup.findAll(attrs={'class':'pgr'}):
 
99
            r.extract()
 
100
        for r in soup.findAll(attrs={'class':['di_dr', 'doc_image']}):
 
101
            del r['style']
 
102
        for r in soup.findAll(attrs={'class':'cm-i-a'}):
 
103
            r.replaceWith('<div style="clear:both"></div>' + r.prettify() + '<div style="clear:both"></div>')
 
104
        return soup
 
105
 
 
106
    keep_only_tags = [
 
107
        dict(name='div', attrs={'class':'cf txt'})
 
108
    ]
 
109
 
 
110
    remove_tags = [
 
111
        dict(name='p', attrs={'class':['r tr', 'l l-2', 'wykop', 'tags']}),
 
112
        dict(name='div', attrs={'class':['doc_actions', 'cf', 'fr1_cl']}),
 
113
        dict(name='div', attrs={'id':'vote'}),
 
114
        dict(name='link'),
 
115
        dict(name='a', attrs={'class':'img_enlarge'})
 
116
    ]
 
117
 
 
118
    extra_css = '''
 
119
            h1 {font-size:150%}
 
120
            p.limiter {font-size:150%; 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
        '''