From cfc88d89edb8277bc04cb4d79a2371fb384f6b11 Mon Sep 17 00:00:00 2001 From: PrgmrBill Date: Tue, 12 May 2015 20:04:28 -0400 Subject: [PATCH] Fixes #39 - adds whitelistDomainPattern option This option allows users to specify which domains they want to show titles for --- README.md | 6 ++++++ config.py | 2 +- plugin.py | 27 ++++++++++++++++++++++++++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index eb67a76..f9d0bc0 100644 --- a/README.md +++ b/README.md @@ -164,6 +164,8 @@ titles will be shown in all channels. Default value: `""` `ignoredDomainPattern` - ignore domains matching this pattern. Default value: `""` +`whitelistDomainPattern` - ignore any link without a domain matching this pattern. Default value: `""` + ### Tip ### You can ignore domains that you know aren't websites. This prevents a request from being made at all. @@ -178,6 +180,10 @@ Ignore `*.tk` and `buzzfeed.com` !config supybot.plugins.SpiffyTitles.ignoredDomainPattern (\.tk|buzzfeed\.com) +Ignore all links except youtube, imgur, and reddit + + !config supybot.plugins.SpiffyTitles.whitelistDomainPattern /(reddit\.com|youtube\.com|youtu\.be|imgur\.com)/ + `userAgents` - A comma separated list of strings of user agents randomly chosen when requesting. `urlRegularExpression` - A regular expression used to match URLs. You shouldn't need to change this. diff --git a/config.py b/config.py index 94483ed..fcfe46f 100644 --- a/config.py +++ b/config.py @@ -110,7 +110,7 @@ conf.registerGlobalValue(SpiffyTitles, 'youtubeDeveloperKey', # Link cache lifetime conf.registerGlobalValue(SpiffyTitles, 'linkCacheLifetimeInSeconds', - registry.Integer(60, _("""Link cache lifetime in seconds"""))) + registry.Integer(60, _("""Link cache lifetime in seconds"""))) diff --git a/plugin.py b/plugin.py index a9c521c..378f16b 100644 --- a/plugin.py +++ b/plugin.py @@ -116,7 +116,13 @@ class SpiffyTitles(callbacks.Plugin): is_ignored = self.is_ignored_domain(domain) if is_ignored: - self.log.info("SpiffyTitles: ignoring url due to pattern match: %s" % (url)) + self.log.info("SpiffyTitles: URL ignored due to domain blacklist match: %s" % url) + return + + is_whitelisted_domain = self.is_whitelisted_domain(domain) + + if self.registryValue("whitelistDomainPattern") and not is_whitelisted_domain: + self.log.info("SpiffyTitles: URL ignored due to domain whitelist mismatch: %s" % url) return """ @@ -263,6 +269,25 @@ class SpiffyTitles(callbacks.Plugin): except re.Error: self.log.error("SpiffyTitles: invalid regular expression: %s" % (pattern)) + def is_whitelisted_domain(self, domain): + """ + Checks domain against a regular expression + """ + pattern = self.registryValue("whitelistDomainPattern") + + if pattern: + self.log.debug("SpiffyTitles: matching %s against %s" % (domain, str(pattern))) + + try: + pattern_search_result = re.search(pattern, domain) + + if pattern_search_result is not None: + match = pattern_search_result.group() + + return match + except re.Error: + self.log.error("SpiffyTitles: invalid regular expression: %s" % (pattern)) + def get_video_id_from_url(self, url, info): """ Get YouTube video ID from URL