summaryrefslogtreecommitdiff
path: root/app/books/kindleparser.py
diff options
context:
space:
mode:
Diffstat (limited to 'app/books/kindleparser.py')
-rw-r--r--app/books/kindleparser.py57
1 files changed, 57 insertions, 0 deletions
diff --git a/app/books/kindleparser.py b/app/books/kindleparser.py
new file mode 100644
index 0000000..b2f0191
--- /dev/null
+++ b/app/books/kindleparser.py
@@ -0,0 +1,57 @@
+"""
+kindle My Clippings.txt parsed to json by:
+klip: https://www.npmjs.org/package/klip
+This script then parses that json and tries to load it into the django database
+"""
+import json
+import datetime
+from django.core.exceptions import ObjectDoesNotExist
+from books.models import Book, BookHighlights
+
+def parse_kindle_clippings(path):
+ json_data = open(path)
+ data = json.load(json_data)
+ for item in data:
+ for clip in item['clippings']:
+ if clip["type"] != "Bookmark":
+ book = clip['title']
+ clip_date = datetime.datetime.strptime(clip['date'], "%Y-%m-%dT%H:%M:%S.000Z")
+ try:
+ body_markdown = clip['content']
+ except KeyError:
+ body_markdown = ""
+ try:
+ location = int(clip['locationRange'])
+ except:
+ location = 0
+ try:
+ page = int(clip['pageRange'])
+ except:
+ page = 0
+ try:
+ author_name = clip['author']
+ except KeyError:
+ author_name = ''
+ try:
+ #see if we already have this book:
+ row = Book.objects.get(title=clip['title'])
+ except ObjectDoesNotExist:
+ b, created = Book.objects.get_or_create(
+ title = clip['title'],
+ author_name = author_name,
+ read_date = clip_date
+ )
+ try:
+ #see if we already this highlight
+ row = BookHighlights.objects.get(book__title=clip['title'], date_added=clip_date )
+ #if we don't create a new book highlight
+ except ObjectDoesNotExist:
+ book = Book.objects.get(title=clip['title'])
+ bh, created = BookHighlights.objects.get_or_create(
+ book = book,
+ page = page,
+ location = location,
+ date_added = clip_date,
+ body_markdown = body_markdown
+ )
+