Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Recipes

Notices

Reply
 
Thread Tools Search this Thread
Old 04-11-2011, 01:41 PM   #1
thczv
Junior Member
thczv began at the beginning.
 
Posts: 8
Karma: 10
Join Date: Mar 2011
Device: Kindle
SacBee print_version syntax

I am trying to figure out the syntax for this. But the going is slow because I don't really know what I am doing and am figuring it out by trial and error. What I need to do is insert "v-print/" within a url. For example, here is a typical article from the Sacramento Bee rss feed:

http://www.sacbee.com/2011/04/09/354...i_rss=Business

I need to automatically insert "v-print/" like so:

http://www.sacbee.com/2011/04/09/v-p...education.html

It doesn't appear to matter whether "#mi_rss=Business" appears at the end.

The url includes the date, so a simple replace won't work. I tried doing it with wildcards, but it hasn't worked so far.

Do any of you know a simple way to do this?

Thanks.
thczv is offline   Reply With Quote
Old 04-11-2011, 03:52 PM   #2
Starson17
Wizard
Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.
 
Posts: 4,004
Karma: 177841
Join Date: Dec 2009
Device: WinMo: IPAQ; Android: HTC HD2, Archos 7o; Java:Gravity T
Quote:
Originally Posted by thczv View Post
I am trying to figure out the syntax for this. But the going is slow because I don't really know what I am doing and am figuring it out by trial and error. What I need to do is insert "v-print/" within a url. For example, here is a typical article from the Sacramento Bee rss feed:

http://www.sacbee.com/2011/04/09/354...i_rss=Business

I need to automatically insert "v-print/" like so:

http://www.sacbee.com/2011/04/09/v-p...education.html

It doesn't appear to matter whether "#mi_rss=Business" appears at the end.

The url includes the date, so a simple replace won't work. I tried doing it with wildcards, but it hasn't worked so far.

Do any of you know a simple way to do this?

Thanks.
The are only a few ways this job (URL modification) is usually done in recipes. They are: 1) replace, 2) partition/rpartition, and 3) split/join.

Use replace whenever you are replacing some part of the string (usually in the middle) with some other string and the 1) 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. Replace opens up the string into two parts one 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 for you to find (as in your case) 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. You find the part to change by counting the cut up pieces (between slashes) and changing the pieces you need to change.

Try this:
Code:
    def print_version(self,url):
        segments = url.split('/')
        printURL = '/'.join(segments[0:6]) + '/v-print/' + '/'.join(segments[6:])
        return printURL

Last edited by Starson17; 05-05-2011 at 09:16 AM.
Starson17 is offline   Reply With Quote
Advert
Old 04-11-2011, 04:05 PM   #3
thczv
Junior Member
thczv began at the beginning.
 
Posts: 8
Karma: 10
Join Date: Mar 2011
Device: Kindle
This appears to work great. Thank you. Could you explain the nuts and bolts logic of what this does, or direct me to a web site where I can learn about it?

Thanks again.
thczv is offline   Reply With Quote
Old 04-11-2011, 04:21 PM   #4
Starson17
Wizard
Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.
 
Posts: 4,004
Karma: 177841
Join Date: Dec 2009
Device: WinMo: IPAQ; Android: HTC HD2, Archos 7o; Java:Gravity T
Quote:
Originally Posted by thczv View Post
This appears to work great. Thank you. Could you explain the nuts and bolts logic of what this does, or direct me to a web site where I can learn about it?

Thanks again.
I thought I was going above and beyond to give you the whole list of options and why I chose split/join, but sure:
Code:
    def print_version(self,url):
        #split will chop up the URL into a Python list of string pieces
        #each one was a string between a slash
        segments = url.split('/') 
        #segments is now the name of that list of strings
        printURL = '/'.join(segments[0:6]) + '/v-print/' + '/'.join(segments[6:])
        #segments[0:6] is a list containing the first six string pieces.
        # '/'.join(segments[0:6]) is a single string where the first six strings 
        #have been concatenated back as a single string, with the slashes put back 
        # segments[6:] is a list of all string pieces after the sixth
        # see above for what '/'.join(segments[6:]) is
        # The + signs concatenate it together, putting  '/v-print/' in the middle with the slashes 
        return printURL
Google slice and python and list and string
Starson17 is offline   Reply With Quote
Old 04-11-2011, 04:26 PM   #5
thczv
Junior Member
thczv began at the beginning.
 
Posts: 8
Karma: 10
Join Date: Mar 2011
Device: Kindle
You did go above and beyond. I really appreciate the additional explanation.

Thanks.
thczv is offline   Reply With Quote
Advert
Old 04-12-2011, 12:46 AM   #6
audreypots
Member
audreypots began at the beginning.
 
audreypots's Avatar
 
Posts: 10
Karma: 10
Join Date: Apr 2011
Device: Kindle
sorry to hijack the thread! but this is so helpful! thanks Starson17 and to the OP
audreypots is offline   Reply With Quote
Old 04-12-2011, 09:38 AM   #7
Starson17
Wizard
Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.Starson17 can program the VCR without an owner's manual.
 
Posts: 4,004
Karma: 177841
Join Date: Dec 2009
Device: WinMo: IPAQ; Android: HTC HD2, Archos 7o; Java:Gravity T
Quote:
Originally Posted by audreypots View Post
sorry to hijack the thread! but this is so helpful! thanks Starson17 and to the OP
On my ToDo list is a more comprehensive "How to write a recipe" sticky. Mostly it will be links to relevant resources as the User Guid already has an excellent introduction and tutorial on the subject.

I wrote a big chunk of it, but before it was ready, a lot of my links to relevant material changed when Kovid switched the bug tracker and rewrote/reorganized some of the User Guide. I also decided I was repeating too much of what Kovid had already written and decided I needed to focus even more on links, not repeating things. I've started over from the beginning.

@audreypots You aren't hijacking. I wrote the answer for people like you - hoping to explain how this problem is usually solved and to encourage others to join in and write a few recipes. Thanks for posting that you found it helpful.

Last edited by Starson17; 04-12-2011 at 09:43 AM.
Starson17 is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Markdown Syntax in Comments itimpi Calibre 11 01-25-2011 11:28 AM
Removing header syntax. boromirofborg Calibre 0 07-21-2010 12:33 AM
Metadata Filename Syntax gandor62 Calibre 15 07-18-2010 03:46 AM
Invalid Syntax Error msprang Sony Reader 7 11-07-2009 01:11 PM
Using PubDate in print_version of custom news source mobilereader72 Calibre 4 05-30-2009 05:52 PM


All times are GMT -4. The time now is 06:10 PM.


MobileRead.com is a privately owned, operated and funded community.