-
-
Notifications
You must be signed in to change notification settings - Fork 16
Team 8 #4
base: master
Are you sure you want to change the base?
Team 8 #4
Changes from 29 commits
eb1a327
6f85d43
895fefa
f56b6c8
8716582
e4c4190
aa0aca1
2694897
640e889
dc6aff5
d838411
b8d7356
996570d
c66cdd8
98e4d6e
751603b
012aff8
c640b7a
f31430a
d900240
642762a
84a773a
4081ecf
da1c851
f7da5b8
d9095e7
fb0423f
b33bf1f
7015863
c546445
b93ae6a
3994f68
95cf6f3
0f3acb4
684e921
3c7e1cd
75fb50c
bd44a3f
96551b3
631d2ce
a402c17
dac90dd
34ec1f8
d307309
c7912d0
2b9752a
9f8b102
44e0f30
9b770ea
d3300d1
644f51f
285ec33
1744c07
128234c
af7b064
4b53d9b
29ad19d
e1c5b03
cb26052
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| Beautiful is better than ugly. | ||
| Explicit is better than implicit. | ||
| Simple is better than complex. | ||
| Complex is better than complicated. | ||
| Flat is better than nested. | ||
| Sparse is better than dense. | ||
| Readability counts. | ||
| Special cases aren't special enough to break the rules. | ||
| Although practicality beats purity. | ||
| Errors should never pass silently. | ||
| Unless explicitly silenced. | ||
| In the face of ambiguity, refuse the temptation to guess. | ||
| There should be one-- and preferably only one --obvious way to do it. | ||
| Although that way may not be obvious at first unless you're Dutch. | ||
| Now is better than never. | ||
| Although never is often better than right now. | ||
| If the implementation is hard to explain, it's a bad idea. | ||
| If the implementation is easy to explain, it may be a good idea. | ||
| Namespaces are one honking great idea -- let's do more of those! | ||
| I like penis. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,27 @@ | ||
| # coding=utf-8 | ||
| import logging | ||
| from typing import Any, Dict | ||
| import aiohttp | ||
| import async_timeout | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could just use |
||
| import random | ||
| import asyncio | ||
| import pprint | ||
|
|
||
| from discord.ext.commands import AutoShardedBot, Context, command | ||
| from discord import Embed | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
| SNEKFILE = 'bot/cogs/data/quote.txt' | ||
|
|
||
| FIRST_EMOJI = "💉" | ||
| SECOND_EMOJI = "💊" | ||
| THIRD_EMOJI = "🌡️" | ||
| FOURTH_EMOJI = "☠️" | ||
| FIFTH_EMOJI = "⚗️" | ||
|
|
||
| snakelist = [] | ||
|
|
||
|
|
||
| class Snakes: | ||
| """ | ||
|
|
@@ -15,6 +31,52 @@ class Snakes: | |
| def __init__(self, bot: AutoShardedBot): | ||
| self.bot = bot | ||
|
|
||
| async def cache_snakelist(self): | ||
| global snakelist | ||
| snakelist = await self.get_snake_list() | ||
|
|
||
| async def get_wiki_json(self, params): | ||
| async with aiohttp.ClientSession(headers={'User-Agent': 'DevBot v.10'}) as cs: | ||
| async with async_timeout.timeout(20): | ||
| async with cs.get("https://en.wikipedia.org/w/api.php", params=params) as r: | ||
| log.info(f"{r.url}: {r.status}: {r.reason}") | ||
| return await r.json() | ||
|
|
||
| async def cont_query(self, params): | ||
| last_continue = {} | ||
|
|
||
| while True: | ||
| req = params.copy() | ||
| req.update(last_continue) | ||
|
|
||
| request = await self.get_wiki_json(req) | ||
|
|
||
| if 'query' not in request: | ||
| break | ||
|
|
||
| pages = request['query']['pages']['13205433']['links'] | ||
| yield pages | ||
|
|
||
| if 'continue' not in request: | ||
| break | ||
|
|
||
| last_continue = request['continue'] | ||
|
|
||
| async def get_snake_list(self): | ||
| ambiguous = ["(disambiguation)", "Wikipedia:", "Help:", "Category:"] | ||
|
|
||
| snake_list = [] | ||
| result = self.cont_query( | ||
| {'action': 'query', 'titles': 'list_of_snakes_by_common_name', 'prop': 'links', 'format': 'json'}) | ||
| async for dicks in result: | ||
| listed = dicks | ||
| for item in listed: | ||
| if not any(s in item['title'] for s in ambiguous): | ||
| snake_list.append(item['title']) | ||
|
|
||
| snake_list.append("trouser snake") | ||
| return snake_list | ||
|
|
||
| async def get_snek(self, name: str = None) -> Dict[str, Any]: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the docstring for this method is bad. |
||
| """ | ||
| Go online and fetch information about a snake | ||
|
|
@@ -28,9 +90,60 @@ async def get_snek(self, name: str = None) -> Dict[str, Any]: | |
| :param name: Optional, the name of the snake to get information for - omit for a random snake | ||
| :return: A dict containing information on a snake | ||
| """ | ||
| global snakelist | ||
| snake_name = name | ||
| name = name.replace(" ", "_") # sanitize name | ||
|
|
||
| text_params = {'action': 'query', | ||
| 'titles': name, | ||
| 'prop': 'extracts', | ||
| 'exsentences': '2', | ||
| 'explaintext': '1', | ||
| 'autosuggest': '1', | ||
| 'redirects': '1', | ||
| 'format': 'json'} | ||
|
|
||
| image_name_params = {'action': 'query', | ||
| 'titles': name, | ||
| 'prop': 'images', | ||
| 'redirects': '1', | ||
| 'autosuggest': '1', | ||
| 'imlimit': '1', | ||
| 'format': 'json'} | ||
|
|
||
| text_json = await self.get_wiki_json(text_params) | ||
| image_name_json = await self.get_wiki_json(image_name_params) | ||
| snake_image = "https://pbs.twimg.com/profile_images/662615956670144512/dqsVK6Nw_400x400.jpg" | ||
|
|
||
| page_id = list(text_json['query']['pages'].keys())[0] | ||
| if page_id == "-1" or snake_name not in snakelist: # No entry on the wiki | ||
| snake_dict = {"name": snake_name, | ||
| "snake_text": "You call that a snake?\nTHIS is a snake!", | ||
| "snake_image": snake_image} | ||
| return snake_dict | ||
|
|
||
| image_id = image_name_json['query']['pages'][page_id]['images'][0]['title'] | ||
|
|
||
| image_url_params = {'action': 'query', | ||
| 'titles': image_id, | ||
| 'prop': 'imageinfo', | ||
| 'redirects': '1', | ||
| 'autosuggest': '1', | ||
| 'iiprop': 'url', | ||
| 'format': 'json'} | ||
|
|
||
| image_url_json = await self.get_wiki_json(image_url_params) | ||
|
|
||
| snake_image_id = list(image_url_json['query']['pages'].keys())[0] | ||
| snake_image = image_url_json['query']['pages'][snake_image_id]['imageinfo'][0]['url'] | ||
| snake_text = text_json['query']['pages'][page_id]['extract'] | ||
|
|
||
| snake_dict = {"name": snake_name, "snake_text": snake_text, "snake_image": snake_image} | ||
| return snake_dict | ||
|
|
||
| @command() | ||
| async def get(self, ctx: Context, name: str = None): | ||
| global snakelist | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This code is invalid as Python docstrings will only work when on the first line of a function, take a look at this example: >>> def func():
... """Hello this is my docstring"""
... print("Hello")
...
>>> func.__doc__
'Hello this is my docstring'
>>> def func():
... print("Hello")
... """Hello this is my docstring"""
...
>>> func.__doc__
None
>>>
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Runew0lf wrote it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Runew0lf fixed it |
||
| """ | ||
| Go online and fetch information about a snake | ||
|
|
||
|
|
@@ -40,10 +153,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 | ||
| """ | ||
| if name is None: | ||
| name = random.choice(snakelist) | ||
| elif name == "snakes on a plane": | ||
| await ctx.send("https://media.giphy.com/media/5xtDartXnQbcW5CfM64/giphy.gif") | ||
| elif name == "python": | ||
| with open(SNEKFILE, 'r') as file: | ||
| text = file.read() | ||
| snake_embed = Embed(color=ctx.me.color, title="SNEK") | ||
| snake_embed.add_field(name="Python", value=f"*{text}*") | ||
| snake_embed.set_thumbnail(url="http://www.pngall.com/wp-content/uploads/2016/05/Python-Logo-Free-PNG-Image.png") | ||
| await ctx.send(embed=snake_embed) | ||
|
|
||
| snake = await self.get_snek(name) | ||
| snake_embed = Embed(color=ctx.me.color, title="SNEK") | ||
| snake_embed.add_field(name=snake['name'], value=snake['snake_text']) | ||
| snake_embed.set_thumbnail(url=snake['snake_image']) | ||
| await ctx.send(embed=snake_embed) | ||
|
|
||
| # Any additional commands can be placed here. Be creative, but keep it to a reasonable amount! | ||
|
|
||
|
|
||
| def setup(bot): | ||
| bot.add_cog(Snakes(bot)) | ||
| log.info("Cog loaded: Snakes") | ||
| bot.loop.create_task(Snakes(bot).cache_snakelist()) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of creating two instances of the same class, the
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Runew0lf wrote it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ruinew0lf fixed it. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any context behind this line?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed Runew0lf added some weird quote thing and appended something important.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you unappend something important? also you should really know what that weird quote thing is. open your python interpreter and type
import thisThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
God damnit bisk!
FIXED!