Generates a host list of first-party trackers for ad-blocking.
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.

61 lines
1.5 KiB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
  1. #!/usr/bin/env python3
  2. import database
  3. import argparse
  4. import sys
  5. import time
  6. import typing
  7. FUNCTION_MAP = {
  8. "zone": database.Database.set_zone,
  9. "hostname": database.Database.set_hostname,
  10. "asn": database.Database.set_asn,
  11. "ip4network": database.Database.set_ip4network,
  12. "ip4address": database.Database.set_ip4address,
  13. }
  14. if __name__ == "__main__":
  15. # Parsing arguments
  16. parser = argparse.ArgumentParser(description="Import base rules to the database")
  17. parser.add_argument(
  18. "type", choices=FUNCTION_MAP.keys(), help="Type of rule inputed"
  19. )
  20. parser.add_argument(
  21. "-i",
  22. "--input",
  23. type=argparse.FileType("r"),
  24. default=sys.stdin,
  25. help="File with one rule per line",
  26. )
  27. parser.add_argument(
  28. "-f",
  29. "--first-party",
  30. action="store_true",
  31. help="The input only comes from verified first-party sources",
  32. )
  33. args = parser.parse_args()
  34. DB = database.Database()
  35. fun = FUNCTION_MAP[args.type]
  36. source: database.RulePath
  37. if args.first_party:
  38. source = database.RuleFirstPath()
  39. else:
  40. source = database.RuleMultiPath()
  41. for rule in args.input:
  42. rule = rule.strip()
  43. try:
  44. fun(
  45. DB,
  46. rule,
  47. source=source,
  48. updated=int(time.time()),
  49. )
  50. except ValueError:
  51. DB.log.error(f"Could not add rule: {rule}")
  52. DB.save()