added ability to configure user's own API key

This commit is contained in:
Adam Piontek 2018-08-26 11:35:13 -04:00
parent 6c2834ec94
commit f1db4cb4c3
2 changed files with 33 additions and 2 deletions

View file

@ -5,8 +5,17 @@ This is a plugin for [Errbot](http://errbot.io/) that fetches random cat images
Users can optionally specify whether to fetch a `gif` (animated) or a `still` (jpg/png) image:
## Usage:
- `!catto` Fetches a random cat image, animated (gif) or still (jpg/png)
- `!catto gif` Fetches a random cat animated gif image
- `!catto still` Fetches a random cat still jpg/png image
- `!catto jpg` Fetches a random cat jpg image
- `!catto png` Fetches a random cat png image
## Configuration:
It's possible to use your own Cat API key. Configure with:
```
!plugin config Catto {'CATAPI_KEY': 'your-api-key-here'}
```

View file

@ -3,16 +3,38 @@ import json
import random
import requests
from errbot import BotPlugin, botcmd
from itertools import chain
CONFIG_TEMPLATE = {'CATAPI_KEY': '9880edd3-3c4f-4758-aebe-c7182aeb3a70'}
class Catto(BotPlugin):
"""Fetch random cat images URLs"""
min_err_version = '3.0.0' # Optional, but recommended
def get_configuration_template(self):
return CONFIG_TEMPLATE
def configure(self, configuration):
if configuration is not None and configuration != {}:
config = dict(chain(CONFIG_TEMPLATE.items(),
configuration.items()))
else:
config = CONFIG_TEMPLATE
super(Catto, self).configure(config)
# def activate(self):
# """Activates plugin, loading default configuration"""
# super(Catto, self).activate()
# if self.config
# self.configure(CONFIG_TEMPLATE)
# super().activate()
# super(Catto, self).activate()
# self.configure(CONFIG_TEMPLATE)
def get_catapi_pic(self, type):
api_url = 'https://api.thecatapi.com/v1/images/search'
api_key = '9880edd3-3c4f-4758-aebe-c7182aeb3a70'
querystring = {"size":"full","mime_types":type,"format":"json","order":"RANDOM","limit":"1"}
headers = {'Content-Type': "application/json",'x-api-key': api_key}
headers = {'Content-Type': "application/json",'x-api-key': self.config['CATAPI_KEY']}
try:
response = requests.request("GET", api_url, headers=headers, params=querystring)