Skip to content
This repository was archived by the owner on Mar 14, 2021. It is now read-only.
Open
Changes from 6 commits
Commits
Show all changes
54 commits
Select commit Hold shift + click to select a range
0aa95cb
Merge pull request #1 from discord-python/master
schwartzadev Mar 23, 2018
c2505ce
add preliminary snake 'get' command
schwartzadev Mar 23, 2018
8c25449
allow user input for snake names
schwartzadev Mar 23, 2018
e3f0c70
change imports order, index format string
schwartzadev Mar 23, 2018
611501f
add interim fancy message formatting, handle special python case
schwartzadev Mar 23, 2018
85bb282
add snake to query string for images
schwartzadev Mar 23, 2018
243b51f
add list of snake facts, sources files
schwartzadev Mar 24, 2018
b4e6468
update sources
schwartzadev Mar 24, 2018
cea4cf3
add snake fact command
schwartzadev Mar 24, 2018
3a37252
clean up code - type hinting, spelling, URL formatting, etc. (thanks …
schwartzadev Mar 24, 2018
6602d02
fix broken URL
schwartzadev Mar 24, 2018
62401e5
resolve PEP E226
schwartzadev Mar 24, 2018
7f63f4f
implement more of @1mn's suggestions
schwartzadev Mar 24, 2018
0137051
use the snake emoji
schwartzadev Mar 24, 2018
1e710db
integrate with the API - thanks @prithajnath - remove message from re…
schwartzadev Mar 24, 2018
5844b02
Merge pull request #2 from discord-python/master
schwartzadev Mar 24, 2018
3656c01
Add search endpoint and auth headers
prithajnath Mar 24, 2018
48f8de4
Merge pull request #3 from discord-python/master
schwartzadev Mar 25, 2018
bf2f9ea
remove repetitive embed code, fix linting errors (hopefully!)
schwartzadev Mar 25, 2018
d8a26ba
resolve flake8 - P103
schwartzadev Mar 25, 2018
9c8a9f4
resolve linting issues
schwartzadev Mar 25, 2018
697f557
implement random api endpoint
schwartzadev Mar 25, 2018
0951f92
implement titlecase for common and scientific names, remove unused al…
schwartzadev Mar 25, 2018
544bed5
return number of matches in searches, add handling of searches with 0…
schwartzadev Mar 25, 2018
a2255e7
add qwant api in favor of unsplash, including handling for small numb…
schwartzadev Mar 25, 2018
9b2f8d0
rm prints
schwartzadev Mar 25, 2018
f30abab
Merge pull request #4 from schwartzadev/qwant-images
schwartzadev Mar 25, 2018
48f2584
add newline between imports (I201) for linting
schwartzadev Mar 25, 2018
e5901b6
Add APIs used
prithajnath Mar 25, 2018
5770c79
Add some GIFs for facts
prithajnath Mar 25, 2018
259c5a6
linting updates
schwartzadev Mar 25, 2018
2d45b2f
add list of zen python sayings
schwartzadev Mar 25, 2018
6776264
use response.json() for qwant access
schwartzadev Mar 25, 2018
03f9b61
handle snakes without a location, reduce variance in photos to three …
schwartzadev Mar 25, 2018
a27582a
Add cat input for GIF
prithajnath Mar 25, 2018
e2d556b
start video command - needs formatting work for embed
schwartzadev Mar 25, 2018
003ace9
remove embed (Discord is finnicky), fix command format
schwartzadev Mar 25, 2018
2366c43
update docstring for get
schwartzadev Mar 25, 2018
ea31798
update video docstring
schwartzadev Mar 25, 2018
a9f8863
fix get_video_json docstring
schwartzadev Mar 25, 2018
01c701b
Add Zen of Python
prithajnath Mar 25, 2018
9d28fa3
Merge pull request #6 from schwartzadev/video-command
prithajnath Mar 25, 2018
c98bc6e
Merge branch 'master' into prithaj_gif_categories
schwartzadev Mar 25, 2018
d2482b2
Merge pull request #5 from schwartzadev/prithaj_gif_categories
schwartzadev Mar 25, 2018
f1b7a2c
Add alias for this
prithajnath Mar 25, 2018
ff361b8
Add descp for zen
prithajnath Mar 25, 2018
13937c1
Merge branch 'prithaj_zen_of_python' of https://github.com/schwartzad…
schwartzadev Mar 25, 2018
3279319
lint, fix spelling
schwartzadev Mar 25, 2018
e0d2bf2
lint, fix spelling
schwartzadev Mar 25, 2018
361cc45
Merge pull request #7 from schwartzadev/prithaj_zen_of_python
schwartzadev Mar 25, 2018
ac33379
Merge branch 'master' of https://github.com/schwartzadev/code-jam-1
schwartzadev Mar 25, 2018
375f4bc
fix arguments, lint, and format for get_snek_fact
schwartzadev Mar 25, 2018
71a0f44
linting
schwartzadev Mar 25, 2018
99f00f2
add information about credentials
schwartzadev Mar 26, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions bot/cogs/snakes.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
# coding=utf-8
import json
import logging
import os
import random
from typing import Any, Dict

import aiohttp

import discord
from discord.ext.commands import AutoShardedBot, Context, command

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -29,6 +35,29 @@ async def get_snek(self, name: str = None) -> Dict[str, Any]:
:return: A dict containing information on a snake
"""

async def get_snek_qwant_json(self, snake_name):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

snake_name should be type hinted, as should the return type.

"""
Gets the json from Unsplash for a given snake query
:param snake_name: name of the snake
:return: the full JSON from the search API
"""
url = "https://api.unsplash.com/search/photos?client_id" \
"={0}&query={1}+snake".format(os.environ.get("UNSPLASH_CLIENT_ID"), snake_name)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this string is a bit messy. prefer implicit string joins inside paranthesis and f-strings to \ line continuation and formats.

client_id = os.environ.get("UNSPLASH_CLIENT_ID")
url = (
    "https://api.unsplash.com/search/photos?client_id" 
    f"={client_id}&query={snake_name}+snake"
)

async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
response = await response.read()
return json.loads(response.decode("utf-8"))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't you just use return await response.json()?


async def get_snek_image(self, name):
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name should be type hinted, as should return type.

"""
Gets the URL of a snake image
:param name: name of snake
:return: image url
"""
json_response = await self.get_snek_qwant_json(name)
rand = random.randint(0, 9) # prevents returning the same image every time
return json_response['results'][rand]['urls']['small']

@command()
async def get(self, ctx: Context, name: str = None):
"""
Expand All @@ -40,6 +69,28 @@ async def get(self, ctx: Context, name: str = None):
:param ctx: Context object passed from discord.py
:param name: Optional, the name of the snake to get information for - omit for a random snake
"""
embed = discord.Embed(color=0x3E885B)
if name == "python":
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be case insensitive.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks - so when I change line 73 to if name.lower() == "python":, I get an error about comparing NoneTypes since the default name is None (see the method declaration). How should I get around this? Should I do an "if None" check before the python special case or should I do something else?

(discord.ext.commands.errors.CommandInvokeError: Command raised an exception: AttributeError: 'NoneType' object has no attribute 'lower' is the error for reference)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if name and name.lower() == "python":

would work for me.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep - looks good, thanks

# handle Python special case
embed.add_field(
name="Python (programming language)",
value="*Guido van Rossum*\n\n"
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

multiline implicit string joins should be wrapped inside paranthesis, so that it's clear that they're one thing. This is a bit too implicit.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you explain this? I'm not super clear as to what you mean...

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

value=(
    "*Guido van Rossum*\n\n"
    "This language is neither dangerous nor venomous and be found in software globally"
),

this is one way.

or if you prefer it, this is acceptable too

value=("*Guido van Rossum*\n\n"
       "This language is neither dangerous nor venomous and be found in software globally"),

"This language is neither dangerous nor venomous and be found in software globally",
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless you're a pirate, this should be "and can be found".

inline=False
)
embed.set_image(url=await self.get_snek_image("python programming language"))
else:
url = await self.get_snek_image(name) # not limited to snakes - user can search anything they like
embed.add_field(
name="common name",
value="*sci name*\n\nThis snake is (**not**) venomous and can be found in (...)",
inline=False
)
embed.set_image(url=url)
await ctx.channel.send(
content=ctx.message.author.mention + " Here's your snek!",
embed=embed
)

# Any additional commands can be placed here. Be creative, but keep it to a reasonable amount!

Expand Down