bukson / steampy

A Steam trading library for python 3
MIT License
572 stars 154 forks source link

How to keep long-time logining status #326

Open 1e0n-xx opened 11 months ago

1e0n-xx commented 11 months ago

Basically, when you login in and save the pkl. But the validating time is only for 24 hours. How to keep a living session for long time

SamuelKollar commented 11 months ago

308 talks about this if you read to the end, I am currently having the same issue but dont have time to look at the code and make a pr

1e0n-xx commented 11 months ago

308 talks about this if you read to the end, I am currently having the same issue but dont have time to look at the code and make a pr

Really appreciate it. I have maFile for each account, I want to know if there are any useful info in maFile session part

Darkinvo commented 11 months ago

you should after login save cookies in file json or txt and for next time use cookies file and without try login and next do other's work you need

from steampy.client import SteamClient

steam = SteamClient(API_KEY)
steam.login(USERNAME,PASSWORD,STEAM_GAURD)
steam._session.cookies.get_dict()
...
SamuelKollar commented 10 months ago

First I log in like this, using login() method which works perfectly fine

try: with open('steam_cookies.json', 'r', encoding='utf-8') as f: cookies = json.load(f) print('loaded cookies',flush=True) except: cookies = None

steam_client = SteamClient(<bot_api_key>)

steam_client._session.cookies.set("steamRememberLogin", cookies)

try:
  steam_client.login(username, pswrd, steam_guard)
  print('logged in',flush=True)
except:
  steam_client = None  

After the session drops I try to log in using set_login_cookies() if I have cookies from previous login, if not I try to logout and in again

if not session_alive: if cookies != None: steam_client.set_login_cookies(cookies) print('logged in with set_login_cookies',flush=True) else: print('logout - login method',flush=True) steam_client.logout()

      steam_client = SteamClient(<bot_api_key>)
      steam_client._session.cookies.set("steamRememberLogin", 'true')
      steam_client.login(username, pswrd, steam_guard)

The problem is that from my testing I can see that sending trades work fine the first day but after that it stops working, if you have any suggestions how I can keep the session alive for longer or if there is a better way of refreshing the session please let me know.

Darkinvo commented 10 months ago

this is my example code for help you fast solutions:

import json

with open("file_cookies.json") as file_cookie:
   cookie =  json.load(file_cookie) 

   if cookie["LoginSteamSource"] == "":
      ### do Login
      ### after login save login cookies in file_cookies.json
   else:
       if cookie["LoginSteamSource"]: ## check is alive or not
            ....
       else:
         ### do login again and save cookies in file_cookies.json

       steamClient = SteamClient("apikey",username,cookie) # cookie type(dict)
       ### go next work you need 

######################################

any time first check file cookie and next do if need login again else do any work you need for bot ... good luck ;) mybe this example helped you if ... :D

SamuelKollar commented 10 months ago

What function do you use for login ? Because when I use login() first time and try to use it second time afte the session expires it doesnt work, it only works the first time.

Darkinvo commented 10 months ago

What function do you use for login ? Because when I use login() first time and try to use it second time afte the session expires it doesnt work, it only works the first time.


class SteamBot():

    _game = GameOptions

    def __init__(self,steamid:str,limit:int,game:str):

        self.steamid = steamid
        self.limit = limit
        self.cookies = {}
        self.bot = None
        self.game = None

        if game == "dota2": self.game = GameOptions.DOTA2
        elif game == "cs2": self.game = GameOptions.CS
        elif game == "pubg": self.game = GameOptions.PUBG
        elif game == "rust": self.game = GameOptions.RUST
        else : self.game = GameOptions.STEAM

    def _login(self):

        bot =  SteamClient(API_KEY)
        bot.login(USERNMAE,PASSWORD,STEAM_GUARD)
        cookie  = bot._session.cookies.get_dict()

        cookie['next_login']=f"{dt.datetime.now() + dt.timedelta(days=7)}"    
        json_update(cookie,COOKIE_FILE) # json file cookie updated

        self.bot = bot
        self.cookies.update(cookie)

    def _login_cookie(self):
        bot = SteamClient(API_KEY,username=USERNMAE,steam_guard=STEAM_GUARD,login_cookies=self.cookies)
        self.bot = bot

    def _check_login(self):

        cookie = json_read(COOKIE_FILE)
        # cookie haved or if haved is alive token bot or not
        if cookie['next_login'] == "" or fix_datetime(cookie['next_login']) < dt.datetime.now():
            self._login()

        else:
            self.cookies.update(cookie)
            self._login_cookie()

    def get_items_inventory_user(self):
        self._check_login()

        inventory_user = self.bot.get_partner_inventory(
            partner_steam_id=self.steamid,
            game=self.game,
            merge=True,
            count=self.limit
        )

        # with open("steambot/items.json", 'w') as file:
        #     json.dump(inventory_user,file,cls=PrettyJSONEncoder)
        #     file.close()

        return inventory_user

this is my code use for bot and worked for me ... anyway you can read this and used ...

SamuelKollar commented 10 months ago

I just tried it and it worked as a login the first time after the bot logged off but after that it stopped working again.

SamuelKollar commented 10 months ago

If I have a login session already how should I handle when is_session_alive() returns False and I want to relogin again. Since doing login() the second time doesnt work.

Darkinvo commented 10 months ago

you just first time login save cookies in a file and second you do not need login again . use cookies before saved use:

login_cookies  = {} ## saved before with first time login ... 
SteamClient(API_KEY,username=USERNMAE,steam_guard=STEAM_GUARD,login_cookies=self.cookies)
...
1e0n-xx commented 10 months ago

cookies before saved use:

how long do these cookies alive, 1week or longer?