Skip to content

Instantly share code, notes, and snippets.

@funilrys
Last active July 14, 2019 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save funilrys/284f1c05119db5c7696eacdd8029e96a to your computer and use it in GitHub Desktop.
Save funilrys/284f1c05119db5c7696eacdd8029e96a to your computer and use it in GitHub Desktop.
Convert AdBlock - filter list - formated file into hosts file using PyFunceble.

Note

This is a quick and dirty solution for StevenBlack/hosts#1012. Let me know if you want it as a PyPi package from it.

Requirements

$ pip3 install --user PyFunceble

More info about PyFunceble

More info about the AdBlock decoder

How to run ?

$ chmod +x convert_adblock_to_hosts.py
$ ./convert_adblock_to_hosts.py -f my_awesome_filter_list -o my_new_and_awesome_hosts_file
#!/usr/bin/env python3
import argparse
from PyFunceble import is_domain
from PyFunceble.adblock import AdBlock
HOSTS_IP = "0.0.0.0"
FILE_ENCODING = "utf-8"
def convert_to_hosts_format(
file_to_convert, destination=None, print_to_stdout=True, aggressive=False
):
"""
Convert the given file into the hosts file format.
:param open file_to_convert: The file we have to convert.
:param str destination:
The destination.
.. note::
If :code:`None` is given we output to :code:`stdout`.
:param bool print_to_stdout:
Use this if you only want the output of this function.
:param bool aggressive:
An experimental argument of PyFunceble which extract
every domain of the given file without interpretation
of what is really blocked by the filter list.
.. warning::
Use at your own risk.
:return: The converted content.
:rtype: list
"""
converted = []
if isinstance(file_to_convert, list):
for file in file_to_convert:
converted.extend(
[
f"{HOSTS_IP} {x}"
for x in AdBlock(file, aggressive=aggressive).decode()
if is_domain(x)
]
)
else:
converted = [
f"{HOSTS_IP} {x}"
for x in AdBlock(file_to_convert, aggressive=aggressive).decode()
if is_domain(x)
]
if destination:
with open(destination, "w", encoding=FILE_ENCODING) as file_stream:
file_stream.write(
"# Hosts File generated with the help of the AdBlock decoder of PyFunceble - https://github.com/funilrys/PyFunceble\n\n"
)
file_stream.write("\n".join(converted))
elif print_to_stdout:
print("\n".join(converted))
return converted
if __name__ == "__main__":
PARSER = argparse.ArgumentParser(
description="A simple tool to convert AdBlock formatted files into hosts file.", # pylint: disable=line-too-long
epilog="Crafted with ♥ by Nissar Chababy (Funilrys)",
add_help=True,
)
PARSER.add_argument(
"--aggressive",
action="store_true",
help="[USE AT YOUR OWN RISK AS IT IS EXPERIMENTAL] Activate "
"the extraction of everything regardless of the interpretation of AdBlock/UBlock.",
)
PARSER.add_argument(
"-f",
"--file",
type=argparse.FileType("r", encoding=FILE_ENCODING),
nargs="+",
help="Give us the file to convert.",
)
PARSER.add_argument(
"-o", "--output", type=str, help="Tell us where we save the converted data."
)
ARGS = PARSER.parse_args()
convert_to_hosts_format(ARGS.file, ARGS.output, print_to_stdout=True, aggressive=ARGS.aggressive)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment