View Single Post
Old 01-22-2011, 06:06 AM   #63
pchrist7
Addict
pchrist7 is kind to children and small, furry animalspchrist7 is kind to children and small, furry animalspchrist7 is kind to children and small, furry animalspchrist7 is kind to children and small, furry animalspchrist7 is kind to children and small, furry animalspchrist7 is kind to children and small, furry animalspchrist7 is kind to children and small, furry animalspchrist7 is kind to children and small, furry animalspchrist7 is kind to children and small, furry animalspchrist7 is kind to children and small, furry animalspchrist7 is kind to children and small, furry animals
 
pchrist7's Avatar
 
Posts: 385
Karma: 6514
Join Date: Aug 2010
Location: Denmark
Device: Kindle 3 3G+Wifi, Oasis
This should do it

Quote:
Originally Posted by beckywc View Post
What I want the custom column to show is the date read if > 95%. If the book shows >0% but < 95% I want the colunn to show reading and if the book is 0% or unopened not read.
Hi beckywc.
With great help from chaley, once again, I think I have what you want.
In exchange I want your help to create a howto for the sticky thread about custom columns
Please look closely at the instructions, grammar, punctuations etc.
I want this to be as precise as possible.
Also, do you understand the Documentation part in the Template function ?
Please suggest corrections, clarifications.
So please try the following instructions, to the letter, to see if I got it all right:
And of course, please post all comments to this thread.


Howto:

Do you have a Kindle ?
Did you ever want Calibre to show "Read Status" of your books in a column ?
Automagically ?

Then the following might for you as well.

You must be running Calibre 0.7.40 or newer to use this.
Also you must use "Fetch annotations" from you Kindle.

The following example is based on the following Kindler requirements:
"What I want is a custom column to show the "Last Opened" Date if Last location read > 95%.
If the book shows >0% but < 95%, I want the column to show "Reading"
And if the book is 0% or unopened the column should show "Not Read".


How-to "Fetch annotations" from you Kindle:
Start Calibre,
Connect your Kindle, wait a bit,
Click an the arrow just to the right of the "Send to Device" icon,
choose "Fetch Annotations"
and the Kindle "Book status" is added to the Calibre book details, in the comments.

You should Fetch fresh Annotations everytime you connect your Kindle to get updated information.

This is just an example of what I could with my K3:

25-12-2010
Last Page Read: Location 7365 (68%)
Location 4603 • Bookmark
Location 4942 • Bookmark
Location 4980 • Bookmark
Location 5984 • Bookmark
Location 7363 • Bookmark


The best/fastest way, but hardest way, to get the needed information into a custom column, is to use Python Template functions.
Sounds scary, but don't worry. Copy/paste is your helper

Simply do as follows:

1) go to Preferences -> Advanced -> Template Functions.

2) in the Function box type: kindle_read_status

3) in the Arg count box type: 4

4) in the Documentation box, paste the following:
Code:
Check if the associated field contains a kindle annotation for percent read. If not, return no_page_read_str. If so, then compare that percent against is_read_pct. If the val is larger, then return date, if the val is >0 and < is_read_pct return is_reading_str. Otherwise return is_not_read_str. 

One usage: {comments:kindle_read_status(95,Not Read,Reading)}
( use the above when defining your custom column )
95 goes into variable is_read_pct
Not Read goes into variable no_page_read_str
Reading goes into variable is_reading_str

Feel free to change the %, the test strings to suit your needs.
Do this in the custom column definition.
5) In the Program Code box, paste the following:
Code:
def evaluate(self, formatter, kwargs, mi, locals, val, is_read_pct,  
				is_reading_str, no_page_read_str):
	try:
		test_val = int(is_read_pct)
	except:
		return 'is_read_pct is not a number'

	import re
	mg = re.match('.*\s(\d+[-/]\d+[-/]\d\d\d\d).*?Last Page Read: Location \d+ \((\d+)%\)', val, re.I + re.DOTALL);
	if mg is None:
		return no_page_read_str
	date = mg.group(1)
	pct = mg.group(2)
	try:
		f = int(pct)
		if f > test_val:
			return date
		elif f > 0:
			return is_reading_str + ': ' + pct + '%'
	except:
		pass
	return no_page_read_str
6) press the Create button, then the Apply button.

7) Still in Preferences, Choose Add your own columns

8) Choose Add custom column bar at the bottom

9) enter the following into your custom column:
Lookup name: read_status
Column Heading: Read Status
Column type: Column built from other columns
Template:
Code:
{comments:kindle_read_status(95,Reading,Not Read)}
10) Choose Apply

11) Click Ok to the Restart Warning

12) Shutdown Calibre

13) Start Calibre

If you followed instructions minutely, you should have a new column showing the "Read Status" for your books.

If you like this functionality, please remember to send some Karma to chaley

https://www.mobileread.com/forums/rep....php?p=1351864

He (?) has been absolutely fantastic and helpfull with this.

If this fails, please try one more time before posting to this thread.
This has been tested by several Kindlers succesfully.
Enjoy

Edit: Using the code formatting of the Template function documentation, looses all line breaks during copy/paste. Any suggestions ?
Also, need help with the "chaley karma" link

Last edited by pchrist7; 01-23-2011 at 05:10 AM. Reason: clarification
pchrist7 is offline   Reply With Quote