| 1 |
#!/usr/bin/env python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
__license__ = 'GPL v3' |
| 5 |
__copyright__ = 'Chris Moore 2025' |
| 6 |
__version__ = '1.0' |
| 7 |
|
| 8 |
import os |
| 9 |
|
| 10 |
from calibre import walk |
| 11 |
from calibre.ptempfile import PersistentTemporaryFile |
| 12 |
from calibre.utils.zipfile import ZipFile |
| 13 |
from calibre.web.feeds.news import BasicNewsRecipe |
| 14 |
|
| 15 |
|
| 16 |
class lwn_epub(BasicNewsRecipe): |
| 17 |
__author__ = 'Chris Moore' |
| 18 |
title = 'lwn' |
| 19 |
language = 'en' |
| 20 |
publisher = 'lwn.net' |
| 21 |
publication_type = 'magazine' |
| 22 |
description = 'The weekly subscriber-only edition from Linux Weekly News' |
| 23 |
needs_subscription = True |
| 24 |
|
| 25 |
conversion_options = { |
| 26 |
'publisher': publisher, |
| 27 |
'language': language, |
| 28 |
'comments': description, |
| 29 |
'no_default_epub_cover': True, |
| 30 |
'preserve_cover_aspect_ratio': True, |
| 31 |
} |
| 32 |
|
| 33 |
def build_index(self): |
| 34 |
browser = self.get_browser() |
| 35 |
browser.open('https://lwn.net/current') |
| 36 |
|
| 37 |
# find the link |
| 38 |
epublink = browser.find_link(text='Download EPUB') |
| 39 |
|
| 40 |
# download ebook |
| 41 |
self.report_progress(0, _('Downloading ePUB')) |
| 42 |
response = browser.follow_link(epublink) |
| 43 |
book_file = PersistentTemporaryFile(suffix='.epub') |
| 44 |
book_file.write(response.read()) |
| 45 |
book_file.close() |
| 46 |
|
| 47 |
# convert |
| 48 |
self.report_progress(0.2, _('Converting to OEB')) |
| 49 |
oeb = self.output_dir + '/INPUT/' |
| 50 |
if not os.path.exists(oeb): |
| 51 |
os.makedirs(oeb) |
| 52 |
with ZipFile(book_file.name) as f: |
| 53 |
f.extractall(path=oeb) |
| 54 |
|
| 55 |
for f in walk(oeb): |
| 56 |
if f.endswith('.opf'): |
| 57 |
return f |
| 58 |
|
| 59 |
def get_browser(self): |
| 60 |
br = BasicNewsRecipe.get_browser(self) |
| 61 |
if self.username is not None and self.password is not None: |
| 62 |
br.open('https://lwn.net/login') |
| 63 |
br.select_form(name='loginform') |
| 64 |
br['uname'] = self.username |
| 65 |
br['pword'] = self.password |
| 66 |
br.submit() |
| 67 |
return br |