looks good
This commit is contained in:
parent
82561f050f
commit
353284b2c3
33
garfmain.py
33
garfmain.py
@ -5,8 +5,8 @@ import subprocess
|
||||
|
||||
from garfpy import(
|
||||
logger, is_private,
|
||||
kroger_token, find_store, search_product,
|
||||
aod_message, wikisum, generate_qr, GarfAI, GarfbotRespond)
|
||||
aod_message, generate_qr,
|
||||
Kroger, GarfAI, GarfbotRespond)
|
||||
|
||||
|
||||
gapikey = config.GIF_TOKEN
|
||||
@ -20,9 +20,9 @@ intents.messages = True
|
||||
intents.message_content = True
|
||||
garfbot = discord.Client(intents=intents)
|
||||
|
||||
|
||||
garf_respond = GarfbotRespond()
|
||||
garfield = GarfAI()
|
||||
kroger = Kroger()
|
||||
|
||||
|
||||
@garfbot.event
|
||||
@ -49,9 +49,9 @@ async def on_message(message):
|
||||
return
|
||||
|
||||
if lower.startswith("hey garfield") or isinstance(message.channel, discord.DMChannel):
|
||||
question = content[12:] if lower.startswith("hey garfield") else message.content
|
||||
answer = await garfield.generate_chat(question)
|
||||
logger.info(f"Chat Request - User: {user}, Server: {guild}, Prompt: {question}")
|
||||
prompt = content[12:] if lower.startswith("hey garfield") else message.content
|
||||
answer = await garfield.generate_chat(prompt)
|
||||
logger.info(f"Chat Request - User: {user}, Server: {guild}, Prompt: {prompt}")
|
||||
await message.channel.send(answer)
|
||||
|
||||
if lower.startswith('garfpic '):
|
||||
@ -62,8 +62,8 @@ async def on_message(message):
|
||||
|
||||
# Wikipedia
|
||||
if lower.startswith('garfwiki '):
|
||||
search_term = message.content[9:]
|
||||
summary = await wikisum(search_term)
|
||||
query = message.content[9:]
|
||||
summary = await garfield.wikisum(query)
|
||||
await message.channel.send(summary)
|
||||
|
||||
# QR codes
|
||||
@ -124,21 +124,8 @@ async def on_message(message):
|
||||
# Kroger Shopping
|
||||
if lower.startswith("garfshop "):
|
||||
try:
|
||||
kroken = kroger_token()
|
||||
kroger_query = message.content.split()
|
||||
product = " ".join(kroger_query[1:-1])
|
||||
zipcode = kroger_query[-1]
|
||||
loc_data = find_store(zipcode, kroken)
|
||||
loc_id = loc_data['data'][0]['locationId']
|
||||
store_name = loc_data['data'][0]['name']
|
||||
product_query = search_product(product, loc_id, kroken)
|
||||
products = product_query['data']
|
||||
sorted_products = sorted(products, key=lambda item: item['items'][0]['price']['regular'])
|
||||
response = f"Prices for `{product}` at `{store_name}` near `{zipcode}`:\n"
|
||||
for item in sorted_products:
|
||||
product_name = item['description']
|
||||
price = item['items'][0]['price']['regular']
|
||||
response += f"- `${price}`: {product_name} \n"
|
||||
query = message.content[9:]
|
||||
response = kroger.garfshop(query)
|
||||
await message.channel.send(response)
|
||||
except Exception as e:
|
||||
await message.channel.send(f"`GarfBot Error: {str(e)}`")
|
||||
|
@ -1,17 +1,10 @@
|
||||
# garfpy/__init__.py
|
||||
|
||||
from .log import logger
|
||||
from .kroger import(
|
||||
kroger_token, find_store, search_product
|
||||
)
|
||||
from .garfai import(
|
||||
garfpic,
|
||||
process_image_requests,
|
||||
generate_chat
|
||||
)
|
||||
from .kroger import Kroger
|
||||
from .iputils import is_private
|
||||
from .aod import aod_message
|
||||
from .wiki import wikisum
|
||||
from .qr import generate_qr
|
||||
from .kroger import Kroger
|
||||
from .garfai import GarfAI
|
||||
from .respond import GarfbotRespond
|
@ -4,6 +4,7 @@ import config
|
||||
import aiohttp
|
||||
import asyncio
|
||||
import discord
|
||||
import wikipedia
|
||||
from openai import AsyncOpenAI
|
||||
from garfpy import logger
|
||||
|
||||
@ -66,8 +67,6 @@ class GarfAI:
|
||||
self.image_request_queue.task_done()
|
||||
await asyncio.sleep(2)
|
||||
|
||||
# GarfChats
|
||||
@staticmethod
|
||||
async def generate_chat(self, question):
|
||||
try:
|
||||
client = AsyncOpenAI(api_key = self.openaikey)
|
||||
@ -89,3 +88,11 @@ class GarfAI:
|
||||
except Exception as e:
|
||||
logger.info(e, flush=True)
|
||||
return f"`GarfBot Error: Lasagna`"
|
||||
|
||||
async def wikisum(self, query):
|
||||
try:
|
||||
summary = wikipedia.summary(query)
|
||||
garfsum = await self.generate_chat(f"Please summarize in your own words: {summary}")
|
||||
return garfsum
|
||||
except Exception as e:
|
||||
return e
|
@ -4,45 +4,67 @@ from base64 import b64encode
|
||||
from garfpy import logger
|
||||
|
||||
|
||||
client_id = config.CLIENT_ID
|
||||
client_secret = config.CLIENT_SECRET
|
||||
class Kroger:
|
||||
def __init__(self):
|
||||
self.client_id = config.CLIENT_ID
|
||||
self.client_secret = config.CLIENT_SECRET
|
||||
self.auth = b64encode(f"{self.client_id}:{self.client_secret}".encode()).decode()
|
||||
|
||||
auth = b64encode(f"{client_id}:{client_secret}".encode()).decode()
|
||||
def kroger_token(self):
|
||||
headers = {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'Authorization': f'Basic {self.auth}'
|
||||
}
|
||||
|
||||
def kroger_token():
|
||||
headers = {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'Authorization': f'Basic {auth}'
|
||||
}
|
||||
response = requests.post('https://api.kroger.com/v1/connect/oauth2/token', headers=headers, data={
|
||||
'grant_type': 'client_credentials',
|
||||
'scope': 'product.compact'
|
||||
})
|
||||
|
||||
response = requests.post('https://api.kroger.com/v1/connect/oauth2/token', headers=headers, data={
|
||||
'grant_type': 'client_credentials',
|
||||
'scope': 'product.compact'
|
||||
})
|
||||
response.raise_for_status()
|
||||
return response.json()['access_token']
|
||||
|
||||
response.raise_for_status()
|
||||
return response.json()['access_token']
|
||||
def find_store(self, zipcode, kroken):
|
||||
headers = {
|
||||
'Authorization': f'Bearer {kroken}',
|
||||
}
|
||||
params = {
|
||||
'filter.zipCode.near': zipcode,
|
||||
'filter.limit': 1,
|
||||
}
|
||||
response = requests.get('https://api.kroger.com/v1/locations', headers=headers, params=params)
|
||||
return response.json()
|
||||
|
||||
def find_store(zipcode, kroken):
|
||||
headers = {
|
||||
'Authorization': f'Bearer {kroken}',
|
||||
}
|
||||
params = {
|
||||
'filter.zipCode.near': zipcode,
|
||||
'filter.limit': 1,
|
||||
}
|
||||
response = requests.get('https://api.kroger.com/v1/locations', headers=headers, params=params)
|
||||
return response.json()
|
||||
def search_product(self, product, loc_id, kroken):
|
||||
logger.info(f"Searching for {product}...")
|
||||
headers = {
|
||||
'Authorization': f'Bearer {kroken}',
|
||||
}
|
||||
params = {
|
||||
'filter.term': product,
|
||||
'filter.locationId': loc_id,
|
||||
'filter.limit': 10
|
||||
}
|
||||
response = requests.get('https://api.kroger.com/v1/products', headers=headers, params=params)
|
||||
return response.json()
|
||||
|
||||
def search_product(product, loc_id, kroken):
|
||||
logger.info(f"Searching for {product}...")
|
||||
headers = {
|
||||
'Authorization': f'Bearer {kroken}',
|
||||
}
|
||||
params = {
|
||||
'filter.term': product,
|
||||
'filter.locationId': loc_id,
|
||||
'filter.limit': 10
|
||||
}
|
||||
response = requests.get('https://api.kroger.com/v1/products', headers=headers, params=params)
|
||||
return response.json()
|
||||
def garfshop(self, query):
|
||||
try:
|
||||
query = query.split()
|
||||
kroken = self.kroger_token()
|
||||
product = query[-2]
|
||||
zipcode = query[-1]
|
||||
loc_data = self.find_store(zipcode, kroken)
|
||||
loc_id = loc_data['data'][0]['locationId']
|
||||
store_name = loc_data['data'][0]['name']
|
||||
product_query = self.search_product(product, loc_id, kroken)
|
||||
products = product_query['data']
|
||||
sorted_products = sorted(products, key=lambda item: item['items'][0]['price']['regular'])
|
||||
response = f"Prices for `{product}` at `{store_name}` near `{zipcode}`:\n"
|
||||
for item in sorted_products:
|
||||
product_name = item['description']
|
||||
price = item['items'][0]['price']['regular']
|
||||
response += f"- `${price}`: {product_name} \n"
|
||||
return response
|
||||
except Exception as e:
|
||||
return e
|
||||
|
@ -1,12 +0,0 @@
|
||||
import wikipedia
|
||||
from garfpy import GarfAI
|
||||
|
||||
async def wikisum(search_term):
|
||||
try:
|
||||
summary = wikipedia.summary(search_term)
|
||||
garfsum = await GarfAI.generate_chat(f"Please summarize in your own words: {summary}")
|
||||
|
||||
return garfsum
|
||||
|
||||
except Exception as e:
|
||||
return e
|
Loading…
x
Reference in New Issue
Block a user