Skip to content

Commit

Permalink
Updating get_transactions to take timestamp as an arg instead of int
Browse files Browse the repository at this point in the history
  • Loading branch information
DMcP89 committed May 27, 2024
1 parent f7039e7 commit 8345351
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
7 changes: 5 additions & 2 deletions harambot/cogs/yahoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
19 changes: 11 additions & 8 deletions harambot/services/transaction_polling.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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,
Expand All @@ -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()
7 changes: 3 additions & 4 deletions harambot/yahoo_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 8345351

Please sign in to comment.