Skip to content

Instantly share code, notes, and snippets.

@guyru
Last active August 29, 2015 14:16
Show Gist options
  • Save guyru/d35712edcc5b6adf364c to your computer and use it in GitHub Desktop.
Save guyru/d35712edcc5b6adf364c to your computer and use it in GitHub Desktop.
Skeleton for adding a QR code at the end of articles
from calibre.web.feeds.news import BasicNewsRecipe
from calibre.ebooks.BeautifulSoup import BeautifulSoup, Tag
import urllib
class QrCodeSkeletonRecipe(BasicNewsRecipe):
# If True, it will add a QR code with the article's URL at the
# end of each article.
qr_code = True
# If keep_only tags is used, don't forget to add the qr-code to it.
# keep_only_tags = [
# dict(name='p', attrs={'class':'qr-code'}),
# ]
# Add a QR-code for sharing
@staticmethod
def get_qr_code_img(data, size='450x450'):
# The URL of the http://goqr.me/ service
qr_api_url = 'https://api.qrserver.com/v1/create-qr-code/?'
qr_img = Tag(BeautifulSoup(), 'img')
parameters = urllib.urlencode({'data': data, 'size': size})
qr_img['src'] = qr_api_url + parameters
# We set the width to enable zooming-in on Kindle devices
qr_img['style'] = 'width: 3cm; height: 3cm;'
return qr_img
def preprocess_raw_html(self, raw_html, url):
if self.qr_code:
qr_img = self.get_qr_code_img(url)
soup = BeautifulSoup(raw_html)
soup.body.append('<p class="qr-code" style="text-align:center;">Share article<br/>%s</p>' % qr_img)
raw_html = soup.prettify()
return raw_html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment