cwendt94/espn-api

Activity time stamp (football)

BrianPaur opened this issue · 2 comments

Sport

All

Summary

hi, thanks for putting this together. I've been using the past couple of years and it's been a good project to learn from.

Request: Can a time stamp be added to the .activity file?

Reason: So that I could see the time when someone traded to track it.

I added it to my own branch and was able to get it to work. I'm self-taught though and I don't know if it follows best practices. The date is in epoch on the request so I had to write something in to convert it. But this is working for my needs for now. Lines I updated below.

.league

def recent_activity(self, size: int = 25, msg_type: str = None) -> List[Activity]:
    '''Returns a list of recent league activities (Add, Drop, Trade)'''
    if self.year < 2019:
        raise Exception('Cant use recent activity before 2019')

    msg_types = [178,180,179,239,181,244]
    if msg_type in ACTIVITY_MAP:
        msg_types = [ACTIVITY_MAP[msg_type]]
    params = {
        'view': 'kona_league_communication'
    }

    filters = {"topics":{"filterType":{"value":["ACTIVITY_TRANSACTIONS"]},"limit":size,"limitPerMessageSet":{"value":25},"offset":0,"sortMessageDate":{"sortPriority":1,"sortAsc":False},"sortFor":{"sortPriority":2,"sortAsc":False},"filterIncludeMessageTypeIds":{"value":msg_types}}}
    headers = {'x-fantasy-filter': json.dumps(filters)}
    data = self.espn_request.league_get(extend='/communication/', params=params, headers=headers)
    data = data['topics']
    activity = [Activity(topic, self.player_map, self.get_team_data, self.player_info, None) for topic in data]
    return activity

.activity

from .constant import ACTIVITY_MAP
import datetime

class Activity(object):
    def __init__(self, data, player_map, get_team_data, player_info, date):
        # List of list (Team, action, Player, Time Stamp)
        self.actions = []
        # needed to format from epoch datetime to readable format
        self.date = datetime.datetime.fromtimestamp(data['date']/1000).strftime('%m-%d-%Y %H:%M:%S')

        for msg in data['messages']:
            team = ''
            action = 'UNKNOWN'
            player = None
            bid_amount = 0
            msg_id = msg['messageTypeId']
            if msg_id == 244:
                team = get_team_data(msg['from'])
            elif msg_id == 239:
                team = get_team_data(msg['for'])
            else:
                team = get_team_data(msg['to'])
            if msg_id in ACTIVITY_MAP:
                action = ACTIVITY_MAP[msg_id]
            if action == 'WAIVER ADDED':
                bid_amount = msg.get('from', 0)
            if team:
                for team_player in team.roster:
                    if team_player.playerId == msg['targetId']:
                        player = team_player
                        break
            if not player:
                player = player_info(playerId=msg['targetId'])
            # updated to remove bid (I didn't need. I get others might want it) and add date
            self.actions.append((team, action, player, self.date))

    def __repr__(self):
        # updated to fit date
        return 'Activity(' + ' '.join("(%s,%s,%s,%s)" % tup[0:4] for tup in self.actions) + ')'

thanks!
Screenshot 2023-08-19 191058

Hey @BrianPaur so the date is already part of activity and would just need to be converted when accessing the class. Because the date is the same for the whole activity I think it makes more sense just to have it outside of action and then can be accessed with league. recent_activity [0].date.

@cwendt94 you're right... Thanks for replying back. I started accessing the date that way. I think it makes more sense.

thanks!