Created
August 6, 2021 07:35
-
-
Save brainplot/8aa46c60340a282fb7589b5c7dd8a4a6 to your computer and use it in GitHub Desktop.
Small script to clean up my uBlock origin rules
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tld==0.12.6 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from tld import get_tld | |
def cleanup_hostname(hostname): | |
if hostname != '*' and hostname != 'behind-the-scene': | |
res = get_tld(hostname, as_object=True, fix_protocol=True) | |
if res.subdomain == 'www': | |
return res.fld | |
return hostname | |
class DynamicRule: | |
def __init__(self, source, destination, request_type, action): | |
self.source = source | |
self.destination = destination | |
self.request_type = request_type | |
self.action = action | |
def __str__(self): | |
return f'{self.source} {self.destination} {self.request_type} {self.action}' | |
def __repr__(self): | |
clazz = self.__class__.__name__ | |
src = self.source | |
dst = self.destination | |
rt = self.request_type | |
action = self.action | |
return f'{clazz}({src}, {dst}, {rt}, {action})' | |
def cleanup(self): | |
return DynamicRule(cleanup_hostname(self.source), self.destination, self.request_type, self.action) | |
class PSSwitch: | |
valid_switches = [ | |
'no-strict-blocking', | |
'no-popups', | |
'no-cosmetic-filtering', | |
'no-remote-fonts', | |
'no-large-media', | |
'no-csp-reports', | |
'no-scripting' | |
] | |
def __init__(self, switch, hostname, enabled): | |
if not switch.rstrip(':') in PSSwitch.valid_switches: | |
raise ValueError(f'"{switch}" is not a valid per-site-switch') | |
if not hostname: | |
raise ValueError(f'Hostname cannot be empty or `None`') | |
self.switch = switch | |
self.hostname = hostname | |
self.enabled = enabled | |
def __str__(self): | |
return f'{self.switch} {self.hostname} {self.enabled}' | |
def __repr__(self): | |
clazz = self.__class__.__name__ | |
return f'{clazz}({self.switch}, {self.hostname}, {self.enabled})' | |
def cleanup(self): | |
return PSSwitch(self.switch, cleanup_hostname(self.hostname), self.enabled) | |
def process(line): | |
parts = line.split(' ') | |
ret = None | |
if len(parts) == 3: | |
ret = PSSwitch(parts[0], parts[1], parts[2]) | |
elif len(parts) == 4: | |
ret = DynamicRule(parts[0], parts[1], parts[2], parts[3]) | |
else: | |
raise ValueError(f'Current line does not form a valid rule: {line}') | |
return ret | |
for line in sys.stdin: | |
line = line.strip() | |
rule = process(line) | |
rule = rule.cleanup() | |
print(rule) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment