Skip to content

Commit

Permalink
Merge pull request #1 from vantuz-subhuman/1.2.1-basset-lover
Browse files Browse the repository at this point in the history
1.2.1 basset lover
  • Loading branch information
vsubhuman authored Aug 27, 2017
2 parents a0f7ebc + 341be31 commit b649cf0
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 13 deletions.
21 changes: 13 additions & 8 deletions src/animalbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@
import telegram
from vutil import *

version = '1.2'
version = '1.2.1'
bot_username = os.environ['bot_username'].lower()
bot = telegram.bot(os.environ['bot_token'])
environment = os.environ.get('environment', 'test')
developers = set(split(os.environ.get('developers')))
basset_lovers = set(split(os.environ.get('basset_lovers')))


def handler(evt, ctx):
Expand All @@ -29,15 +31,17 @@ def handle_message(msg):
if chat_type != 'private':
bot.leave_chat(chat_id)
return
elif uname != 'vsubhuman':
elif uname not in developers:
print("Ignoring non-developer: %s" % uname)
return
words = split(lower(text))
cmd = next(iter(words), None)
case(cmd, {
'/am_i_basset_lover': lambda: bot.send_message(chat_id, "Yes" if uname in basset_lovers else "No"),
'/why': lambda: send_why(chat_id, join(words[1:])),
'/animal': lambda: random.choice([send_cate, send_doggo])(chat_id, fname),
'/cate': lambda: send_cate(chat_id, fname),
'/doggo': lambda: send_doggo(chat_id, fname),
'/animal': lambda: random.choice([send_cate, send_doggo])(chat_id, fname, uname),
'/cate': lambda: send_cate(chat_id, fname, uname),
'/doggo': lambda: send_doggo(chat_id, fname, uname),
'/start': lambda: send_help(chat_id, fname),
'/help': lambda: send_help(chat_id, fname),
'/version': lambda: bot.send_message(chat_id, "Version: %s" % version),
Expand Down Expand Up @@ -96,7 +100,7 @@ def send_help(chat_id, fname):
return bot.send_message(chat_id, text)


def send_cate(chat_id, fname):
def send_cate(chat_id, fname, _):
type = random.choice(['jpg', 'gif'])
imgs = cates.get_cates(type)
for i in range(0, len(imgs)):
Expand All @@ -121,8 +125,9 @@ def send_cate(chat_id, fname):
raise


def send_doggo(chat_id, fname):
imgs, text = destruct(doggos.get_doggos(), 'images', 'text')
def send_doggo(chat_id, fname, uname):
basset_lover = uname in basset_lovers
imgs, text = destruct(doggos.get_doggos(is_basset_lover=basset_lover), 'images', 'text')
text = text or "Here's your awesome dog pic, %s!" % fname
for i in range(0, len(imgs)):
try:
Expand Down
45 changes: 40 additions & 5 deletions src/doggos.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,53 @@
import os
import random

from requests_futures import sessions

from vutil import *


def get_doggos(num=3):
r1, r2 = get_async(
"https://api.thedogapi.co.uk/v2/dog.php?limit=%s" % num,
"https://dog-api.kinduff.com/api/facts")
def get_basset_chance(default):
str_value = os.environ.get('basset_chance')
if str_value:
try:
return float(str_value)
except Exception as e:
print("Failed to parse \"basset_chance\" '%s'! %s" % (str_value, e))
print("Using default \"basset_chance\": %s" % default)
return default


basset_chance = get_basset_chance(0.5)


def get_doggos(num=3, is_basset_lover=False):
print("Acquiring doggos")
is_basset = is_basset_lover and random.random() < basset_chance
if is_basset:
print("Basset lover mode")
img_url = basset_url() if is_basset else dog_url(num)
r1, r2 = get_async(img_url, "https://dog-api.kinduff.com/api/facts")
images = get_bassets(r1, num) if is_basset else r1.json().get('data')
return {
'images': r1.json().get('data'),
'images': images,
'text': head(r2.json().get('facts'))
}


def dog_url(num):
return "https://api.thedogapi.co.uk/v2/dog.php?limit=%s" % num


def basset_url():
return "https://dog.ceo/api/breed/hound/basset/images"


def get_bassets(r, num):
all_urls = r.json().get('message', [])
url = random.sample(all_urls, num)
return list({'url': x} for x in url)


def get_async(*urls):
session = sessions.FuturesSession()
futures = map(session.get, urls)
Expand Down

0 comments on commit b649cf0

Please sign in to comment.