def test_parse_filterlist(): result = parse_filterlist([ '[Adblock Plus 1.1]', ' ! Last modified: 26 Jul 2018 02:10 UTC ', '! Homepage : http://aaa.com/b', '||example.com^', '! Checksum: OaopkIiiAl77sSHk/VAWDA', '! Note: bla bla' ]) assert next(result) == Header('Adblock Plus 1.1') # Check that trailing space is not stripped (like in ABP). assert next(result) == Metadata('Last modified', '26 Jul 2018 02:10 UTC ') assert next(result) == Metadata('Homepage', 'http://aaa.com/b') assert next(result).type == 'filter' assert next(result) == Metadata('Checksum', 'OaopkIiiAl77sSHk/VAWDA') assert next(result).type == 'comment' with pytest.raises(StopIteration): next(result)
def get_rules_for_url(url): """This function gets all the adblock rules appliciable to a specific domain. Args: url (str): The url to check. In format https://url or url. Returns: [list]: The list of appliciable rules. """ # Make sure we do not have https/http with the url. url = url.replace("https://", "") url = url.replace("http://", "") url = url.replace("/", "") css_rules = [] with open("list.txt") as filterlist: rules = parse_filterlist(filterlist) rules = [rule for rule in rules if isinstance(rule, Filter)] for rule in rules: if rule.selector.get("type") == "css": # Check if there is a specific domain for the specific rule. options = [(key, value) for key, value in rule.options if key == "domain"] # If not, its appliciable everywhere, add it to list. if len(options) == 0: css_rules.append(rule.selector.get("value")) continue # there is only one domain option, if applicable then add rule. _, domains = options[0] domains = [(domain, applicable) for domain, applicable in domains if applicable == True] if len(domains) == 0: css_rules.append(rule.selector.get("value")) continue # Loop through the domains, if domain is matched, add rule. for domain_opts, _ in domains: if domain_opts in url: css_rules.append(rule.selector.get("value")) return css_rules
def fl_lines(): with open(os.path.join(DATA_PATH, 'filterlist.txt')) as f: return list(parse_filterlist(f))
def test_exception_timing(): result = parse_filterlist(['! good line', '%bad line%']) assert next(result) == Comment('good line') with pytest.raises(ParseError): next(result)
def test_parse_filterlist(): result = parse_filterlist(['! foo', '! Title: bar']) assert list(result) == [Comment('foo'), Metadata('Title', 'bar')]
def test_exception_timing(): result = parse_filterlist(['! good line', '%bad line%']) assert next(result) == Comment('good line') with pytest.raises(ParseError): next(result)
def test_parse_filterlist(): result = parse_filterlist(['! foo', '! Title: bar']) assert list(result) == [Comment('foo'), Metadata('Title', 'bar')]