01-18-2010, 01:14 PM | #1 |
Member
Posts: 23
Karma: 22
Join Date: Dec 2009
Device: Kindle DX
|
Recipes - Re-usable code
This thread can be used to post recipe code that might be re-usable or of general use to other coders.
Would a moderator kindly consider making this a STICKY? Moderator Notice
This thread has been made a sticky, and unlike most other sticky threads, this one is open to all who have a useful bit of code to post or a useful recipe tip. Do not use this thread to ask any questions. Start a new thread. Posts that don't belong here will be deleted, but you are encouraged to post if you have something to share. Please add a descriptive title to each post and explain how to use your code. Post the code in code tags and if it's longer than 4-5 lines, put spoiler tags around it to collapse it and make this thread more readable. Last edited by Starson17; 05-16-2011 at 11:55 AM. |
01-18-2010, 01:25 PM | #2 |
Member
Posts: 23
Karma: 22
Join Date: Dec 2009
Device: Kindle DX
|
Create a "virtual feed"
The PARSE_FEEDS method below is used to create a virtual feed from an RSS feed. in this case, it uses the PARSE_FEEDS method to:
Get all articles associated with the feed via the BasicRecipe methods Scan thru the articles and find titles that contain the word "recipe" Delete these articles from their existing feed Create a new feed section called "recipes" to hold these articles As-is looks like: Code:
Main Feed Article a Recipe a Recipe b Article b Code:
Main Feed Article a Article b Recipes Recipe a Recipe b Spoiler:
Last edited by Starson17; 02-25-2011 at 10:06 PM. |
Advert | |
|
01-19-2010, 02:05 PM | #3 |
Member
Posts: 23
Karma: 22
Join Date: Dec 2009
Device: Kindle DX
|
Add FEED objects during PARSE_INDEX
There is a slight difference between the Feeds object that is returned from PARSE_FEEDS and the object returned when using PARSE_INDEX.
The module below (FEED_TO_INDEX_APPEND) accepts a Feed object and an Index object. The Feed object is traversed and appended to the Index. This can be useful in mixing the use of an index as well as RSS feeds in a single recipe. It could be called in a way similar to: Spoiler:
Here is the conversion module: Spoiler:
Last edited by Starson17; 02-25-2011 at 10:07 PM. |
01-24-2010, 11:13 AM | #4 |
Member
Posts: 23
Karma: 22
Join Date: Dec 2009
Device: Kindle DX
|
Add dates to Feed title
Here's a module that will add the earliest/latest feed dates to the title of the feed (and therefore the Table of Contents entry as well). Useful when feeds aren't updated on a regular basis ...
The GET_FEED_DATES procedure accepts a single feed object (not a list of feeds, but a FOR loop could deal with a list). It also accepts a date mask of the begin/end dates for use with STRFTIME. Here's an example of calling the procedure: Spoiler:
Here's the procedure itself: Spoiler:
Last edited by Starson17; 02-25-2011 at 10:07 PM. |
04-11-2010, 06:24 AM | #5 |
Junior Member
Posts: 5
Karma: 12
Join Date: Apr 2010
Device: dr1000
|
Default cover with custom image
Instead of fully replacing the cover using the cover_url, just replace the stock image of the default cover with a given image.
Add a cover_img_url field with the url in the definition of the class, and add the following code (imports and fuctions). There is no need to call any of these functions directly, all is handled via the redefined default_cover function. The code is based on the code from BasicRecipe itself Spoiler:
Last edited by Starson17; 02-25-2011 at 10:09 PM. Reason: refactored _download_cover_img |
Advert | |
|
10-16-2010, 12:36 PM | #6 |
Wizard
Posts: 4,004
Karma: 177841
Join Date: Dec 2009
Device: WinMo: IPAQ; Android: HTC HD2, Archos 7o; Java:Gravity T
|
Remove articles from feed
This code will easily remove articles from feeds based upon a character string in the title of the article or part of the URL. This is specifically for The New Yorker recipe to remove video links (word "video" in the title of the article) and Goings On About Town (GOAT in the url), but it can be easily adapted for any recipe:
Spoiler:
In addition to matching on the url and/or the title of the article, one can match on the article date or summary with article.date or article.text_summary. Last edited by Starson17; 02-17-2011 at 04:38 PM. |
10-16-2010, 08:07 PM | #7 |
Addict
Posts: 264
Karma: 62
Join Date: May 2010
Device: kindle 2, kindle 3, Kindle fire
|
One of several ways to Do a print Friendly Version
If the rss feed goes to a pages that has a nice link to get a printer friendly version that is clean. Then this method can work really well for you in most cases. You will have to know a little bit about regular expressions or of course you can do like I do and cheat and use firebug to get the link say for instance /services/printVersion.asp then let http://www.txt2re.com/index.php3 convert it to python reg expression.
then you could use code like this... put this at the top to allow for the use of the tempfile usage Code:
from calibre.ptempfile import PersistentTemporaryFile Spoiler:
|
11-15-2010, 10:11 AM | #8 |
Wizard
Posts: 4,004
Karma: 177841
Join Date: Dec 2009
Device: WinMo: IPAQ; Android: HTC HD2, Archos 7o; Java:Gravity T
|
Recipe to download an EPUB from feed
So that this doesn't get lost, I'm going to repost it here. It's a recipe that grabs a single link to an EPUB.
The recipe modifies build_index, which is the method that gets the masthead image and cover, parses the feed for articles, retrieves the articles, removes tags from articles, etc. All of those steps ultimately produce a local directory structure that looks like an unzipped EPUB. The recipe grabs the link to one EPUB (the first in the RSS feed), saves the EPUB locally, extracts it, and passes the result back into the recipe system as though all the other steps had been completed normally. To use the recipe, just modify these lines: epub_feed = "http://feeds.feedburner.com/NowEpubEditions" soup = self.index_to_soup(epub_feed) url = soup.find(name = 'feedburnerriglink').string so that "url" points to an EPUB as in: "http://some.place.com/epubfile.epub" The sample below grabs the first EPUB in an RSS feed, but you can just supply a single URL directly or grab it from the front page of a newspaper. I've posted a complete recipe to emphasize that the normal recipe methods, like "feeds", "remove_tags", etc. should all be omitted. Spoiler:
|
11-23-2010, 02:54 PM | #9 |
Guru
Posts: 800
Karma: 194644
Join Date: Dec 2007
Location: Argentina
Device: Kindle Voyage
|
Small piece of code to convert all links to text:
Spoiler:
Last edited by Starson17; 02-25-2011 at 10:10 PM. |
12-27-2010, 01:54 AM | #10 |
Junior Member
Posts: 3
Karma: 16
Join Date: Dec 2010
Device: PRS 950
|
Recipe to keep track of feed items already downloaded and only download new items
Here is a recipe template that keeps track of already downloaded feed items and only downloads items that it hasn't seen before or whose description, content, or URL have changed. It does so by overriding the parse_feeds method.
Some caveats:
Spoiler:
Last edited by Starson17; 02-25-2011 at 10:10 PM. Reason: Typos and grammar. |
01-18-2011, 11:40 AM | #11 |
Wizard
Posts: 4,004
Karma: 177841
Join Date: Dec 2009
Device: WinMo: IPAQ; Android: HTC HD2, Archos 7o; Java:Gravity T
|
Rotate images
This code will rotate images so that the long dimension is vertical. It's been updated and revised from code found here after python magick wand was replaced by calibre.utils.magick and calibre.utils.magick.draw:
The code would typically be used for image-based recipes (comics) read on devices that don't have a g-sensor based autorotation capability or where you prefer to lock your reader software into portrait orientation. This code makes the long dimension of the image appear in the long dimension of the screen on a handheld device that can be rotated for better viewing. Don't use it for recipes being read on a non-rotatable PC monitor (unless you want to view images with your head tilted 90 degrees). You may also want to set extra_css to include something like: img {max-height:95%; min-height:95%;} This leaves some room above and below the image for the links typically displayed in recipes. Spoiler:
Last edited by Starson17; 05-16-2011 at 11:57 AM. |
05-16-2011, 10:26 AM | #12 |
Wizard
Posts: 4,004
Karma: 177841
Join Date: Dec 2009
Device: WinMo: IPAQ; Android: HTC HD2, Archos 7o; Java:Gravity T
|
Print_version and string handling.
The question of how to modify a URL in a recipe comes up often, particularly when using print_version. It's basic string handling in Python, but I thought I'd post some tips here:
The most common three ways this job (URL modification) is accomplished in recipes are to use : 1) replace, 2) partition/rpartition, and 3) split/join. You can read about them here: Use replace whenever you are replacing some part of the URL string (usually in the middle) with some other string and 1) the part being replaced never changes (this lets you find it) and 2) you don't want to change the two parts on either side of the part being replaced. You can keep the part you are replacing (by inserting itself back in) and you can add additional stuff in the middle, but you can't change the first and last parts. Use partition/rpartition when you want to split the string into three parts and the part in the middle never changes (so you can find it), but you want to change one or more of the three parts. If there isn't any unchanging part in the middle to find, then the solution most commonly used is split/join with splitting being done on the slash. It splits the URL into each part between slashes and you can do whatever you want to each piece, then put them together with join (which adds back the original slashes). You find the part to change by counting the cut up pieces (between slashes) and changing the pieces you need to change and inserting anything needed between them. This shows a URL having the string "/v-print/" being inserted after the 6th slash to change http://www.03.com/04/05/06/07/08.html to http://www.03.com/04/05/06/v-print/07/08.html: Code:
def print_version(self,url): segments = url.split('/') printURL = '/'.join(segments[0:6]) + '/v-print/' + '/'.join(segments[6:]) return printURL Also see TonytheBookworm's method described above. Last edited by Starson17; 11-04-2011 at 09:50 AM. |
07-08-2011, 05:34 PM | #13 |
Connoisseur
Posts: 82
Karma: 10
Join Date: Oct 2010
Device: Kindle
|
Set a recipe that generates a typical e-book rather than a periodical
Sometimes, one may wish to use a recipe to generate a typical e-book rather than a periodical. You may make that possible by adding the following piece of code inside your recipe class.
Spoiler:
The above basically changes one line in the implementation of the following function of calibre.web.feeds.news Code:
def create_opf(self, feeds, dir=None): Code:
mi.publication_type = 'periodical:'+self.publication_type+':'+self.short_title() Code:
mi.publication_type = self.publication_type+':'+self.short_title() For an example recipe, check the "Ming Pao - Hong Kong" recipe in your Calibre copy or here. You may also get insights on how to append the publication date in the e-book title, which could be useful for differentiating e-books generated on different dates. Moderator Notice: The difference between a typical ebook and a "periodical" is relevant for a Kindle, but may not be relevant for other ebook readers. Last edited by tylau0; 07-09-2011 at 10:32 AM. |
08-21-2011, 09:22 PM | #14 |
Guru
Posts: 800
Karma: 194644
Join Date: Dec 2007
Location: Argentina
Device: Kindle Voyage
|
Handling redirections
Let us assume that you have a feed with links that all point to redirected pages. By default Calibre does not handle this case so the safest way of doing this could be summarized like this:
Code:
def print_version(self, url): return self.browser.open_novisit(url).geturl() Last edited by kiklop74; 08-21-2011 at 09:38 PM. |
11-02-2011, 04:41 PM | #15 |
Wizard
Posts: 4,004
Karma: 177841
Join Date: Dec 2009
Device: WinMo: IPAQ; Android: HTC HD2, Archos 7o; Java:Gravity T
|
Convert Images to Grayscale
For those who have grayscale only readers, this will convert images to grayscale. It saves between 8% and 20% of space. My tests on raw individual images showed 8%, but tests reported by others showed up to 20% reductions for the final ebook. YMMV
Spoiler:
Last edited by Starson17; 11-04-2011 at 09:51 AM. |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
DR800 The working (usable) screen resolution | PaulS | iRex | 7 | 04-23-2010 01:27 PM |
Let's create a source code repository for DR 800 related code? | jraf | iRex | 3 | 03-11-2010 01:26 PM |
any usable epub reader? | janw | iRex | 10 | 09-04-2009 01:25 PM |
FICTIONWISE, still usable? | jcbeam | Amazon Kindle | 4 | 03-19-2009 02:17 PM |
iLiad usable for scientists? | doctorow | iRex | 5 | 08-14-2006 06:00 PM |