Skip to content

Commit

Permalink
Fixed bug where book with published date with year only wouldn't parse
Browse files Browse the repository at this point in the history
  • Loading branch information
diogomonica committed Jul 27, 2020
1 parent 8bb34b2 commit e2d0dec
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
18 changes: 18 additions & 0 deletions highlights/migrations/0005_auto_20200727_1423.py
@@ -0,0 +1,18 @@
# Generated by Django 3.0.7 on 2020-07-27 14:23

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('highlights', '0004_auto_20200705_1044'),
]

operations = [
migrations.AlterField(
model_name='volume',
name='published_date',
field=models.DateField(blank=True, null=True),
),
]
2 changes: 1 addition & 1 deletion highlights/models.py
Expand Up @@ -23,7 +23,7 @@ class Volume(models.Model):
title = models.CharField(max_length=255)
subtitle = models.CharField(max_length=255)
description = models.TextField()
published_date = models.DateField()
published_date = models.DateField(blank=True, null=True)
publisher = models.CharField(max_length=255)
created_at = models.DateTimeField(auto_now=True)
page_count = models.IntegerField()
Expand Down
16 changes: 14 additions & 2 deletions highlights/views.py
Expand Up @@ -12,7 +12,7 @@
from django.contrib.auth.models import User
from .models import Email, Volume, Highlight, Entry
from bs4 import BeautifulSoup

from datetime import datetime

__BASEURL = 'https://www.googleapis.com/books/v1'

Expand Down Expand Up @@ -47,6 +47,16 @@ def to_dict(email_message):
email_message_data['alternatives'] = email_message.alternatives
return email_message_data

def try_strptime(s, fmts=['%Y-%m-%d','%Y-%m','%Y']):
for fmt in fmts:
try:
logging.warning("Trying: %s with %s" % (s, fmt))
return datetime.strptime(s, fmt).strftime('%Y-%m-%d')
except:
continue

return None # or reraise the ValueError if no format matched, if you prefer

def find_or_create_user(email):
try:
user = User.objects.get(username=email)
Expand Down Expand Up @@ -109,10 +119,12 @@ def find_or_create_volume(content):
params['title'] = google_books_reply['items'][0]['volumeInfo'].get('title',"")
params['subtitle'] = google_books_reply['items'][0]['volumeInfo'].get('subtitle',"")
params['description'] = google_books_reply['items'][0]['volumeInfo'].get('description',"")
params['published_date'] = google_books_reply['items'][0]['volumeInfo'].get('publishedDate')
params['publisher'] = google_books_reply['items'][0]['volumeInfo'].get('publisher',"Unkown Publisher")
params['page_count'] = google_books_reply['items'][0]['volumeInfo'].get('pageCount',"0")
# Published date might not confirm to the right format. Sometimes the google api only returns the year.
params['published_date']= try_strptime(google_books_reply['items'][0]['volumeInfo'].get('publishedDate'))

logging.warning(params)
volume = Volume.objects.create(**params)
logging.info("Created new volume with ID: %s" % google_id)

Expand Down

0 comments on commit e2d0dec

Please sign in to comment.