Merge commands branch

This commit is contained in:
REDNBLACK 2017-04-16 16:35:12 +03:00
parent 4ad3dfaac4
commit ea78d4f699
11 changed files with 150 additions and 0 deletions

BIN
data/Futura.ttc Normal file

Binary file not shown.

BIN
data/vzhuh_sample.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 233 KiB

View File

@ -1,2 +1,3 @@
python-telegram-bot==5.3.0
redis==2.10.5
Pillow==4.1.0

View File

@ -6,6 +6,14 @@ from src.handler.commands.get_stats import GetStats
from src.handler.commands.moderate import Moderate
from src.handler.commands.chance import Chance
from src.handler.commands.butts import Butts
from src.handler.commands.boobs import Boobs
from src.handler.commands.meow import Meow
from src.handler.commands.woof import Woof
from src.handler.commands.borscht import Borscht
from src.handler.commands.xkcd import XKCD
from src.handler.commands.vzhuh import Vzhuh
commands = {}
for clazz in Base.__subclasses__():
command_name = getattr(clazz, 'name')

View File

@ -0,0 +1,16 @@
from .base import Base
import json
from urllib.request import urlopen
class Boobs(Base):
name = 'boobs'
aliases = ['80085', '(.)(.)']
@staticmethod
def execute(bot, command):
response = urlopen('http://api.oboobs.ru/noise/1')
data = json.loads(response.read().decode('utf-8'))
url = 'http://media.oboobs.ru/' + data[0]['preview']
bot.send_photo(chat_id=command.chat_id, photo=url)

View File

@ -0,0 +1,23 @@
from .base import Base
import json
from src.utils import random_element
from urllib.request import urlopen
class Borscht(Base):
name = 'borscht'
images = None
@staticmethod
def execute(bot, command):
if Borscht.images is None:
Borscht.images = Borscht.__preload()
bot.send_photo(chat_id=command.chat_id, photo=random_element(Borscht.images))
@staticmethod
def __preload():
response = urlopen('https://api.cognitive.microsoft.com/bing/v5.0/images/search?q=%D0%B1%D0%BE%D1%80%D1%89&mkt=en-us&safe-search=strict&image-type=photo&subscription-key=dd95294bc02748a1ab5152d36fdbbdac')
data = json.loads(response.read().decode('utf-8'))
return list(map(lambda e: e['contentUrl'], data['value']))

View File

@ -0,0 +1,16 @@
from .base import Base
import json
from urllib.request import urlopen
class Butts(Base):
name = 'butts'
aliases = ['(_._)', '(_*_)', '(Y)']
@staticmethod
def execute(bot, command):
response = urlopen('http://api.obutts.ru/noise/1')
data = json.loads(response.read().decode('utf-8'))
url = 'http://media.obutts.ru/' + data[0]['preview']
bot.send_photo(chat_id=command.chat_id, photo=url)

View File

@ -0,0 +1,15 @@
from .base import Base
from urllib.request import build_opener, HTTPRedirectHandler
class Meow(Base):
name = 'meow'
aliases = [':3', '=3']
@staticmethod
def execute(bot, command):
opener = build_opener(HTTPRedirectHandler)
request = opener.open('http://thecatapi.com/api/images/get?format=src')
url = request.url
bot.send_photo(chat_id=command.chat_id, photo=url)

View File

@ -0,0 +1,40 @@
from .base import Base
from PIL import Image, ImageFont, ImageDraw
class Vzhuh(Base):
name = 'vzhuh'
@staticmethod
def execute(bot, command):
text = Vzhuh.format_text('вжух ' + ' '.join(command.args))
Vzhuh.create_image(text)
bot.send_photo(chat_id=command.chat_id, photo=open('data/sample-out.png', 'rb'))
@staticmethod
def format_text(text):
result = []
current_part = ''
if len(text) > 8:
parts = text.upper().split(' ')
for part in parts:
if len(current_part) + len(part) <= 10:
current_part += part + ' '
else:
result.append(current_part)
result.append(part)
current_part = ''
result.append(current_part)
else:
result.append(text)
return '\r\n'.join(result)
@staticmethod
def create_image(text):
img = Image.open("data/vzhuh_sample.png")
draw = ImageDraw.Draw(img)
font = ImageFont.truetype("data/Futura.ttc", 40, index=2)
draw.text((222, 280), text, (0, 0, 0), font=font)
img.save('data/sample-out.png')

View File

@ -0,0 +1,15 @@
from .base import Base
from urllib.request import urlopen, Request
class Woof(Base):
name = 'woof'
@staticmethod
def execute(bot, command):
req = Request("http://loremflickr.com/500/410/dog", headers={'User-Agent': "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36"})
output = open("data/woof-photo.jpg", "wb")
output.write(urlopen(req).read())
output.close()
bot.send_photo(chat_id=command.chat_id, photo=open('data/woof-photo.jpg', 'rb'))

View File

@ -0,0 +1,16 @@
from .base import Base
import json
import random
from urllib.request import urlopen
class XKCD(Base):
name = 'xkcd'
@staticmethod
def execute(bot, command):
last_id = json.loads(urlopen("http://xkcd.com/info.0.json").read().decode('utf-8'))['num']
id = random.randint(1, last_id)
url = json.loads(urlopen('http://xkcd.com/' + str(id) + '/info.0.json').read().decode('utf-8'))['img']
bot.send_photo(chat_id=command.chat_id, photo=url)