customFilters.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 antennaDB
  10. import signoffs
  11. def randomFileName():
  12. return ''.join(random.choice(string.ascii_letters) for x in range(32))
  13. filterFile = getenv('PATH_INFO')
  14. filterFilePathParts = filterFile.split("/")
  15. if "" in filterFilePathParts:
  16. filterFilePathParts.remove("")
  17. db = antennaDB.AntennaDB()
  18. lastRead = "Last Read:" + str(int(time.mktime(datetime.datetime.utcnow().utctimetuple()))) + "\n"
  19. if not filterFile or not exists("customfilters/"+filterFilePathParts[0]):
  20. if not getenv('QUERY_STRING') or not getenv('QUERY_STRING').lower() in ["y","ye","yes"]:
  21. print("10 Type 'y' if you want to create a filter, otherwise go back.\r")
  22. else:
  23. filterFile = randomFileName()
  24. while exists("customfilters/"+filterFile):
  25. filterFile = randomFileName()
  26. with open("customfilters/"+filterFile, "w") as f:
  27. f.write(lastRead)
  28. print("30 gemini://"+getenv('SERVER_NAME')+getenv('SCRIPT_NAME')+"/"+filterFile+"\r")
  29. else:
  30. f = open("customfilters/"+filterFilePathParts[0], "r")
  31. rules = f.read().split("\n")
  32. f.close()
  33. f = open("customfilters/"+filterFilePathParts[0], "w")
  34. rules = list(filter(("").__ne__, rules))
  35. rules.pop(0)
  36. affectedRule = getenv('QUERY_STRING')
  37. affectedRule = unquote(affectedRule)
  38. if filterFilePathParts[-1] == "add":
  39. if not affectedRule:
  40. print("10 Rule to add:\r\n")
  41. else:
  42. if affectedRule not in rules:
  43. rules.insert(0, affectedRule)
  44. print("30 gemini://"+getenv('SERVER_NAME')+getenv('SCRIPT_NAME')+"/"+filterFilePathParts[0]+"\r\n")
  45. elif filterFilePathParts[-1] == "remove":
  46. if not affectedRule or not affectedRule in rules:
  47. print("10 Rule to remove:\r\n")
  48. else:
  49. rules.remove(affectedRule)
  50. print("30 gemini://"+getenv('SERVER_NAME')+getenv('SCRIPT_NAME')+"/"+filterFilePathParts[0]+"\r\n")
  51. elif filterFilePathParts[-1] == "read":
  52. print("20 text/gemini\r")
  53. print("# Your Filter Rules\n")
  54. if not rules:
  55. 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")
  56. for rule in rules:
  57. print("=> remove?"+rule+" "+rule+" (Click to remove)")
  58. print("")
  59. feedURLSet = set()
  60. for entry in db.getEntries():
  61. if entry.feedurl in rules:
  62. continue
  63. feedURLSet.add(entry.feedurl)
  64. for feedURL in feedURLSet:
  65. print("=> add?"+feedURL+" Click to block '"+feedURL+"'")
  66. print("\n=> "+getenv('SCRIPT_NAME')+"/"+filterFilePathParts[0]+"/add Click to add custom rule")
  67. else: # This is where we end up when we just want to read the feed.
  68. print("20 text/gemini\r")
  69. print("=> "+getenv('SCRIPT_NAME')+"/"+filterFilePathParts[0]+"/read Configure your filter.\n")
  70. print("# Your Filtered Feed")
  71. datestamp = "0000-00-00"
  72. for entry in db.getEntries():
  73. blocked = False
  74. for rule in rules:
  75. blocked = entry.link.startswith(rule) or entry.feedurl.startswith(rule)
  76. if blocked:
  77. break
  78. if blocked:
  79. continue
  80. timestamp = datetime.datetime.utcfromtimestamp(entry.updated).strftime('%Y-%m-%d')
  81. if not datestamp == timestamp:
  82. datestamp = timestamp
  83. print("")
  84. print("=> " + entry.link + " " + timestamp + " " + entry.author + ": " + entry.title)
  85. print("\n> " + signoffs.getsig())
  86. f.write(lastRead + "\n".join(rules) + "\n")
  87. f.close()