-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
32 lines (29 loc) · 1.03 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from chalice import Chalice
import requests
import urllib
GATHERER_TYPEAHEAD_URI = 'http://gatherer.wizards.com/Handlers/InlineCardSearch.ashx?nameFragment=%s'
GATHERER_URI = 'http://gatherer.wizards.com/Handlers/Image.ashx?type=card'
app = Chalice(app_name='magiccardbot')
@app.route('/magic-cards', methods=['GET'])
def magic_cards():
json = app.current_request.query_params
# retrieve card information
card_name = urllib.quote_plus(json['text'])
# try to derive the card name from a fragment
cards_json = requests.get(GATHERER_TYPEAHEAD_URI % card_name).json()
if json['token'] == '{{your token here}}' and len(cards_json['Results']) > 0:
card_name = cards_json['Results'][0]['Name']
# Get card image uri
response = '{}&name={}'.format(GATHERER_URI, urllib.quote_plus(card_name))
out_json = {
"response_type": "in_channel",
"attachments": [
{
"text": json['text'],
"image_url": response,
}
]
}
return out_json
else:
return {"text": "Card not found"}