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

« back to all changes in this revision

Viewing changes to recipes/the_new_republic.recipe

  • Committer: Package Import Robot
  • Author(s): Martin Pitt
  • Date: 2013-08-01 18:17:22 UTC
  • mfrom: (1.5.2)
  • Revision ID: package-import@ubuntu.com-20130801181722-rzupi33y7ut6qzat
Tags: 0.9.41+dfsg-1
* New upstream release.
* debian/local/calibre.desktop: Rename to calibre-gui.desktop to match its
  window class. (LP: #1206687)
* Add debian/local/ebook-viewer.desktop, so that one can directly open
  *.epub or *.mobi files in file browsers. Thanks Korey Lu!
  (Closes: #664182)
* Make it possible to auto-start calibre when connecting an e-book reader
  device:
  - Add debian/local/mime/calibre.xml MIME association, install in
    debian/calibre.install.
  - debian/local/calibre-gui.desktop: Add MIME type and file argument.
  - Thanks to Thanks Korey Lu! (Closes: #715246)

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
import re
2
 
from calibre.web.feeds.recipes import BasicNewsRecipe
3
 
 
4
 
 
5
 
class TNR(BasicNewsRecipe):
6
 
 
7
 
    title = 'The New Republic'
8
 
    __author__ = 'Krittika Goyal'
9
 
 
10
 
    description = '''The New Republic is a journal of opinion with an emphasis
11
 
    on politics and domestic and international affairs. It carries feature
12
 
    articles by staff and contributing editors. The second half of each issue
13
 
    is devoted to book and the arts, theater, motion pictures, music and art.'''
14
 
 
15
 
    language = 'en'
16
 
    encoding = 'UTF-8'
17
 
    needs_subscription = True
18
 
 
19
 
    preprocess_regexps = [
20
 
        (re.compile(r'<!--.*?-->', re.DOTALL), lambda m: ''),
21
 
        (re.compile(r'<script.*?</script>', re.DOTALL), lambda m: ''),
22
 
    ]
23
 
 
24
 
    def get_browser(self):
25
 
        br = BasicNewsRecipe.get_browser(self)
26
 
        br.open('http://www.newrepublic.com/user')
27
 
        br.select_form(nr=1)
28
 
        try:
29
 
            br['user'] = self.username
30
 
        except:
31
 
            br['name'] = self.username
32
 
        br['pass'] = self.password
33
 
        self.log('Logging in...')
34
 
        raw = br.submit().read()
35
 
        if 'SIGN OUT' not in raw:
36
 
            raise ValueError('Failed to log in to tnr.com, check your username and password')
37
 
        self.log('Logged in successfully')
38
 
        return br
39
 
 
40
 
    def parse_index(self):
41
 
        raw = self.index_to_soup('http://www.newrepublic.com/current-issue', raw=True)
42
 
        # raw = self.index_to_soup(open('/t/raw.html').read().decode('utf-8'), raw=True)
43
 
        for pat, sub in self.preprocess_regexps:
44
 
            raw = pat.sub(sub, raw)
45
 
        soup = self.index_to_soup(raw)
46
 
        feed_title = 'The New Republic Magazine Articles'
47
 
 
48
 
        articles = []
49
 
        for div in soup.findAll('div', attrs={'class':lambda x: x and 'field-item' in x.split()}):
50
 
            a = div.find('a', href=True, attrs={'class':lambda x: x != 'author'})
51
 
            if a is not None:
52
 
                art_title = self.tag_to_string(a)
53
 
                url = a.get('href')
54
 
                num = re.search(r'/(\d+)/', url)
55
 
                if num is not None:
56
 
                    art = num.group(1)
57
 
                    url = 'http://www.newrepublic.com/node/%s/print'%art
58
 
                    self.log.info('\tFound article:', art_title, 'at', url)
59
 
                    article = {'title':art_title, 'url':url, 'description':'', 'date':''}
60
 
                    articles.append(article)
61
 
 
62
 
        return [(feed_title, articles)]
63
 
 
 
1
import re
 
2
from calibre.web.feeds.recipes import BasicNewsRecipe
 
3
 
 
4
 
 
5
class TNR(BasicNewsRecipe):
 
6
 
 
7
    title = 'The New Republic'
 
8
    __author__ = 'Krittika Goyal'
 
9
 
 
10
    description = '''The New Republic is a journal of opinion with an emphasis
 
11
    on politics and domestic and international affairs. It carries feature
 
12
    articles by staff and contributing editors. The second half of each issue
 
13
    is devoted to book and the arts, theater, motion pictures, music and art.'''
 
14
 
 
15
    language = 'en'
 
16
    encoding = 'UTF-8'
 
17
    needs_subscription = True
 
18
 
 
19
    preprocess_regexps = [
 
20
        (re.compile(r'<!--.*?-->', re.DOTALL), lambda m: ''),
 
21
        (re.compile(r'<script.*?</script>', re.DOTALL), lambda m: ''),
 
22
    ]
 
23
 
 
24
    def get_browser(self):
 
25
        br = BasicNewsRecipe.get_browser(self)
 
26
        br.open('http://www.newrepublic.com/user')
 
27
        br.select_form(nr=1)
 
28
        try:
 
29
            br['user'] = self.username
 
30
        except:
 
31
            br['name'] = self.username
 
32
        br['pass'] = self.password
 
33
        self.log('Logging in...')
 
34
        raw = br.submit().read()
 
35
        if 'SIGN OUT' not in raw:
 
36
            raise ValueError('Failed to log in to tnr.com, check your username and password')
 
37
        self.log('Logged in successfully')
 
38
        return br
 
39
 
 
40
    def parse_index(self):
 
41
        raw = self.index_to_soup('http://www.newrepublic.com/current-issue', raw=True)
 
42
        # raw = self.index_to_soup(open('/t/raw.html').read().decode('utf-8'), raw=True)
 
43
        for pat, sub in self.preprocess_regexps:
 
44
            raw = pat.sub(sub, raw)
 
45
        soup = self.index_to_soup(raw)
 
46
        feed_title = 'The New Republic Magazine Articles'
 
47
 
 
48
        articles = []
 
49
        for div in soup.findAll('div', attrs={'class':lambda x: x and 'field-item' in x.split()}):
 
50
            a = div.find('a', href=True, attrs={'class':lambda x: x != 'author'})
 
51
            if a is not None:
 
52
                art_title = self.tag_to_string(a)
 
53
                url = a.get('href')
 
54
                num = re.search(r'/(\d+)/', url)
 
55
                if num is not None:
 
56
                    art = num.group(1)
 
57
                    url = 'http://www.newrepublic.com/node/%s/print'%art
 
58
                    self.log.info('\tFound article:', art_title, 'at', url)
 
59
                    article = {'title':art_title, 'url':url, 'description':'', 'date':''}
 
60
                    articles.append(article)
 
61
 
 
62
        return [(feed_title, articles)]
 
63