Advertisement
Guest User

Untitled

a guest
Aug 30th, 2017
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.67 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. from steam.webapi import WebAPI
  3. import ts3
  4. import pymysql.cursors
  5.  
  6.  
  7. ts3_query_user     = "serveradmin"
  8. ts3_query_password = ""
  9.  
  10. database_server_address  = "localhost"
  11. database_server_user     = "teamspeaksteam"
  12. database_server_password = "lol"
  13.  
  14. # Let's open a ts3 query connection
  15. with ts3.query.TS3Connection(database_server_address) as ts3conn:
  16.     try:
  17.         # login using the teamspeak query credentials
  18.         ts3conn.login(
  19.             client_login_name=ts3_query_user,
  20.             client_login_password=ts3_query_password,
  21.         )
  22.         # stop the script when an error occuress during authentication
  23.     except ts3.query.TS3QueryError as err:
  24.         print("Login failed:", err.resp.error["msg"])
  25.         exit(1)
  26.  
  27.     # user session id 1 for the login
  28.     ts3conn.use(sid=1)
  29.  
  30.     # connecting with the database to lookup the teamspeak id for the steam id
  31.     connection = pymysql.connect(
  32.         host=database_server_address,
  33.         user=database_server_user,
  34.         password=database_server_password,
  35.         db='teamspeaksteam',
  36.         charset='utf8mb4',
  37.         cursorclass=pymysql.cursors.DictCursor
  38.         )
  39.  
  40.     online_clients = []
  41.  
  42.     clientlist = ts3conn.clientlist()
  43.  
  44.     for client in clientlist:
  45.         if client["client_type"] == "0":
  46.             online_clients.append(int(client["client_database_id"]))
  47.  
  48.     online_clients = str(tuple(online_clients))
  49.     print(online_clients)
  50.  
  51.  
  52.     try:
  53.         with connection.cursor() as cursor:
  54.             # Read a single record
  55.             sql = "SELECT `SteamID`, `TeamspeakID` FROM `Links` WHERE `TeamspeakID` in " + online_clients
  56.             cursor.execute(sql)
  57.             result = cursor.fetchall()
  58.             print(result)
  59.     finally:
  60.         connection.close()
  61.  
  62.     api = WebAPI("")
  63.  
  64.     steamids = ""
  65.     for item in result:
  66.         steamids += item["SteamID"] + ","
  67.  
  68.    
  69.    
  70.     response = api.ISteamUser.GetPlayerSummaries(steamids=steamids[:-1])["response"]
  71.  
  72.     for player in response["players"]:
  73.         # if "spielt: " not in ts3conn.clientinfo
  74.         for item in result:
  75.             if item["SteamID"] == player["steamid"]:
  76.                 for client in clientlist:
  77.                     if int(item["TeamspeakID"]) == int(client["client_database_id"]):
  78.                         # ts3conn.clientpoke(clid=client["clid"], msg=player.get("gameextrainfo"))
  79.                         group_exists = False
  80.  
  81.  
  82.                         game = str("spielt: " + str(player.get("gameextrainfo")))
  83.  
  84.                         if game == "spielt: PLAYERUNKNOWN'S BATTLEGROUNDS":
  85.                             game = "spielt: PUBG"
  86.                         game = game[:119]
  87.  
  88.                         print(game)
  89.  
  90.                         for group in list(ts3conn.servergrouplist()):
  91.                             if "spielt: " in group["name"]:
  92.                                 if game == group["name"] and len(game) >= len("spielt: "):
  93.                                     print("adding:" + game)
  94.                                     try:
  95.                                         ts3conn.servergroupaddclient(sgid=group["sgid"], cldbid=int(client["client_database_id"]))
  96.                                         ts3conn.servergroupaddperm(sgid=group["sgid"], permid=149, permvalue=2, permnegated=0, permskip=0)
  97.                                     except:
  98.                                         pass
  99.                                     group_exists = True
  100.                                 else:
  101.                                     print(group)
  102.                                     print(client)
  103.                                     ts3conn.servergroupaddperm(sgid=group["sgid"], permid=149, permvalue=2, permnegated=0, permskip=0)
  104.                                     try:
  105.                                         ts3conn.servergroupdelclient(sgid=group["sgid"], cldbid=int(client["client_database_id"]))
  106.                    
  107.                                     except:
  108.                                         pass
  109.                                
  110.                         if (not group_exists) and player.get("gameextrainfo"):
  111.                             print(group_exists)
  112.                             print(player["gameextrainfo"])
  113.                             print(game)
  114.                             ts3conn.servergroupadd(name=game)
  115.                             ts3conn.servergroupaddperm(sgid=group["sgid"], permid=149, permvalue=2, permnegated=0, permskip=0)
  116.                             for group in list(ts3conn.servergrouplist()):
  117.                                 if "spielt: " in group["name"]:
  118.                                     if "spielt: " + str(player.get("gameextrainfo")) == group["name"]:
  119.  
  120.                                         ts3conn.servergroupaddperm(sgid=group["sgid"], permid=149, permvalue=2, permnegated=0, permskip=0)
  121.  
  122.  
  123.                                         print("adding:" + player.get("gameextrainfo"))
  124.                                         try:
  125.                                             ts3conn.servergroupaddclient(sgid=group["sgid"], cldbid=int(client["client_database_id"]))
  126.                                         except:
  127.                                             pass
  128.                                         group_exists = True
  129.                                     else:
  130.                                         try:
  131.                                             ts3conn.servergroupdelclient(sgid=group["sgid"], cldbid=int(client["client_database_id"]))
  132.                                         except:
  133.                                             pass
  134.                            
  135.  
  136.         # print(player["steamid"], player["gameextrainfo"])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement