Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

118 lines
4.3KB

  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. import json
  18. from sqlalchemy.sql import func
  19. import markdown
  20. # function to retrieve data about a single resource from the database
  21. def get_resource(resource_id):
  22. resource = Resource.query.filter_by(id=resource_id).first()
  23. if resource is None:
  24. abort(404)
  25. return resource
  26. # function to retrieve data about a resource and its relationships
  27. def get_full_resource(resource_id):
  28. resource = get_resource(resource_id)
  29. resource = append_relationships(resource)
  30. if resource.type == 'book':
  31. # render Markdown as HTML
  32. resource.description = markdown.markdown(resource.description)
  33. # get additional book metadata
  34. book_data = get_book_data(resource.isbn)
  35. if book_data:
  36. resource.__dict__.update(book_data)
  37. # if there's a GitHub repository link, get last GitHub commit date
  38. if resource.repositoryUrl and "github" in resource.repositoryUrl:
  39. # get commit date
  40. date = get_commit_date(resource.repositoryUrl)
  41. resource.__dict__['commitDate'] = date
  42. return resource
  43. # function to retrieve data about a curated list of resources
  44. def get_curated_resources(resource_ids):
  45. resources = Resource.query.filter(Resource.id.in_(resource_ids)).filter_by(published=True).order_by(func.random()).all()
  46. # append relationships to each resource
  47. append_relationships_multiple(resources)
  48. return resources
  49. # function to delete a single resource
  50. def delete_resource(resource_id):
  51. deletion = Resource.query.get(resource_id)
  52. db.session.delete(deletion)
  53. db.session.commit()
  54. flash('Successfully deleted!')
  55. # function to get filters for a specific field
  56. def get_filter_values(field, type):
  57. # get field values for filter
  58. field_filter = Resource.query.filter_by(type=type).filter_by(published=True).with_entities(getattr(Resource, field))
  59. # turn SQLAlchemy object into list
  60. field_filter = [i for i, in field_filter]
  61. # split each element on '/' (useful for scriptingLanguage only)
  62. field_filter = [y for x in field_filter for y in x.split(' / ')]
  63. # consolidate duplicate values
  64. field_filter = list(dict.fromkeys(field_filter))
  65. # filter None values from list
  66. field_filter = filter(None, field_filter)
  67. # sort list by alphabetical order
  68. field_filter = sorted(field_filter)
  69. return field_filter
  70. # function to get book data including metadata and covers
  71. def get_book_data(isbn):
  72. try:
  73. book = meta(isbn)
  74. description = {'desc': desc(isbn)}
  75. book.update(description)
  76. #book = get_book_cover(book)
  77. return book
  78. except:
  79. pass
  80. # function to get book cover data
  81. def get_book_cover(book):
  82. # get highest-resolution book cover possible
  83. openl_url = 'https://covers.openlibrary.org/b/isbn/' + book['ISBN-13'] + '-L.jpg?default=false'
  84. request = requests.get(openl_url)
  85. if request.status_code != 200:
  86. book.update(cover(isbn))
  87. else:
  88. book_cover = {'thumbnail': openl_url}
  89. book.update(book_cover)
  90. return book
  91. # function to retrieve last updated date from the database
  92. def get_last_date():
  93. resource = Resource.query.order_by(Resource.created.asc()).filter_by(published=True).first()
  94. return resource.created.isoformat()
  95. # function to retrieve last commit date from a GitHub repository for a tool
  96. def get_commit_date(repositoryUrl):
  97. # change repository URL to API URL
  98. api_url = repositoryUrl.replace("https://github.com", "https://api.github.com/repos")
  99. # get default branch name
  100. r = requests.get(api_url)
  101. r = json.loads(r.content)
  102. branch = r['default_branch']
  103. # get date of last commit on default branch
  104. api_url = api_url + "/branches/" + branch
  105. r = requests.get(api_url)
  106. r = json.loads(r.content)
  107. date = r['commit']['commit']['author']['date'][0:10]
  108. return date