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.

71 lines
2.0 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
  1. from flask import Flask, Response, jsonify, render_template
  2. from base64 import b64encode
  3. import feedparser
  4. import re
  5. from dotenv import load_dotenv, find_dotenv
  6. load_dotenv(find_dotenv())
  7. import requests
  8. import json
  9. import os
  10. import random
  11. GOODREADS_RSS_URL = os.getenv("GOODREADS_RSS_URL")
  12. PROGRESS_REGEX = r".*<img .* alt=\"([^\"]*) by ([^\"]*)\".*src=\"([^\"]*)\".*.* is on page ([0-9]*) of ([0-9]*) of <a.*"
  13. READ_REGEX = r".*<img .* alt=\"([^\"]*) by ([^\"]*)\".*src=\"([^\"]*)\".*finished reading.*"
  14. def loadImageB64(url):
  15. resposne = requests.get(url)
  16. return b64encode(resposne.content).decode("ascii")
  17. def makeSVG(book, author, progress, img_url):
  18. img = ''
  19. if img_url != '':
  20. img = loadImageB64(img_url)
  21. dataDict = {
  22. "progress": progress,
  23. "author": author,
  24. "book_name": book,
  25. "img": img,
  26. }
  27. return render_template("goodreads.html.j2", **dataDict)
  28. app = Flask(__name__)
  29. @app.route("/", defaults={"path": ""})
  30. @app.route("/<path:path>")
  31. def last_activity(path):
  32. activityFeed = feedparser.parse(GOODREADS_RSS_URL)
  33. data = ""
  34. entries = activityFeed.entries
  35. pr = re.compile(PROGRESS_REGEX)
  36. rr = re.compile(READ_REGEX)
  37. book, author, img, progress = '', '', '', ''
  38. for i in entries:
  39. i["summary"] = i["summary"].replace("&lt;","<").replace("&gt;",">").replace("&quot;","\"").replace("\n","")
  40. print(i["summary"])
  41. res = pr.search(i["summary"])
  42. if res:
  43. book, author, img = res.group(1,2,3)
  44. progress = int(100*int(res.group(4))/int(res.group(5)))
  45. else:
  46. res = rr.search(i["summary"])
  47. if res:
  48. book, author, img = res.group(1,2,3)
  49. progress = 100
  50. else:
  51. continue
  52. break
  53. svg = makeSVG(book, author, progress, img)
  54. resp = Response(svg, mimetype="image/svg+xml")
  55. resp.headers["Cache-Control"] = "s-maxage=1"
  56. return resp
  57. if __name__ == "__main__":
  58. app.run(debug=True)