View Single Post
Old 05-12-2019, 01:47 PM   #10
siebert
Developer
siebert has a complete set of Star Wars action figures.siebert has a complete set of Star Wars action figures.siebert has a complete set of Star Wars action figures.
 
Posts: 155
Karma: 280
Join Date: Nov 2010
Device: Kindle 3 (Keyboard) 3G / iPad 9 WiFi / Google Pixel 6a (Android)
This is my test recipe which created the ebooks from the screenshots above.

Code:
#!/usr/bin/env  python
# -*- coding: utf-8 mode: python -*-

__license__   = 'GPL v3'
__copyright__ = 'Steffen Siebert <calibre at steffensiebert.de>'
__version__   = '1.0'

""" Create dummy ebook to test navigation elements. """

import re
import string
from calibre.web.feeds.recipes import BasicNewsRecipe
from calibre.ptempfile import PersistentTemporaryFile

class NavigationTest(BasicNewsRecipe):
    __author__ = 'Steffen Siebert'
    title = 'Navigation Test'
    description = 'Navigation Test'
    publisher ='Steffen Siebert'
    lang = 'de-DE'
    language = 'de'
    publication_type = 'magazine'
    articles_are_obfuscated = True
    use_embedded_content = False
    no_stylesheets = True
    conversion_options = {'comments': description, 'language': language, 'publisher': publisher}

    feeds = 3
    """ The number of feeds to generate. """
    articles_per_feed = 3
    """ The number of articles to generate for each feed. """
    
    LOREM_IPSUM = """Line 1<br>line 2<br>line 3<br><br>line a<br>line b<br>
    <p>Lorem ipsum dolor sit amet, consectetur adipisici elit, sed eiusmod tempor incidunt ut labore et
    dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquid ex ea commodi
    consequat. Quis aute iure reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur.
    Excepteur sint obcaecat cupiditat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>
    <p>Duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat, vel illum dolore eu 
    feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit
    augue duis dolore te feugait nulla facilisi. Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy
    nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat.</p>"""
    """ Dummy text. """

    """
    Calibre recipe to create dummy ebook for testing navigation elements.
    """

    def generate_image(self, feed, article):
        try:
            from PIL import Image, ImageDraw, ImageFont
            Image, ImageDraw, ImageFont
        except ImportError:
            import Image, ImageDraw, ImageFont

        font_path = P('fonts/liberation/LiberationSerif-Bold.ttf')
        img = Image.new('RGB', (self.MI_WIDTH, self.MI_HEIGHT), 'white')
        draw = ImageDraw.Draw(img)
        font = ImageFont.truetype(font_path, 22)
        text = "Image of feed %s article %s" % (feed, article)
        width, height = draw.textsize(text, font=font)
        left = max(int((self.MI_WIDTH - width)/2.), 0)
        top = max(int((self.MI_HEIGHT - height)/2.), 0)
        draw.text((left, top), text, fill=(255,0,0), font=font)
        output = PersistentTemporaryFile('_fa.jpg')
        img.save(output, 'JPEG')
        output.close()
        return output.name

    def get_obfuscated_article(self, url):
        result = re.match("^http://dummy/feed_([0-9]+)/article_([0-9]+).html$", url)
        feed = result.group(1)
        article = result.group(2)
        imageUrl = "file:///%s" % self.generate_image(feed, article)

        # Generate content into new temporary html file.
        html = PersistentTemporaryFile('_fa.html')
        html.write('<html>\n<head>\n<title>Feed %s Article %s</title>\n</head>\n' % (feed, article))
        html.write("<body>\n<h1>Feed %s Article %s</h1>\n" % (feed, article))
        html.write('<p><img src="%s" alt="Image of feed %s article %s"></p>' % (imageUrl, feed, article))
        html.write(self.LOREM_IPSUM)
        html.write("</body>\n</html>\n")
        html.close()

        return html.name

    def parse_index(self):
        feeds = []

        for feed in range(1, self.feeds + 1):
            feedName = "Feed %i" % feed
            articles = []
            for article in range(1, self.articles_per_feed + 1):
                url = "http://dummy/feed_%i/article_%i.html" % (feed, article)
                title = "Feed %i Article %i" % (feed, article)
                articles.append({'title': title, 'url': url, 'date': ''})
            feeds.append((feedName, articles))

        return feeds
siebert is offline   Reply With Quote