Skip to content

Commit

Permalink
Updating report service
Browse files Browse the repository at this point in the history
  • Loading branch information
DMcP89 committed Aug 29, 2024
1 parent e6e39cf commit 668c169
Showing 1 changed file with 30 additions and 21 deletions.
51 changes: 30 additions & 21 deletions harambot/services/reports.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from playhouse.shortcuts import model_to_dict
from yahoo_oauth import OAuth2
import logging
import time

from discord import SyncWebhook
from discord.errors import NotFound

Expand All @@ -8,17 +9,19 @@
from harambot.yahoo_api import Yahoo
from harambot import utils

import logging

from datetime import datetime, timedelta

logging.basicConfig()
logger = logging.getLogger("harambot.transaction_polling")
if "LOGLEVEL" in settings:
logger.setLevel(settings.LOGLEVEL)
logging.getLogger("discord").setLevel(settings.LOGLEVEL)
logging.getLogger("peewee").setLevel(settings.LOGLEVEL)
else:
logger.setLevel("DEBUG")
logging.getLogger("discord").setLevel("DEBUG")
logging.getLogger("peewee").setLevel("DEBUG")

logging.getLogger("yahoo_oauth").disabled = True

Expand All @@ -32,39 +35,45 @@

def poll_transactions(guild: Guild):
logger.info("Polling transactions for {}".format(guild.guild_id))
YahooAPI = Yahoo(
OAuth2(
settings.yahoo_key,
settings.yahoo_secret,
store_file=False,
**model_to_dict(guild),
),
guild.league_id,
guild.league_type,
)
ts = datetime.now() - timedelta(days=160)
YahooAPI = Yahoo()
ts = datetime.now() - timedelta(minutes=5)
try:
transactions = YahooAPI.get_transactions(timestamp=ts.timestamp())
transactions = YahooAPI.get_transactions(
timestamp=ts.timestamp(), guild_id=guild.guild_id
)
if transactions:
for transaction in transactions:
embed = embed_functions_dict[transaction["type"]](transaction)
webhook = SyncWebhook.from_url(guild.transaction_polling_webhook)
webhook = SyncWebhook.from_url(
guild.transaction_polling_webhook
)
try:
webhook.send(embed=embed)
except NotFound:
logger.exception("Webhook not found for {}".format(guild.guild_id))
logger.exception(
"Webhook not found for {}".format(guild.guild_id)
)
time.sleep(
1
) # sleep for 1 second between transactions to avoid rate limits
except Exception:
logger.info("Error fetching transactions for {}".format(guild.guild_id))
logger.info(
"Error fetching transactions for {}".format(guild.guild_id)
)


def report_service():
logger.info("Starting transaction polling service")
for guild in Guild.select().where(Guild.transaction_polling_service_enabled == 1):
for guild in Guild.select().where(
Guild.transaction_polling_service_enabled == 1
):
poll_transactions(guild=guild)
time.sleep(
5
) # sleep for 5 seconds between each guild to avoid rate limits


if __name__ == "__main__":
logger.info("Running Report Service")
report_service()

print(__name__)
logger.info("Report Service Finished")

0 comments on commit 668c169

Please sign in to comment.