aboutsummaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
Diffstat (limited to 'modules')
-rw-r--r--modules/bitly.py22
-rw-r--r--modules/git_webhooks/__init__.py5
-rw-r--r--modules/git_webhooks/github.py2
-rw-r--r--modules/shorturl.py57
-rw-r--r--modules/title.py5
-rw-r--r--modules/tweets/__init__.py3
-rw-r--r--modules/tweets/format.py4
7 files changed, 70 insertions, 28 deletions
diff --git a/modules/bitly.py b/modules/bitly.py
new file mode 100644
index 00000000..52703fe9
--- /dev/null
+++ b/modules/bitly.py
@@ -0,0 +1,22 @@
+#--depends-on commands
+#--require-config bitly-api-key
+
+import re
+from src import ModuleManager, utils
+
+URL_BITLYSHORTEN = "https://api-ssl.bitly.com/v3/shorten"
+
+class Module(ModuleManager.BaseModule):
+ def on_load(self):
+ self.exports.add("shorturl-s-bitly", self._shorturl)
+ def _shorturl(self, url):
+ if len(url) < 22:
+ return None
+
+ page = utils.http.request(URL_BITLYSHORTEN, get_params={
+ "access_token": self.bot.config["bitly-api-key"],
+ "longUrl": url}, json=True)
+
+ if page and page.data["data"]:
+ return page.data["data"]["url"]
+ return None
diff --git a/modules/git_webhooks/__init__.py b/modules/git_webhooks/__init__.py
index 55a45999..83e27d68 100644
--- a/modules/git_webhooks/__init__.py
+++ b/modules/git_webhooks/__init__.py
@@ -1,3 +1,8 @@
+#--depends-on channel_access
+#--depends-on check_mode
+#--depends-on commands
+#--depends-on shorturl
+
import itertools, json, re, urllib.parse
from src import ModuleManager, utils
from . import colors, gitea, github
diff --git a/modules/git_webhooks/github.py b/modules/git_webhooks/github.py
index 1df948e4..a5fd32ed 100644
--- a/modules/git_webhooks/github.py
+++ b/modules/git_webhooks/github.py
@@ -343,7 +343,7 @@ class GitHub(object):
url = ""
if data["check_run"]["details_url"]:
url = data["check_run"]["details_url"]
- url = " - %s" % self.exports.get_one("shortlink")(url)
+ url = " - %s" % self.exports.get_one("shorturl-any")(url)
duration = ""
if data["check_run"]["completed_at"]:
diff --git a/modules/shorturl.py b/modules/shorturl.py
index af8f80c0..6cb7073b 100644
--- a/modules/shorturl.py
+++ b/modules/shorturl.py
@@ -1,38 +1,53 @@
-#--depends-on commands
-#--require-config bitly-api-key
-
import re
from src import ModuleManager, utils
-URL_BITLYSHORTEN = "https://api-ssl.bitly.com/v3/shorten"
-
+@utils.export("serverset", {"setting": "url-shortener",
+ "help": "Set URL shortener service", "example": "bitly"})
+@utils.export("botset", {"setting": "url-shortener",
+ "help": "Set URL shortener service", "example": "bitly"})
class Module(ModuleManager.BaseModule):
- _name = "Short"
-
def on_load(self):
- self.exports.add("shortlink", self._shortlink)
+ self.exports.add("shorturl", self._shorturl)
+ self.exports.add("shorturl-any", self._shorturl_any)
- def _shortlink(self, url):
- if not re.match(utils.http.REGEX_URL, url):
- url = "http://%s" % url
+ def _get_shortener(self, name):
+ return self.exports.get_one("shorturl-s-%s" % name, None)
+ def _call_shortener(self, shortener_name, url):
+ shortener = self._get_shortener(shortener_name)
+ if shortener == None:
+ return None
+ short_url = shortener(url)
+ if short_url == None:
+ return None
+ return short_url
- page = utils.http.request(URL_BITLYSHORTEN, get_params={
- "access_token": self.bot.config["bitly-api-key"],
- "longUrl": url}, json=True)
+ def _shorturl_any(self, url):
+ global_shortener_name = self.bot.get_setting("url-shortener", "bitly")
+ if global_shortener_name:
+ return self._call_shortener(global_shortener_name, url) or url
- if page and page.data["data"]:
- return page.data["data"]["url"]
- return url
+ shortener_name = self.exports.find_one("shorturl-s-", None)
+ if shortener_name == None:
+ return url
+ return self._call_shortener(shortener_name, url) or url
+
+ def _shorturl(self, server, url):
+ shortener_name = server.get_setting("url-shortener", "bitly")
+ if shortener_name == None:
+ return url
+ return self._call_shortener(shortener_name, url) or url
@utils.hook("received.command.shorten")
def shorten(self, event):
"""
- :help: Shorten a given URL using the is.gd service
+ :help: Shorten a given URL
:usage: <url>
"""
url = None
if len(event["args"]) > 0:
url = event["args_split"][0]
+ if not re.match(utils.http.REGEX_URL, url):
+ url = "http://%s" % url
else:
url = event["target"].buffer.find(utils.http.REGEX_URL)
if url:
@@ -40,7 +55,5 @@ class Module(ModuleManager.BaseModule):
if not url:
raise utils.EventError("No URL provided/found.")
- if url:
- event["stdout"].write("Shortened URL: %s" % self._shortlink(url))
- else:
- event["stderr"].write("Unable to shorten that URL.")
+ event["stdout"].write("Shortened URL: %s" % self._shorturl(
+ event["server"], url))
diff --git a/modules/title.py b/modules/title.py
index 6e1e729e..7c32a0de 100644
--- a/modules/title.py
+++ b/modules/title.py
@@ -1,5 +1,6 @@
#--depends-on commands
#--depends-on config
+#--depends-on shorturl
import hashlib, re, urllib.parse
from src import EventManager, ModuleManager, utils
@@ -40,8 +41,8 @@ class Module(ModuleManager.BaseModule):
"\r", "").replace(" ", " ").strip()
if channel.get_setting("title-shorten", False):
- short_url = self.exports.get_one("shortlink", lambda x: x
- )(url)
+ short_url = self.exports.get_one("shorturl")(
+ event["server"], url)
return "%s - %s" % (title, short_url)
return title
else:
diff --git a/modules/tweets/__init__.py b/modules/tweets/__init__.py
index afe9ea4c..16f179d8 100644
--- a/modules/tweets/__init__.py
+++ b/modules/tweets/__init__.py
@@ -1,5 +1,6 @@
#--depends-on commands
#--depends-on permissions
+#--depends-on shorturl
#--require-config twitter-api-key
#--require-config twitter-api-secret
#--require-config twitter-access-token
@@ -34,8 +35,8 @@ class BitBotStreamListener(tweepy.StreamListener):
if server and channel_name in server.channels:
follows.append([server, server.channels.get(channel_name)])
- tweet = format._tweet(_exports, status)
for server, channel in follows:
+ tweet = format._tweet(_exports, server, status)
_events.on("send.stdout").call(target=channel,
module_name="Tweets", server=server, message=tweet)
diff --git a/modules/tweets/format.py b/modules/tweets/format.py
index c41fde17..090ec6cd 100644
--- a/modules/tweets/format.py
+++ b/modules/tweets/format.py
@@ -6,7 +6,7 @@ def _timestamp(dt):
since, unit = utils.time_unit(seconds_since)
return "%s %s ago" % (since, unit)
-def _tweet(exports, tweet):
+def _tweet(exports, server, tweet):
linked_id = tweet.id
username = tweet.user.screen_name
@@ -17,7 +17,7 @@ def _tweet(exports, tweet):
tweet_link = "https://twitter.com/%s/status/%s" % (username,
linked_id)
- short_url = exports.get_one("shortlink")(tweet_link)
+ short_url = exports.get_one("shorturl")(server, tweet_link)
short_url = " - %s" % short_url if short_url else ""
created_at = _timestamp(tweet.created_at)