You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 line
3.1KB

  1. # @name: resources.py
  2. # @creation_date: 2022-02-23
  3. # @license: The MIT License <https://opensource.org/licenses/MIT>
  4. # @author: Simon Bowie <ad7588@coventry.ac.uk>
  5. # @purpose: functions for resources
  6. # @acknowledgements:
  7. # isbntools: https://isbntools.readthedocs.io/en/latest/info.html
  8. # regex for URLs: https://gist.github.com/gruber/249502
  9. from flask import Blueprint, render_template, request, flash, redirect, url_for
  10. from .models import Resource
  11. from werkzeug.exceptions import abort
  12. from . import db
  13. from .relationships import *
  14. from isbntools.app import *
  15. import requests
  16. import re
  17. from sqlalchemy.sql import func
  18. # function to retrieve data about a single resource from the database
  19. def get_resource(resource_id):
  20. resource = Resource.query.filter_by(id=resource_id).first()
  21. if resource is None:
  22. abort(404)
  23. return resource
  24. # function to retrieve data about a resource and its relationships
  25. def get_full_resource(resource_id):
  26. resource = get_resource(resource_id)
  27. resource = append_relationships(resource)
  28. if resource.type == 'book':
  29. book_data = get_book_data(resource.isbn)
  30. if book_data:
  31. resource.__dict__.update(book_data)
  32. return resource
  33. # function to retrieve data about a curated list of resources
  34. def get_curated_resources(resource_ids):
  35. resources = Resource.query.filter(Resource.id.in_(resource_ids)).order_by(func.random()).all()
  36. # append relationships to each resource
  37. append_relationships_multiple(resources)
  38. return resources
  39. # function to delete a single resource
  40. def delete_resource(resource_id):
  41. deletion = Resource.query.get(resource_id)
  42. db.session.delete(deletion)
  43. db.session.commit()
  44. flash('Successfully deleted!')
  45. # function to get filters for a specific field
  46. def get_filter_values(field, type):
  47. # get field values for filter
  48. field_filter = Resource.query.filter_by(type=type).with_entities(getattr(Resource, field))
  49. # turn SQLAlchemy object into list
  50. field_filter = [i for i, in field_filter]
  51. # split each element on '/' (useful for scriptingLanguage only)
  52. field_filter = [y for x in field_filter for y in x.split(' / ')]
  53. # consolidate duplicate values
  54. field_filter = list(dict.fromkeys(field_filter))
  55. # filter None values from list
  56. field_filter = filter(None, field_filter)
  57. # sort list by alphabetical order
  58. field_filter = sorted(field_filter)
  59. return field_filter
  60. # function to get book data including metadata and covers
  61. def get_book_data(isbn):
  62. try:
  63. book = meta(isbn)
  64. description = {'desc': desc(isbn)}
  65. book.update(description)
  66. #book = get_book_cover(book)
  67. return book
  68. except:
  69. pass
  70. # function to get book cover data
  71. def get_book_cover(book):
  72. # get highest-resolution book cover possible
  73. openl_url = 'https://covers.openlibrary.org/b/isbn/' + book['ISBN-13'] + '-L.jpg?default=false'
  74. request = requests.get(openl_url)
  75. if request.status_code != 200:
  76. book.update(cover(isbn))
  77. else:
  78. book_cover = {'thumbnail': openl_url}
  79. book.update(book_cover)
  80. return book