Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- import requests
- import json
- import os
- from time import sleep
- from datetime import datetime, timedelta
- file_dir = os.path.dirname(os.path.realpath(__file__)) + '\win_lose_stats.txt'
- total_games = 0
- delay = 30
- def win_lose():
- summoner_id = 'Dumb%20Slμt'
- API_KEY = 'RGAPI-2df9a121-7ea6-413f-8043-b44c16ef3c9f'
- amount_matches = 10
- print('Analysing summoner data')
- summoner_data_url = 'https://euw1.api.riotgames.com/lol/summoner/v4/summoners/by-name/' + summoner_id + \
- '?api_key=' + API_KEY
- response = requests.get(summoner_data_url)
- if not response.status_code == 200:
- raise ValueError("Couldn't read summoner data")
- summoner_data = json.loads(response.content.decode('utf-8'))
- account_id = summoner_data['accountId']
- print('Resetting .txt file')
- # Create file or reset the file to avoid invalid data
- with open(file_dir, 'w') as fd:
- fd.write('')
- while 1:
- print('Checking matches..')
- msg = check_matches(account_id, amount_matches, API_KEY)
- if not msg:
- print('No new data\n')
- else:
- print('Matches were successfully analysed')
- write_txt_file(msg)
- print('Text file has been updated\n')
- sleep(delay)
- def write_txt_file(text):
- with open(file_dir, 'w') as fd:
- fd.write(text)
- def check_matches(account_id, amount_matches, API_KEY):
- match_url = 'https://euw1.api.riotgames.com/lol/match/v4/matchlists/by-account/' + account_id + '?endIndex=' + \
- str(amount_matches) + '&api_key=' + API_KEY
- response = requests.get(match_url)
- if not response.status_code == 200:
- raise ValueError("Couldn't read total match data")
- match_data = json.loads(response.content.decode('utf-8'))
- global total_games
- prev_total = total_games
- total_games = match_data['totalGames']
- if prev_total == match_data['totalGames']:
- return False
- previous_timestamp = datetime.now()
- statistics = []
- for matches in match_data['matches']:
- timestamp_unix = matches['timestamp'] / 1000 # It's in milliseconds..
- timestamp = datetime.fromtimestamp(timestamp_unix)
- time_diff = previous_timestamp - timestamp
- if time_diff < timedelta(hours=5):
- game_id = matches['gameId']
- statistics.append(check_win_of_game(game_id, account_id, API_KEY))
- previous_timestamp = timestamp # So we can compare with latest timestamp
- wins = statistics.count(True)
- lose = len(statistics) - wins
- msg = 'Win/Loss:\n' + str(wins) + ' W - ' + str(lose) + ' L'
- return msg
- def check_win_of_game(game_id, account_id, API_KEY):
- current_game_url = 'https://euw1.api.riotgames.com/lol/match/v4/matches/' + str(game_id) + '?api_key=' + API_KEY
- response = requests.get(current_game_url)
- if not response.status_code == 200:
- raise ValueError("Couldn't read total match data")
- game_data = json.loads(response.content.decode('utf-8'))
- participants = game_data['participantIdentities']
- for players in participants:
- player_id = players['player']['accountId']
- if account_id == player_id:
- participant_id = players['participantId']
- win = game_data['participants'][int(participant_id) - 1]['stats']['win']
- return win
- raise ValueError('No data for match was found..')
- if __name__ == '__main__':
- win_lose()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement