diff options
author | luxagraf <sng@luxagraf.net> | 2015-10-01 22:10:21 -0400 |
---|---|---|
committer | luxagraf <sng@luxagraf.net> | 2015-10-01 22:10:21 -0400 |
commit | e0de7a6c08bbbd31fae2e7b796457a982cb3e895 (patch) | |
tree | 998de97c36aef12098f5545d3dcfd690e600b23b /app/books | |
parent | 93ac5f4acb8d1249461425c97d46bbf69915112d (diff) |
did a lot of work on the books section
Diffstat (limited to 'app/books')
-rw-r--r-- | app/books/kindleparser.py | 14 | ||||
-rw-r--r-- | app/books/models.py | 19 | ||||
-rw-r--r-- | app/books/retriever.py | 60 | ||||
-rw-r--r-- | app/books/urls.py | 9 | ||||
-rw-r--r-- | app/books/views.py | 32 |
5 files changed, 128 insertions, 6 deletions
diff --git a/app/books/kindleparser.py b/app/books/kindleparser.py index aaa742f..44a4686 100644 --- a/app/books/kindleparser.py +++ b/app/books/kindleparser.py @@ -22,11 +22,11 @@ def parse_kindle_clippings(path): except KeyError: body_markdown = "" try: - location = int(clip['locationRange']) + location = clip['locationRange'] except: location = 0 try: - page = int(clip['pageRange']) + page = int(clip['pageRange'][0]) except: page = 0 try: @@ -44,13 +44,21 @@ def parse_kindle_clippings(path): ) try: #see if we already this highlight - row = BookHighlight.objects.get( + bh = BookHighlight.objects.get( book__title=clip['title'], date_added=clip_date ) + #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, diff --git a/app/books/models.py b/app/books/models.py index cfa8379..14c7160 100644 --- a/app/books/models.py +++ b/app/books/models.py @@ -4,6 +4,7 @@ from django.conf import settings from django.template.defaultfilters import slugify from taggit.managers import TaggableManager +import markdown class Book(models.Model): title = models.CharField(max_length=200) @@ -13,8 +14,13 @@ class Book(models.Model): read_date = models.DateTimeField() isbn = models.CharField(max_length=100, blank=True, null=True) body_html = models.TextField(null=True, blank=True) + read_in = models.TextField(null=True, blank=True) url = models.CharField(max_length=200, blank=True, null=True) tags = TaggableManager() + pages = models.CharField(max_length=5, blank=True, null=True) + publish_date = models.CharField(max_length=40, blank=True, null=True) + publish_place = models.CharField(max_length=100, blank=True, null=True) + openlib_url = models.CharField(max_length=400, blank=True, null=True) RATINGS = ( ('1', "1 Star"), ('2', "2 Stars"), @@ -22,7 +28,7 @@ class Book(models.Model): ('4', "4 Stars"), ('5', "5 Stars"), ) - rating = models.CharField(max_length=1, choices=RATINGS, null=True) + rating = models.CharField(max_length=1, choices=RATINGS, null=True, blank=True) enable_comments = models.BooleanField(default=False) image = models.FileField(upload_to='book-covers/', null=True, blank=True) @@ -32,8 +38,11 @@ class Book(models.Model): def __str__(self): return self.title + def get_absolute_url(self): + return "/books/%s" % (self.slug) + def get_image_url(self): - return '/media/%s' % (self.image) + return '%sbook-covers/%s.jpg' % (settings.IMAGES_URL, self.slug) def admin_thumbnail(self): return force_text('<a href=""><img src="%s" width="100" style="width:100px"></a>' % (self.get_image_url())) @@ -44,11 +53,12 @@ class Book(models.Model): self.slug = slugify(self.title[:50]) super(Book, self).save() +#class Book class BookHighlight(models.Model): book = models.ForeignKey(Book) page = models.PositiveSmallIntegerField(null=True) - location = models.PositiveSmallIntegerField(null=True) + location = models.CharField(max_length=200, blank=True, null=True) date_added = models.DateTimeField() body_markdown = models.TextField() @@ -58,6 +68,9 @@ class BookHighlight(models.Model): def __str__(self): return "%s%s" % (self.book.title, self.id) + def body_html(self): + return markdown.markdown(self.body_markdown, extensions=['extra'], safe_mode=False) + class BookNote(BookHighlight): pass diff --git a/app/books/retriever.py b/app/books/retriever.py new file mode 100644 index 0000000..e98f46f --- /dev/null +++ b/app/books/retriever.py @@ -0,0 +1,60 @@ +import requests +url = "https://www.googleapis.com/books/v1/volumes?q=isbn:%s&key=AIzaSyCCYn5v58R08msS06pRfOzTHANUuG2tSHI" % (book.isbn) + +def get_book_isbn(book): + print(book.title) + if book.author_name != "Instapaper": + url = "https://www.googleapis.com/books/v1/volumes?q=%s+inauthor:%s&key=AIzaSyCCYn5v58R08msS06pRfOzTHANUuG2tSHI" % (book.title, book.author_name) + r = requests.get(url, timeout=3.001) + data = r.json() + isbn10 = "" + isbn13 = "" + for item in data["items"]: + for isbn in item["volumeInfo"]["industryIdentifiers"]: + print(isbn['identifier']) + if isbn['type'] == "ISBN_13": + isbn13 = isbn['identifier'] + elif isbn['type'] == "ISBN_10": + isbn10 = isbn['identifier'] + print(isbn10) + return isbn10 + break + +import json +def get_book_data(book): + if book.author_name != "Instapaper": + url = "https://openlibrary.org/api/books?bibkeys=ISBN:%s&format=json&jscmd=data" % book.isbn + r = requests.get(url, timeout=9.001) + j = json.loads(r.text) + obj = "ISBN:%s" % book.isbn + try: + pages = j[obj]['number_of_pages'] + except: + pages = '' + try: + publish_date = j[obj]['publish_date'] + except: + publish_date = '' + try: + publish_places = j[obj]['publish_places'][0]['name'] + except: + publish_places = '' + try: + openlib_url = j[obj]['url'] + except: + openlib_url = '' + print("pages " + str(pages)) + print("date " + publish_date) + print("place " + publish_places) + print("openlib " + openlib_url) + book.pages = pages + book.publish_date = publish_date + book.publish_place = publish_places + book.openlib_url = openlib_url + book.save() + +def fetch_image(book): + path = '%s/%s/%s.jpg' %(settings.IMAGES_ROOT, 'book-covers', book.slug) + r = requests.get(book.image, timeout=6.001) + im = io.StringIO(r.content) # constructs a StringIO holding the image + im.save(path) diff --git a/app/books/urls.py b/app/books/urls.py new file mode 100644 index 0000000..003d6d6 --- /dev/null +++ b/app/books/urls.py @@ -0,0 +1,9 @@ +from django.conf.urls import * +from django.views.generic.base import RedirectView + +urlpatterns = patterns('', + #url(r'(?P<slug>[-\w]+)/(?P<page>\d+)/$', 'blog.views.entry_list_by_area'), + url(r'(?P<page>\d+)/$', 'books.views.book_list'), + url(r'(?P<slug>[-\w]+)/$', 'books.views.book_detail'), + url(r'^$', RedirectView.as_view(url="/books/1/")), +) diff --git a/app/books/views.py b/app/books/views.py new file mode 100644 index 0000000..7809f17 --- /dev/null +++ b/app/books/views.py @@ -0,0 +1,32 @@ +from django.shortcuts import render_to_response, get_object_or_404 +from django.template import RequestContext +from django.http import Http404 +from django.conf import settings +# from django.views.generic import ListView + +from books.models import Book +from locations.models import Region, Country +from photos.models import Photo + + + +def book_detail(request, slug): + obj = get_object_or_404(Book, slug__exact=slug) + return render_to_response('details/book.html', {'object': obj}, context_instance=RequestContext(request)) + +def book_list(request, page): + request.page_url = '/book/%d/' + request.page = int(page) + try: + is_build = request.POST['builder'] + extra_context={ + 'page':page, + 'MEDIA_URL': settings.BAKED_MEDIA_URL + } + except: + extra_context={'page':page} + context = { + 'object_list': Book.objects.order_by('-read_date').select_related(), + 'page': page + } + return render_to_response("archives/books.html", context, context_instance=RequestContext(request)) |