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.

55 lines
1.4KB

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