This project provides a badge for sharing your current book in your github profile.
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.

91 lines
2.4 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. import requests
  2. from flask import Flask, Response, render_template, request
  3. from base64 import b64encode
  4. import feedparser
  5. import re
  6. from dotenv import load_dotenv, find_dotenv
  7. load_dotenv(find_dotenv())
  8. PROGRESS_REGEX = r".*<img .* alt=\"([^\"]*) by ([^\"]*)\".*src=\"([^\"]*)\".*.* is on page ([0-9]*) of ([0-9]*) of <a.*"
  9. READ_REGEX = (
  10. r".*<img .* alt=\"([^\"]*) by ([^\"]*)\".*src=\"([^\"]*)\".*finished reading.*"
  11. )
  12. def loadImageB64(url):
  13. resposne = requests.get(url)
  14. return b64encode(resposne.content).decode("ascii")
  15. def makeSVG(books):
  16. return render_template("goodreads.html.j2", books=books)
  17. class Book:
  18. def __init__(self, title, author, img_url):
  19. self.title = title
  20. self.author = author
  21. self.img = ""
  22. if img_url != "":
  23. self.img = loadImageB64(img_url)
  24. def __eq__(self, other):
  25. return self.title == other.title and self.author == other.author
  26. app = Flask(__name__)
  27. @app.route("/", defaults={"path": ""})
  28. @app.route("/<path:path>")
  29. def last_activity(path):
  30. goodread_rss_url = (
  31. f'https://www.goodreads.com/user/updates_rss/{request.args.get("id")}'
  32. )
  33. print(goodread_rss_url)
  34. activityFeed = feedparser.parse(goodread_rss_url)
  35. entries = activityFeed.entries
  36. pr = re.compile(PROGRESS_REGEX)
  37. rr = re.compile(READ_REGEX)
  38. books = []
  39. for i in entries:
  40. i["summary"] = (
  41. i["summary"]
  42. .replace("&lt;", "<")
  43. .replace("&gt;", ">")
  44. .replace("&quot;", '"')
  45. .replace("\n", "")
  46. )
  47. res = pr.search(i["summary"])
  48. if res:
  49. title, author, img = res.group(1, 2, 3)
  50. book = Book(title, author, img)
  51. if book not in books:
  52. books.append(book)
  53. # progress = int(100 * int(res.group(4)) / int(res.group(5)))
  54. else:
  55. res = rr.search(i["summary"])
  56. if res:
  57. title, author, img = res.group(1, 2, 3)
  58. book = Book(title, author, img)
  59. if book not in books:
  60. books.append(book)
  61. # progress = 100
  62. else:
  63. continue
  64. if len(books) >= request.args.get("maxbooks", 3):
  65. break
  66. svg = makeSVG(books)
  67. resp = Response(svg, mimetype="image/svg+xml")
  68. resp.headers["Cache-Control"] = "s-maxage=1"
  69. return resp
  70. if __name__ == "__main__":
  71. app.run(debug=True)