diff --git a/harambot/services/reports.py b/harambot/services/reports.py index e81cc55..b829987 100644 --- a/harambot/services/reports.py +++ b/harambot/services/reports.py @@ -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 @@ -8,7 +9,7 @@ from harambot.yahoo_api import Yahoo from harambot import utils -import logging + from datetime import datetime, timedelta logging.basicConfig() @@ -16,9 +17,11 @@ 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 @@ -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")