smiley / steamapi

An unofficial object-oriented Python library for accessing the Steam Web API.
MIT License
456 stars 105 forks source link

Is there a way to access the amount of hours played in the last 2 weeks? #30

Closed alexhutman closed 8 years ago

alexhutman commented 8 years ago

Hello, first off I'm extremely new to GitHub so I hope I'm doing this properly. Second, I've noticed that recently_played only shows the games that were recently played and not the amount of time played in the past two weeks (or the total amount played). Is there any way to access this through steamapi? Thank you!

smiley commented 8 years ago

Yep, I added this a while back:

steamapi/user.py:153

# In the function in charge of creating "SteamApp" (game) objects:
# ...
if 'playtime_2weeks' in game:
    game_obj.playtime_2weeks = game.playtime_2weeks
if 'playtime_forever' in game:
    game_obj.playtime_forever = game.playtime_forever
# ...

When you fetch a games list through a user object (e.g.: me.recently_played), we add a couple more attributes called playtime_2weeks and playtime_forever. They're pretty self-explanatory, except that they're in minutes. (I might add a datetime.timedelta conversion sometime, for ease-of-use)

alexhutman commented 8 years ago

Jeez, I apologize for the stupid question. I'm new to Python (I'm used to Java) so all of this is way different from what I know! One more thing: is there an easy way to search for the game you want from the list? I've tried many things but the only thing that has worked was

def checkIndex(user):
    for i in range(len(user.recently_played)):
        if "Counter-Strike: Global Offensive" == str(memer.recently_played[i]):
            return i
        else:
            return -1

It works but since it is comparing strings it has to be very precise (in case I want to search for another game) so I was wondering if there was something that was built in. Thank you very much!

smiley commented 8 years ago

You can match games by using their app IDs (as found in the game's store URL). Thus, a function scanning a user's recently_played games for a specific ID will look like this:

def get_game_from_user(user, game_id):
    for game in user.recently_played:
        if game.appid == game_id:
            return game

On the topic of Python: You don't have to do i-based for blocks in Python. In Python, every for is actually a foreach. In addition, returning a value from a function is not mandatory. If a function ends not due to a return or a raise (throw), it effectively return-s the special object None: (None is essentially null)

def scan_games(user):
    for game_id in ids_i_want:
        game = get_game_from_user(user, game_id)
        # If the game was not found, "game" will be "None"
        if game: # I prefer the verbose syntax of "if X is not None", but most devs use this one
            print("Found game #{0}, named {1}".format(game_id, game.name))

You can also do object-based equality checks for games. For example, this is valid:

>>> csgo = app.SteamApp(730)
>>> me = user.SteamUser(76561197960434622)  # Steam ID for "afarnsworth"
>>> for game in me.games:
...     if game == csgo:
...         print("Good news, everyone! I have CS:GO!")
...         break
alexhutman commented 8 years ago

Wow! This library is awesome! Thanks for everything!