Skip to content

Instantly share code, notes, and snippets.

@chew-z
Created September 18, 2015 12:55
Show Gist options
  • Save chew-z/744194d530a07d556ccd to your computer and use it in GitHub Desktop.
Save chew-z/744194d530a07d556ccd to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# Takes converted for privoxy (wrongly*) easylist/easyprivacy action file
# and extracts list of adresses that privoxy should block
# See https://github.com/Andrwe/privoxy-blocklist
# and my
# (*) privoxy won't interpret '\.' as regex escape in hostnames
import re
import os
host_pattern = re.compile(
# '^(\.((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\\.)+[A-Za-z]{2,6})')
'^(\.((?!-)[A-Za-z0-9-]{1,63}(?<!-)\\\.)+[A-Za-z]{2,6})(\/|\[)(.*)\n')
os.unlink("easyprivacy.action")
# touch file first if it doesn't exist
output = open("easyprivacy.action", "w")
output.write("# Block from AdBlock easylist\n")
output.write("# \n")
output.write("\n# This is privoxy statement!\n{+block{easylist.}}\n\n")
with open('easyprivacy.script.action', 'r') as f:
for line in f.readlines():
m = host_pattern.match(line)
if m:
if m.group(0) is not None:
# output.write(m.group(0)) # entire match
# output.write("host: " + m.group(1) + "\n") # host
h = re.sub(r"(\\\.)", ".", m.group(1))
output.write(h + m.group(3) + m.group(4) + "\n")
# output.write(m.group(2) + "\n") # some substring
# output.write("separator: " + m.group(3) + "\n") # / or [
# output.write("all the rest: " + m.group(4) + "\n") # rest
output.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment