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.

65 lines
2.3KB

  1. #!/usr/bin/env python3
  2. import database
  3. import argparse
  4. import sys
  5. if __name__ == '__main__':
  6. # Parsing arguments
  7. parser = argparse.ArgumentParser(
  8. description="Export the hostnames rules stored "
  9. "in the Database as plain text")
  10. parser.add_argument(
  11. '-o', '--output', type=argparse.FileType('w'), default=sys.stdout,
  12. help="Output file, one rule per line")
  13. parser.add_argument(
  14. '-f', '--first-party', action='store_true',
  15. help="Only output rules issued from first-party sources")
  16. parser.add_argument(
  17. '-e', '--end-chain', action='store_true',
  18. help="Only output rules that are not referenced by any other")
  19. parser.add_argument(
  20. '-r', '--rules', action='store_true',
  21. help="Output all kinds of rules, not just hostnames")
  22. parser.add_argument(
  23. '-b', '--base-rules', action='store_true',
  24. help="Output base rules "
  25. "(the ones added by ./feed_rules.py) "
  26. "(implies --rules)")
  27. parser.add_argument(
  28. '-d', '--no-dupplicates', action='store_true',
  29. help="Do not output rules that already match a zone/network rule "
  30. "(e.g. dummy.example.com when there's a zone example.com rule)")
  31. parser.add_argument(
  32. '-x', '--explain', action='store_true',
  33. help="Show the chain of rules leading to one "
  34. "(and the number of references they have)")
  35. parser.add_argument(
  36. '-c', '--count', action='store_true',
  37. help="Show the number of rules per type instead of listing them")
  38. args = parser.parse_args()
  39. DB = database.Database()
  40. if args.count:
  41. assert not args.explain
  42. print(DB.count_records(
  43. first_party_only=args.first_party,
  44. end_chain_only=args.end_chain,
  45. no_dupplicates=args.no_dupplicates,
  46. rules_only=args.base_rules,
  47. hostnames_only=not (args.rules or args.base_rules),
  48. ))
  49. else:
  50. for domain in DB.list_records(
  51. first_party_only=args.first_party,
  52. end_chain_only=args.end_chain,
  53. no_dupplicates=args.no_dupplicates,
  54. rules_only=args.base_rules,
  55. hostnames_only=not (args.rules or args.base_rules),
  56. explain=args.explain,
  57. ):
  58. print(domain, file=args.output)