Advertisement
raufdnugroho

Steam Chat Bot

May 20th, 2017
2,621
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.33 KB | None | 0 0
  1. My Custom Bot - (un)Python(ic)
  2. """
  3. Based on https://github.com/ValvePython/steam
  4. """
  5. import time
  6. import datetime as dt
  7. import gevent
  8. import json
  9. import re
  10. from bs4 import BeautifulSoup
  11. from steam import SteamClient
  12. from steam.enums.emsg import EMsg
  13. from steam.core.msg import MsgProto
  14. from steam.guard import SteamAuthenticator
  15.  
  16.  
  17. class CustomClass(object):
  18.     def __init__(self, *args, **kwargs):
  19.         super(CustomClass, self).__init__(*args, **kwargs)
  20.  
  21.     def custom_run_forever(self, sec):
  22.         while 1:
  23.             gevent.sleep(sec)
  24.  
  25.     def post_comment(self, steamid, comment):
  26.         session = self.get_web_session()
  27.         url = "http://steamcommunity.com/comment/Profile/post/{}/-1/".format(
  28.             str(steamid))
  29.         data = {"comment": "{}".format(comment),
  30.                 "count": 6,
  31.                 "sessionid": self._web_session_id}
  32.         result = session.post(url, data=data)
  33.         return result.json()
  34.  
  35.     def fetch_card_data(self):
  36.         session = self.get_web_session()
  37.         url = "http://steamcommunity.com/profiles/STEAMID64/badges/"
  38.         result = session.get(url)
  39.         return result.text
  40.  
  41.     def add_free_license(self, subid):
  42.         session = self.get_web_session()
  43.         url = "http://store.steampowered.com/checkout/addfreelicense/"
  44.         data = {"sessionid": self._web_session_id,
  45.                 "action": "add_to_cart",
  46.                 "subid": subid}
  47.         result = session.post(url, data=data)
  48.         return result
  49.  
  50.  
  51. class MySteamClient(SteamClient, CustomClass):
  52.     pass
  53.  
  54.  
  55. client = MySteamClient()
  56. client.set_credential_location(path)
  57.  
  58. username = "username"
  59. password = "password"
  60. with open("path\secrets.json", "r") as data:
  61.     secrets = json.load(data)
  62.  
  63. sa = SteamAuthenticator(secrets)
  64.  
  65.  
  66. @client.on(EMsg.ClientFriendMsgIncoming)
  67. def onMessage(msg):
  68.     messageText = msg.body.message.rstrip().strip("\0")
  69.  
  70.     if "idle" in messageText:
  71.         while 1:
  72.             soup = BeautifulSoup((client.fetch_card_data()), "html.parser")
  73.             appids = []
  74.             for link in soup.find_all("a"):
  75.                 result = isCardAvailable(link.get("href"))
  76.                 if result:
  77.                     appids.append(result.group(1))
  78.             if not appids:
  79.                 sendMsg = MsgProto(EMsg.ClientFriendMsg)
  80.                 sendMsg.body.steamid = msg.body.steamid_from
  81.                 sendMsg.body.chat_entry_type = 1
  82.                 sendMsg.body.message = "No more games lef to idle."
  83.                 sendMsg.body.rtime32_server_timestamp = int(time.time())
  84.  
  85.                 client.send(sendMsg)
  86.                 gevent.sleep(2)
  87.                 break
  88.             else:
  89.                 sendMsg = MsgProto(EMsg.ClientFriendMsg)
  90.                 sendMsg.body.steamid = msg.body.steamid_from
  91.                 sendMsg.body.chat_entry_type = 1
  92.                 sendMsg.body.message = "Starts idling " + str(appids) + " at " + \
  93.                     dt.datetime.now().time().strftime("%H:%M:%S")
  94.                 sendMsg.body.rtime32_server_timestamp = int(time.time())
  95.  
  96.                 client.games_played(appids)
  97.                 client.send(sendMsg)
  98.                 gevent.sleep(1800)
  99.                 client.disconnect()
  100.                 gevent.sleep(2)
  101.                 client.relogin()
  102.                 gevent.sleep(3)
  103.     elif "stop" in messageText:
  104.         sendMsg = MsgProto(EMsg.ClientFriendMsg)
  105.         sendMsg.body.steamid = msg.body.steamid_from
  106.         sendMsg.body.chat_entry_type = 1
  107.         sendMsg.body.message = "Stops idling at " + \
  108.             dt.datetime.now().time().strftime("%H:%M:%S")
  109.         sendMsg.body.rtime32_server_timestamp = int(time.time())
  110.  
  111.         client.send(sendMsg)
  112.         gevent.sleep(1)
  113.         client.disconnect()
  114.     elif isCdKey(messageText):
  115.         cdkeyResult = client.register_product_key(messageText)
  116.         sendMsg = MsgProto(EMsg.ClientFriendMsg)
  117.         sendMsg.body.steamid = msg.body.steamid_from
  118.         sendMsg.body.chat_entry_type = 1
  119.         sendMsg.body.message = str(cdkeyResult)
  120.         sendMsg.body.rtime32_server_timestamp = int(time.time())
  121.  
  122.         client.send(sendMsg)
  123.     elif "addlicense" in messageText:
  124.         result = client.add_free_license(messageText.replace("addlicense ", ""))
  125.         sendMsg = MsgProto(EMsg.ClientFriendMsg)
  126.         sendMsg.body.steamid = msg.body.steamid_from
  127.         sendMsg.body.chat_entry_type = 1
  128.         sendMsg.body.message = str(result)
  129.         sendMsg.body.rtime32_server_timestamp = int(time.time())
  130.  
  131.         client.send(sendMsg)
  132.     elif "rep" in messageText:
  133.         sendMsg = MsgProto(EMsg.ClientFriendMsg)
  134.         sendMsg.body.steamid = msg.body.steamid_from
  135.         sendMsg.body.chat_entry_type = 1
  136.         sendMsg.body.message = "You're welcome"
  137.         sendMsg.body.rtime32_server_timestamp = int(time.time())
  138.  
  139.         client.post_comment(msg.body.steamid_from, "+ rep :)")
  140.         client.send(sendMsg)
  141.     elif messageText:
  142.         sendMsg = MsgProto(EMsg.ClientFriendMsg)
  143.         sendMsg.body.steamid = msg.body.steamid_from
  144.         sendMsg.body.chat_entry_type = 1
  145.         sendMsg.body.message = "Incorrect command line"
  146.         sendMsg.body.rtime32_server_timestamp = int(time.time())
  147.  
  148.         client.send(sendMsg)
  149.  
  150.  
  151. @client.friends.on(client.friends.EVENT_FRIEND_INVITE)
  152. def handleFriend(friend):
  153.     client.friends.add(friend.steam_id)
  154.  
  155.  
  156. @client.on(EMsg.ClientAddFriendResponse)
  157. def welcomeMessage(wel_message):
  158.     greet = MsgProto(EMsg.ClientFriendMsg)
  159.     greet.body.steamid = wel_message.body.steam_id_added
  160.     greet.body.chat_entry_type = 1
  161.     greet.body.message = "Thanks for adding me <3 [Auto generated message]"
  162.     greet.body.rtime32_server_timestamp = int(time.time())
  163.  
  164.     client.send(greet)
  165.  
  166.  
  167. def isCdKey(cdkey):
  168.     cdkey_regex = re.compile(r'^[A-Z0-9]{5}-[A-Z0-9]{5}-[A-Z0-9]{5}$')
  169.     return cdkey_regex.search(cdkey)
  170.  
  171.  
  172. def isCardAvailable(card):
  173.     card_regex = re.compile(r'steam://run/([0-9]+)')
  174.     return card_regex.search(card)
  175.  
  176.  
  177. if __name__ == "__main__":
  178.     try:
  179.         client.login(username, password, two_factor_code=sa.get_code())
  180.         # client.cli_login()
  181.         start_time = time.time()
  182.         print "Online"
  183.         client.custom_run_forever(3600)
  184.     except KeyboardInterrupt:
  185.         client.logout()
  186.         print "Total time %s seconds" % (time.time() - start_time)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement