Skip to content

Commit dec4474

Browse files
committed
/img and /gif should fail less now.
Images and gifs save to system_temp directory, where they should be deleted by the OS. upload_image now has an option for deleting the image (but this sometimes results in a race condition; needs more testing)
1 parent 054c6e1 commit dec4474

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

Core/Bot.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import random
66
import sys
77
import asyncio
8+
import tempfile
89
import time
910
import signal
1011
import traceback
@@ -13,14 +14,16 @@
1314

1415
import hangups
1516
from hangups.ui.utils import get_conv_name
16-
import io
1717
from Core.Commands.Dispatcher import DispatcherSingleton
1818

1919
from Core.Util import ConfigDict, UtilDB
2020
from Core import Handlers
2121

22+
2223
class HangoutsBotOpener(FancyURLopener):
2324
version = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36'
25+
26+
2427
request.urlretrieve = HangoutsBotOpener().retrieve
2528

2629
__version__ = '1.1'
@@ -238,13 +241,16 @@ def send_message_segments(self, conversation, segments, image_id=None):
238241

239242

240243
@asyncio.coroutine
241-
def upload_image(self, url):
242-
filename = '{}.png'.format(random.randint(0, 9999999999))
244+
def upload_image(self, url, filename=None, delete=False):
245+
if not filename:
246+
tempdir = tempfile.gettempdir()
247+
filename = tempdir + os.sep + '{}.png'.format(random.randint(0, 9999999999))
243248
request.urlretrieve(url, filename)
244249
file = open(filename, "rb")
245250
image_id = yield from self._client.upload_image(file)
246-
file.close()
247-
os.remove(filename)
251+
if delete:
252+
file.close()
253+
os.remove(filename)
248254
return image_id
249255

250256
def list_conversations(self):

Core/Commands/DefaultCommands.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ def karma(bot, event, name=None, *args):
727727
bot.send_message_segments(event.conv, segments)
728728

729729

730-
@DispatcherSingleton.register_aliases(["img", "image"])
730+
@DispatcherSingleton.register_aliases(["img"])
731731
def image(bot, event, *args):
732732
query = ' '.join(args)
733733
url = 'http://ajax.googleapis.com/ajax/services/search/images?v=1.0&rsz=8&safe=active&' \
@@ -747,7 +747,6 @@ def send_image(bot, event, url):
747747
yield from send_image(bot, event, url)
748748

749749

750-
# TODO ASYNC ASAP
751750
@DispatcherSingleton.register
752751
def gif(bot, event, *args):
753752
query = ' '.join(args)

Core/Commands/Dispatcher.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,30 +71,45 @@ def run(self, bot, event, bot_command_char, *args, **kwds):
7171
log.close()
7272
print(traceback.format_exc())
7373

74-
# def register(self, func, aliases=None):
75-
# """Decorator for registering command"""
76-
# if aliases is None or len(aliases) == 0:
77-
# self.commands[func.__name__] = func
78-
# else:
79-
# for alias in aliases:
80-
# self.commands[alias] = func
81-
# return func
82-
83-
8474
def register_aliases(self, aliases=None):
75+
"""Registers a command under the function name & any names specified in aliases.
76+
"""
77+
8578
def func_wrapper(func):
79+
self.commands[func.__name__] = func
8680
for alias in aliases:
8781
self.commands[alias] = func
8882
return func
83+
8984
return func_wrapper
9085

86+
def register_extras(self, is_hidden=False, aliases=None):
87+
"""Registers a function as hidden with aliases, or any combination of that."""
88+
89+
def func_wrapper(func):
90+
if is_hidden and aliases:
91+
self.hidden_commands[func.__name__] = func
92+
for alias in aliases:
93+
self.hidden_commands[alias] = func
94+
elif aliases:
95+
self.commands[func.__name__] = func
96+
for alias in aliases:
97+
self.commands[alias] = func
98+
elif is_hidden:
99+
self.hidden_commands[func.__name__] = func
100+
else:
101+
self.commands[func.__name__] = func
102+
return func
103+
104+
return func_wrapper
91105

92106
def register(self, func):
93107
"""Decorator for registering command"""
94108
self.commands[func.__name__] = func
95109
return func
96110

97111
def register_hidden(self, func):
112+
"""Registers a command as hidden (This makes it only runnable by the Bot and it won't appear in the help menu)"""
98113
self.hidden_commands[func.__name__] = func
99114
return func
100115

0 commit comments

Comments
 (0)