customFilters.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. #!/usr/bin/env python3
  2. # vim: tabstop=4 expandtab
  3. import datetime, time
  4. from urllib.parse import unquote
  5. from os import getenv
  6. from os.path import exists
  7. import string
  8. import random
  9. import URLHelper
  10. import antennaDB
  11. import signoffs
  12. def randomFileName():
  13. return ''.join(random.choice(string.ascii_letters) for x in range(32))
  14. filterFile = getenv('PATH_INFO')
  15. filterFilePathParts = filterFile.split("/")
  16. if "" in filterFilePathParts:
  17. filterFilePathParts.remove("")
  18. db = antennaDB.AntennaDB()
  19. lastRead = "Last Read:" + str(int(time.mktime(datetime.datetime.utcnow().utctimetuple()))) + "\n"
  20. if not filterFile or not exists("customfilters/"+filterFilePathParts[0]):
  21. if not getenv('QUERY_STRING') or not getenv('QUERY_STRING').lower() in ["y","ye","yes"]:
  22. print("10 Type 'y' if you want to create a filter, otherwise go back.\r")
  23. else:
  24. filterFile = randomFileName()
  25. while exists("customfilters/"+filterFile):
  26. filterFile = randomFileName()
  27. with open("customfilters/"+filterFile, "w") as f:
  28. f.write(lastRead)
  29. print("30 gemini://"+getenv('SERVER_NAME')+getenv('SCRIPT_NAME')+"/"+filterFile+"\r")
  30. else:
  31. f = open("customfilters/"+filterFilePathParts[0], "r")
  32. rules = f.read().split("\n")
  33. f.close()
  34. f = open("customfilters/"+filterFilePathParts[0], "w")
  35. rules = list(filter(("").__ne__, rules))
  36. rules.pop(0)
  37. affectedRule = getenv('QUERY_STRING')
  38. if filterFilePathParts[-1] == "add":
  39. if not affectedRule:
  40. print("10 Rule to add:\r\n")
  41. else:
  42. rules.insert(0,unquote(affectedRule))
  43. print("30 gemini://"+getenv('SERVER_NAME')+getenv('SCRIPT_NAME')+"/"+filterFilePathParts[0]+"\r\n")
  44. elif filterFilePathParts[-1] == "remove":
  45. if not affectedRule or not affectedRule in rules:
  46. print("10 Rule to remove:\r\n")
  47. else:
  48. rules.remove(affectedRule)
  49. print("30 gemini://"+getenv('SERVER_NAME')+getenv('SCRIPT_NAME')+"/"+filterFilePathParts[0]+"\r\n")
  50. elif filterFilePathParts[-1] == "read":
  51. print("20 text/gemini\r")
  52. print("# Your Filter Rules\n")
  53. if not rules:
  54. print("Bookmark this URL. It's your personal filter. It'll be here forever if you check it often, but if you don't check it in 90 days it'll be removed.\n\nYou should add some rules here! The way it works is that any link on Antenna that starts with a matching rule is removed from this view.\n\nFor example: the rule \"gemini://rockstar\" will remove every link starting with that, including \"gemini://rockstar.com/posts/1\" or \"gemini://rockstarfamily.org\"\n")
  55. for rule in rules:
  56. print("=> remove?"+rule+" "+rule+" (Click to remove)")
  57. print("")
  58. feedURLSet = set()
  59. for entry in db.getEntries():
  60. if entry.feedurl in rules:
  61. continue
  62. feedURLSet.add(entry.feedurl)
  63. for feedURL in feedURLSet:
  64. print("=> add?"+feedURL+" Click to block '"+feedURL+"'")
  65. print("\n=> "+getenv('SCRIPT_NAME')+"/"+filterFilePathParts[0]+"/add Click to add custom rule")
  66. else: # This is where we end up when we just want to read the feed.
  67. print("20 text/gemini\r")
  68. print("=> "+getenv('SCRIPT_NAME')+"/"+filterFilePathParts[0]+"/read Configure your filter.\n")
  69. print("# Your Filtered Feed")
  70. datestamp = "0000-00-00"
  71. for entry in db.getEntries():
  72. blocked = False
  73. for rule in rules:
  74. blocked = entry.link.startswith(rule) or entry.feedurl.startswith(rule)
  75. if blocked:
  76. break
  77. if blocked:
  78. continue
  79. timestamp = datetime.datetime.utcfromtimestamp(entry.updated).strftime('%Y-%m-%d')
  80. if not datestamp == timestamp:
  81. datestamp = timestamp
  82. print("")
  83. print("=> " + entry.link + " " + timestamp + " " + entry.author + ": " + entry.title)
  84. print("\n> " + signoffs.getsig())
  85. f.write(lastRead + "\n".join(rules) + "\n")
  86. f.close()