summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorlxf <sng@luxagraf.net>2020-12-20 10:41:36 -0500
committerlxf <sng@luxagraf.net>2020-12-20 10:41:36 -0500
commit099d4cf95be062e9f5259a323f54d6e7df4ec301 (patch)
treed0e3e59460888f80a0bb0f108fd10e9b580b8bbe /app
parent25099bf480cbe8b13852ad45152946819ac4a812 (diff)
fixed kindle parser
Diffstat (limited to 'app')
-rw-r--r--app/books/kindleparser.py64
-rw-r--r--app/books/migrations/0015_auto_20201220_1035.py18
-rw-r--r--app/books/models.py2
3 files changed, 40 insertions, 44 deletions
diff --git a/app/books/kindleparser.py b/app/books/kindleparser.py
index 93edfac..b093562 100644
--- a/app/books/kindleparser.py
+++ b/app/books/kindleparser.py
@@ -17,7 +17,6 @@ class KindleClippingsParser(object):
def parse(self):
with open(self.path, 'r') as f:
# Individual highlights within clippings are separated by ==========
- old_title = ''
for highlight in f.read().split("=========="):
page = None
location = None
@@ -40,27 +39,16 @@ class KindleClippingsParser(object):
page = lines[1].split("- Your Highlight on page ")[1].split(" |")[0]
except IndexError:
try:
- location = lines[1].split("- Your Highlight on Location ")[1].split(" |")[0]
+ page = lines[1].split("- Your Highlight on Location ")[1].split(" |")[0]
except IndexError:
page = lines[1].split("- Your Note on page ")[1].split(" |")[0]
date = lines[1]
date = date.split("| Added on ")[1].split(",", 1)[1]
# date string looks like: "November 23, 2020 11:22:41 PM"
date_time_obj = datetime.datetime.strptime(date.strip(), '%B %d, %Y %H:%M:%S %p')
- #print(date, date_time_obj)
- #print(title)
- #print(author)
- #if page:
- # print(page)
- #if location:
- # print(location)
- #print(lines[3])
- #print("---------------")
- if old_title != title:
- self.create_highlight(title, date_time_obj)
- old_title = title
+ self.create_highlight(title, date_time_obj, page, lines[3])
- def create_highlight(self, title, date):
+ def create_highlight(self, title, date, page, clip):
print(title)
try:
book = Book.objects.get(kindle_title=title)
@@ -79,35 +67,25 @@ class KindleClippingsParser(object):
)
if created:
print(book)
- """
- book, created = Book.objects.get_or_create(
- kindle_title=title,
- author_name=author_name,
- )
try:
- #see if we already this highlight
- h = BookHighlight.objects.get(
- book__title=clip['title'],
- )
- #print(bh.book.title)
- print(location)
- #bh.location = location
- #bh.page = page
- #bh.save()
- #if we don't create a new book highlight
- except ObjectDoesNotExist:
- book = Book.objects.get(title=clip['title'])
- print(book.title)
- print(page)
- print(location)
- bh, created = BookHighlight.objects.get_or_create(
- book=book,
- page=page,
- location=location,
- date_added=clip_date,
- body_markdown=body_markdown
- )
- """
+ #see if we already this highlight
+ h = BookHighlight.objects.get(
+ book__title=title,
+ date_highlighted=date,
+ )
+ #if we don't create a new book highlight
+ except ObjectDoesNotExist:
+ print("hightlight: %s" % book.title)
+ print("page : %s" % page)
+ print("on date: %s" % date)
+ print("quote: %s" % clip)
+ print("--------------")
+ bh, created = BookHighlight.objects.get_or_create(
+ book=book,
+ page=page,
+ date_highlighted=date,
+ body_markdown=clip
+ )
diff --git a/app/books/migrations/0015_auto_20201220_1035.py b/app/books/migrations/0015_auto_20201220_1035.py
new file mode 100644
index 0000000..e36aeb7
--- /dev/null
+++ b/app/books/migrations/0015_auto_20201220_1035.py
@@ -0,0 +1,18 @@
+# Generated by Django 3.1 on 2020-12-20 10:35
+
+from django.db import migrations, models
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('books', '0014_auto_20201218_0950'),
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='bookhighlight',
+ name='page',
+ field=models.CharField(max_length=30),
+ ),
+ ]
diff --git a/app/books/models.py b/app/books/models.py
index c003b25..c25cdc5 100644
--- a/app/books/models.py
+++ b/app/books/models.py
@@ -105,7 +105,7 @@ def post_save_events(sender, update_fields, created, instance, **kwargs):
class BookHighlight(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE, blank=True)
- page = models.PositiveSmallIntegerField()
+ page = models.CharField(max_length=30)
date_created = models.DateTimeField(blank=True, auto_now_add=True)
date_highlighted = models.DateTimeField()
body_markdown = models.TextField()