From 04a7ae041cd66fc8ac063a2a223b0794115312f6 Mon Sep 17 00:00:00 2001 From: Jordan Doyle Date: Sat, 22 Jul 2017 20:09:29 +0100 Subject: [PATCH] add a module to grab speedtest results --- dave/dave.py | 7 ++++--- dave/module.py | 9 +++++++++ dave/modules/speedtest.py | 23 +++++++++++++++++++++++ dave/modules/title.py | 2 -- dave/modules/urbandictionary.py | 1 - 5 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 dave/modules/speedtest.py diff --git a/dave/dave.py b/dave/dave.py index e7b9099..0ed921a 100644 --- a/dave/dave.py +++ b/dave/dave.py @@ -77,9 +77,10 @@ class Dave(irc.IRCClient): # we matched a command method[1](self, method[2], nick, channel) - for m in run: - # modules that should always be run regardless of priority - m[0](self, m[1], nick, channel) + if not (hasattr(method[1], "dont_always_run") and method[1].dont_always_run): + for m in run: + # modules that should always be run regardless of priority + m[0](self, m[1], nick, channel) def irc_unknown(self, prefix, command, params): if command == "INVITE": diff --git a/dave/module.py b/dave/module.py index 6473bc8..fe62396 100644 --- a/dave/module.py +++ b/dave/module.py @@ -75,6 +75,15 @@ def priority(priority): return add_attribute +def dont_always_run_if_run(): + """If this function is run, we shouldn't run the functions that should always run""" + def add_attribute(function): + function.dont_always_run = True + return function + + return add_attribute + + def always_run(): """Decorate a function to always run it, even when over prioritied""" def add_attribute(function): diff --git a/dave/modules/speedtest.py b/dave/modules/speedtest.py new file mode 100644 index 0000000..aede9a8 --- /dev/null +++ b/dave/modules/speedtest.py @@ -0,0 +1,23 @@ +from requests import get +from bs4 import BeautifulSoup +import dave.module +from twisted.words.protocols.irc import assembleFormattedText, attributes as A + +@dave.module.match(r'https?://(?:www\.|beta\.)?speedtest\.net/(?:my-)?result/([0-9]+)') +@dave.module.priority(dave.module.Priority.HIGHEST) +@dave.module.dont_always_run_if_run() +def speedtest(bot, args, sender, source): + res = get("http://www.speedtest.net/result/{}".format(args[0]), timeout=3) + + soup = BeautifulSoup(res.text, "html.parser") + download = soup.select(".share-speed.share-download p")[0].text + upload = soup.select(".share-speed.share-upload p")[0].text + ping = soup.select(".share-data.share-ping p")[0].text + isp = soup.select(".share-data.share-isp p")[0].text + + bot.msg(source, assembleFormattedText(A.normal[ + A.bold["{}: ".format(isp)], + "Download: ", A.bold[str(download)], " ", + "Upload: ", A.bold[str(upload)], " ", + "Ping: ", A.bold[str(ping)] + ])) \ No newline at end of file diff --git a/dave/modules/title.py b/dave/modules/title.py index a6875f3..0b5ac54 100644 --- a/dave/modules/title.py +++ b/dave/modules/title.py @@ -6,11 +6,9 @@ from bs4 import BeautifulSoup from requests import get from twisted.words.protocols.irc import assembleFormattedText, attributes as A import dave.config -import socket parse = re.compile(r"(?:(?:https?):\/\/)(?:\S+(?::\S*)?@)?(?:(?!(?:10|127)(?:\.\d{1,3}){3})(?!(?:169\.254|192\.168)(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]-*)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,}))\.?)(?::\d{2,5})?(?:[/?#]\S*)?", re.IGNORECASE) - @dave.module.match(r"(.*)") @dave.module.always_run() def link_parse(bot, args, sender, source): diff --git a/dave/modules/urbandictionary.py b/dave/modules/urbandictionary.py index 6a956f6..362999a 100644 --- a/dave/modules/urbandictionary.py +++ b/dave/modules/urbandictionary.py @@ -2,7 +2,6 @@ """Give the user back urbandictionary results.""" import dave.module import dave.config -import socket import requests import pickle import re -- libgit2 1.7.2