~ubuntu-branches/ubuntu/karmic/calibre/karmic

« back to all changes in this revision

Viewing changes to src/calibre/ebooks/lrf/web/profiles/automatic.py

  • Committer: Bazaar Package Importer
  • Author(s): Martin Pitt
  • Date: 2009-07-30 12:49:41 UTC
  • mfrom: (1.3.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20090730124941-qjdsmri25zt8zocn
Tags: 0.6.3+dfsg-0ubuntu1
* New upstream release. Please see http://calibre.kovidgoyal.net/new_in_6/
  for the list of new features and changes.
* remove_postinstall.patch: Update for new version.
* build_debug.patch: Does not apply any more, disable for now. Might not be
  necessary any more.
* debian/copyright: Fix reference to versionless GPL.
* debian/rules: Drop obsolete dh_desktop call.
* debian/rules: Add workaround for weird Python 2.6 setuptools behaviour of
  putting compiled .so files into src/calibre/plugins/calibre/plugins
  instead of src/calibre/plugins.
* debian/rules: Drop hal fdi moving, new upstream version does not use hal
  any more. Drop hal dependency, too.
* debian/rules: Install udev rules into /lib/udev/rules.d.
* Add debian/calibre.preinst: Remove unmodified
  /etc/udev/rules.d/95-calibre.rules on upgrade.
* debian/control: Bump Python dependencies to 2.6, since upstream needs
  it now.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env  python
2
 
__license__   = 'GPL v3'
3
 
__copyright__ = '2008, Kovid Goyal <kovid at kovidgoyal.net>'
4
 
import os
5
 
 
6
 
from calibre.ebooks.lrf.web.profiles import DefaultProfile
7
 
from calibre.ebooks.BeautifulSoup import BeautifulSoup
8
 
from calibre import iswindows
9
 
from calibre.ebooks.chardet import xml_to_unicode
10
 
 
11
 
class AutomaticRSSProfile(DefaultProfile):
12
 
    '''
13
 
    Make downloading of RSS feeds completely automatic. Only input 
14
 
    required is the URL of the feed.
15
 
    '''
16
 
    
17
 
    max_recursions = 2
18
 
    
19
 
    def __init__(self, *args, **kwargs):
20
 
        self.cindex = 1
21
 
        DefaultProfile.__init__(*args, **kwargs)
22
 
    
23
 
    def fetch_content(self, index):
24
 
        raw = open(index, 'rb').read()
25
 
        if self.encoding:
26
 
            raw = raw.decode(self.encoding)
27
 
            enc = self.encoding
28
 
        else:
29
 
            raw, enc = xml_to_unicode(raw)
30
 
        isoup = BeautifulSoup(raw)
31
 
        for a in isoup.findAll('a', href=True):
32
 
            src = a['href']
33
 
            if src.startswith('file:'):
34
 
                src = src[5:]
35
 
            if os.access(src, os.R_OK):
36
 
                self.fetch_content(src)
37
 
                continue
38
 
            try:
39
 
                src = self.browser.open(src).read()
40
 
            except:
41
 
                continue
42
 
            soup  = BeautifulSoup(src)
43
 
            header, content = [], []
44
 
            head = soup.find('head')
45
 
            if head is not None:
46
 
                for style in head('style'):
47
 
                    header.append(unicode(style))
48
 
            body = soup.find('body')
49
 
            if body is None:
50
 
                continue
51
 
            for tag in body(['p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6']):
52
 
                in_table = False
53
 
                c = tag.parent
54
 
                while c is not None:
55
 
                    if c.name == 'table':
56
 
                        in_table = True
57
 
                        break
58
 
                    c = c.parent
59
 
                if in_table:
60
 
                    continue
61
 
                content.append(unicode(tag))
62
 
                
63
 
            cfile = 'content%d.html'%self.cindex
64
 
            self.cindex += 1
65
 
            cfile = os.path.join(os.path.dirname(index), cfile)
66
 
            html = '<html>\n<head>%s</head>\n<body>%s</body></html>'%('\n'.join(header), '\n'.join(content))
67
 
            
68
 
            open(cfile, 'wb').write(html.encode(enc))
69
 
            a['href'] = ('file:' if iswindows else '') + cfile
70
 
        open(index, 'wb').write(unicode(isoup).encode(enc)) 
71
 
    
72
 
    def build_index(self):
73
 
        index = DefaultProfile.build_index(self)
74
 
        self.fetch_content(index)
75
 
    
 
 
b'\\ No newline at end of file'