Created
September 8, 2016 17:15
-
-
Save malles/0e7c821b08cf835c3e159d7cb57fd9f1 to your computer and use it in GitHub Desktop.
Create short urls and lmgtfy urls in your Hangupsbot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import asyncio | |
from urllib.request import urlopen | |
import plugins | |
def _initialise(bot): | |
plugins.register_user_command(["lmgtfy"]) | |
plugins.register_user_command(["shorten"]) | |
def lmgtfy(bot, event, *args): | |
search = "http://lmgtfy.com/?q={}".format("+".join(args)) | |
message = yield from short_url(search) | |
yield from bot.coro_send_message(event.conv, message) | |
def shorten(bot, event, *args): | |
message = yield from short_url(args[0]) | |
yield from bot.coro_send_message(event.conv, message) | |
@asyncio.coroutine | |
def short_url(url): | |
f = urlopen("http://tinyurl.com/api-create.php?url={}".format(url)) | |
res = f.read().decode('utf-8') | |
if res: | |
return res | |
return url | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment