looks good
This commit is contained in:
33
garfmain.py
33
garfmain.py
@ -5,8 +5,8 @@ import subprocess
|
|||||||
|
|
||||||
from garfpy import(
|
from garfpy import(
|
||||||
logger, is_private,
|
logger, is_private,
|
||||||
kroger_token, find_store, search_product,
|
aod_message, generate_qr,
|
||||||
aod_message, wikisum, generate_qr, GarfAI, GarfbotRespond)
|
Kroger, GarfAI, GarfbotRespond)
|
||||||
|
|
||||||
|
|
||||||
gapikey = config.GIF_TOKEN
|
gapikey = config.GIF_TOKEN
|
||||||
@ -20,9 +20,9 @@ intents.messages = True
|
|||||||
intents.message_content = True
|
intents.message_content = True
|
||||||
garfbot = discord.Client(intents=intents)
|
garfbot = discord.Client(intents=intents)
|
||||||
|
|
||||||
|
|
||||||
garf_respond = GarfbotRespond()
|
garf_respond = GarfbotRespond()
|
||||||
garfield = GarfAI()
|
garfield = GarfAI()
|
||||||
|
kroger = Kroger()
|
||||||
|
|
||||||
|
|
||||||
@garfbot.event
|
@garfbot.event
|
||||||
@ -49,9 +49,9 @@ async def on_message(message):
|
|||||||
return
|
return
|
||||||
|
|
||||||
if lower.startswith("hey garfield") or isinstance(message.channel, discord.DMChannel):
|
if lower.startswith("hey garfield") or isinstance(message.channel, discord.DMChannel):
|
||||||
question = content[12:] if lower.startswith("hey garfield") else message.content
|
prompt = content[12:] if lower.startswith("hey garfield") else message.content
|
||||||
answer = await garfield.generate_chat(question)
|
answer = await garfield.generate_chat(prompt)
|
||||||
logger.info(f"Chat Request - User: {user}, Server: {guild}, Prompt: {question}")
|
logger.info(f"Chat Request - User: {user}, Server: {guild}, Prompt: {prompt}")
|
||||||
await message.channel.send(answer)
|
await message.channel.send(answer)
|
||||||
|
|
||||||
if lower.startswith('garfpic '):
|
if lower.startswith('garfpic '):
|
||||||
@ -62,8 +62,8 @@ async def on_message(message):
|
|||||||
|
|
||||||
# Wikipedia
|
# Wikipedia
|
||||||
if lower.startswith('garfwiki '):
|
if lower.startswith('garfwiki '):
|
||||||
search_term = message.content[9:]
|
query = message.content[9:]
|
||||||
summary = await wikisum(search_term)
|
summary = await garfield.wikisum(query)
|
||||||
await message.channel.send(summary)
|
await message.channel.send(summary)
|
||||||
|
|
||||||
# QR codes
|
# QR codes
|
||||||
@ -124,21 +124,8 @@ async def on_message(message):
|
|||||||
# Kroger Shopping
|
# Kroger Shopping
|
||||||
if lower.startswith("garfshop "):
|
if lower.startswith("garfshop "):
|
||||||
try:
|
try:
|
||||||
kroken = kroger_token()
|
query = message.content[9:]
|
||||||
kroger_query = message.content.split()
|
response = kroger.garfshop(query)
|
||||||
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"
|
|
||||||
await message.channel.send(response)
|
await message.channel.send(response)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
await message.channel.send(f"`GarfBot Error: {str(e)}`")
|
await message.channel.send(f"`GarfBot Error: {str(e)}`")
|
||||||
|
@ -1,17 +1,10 @@
|
|||||||
# garfpy/__init__.py
|
# garfpy/__init__.py
|
||||||
|
|
||||||
from .log import logger
|
from .log import logger
|
||||||
from .kroger import(
|
from .kroger import Kroger
|
||||||
kroger_token, find_store, search_product
|
|
||||||
)
|
|
||||||
from .garfai import(
|
|
||||||
garfpic,
|
|
||||||
process_image_requests,
|
|
||||||
generate_chat
|
|
||||||
)
|
|
||||||
from .iputils import is_private
|
from .iputils import is_private
|
||||||
from .aod import aod_message
|
from .aod import aod_message
|
||||||
from .wiki import wikisum
|
|
||||||
from .qr import generate_qr
|
from .qr import generate_qr
|
||||||
|
from .kroger import Kroger
|
||||||
from .garfai import GarfAI
|
from .garfai import GarfAI
|
||||||
from .respond import GarfbotRespond
|
from .respond import GarfbotRespond
|
@ -4,6 +4,7 @@ import config
|
|||||||
import aiohttp
|
import aiohttp
|
||||||
import asyncio
|
import asyncio
|
||||||
import discord
|
import discord
|
||||||
|
import wikipedia
|
||||||
from openai import AsyncOpenAI
|
from openai import AsyncOpenAI
|
||||||
from garfpy import logger
|
from garfpy import logger
|
||||||
|
|
||||||
@ -66,8 +67,6 @@ class GarfAI:
|
|||||||
self.image_request_queue.task_done()
|
self.image_request_queue.task_done()
|
||||||
await asyncio.sleep(2)
|
await asyncio.sleep(2)
|
||||||
|
|
||||||
# GarfChats
|
|
||||||
@staticmethod
|
|
||||||
async def generate_chat(self, question):
|
async def generate_chat(self, question):
|
||||||
try:
|
try:
|
||||||
client = AsyncOpenAI(api_key = self.openaikey)
|
client = AsyncOpenAI(api_key = self.openaikey)
|
||||||
@ -89,3 +88,11 @@ class GarfAI:
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.info(e, flush=True)
|
logger.info(e, flush=True)
|
||||||
return f"`GarfBot Error: Lasagna`"
|
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,15 +4,16 @@ from base64 import b64encode
|
|||||||
from garfpy import logger
|
from garfpy import logger
|
||||||
|
|
||||||
|
|
||||||
client_id = config.CLIENT_ID
|
class Kroger:
|
||||||
client_secret = config.CLIENT_SECRET
|
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):
|
||||||
|
|
||||||
def kroger_token():
|
|
||||||
headers = {
|
headers = {
|
||||||
'Content-Type': 'application/x-www-form-urlencoded',
|
'Content-Type': 'application/x-www-form-urlencoded',
|
||||||
'Authorization': f'Basic {auth}'
|
'Authorization': f'Basic {self.auth}'
|
||||||
}
|
}
|
||||||
|
|
||||||
response = requests.post('https://api.kroger.com/v1/connect/oauth2/token', headers=headers, data={
|
response = requests.post('https://api.kroger.com/v1/connect/oauth2/token', headers=headers, data={
|
||||||
@ -23,7 +24,7 @@ def kroger_token():
|
|||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
return response.json()['access_token']
|
return response.json()['access_token']
|
||||||
|
|
||||||
def find_store(zipcode, kroken):
|
def find_store(self, zipcode, kroken):
|
||||||
headers = {
|
headers = {
|
||||||
'Authorization': f'Bearer {kroken}',
|
'Authorization': f'Bearer {kroken}',
|
||||||
}
|
}
|
||||||
@ -34,7 +35,7 @@ def find_store(zipcode, kroken):
|
|||||||
response = requests.get('https://api.kroger.com/v1/locations', headers=headers, params=params)
|
response = requests.get('https://api.kroger.com/v1/locations', headers=headers, params=params)
|
||||||
return response.json()
|
return response.json()
|
||||||
|
|
||||||
def search_product(product, loc_id, kroken):
|
def search_product(self, product, loc_id, kroken):
|
||||||
logger.info(f"Searching for {product}...")
|
logger.info(f"Searching for {product}...")
|
||||||
headers = {
|
headers = {
|
||||||
'Authorization': f'Bearer {kroken}',
|
'Authorization': f'Bearer {kroken}',
|
||||||
@ -46,3 +47,24 @@ def search_product(product, loc_id, kroken):
|
|||||||
}
|
}
|
||||||
response = requests.get('https://api.kroger.com/v1/products', headers=headers, params=params)
|
response = requests.get('https://api.kroger.com/v1/products', headers=headers, params=params)
|
||||||
return response.json()
|
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
|
|
Reference in New Issue
Block a user