customFilters.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env python3
  2. # vim: tabstop=4 expandtab
  3. import re
  4. from os import getenv
  5. import antennaDB
  6. import signoffs
  7. import datetime
  8. import hashlib
  9. def getHashes(query = ""):
  10. hashes = []
  11. p = re.compile('^[a-zA-Z\d]{10}$')
  12. if query:
  13. parts = query.split('&')
  14. for part in parts:
  15. if p.match(part):
  16. hashes.append(part)
  17. return hashes
  18. def urlHash(url = ""):
  19. hashObject = hashlib.md5(url.encode())
  20. return hashObject.hexdigest()[0:10]
  21. def filteredEntries(hashes = [], entries = []):
  22. filtered = []
  23. for entry in entries:
  24. if not urlHash(entry.feedurl) in hashes:
  25. filtered.append(entry)
  26. return filtered
  27. def generatePage(hashes = [], entries = [], pathInfo = ""):
  28. fullPath = f"gemini://{getenv('SERVER_NAME')}{getenv('SCRIPT_NAME')}"
  29. print("20 text/gemini\r\n")
  30. if pathInfo == "config":
  31. print("# Configure Your Filter\n")
  32. print(f"=> {fullPath}?{'&'.join(hashes)} Your current filter.\n")
  33. feedurls= []
  34. for entry in entries:
  35. if entry.feedurl in feedurls:
  36. continue
  37. feedurls.append(entry.feedurl)
  38. feedurlHash = urlHash(entry.feedurl)
  39. if feedurlHash in hashes:
  40. tmphashes = hashes.copy()
  41. tmphashes.remove(feedurlHash)
  42. print(f"=> {fullPath}/config?{'&'.join(tmphashes)} Remove '{entry.feedurl}' from filter.")
  43. else:
  44. print(f"=> {fullPath}/config?{'&'.join(hashes + [feedurlHash])} Add '{entry.feedurl}' to filter.")
  45. else: # We're not configuring. Just reading.
  46. print("# Your Antenna View\n")
  47. print(f"=> {fullPath}/config?{'&'.join(hashes)} Configure your filter.")
  48. datestamp = "0000-00-00"
  49. for entry in filteredEntries(hashes, entries):
  50. timestamp = datetime.datetime.utcfromtimestamp(entry.updated).strftime('%Y-%m-%d')
  51. if not datestamp == timestamp:
  52. datestamp = timestamp
  53. print("")
  54. print(f"=> {entry.link} {timestamp} {entry.author}: {entry.title}")
  55. print(f"\n> {signoffs.getsig()}\n")
  56. ## MAIN ##
  57. db = antennaDB.AntennaDB()
  58. pathInfo = getenv('PATH_INFO')
  59. queryString = getenv('QUERY_STRING')
  60. if not pathInfo and not queryString:
  61. print("10 Would you like to create a filter? (y/yes or turn back)")
  62. elif not pathInfo and queryString in ["y","yes"]:
  63. generatePage([], db.getEntries(), "config")
  64. else:
  65. hashes = getHashes(queryString)
  66. generatePage(hashes, db.getEntries(), pathInfo)