GetFriendsLevelInSteam Tool

import requests
import json
import PySimpleGUI as sg
import sqlite3

# params key, steamid
GET_STEAM_LEVEL_URL = 'https://api.steampowered.com/IPlayerService/GetSteamLevel/v1/'
GET_FRIEND_LIST_URL = 'https://api.steampowered.com/ISteamUser/GetFriendList/v1'
# + '&relationship=' + relationship

# params key, steamids
GET_PLAYER_SUMMARIES_URL = 'https://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2'

def getFriendsList(connect, cursor, values):
    cursor.execute('drop table if exists friendslist;')

    cursor.execute('''create table friendslist(
                    steamid int primary key not null,
                    personaname text,
                    player_level int);''')

# -------------------------------------------------------------------------------
    response = requests.get(GET_FRIEND_LIST_URL, params = {'key':values['key'], 'steamid':values['steamid']})

    if response.status_code != 200:
        print(response.text)

    for friend in response.json()['friendslist']['friends']:
        cursor.execute('insert into friendslist(steamid) values(?);', (friend['steamid'], ))

    connect.commit()

def getFriendsName(connect, cursor, values):
    cursor.execute('select count(steamid) from friendslist;')

    for i in range(0, cursor.fetchall()[0][0], 100):
        cursor.execute('select steamid from friendslist limit 100 offset ?;', (i, ))

        steamids = []

        for v in cursor.fetchall():
            steamids.append(str(v[0]))

        response = requests.get(GET_PLAYER_SUMMARIES_URL, params={'key':values['key'], 'steamids':','.join(steamids)})

        if response.status_code != 200:
            print(response.text)
            break

        for player in response.json()['response']['players']:
            cursor.execute('update friendslist set personaname = ? where steamid = ?;', (player['personaname'], player['steamid']))

    connect.commit()

def getFriendsLevel(connect, cursor, values):
    cursor.execute('select steamid from friendslist;')

    for v in cursor.fetchall():
        response = requests.get(GET_STEAM_LEVEL_URL, params={'key':values['key'], 'steamid':v[0]})

        if response.status_code != 200:
            print(response.text)
            break

        player_level = response.json()['response'].get('player_level', -1)

        cursor.execute('update friendslist set player_level = ? where steamid = ?;', (player_level, v[0]))

        cursor.execute('select personaname from friendslist where steamid = ?;', (v[0], ))

        print(cursor.fetchall()[0][0], player_level)

    connect.commit()

def show(connect, cursor, values):
    cursor.execute('select personaname, player_level from friendslist order by player_level asc;')

    for v in cursor.fetchall():
        print(v)

if __name__ == '__main__':
    layout = [[sg.Text('Key:', size=(10, 1)), sg.InputText(key='key')],
              [sg.Text('Steam Id:', size=(10, 1)), sg.InputText(key='steamid')],
              [sg.Button('1. Get friends list', key='button1'),
               sg.Button('2. Get friends name', key='button2'),
               sg.Button('3. Get friends level', key='button3'),
               sg.Button('Show', key='button4')]
             ]

    window = sg.Window('GetFriendsLevelInSteam Tool', layout)

    connect = sqlite3.connect('cache.db')
    cursor = connect.cursor()

    while True:
        event, values = window.read()

        if event == sg.WINDOW_CLOSED:
            break
        elif event == 'button1':
            getFriendsList(connect, cursor, values)
            print('Get friends list ... done')
        elif event == 'button2':
            getFriendsName(connect, cursor, values)
            print('Get friends name ... done')
        elif event == 'button3':
            getFriendsLevel(connect, cursor, values)
            print('Get frineds level ... done')
        elif event == 'button4':
            show(connect, cursor, values)

    cursor.close()
    connect.commit()
    connect.close()

    window.close()

 

上一篇:python连接mysql


下一篇:mongo 进阶——查询 - 掘金