queuefeed.py 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #!/usr/bin/env python3
  2. # vim: tabstop=4 expandtab
  3. import datetime
  4. from os import getenv
  5. from pathlib import Path
  6. from gemini_antenna.db import AntennaDB
  7. from gemini_antenna.URLHelper import URLHelper
  8. def app() -> None:
  9. def log(msg) -> None:
  10. timestamp = datetime.datetime.utcnow().isoformat()
  11. with open(dataroot / "antenna.log", "a") as logfile:
  12. print(timestamp, msg, file=logfile)
  13. def printInvalidURL(feedurl: str) -> None:
  14. log(f"ERROR: pretty sure '{feedurl}' is not a real URL...")
  15. print("20 text/gemini", end="\r\n")
  16. print("Error adding feed.", end="\n\n")
  17. print("Feed URL is not valid.")
  18. def printIsBlocked(feedurl: str) -> None:
  19. log(f"ERROR: feed URL '{feedurl}' is blocked by rules.")
  20. print("20 text/gemini", end="\r\n")
  21. print("Error adding feed.", end="\n\n")
  22. print("Feed URL is blocked by instance rules.")
  23. dataroot = Path(getenv('ANTENNA_DATAROOT', default=str(Path.cwd())))
  24. feedurl = getenv('QUERY_STRING')
  25. db = AntennaDB(dataroot / "antenna.sqlite")
  26. uh = URLHelper(dataroot / "blocklist.txt")
  27. if not feedurl:
  28. print("10 Feed URL:", end="\r\n")
  29. return
  30. try:
  31. feedurl = uh.resolve(feedurl)
  32. except ValueError:
  33. printInvalidURL(feedurl)
  34. return
  35. if not uh.mightBeAURL(feedurl):
  36. printInvalidURL(feedurl)
  37. return
  38. if uh.isBlocked(feedurl):
  39. printIsBlocked(feedurl)
  40. return
  41. db.queueFeed(feedurl)
  42. log(f"INFO: Feed '{feedurl}' added to queue.")
  43. print("20 text/gemini", end="\r\n")
  44. print("Thank you for your submission!", end="\n\n")
  45. print("This feed will be processed in a few minutes.")
  46. def main() -> None:
  47. # TODO: handle exceptions
  48. app()
  49. if __name__ == "__main__":
  50. main()