diff --git a/harambot/cogs/yahoo.py b/harambot/cogs/yahoo.py index 2a03893..7911b93 100644 --- a/harambot/cogs/yahoo.py +++ b/harambot/cogs/yahoo.py @@ -9,6 +9,7 @@ from yahoo_oauth import OAuth2 from playhouse.shortcuts import model_to_dict from typing import List, Optional +from datetime import datetime, timedelta from harambot.yahoo_api import Yahoo from harambot.database.models import Guild @@ -367,8 +368,10 @@ async def waivers(self, interaction: discord.Interaction, days: int = 1): "add": utils.create_add_embed, "drop": utils.create_drop_embed, } - - transactions = self.yahoo_api.get_transactions(days=days) + ts = datetime.now() - timedelta(days=days) + transactions = self.yahoo_api.get_transactions( + timestamp=ts.timestamp() + ) if transactions: await interaction.response.defer() for transaction in transactions: diff --git a/harambot/services/transaction_polling.py b/harambot/services/transaction_polling.py index e275d58..0decb4e 100644 --- a/harambot/services/transaction_polling.py +++ b/harambot/services/transaction_polling.py @@ -9,8 +9,7 @@ from harambot import utils import logging -import random -import time +from datetime import datetime, timedelta logger = logging.getLogger("harambot.transaction_polling") @@ -29,7 +28,7 @@ def poll_transactions(guild: Guild): - random_int = random.randint(1, 20) + logger.info("Polling transactions for {}".format(guild.guild_id)) YahooAPI = Yahoo( OAuth2( settings.yahoo_key, @@ -40,21 +39,25 @@ def poll_transactions(guild: Guild): guild.league_id, guild.league_type, ) - transactions = YahooAPI.get_transactions() + ts = datetime.now() - timedelta(days=160) + transactions = YahooAPI.get_transactions(timestamp=ts.timestamp()) if transactions: for transaction in transactions: embed = embed_functions_dict[transaction["type"]](transaction) webhook = SyncWebhook.from_url(guild.transaction_polling_webhook) webhook.send(embed=embed) - time.sleep(random_int) -with Pool(5) as executor: - results = list( +def report_service(): + logger.info("Starting transaction polling service") + with Pool(5) as executor: executor.map( poll_transactions, Guild.select().where( Guild.transaction_polling_service_enabled == 1 ), ) - ) + + +if __name__ == "__main__": + report_service() diff --git a/harambot/yahoo_api.py b/harambot/yahoo_api.py index 0d7da85..035bc4f 100644 --- a/harambot/yahoo_api.py +++ b/harambot/yahoo_api.py @@ -5,7 +5,7 @@ from yahoo_fantasy_api import game from cachetools import cached, TTLCache -from datetime import datetime, timedelta + logger = logging.getLogger("discord.harambot.yahoo_api") @@ -240,12 +240,11 @@ def get_latest_trade(self): ) return None - def get_transactions(self, days=1): - ts = datetime.now() - timedelta(days=days) + def get_transactions(self, timestamp=0.0): try: transactions = self.league().transactions("add,drop", "") filtered_transactions = [ - t for t in transactions if int(t["timestamp"]) > ts.timestamp() + t for t in transactions if float(t["timestamp"]) > timestamp ] return filtered_transactions except Exception: