search by term
This commit is contained in:
parent
c6220d0ebd
commit
4f55fbb016
27
bot.py
27
bot.py
@ -1,6 +1,7 @@
|
|||||||
import pydle
|
import pydle
|
||||||
import requests
|
import requests
|
||||||
import random
|
import random
|
||||||
|
import re
|
||||||
|
|
||||||
host = "https://collectionapi.metmuseum.org/public/collection/v1/"
|
host = "https://collectionapi.metmuseum.org/public/collection/v1/"
|
||||||
|
|
||||||
@ -8,24 +9,38 @@ host = "https://collectionapi.metmuseum.org/public/collection/v1/"
|
|||||||
r = requests.get(host + 'objects')
|
r = requests.get(host + 'objects')
|
||||||
objects = r.json()['objectIDs']
|
objects = r.json()['objectIDs']
|
||||||
|
|
||||||
def getArt():
|
def getArt(term):
|
||||||
while True:
|
searchObjects = objects
|
||||||
r = requests.get(host + 'objects/' + str(random.choice(objects)))
|
if term:
|
||||||
|
query = {'q': term, 'hasImages': 'true'}
|
||||||
|
r = requests.get(host + 'search', params=query)
|
||||||
|
searchObjects = r.json()['objectIDs']
|
||||||
|
|
||||||
|
if not searchObjects or len(searchObjects) == 0:
|
||||||
|
return None
|
||||||
|
|
||||||
|
tries = 10
|
||||||
|
while tries > 0:
|
||||||
|
r = requests.get(host + 'objects/' + str(random.choice(searchObjects)))
|
||||||
j = r.json()
|
j = r.json()
|
||||||
if j['primaryImage']:
|
if j['primaryImage']:
|
||||||
return j
|
return j
|
||||||
|
tries -= 1
|
||||||
|
return None
|
||||||
|
|
||||||
# Simple echo bot.
|
# Simple echo bot.
|
||||||
class RadioBot(pydle.Client):
|
class RadioBot(pydle.Client):
|
||||||
async def on_connect(self):
|
async def on_connect(self):
|
||||||
await self.join('#dailybot')
|
await self.join('#dailybot')
|
||||||
await self.join('#dailybuild')
|
# await self.join('#dailybuild')
|
||||||
|
|
||||||
async def on_message(self, target, source, message):
|
async def on_message(self, target, source, message):
|
||||||
# don't respond to our own messages, as this leads to a positive feedback loop
|
# don't respond to our own messages, as this leads to a positive feedback loop
|
||||||
if source != self.nickname:
|
if source != self.nickname:
|
||||||
if message.startswith(".art"):
|
if re.match(r'^\.art\b', message):
|
||||||
art = getArt()
|
art = getArt(message[len(".art"):].strip())
|
||||||
|
if art is None:
|
||||||
|
await self.message(target, "No result")
|
||||||
await self.message(target, '"' + art['title'] + '" ' + art['objectDate'] + ' ' + art['primaryImage'])
|
await self.message(target, '"' + art['title'] + '" ' + art['objectDate'] + ' ' + art['primaryImage'])
|
||||||
|
|
||||||
client = RadioBot('ArtBot', realname='ArtBot')
|
client = RadioBot('ArtBot', realname='ArtBot')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user