Skip to content

Instantly share code, notes, and snippets.

@englehardt
Last active April 23, 2020 16:32
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 englehardt/05c6be67d88e2450b091c417f92a1125 to your computer and use it in GitHub Desktop.
Save englehardt/05c6be67d88e2450b091c417f92a1125 to your computer and use it in GitHub Desktop.
ABP blocklist parser options parsing example
def get_option_dict(url, top_level_url, content_type=None):
"""Build an options dict for BlockListParser.
These options are checked here:
* https://github.com/englehardt/abp-blocklist-parser/blob/40f6bb5b91ea403b7b9852a16d6c57d5ec26cf7f/abp_blocklist_parser/RegexParser.py#L104-L117
* https://github.com/englehardt/abp-blocklist-parser/blob/40f6bb5b91ea403b7b9852a16d6c57d5ec26cf7f/abp_blocklist_parser/RegexParser.py#L240-L248
Parameters
----------
url : string
The URL of the requested resource.
top_level_url : string
The URL of the top-level frame of the requested resource
content_type : string
WebExtensions resource type: https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/webRequest/ResourceType
Returns
-------
dict
An "options" dictionary for use with BlockListParser
"""
options = {}
# Add type option. Value doesn't matter.
if content_type:
options[content_type] = True
options["domain"] = urlparse(top_level_url).hostname
# Add third-party option if third party. Value doesn't matter.
if du.get_ps_plus_1(url) != du.get_ps_plus_1(top_level_url):
options["third-party"] = True
return options
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment