Advertisement
hbinderup94

Win loss bot

Jan 6th, 2019
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.54 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import requests
  4. import json
  5. import os
  6. from time import sleep
  7. from datetime import datetime, timedelta
  8.  
  9. file_dir = os.path.dirname(os.path.realpath(__file__)) + '\win_lose_stats.txt'
  10. total_games = 0
  11. delay = 30
  12.  
  13. def win_lose():
  14.     summoner_id = 'Dumb%20Slμt'
  15.     API_KEY = 'RGAPI-2df9a121-7ea6-413f-8043-b44c16ef3c9f'
  16.     amount_matches = 10
  17.  
  18.     print('Analysing summoner data')
  19.  
  20.     summoner_data_url = 'https://euw1.api.riotgames.com/lol/summoner/v4/summoners/by-name/' + summoner_id + \
  21.                         '?api_key=' + API_KEY
  22.     response = requests.get(summoner_data_url)
  23.  
  24.     if not response.status_code == 200:
  25.         raise ValueError("Couldn't read summoner data")
  26.  
  27.     summoner_data = json.loads(response.content.decode('utf-8'))
  28.     account_id = summoner_data['accountId']
  29.  
  30.     print('Resetting .txt file')
  31.     # Create file or reset the file to avoid invalid data
  32.     with open(file_dir, 'w') as fd:
  33.         fd.write('')
  34.  
  35.     while 1:
  36.         print('Checking matches..')
  37.         msg = check_matches(account_id, amount_matches, API_KEY)
  38.  
  39.         if not msg:
  40.             print('No new data\n')
  41.         else:
  42.             print('Matches were successfully analysed')
  43.             write_txt_file(msg)
  44.             print('Text file has been updated\n')
  45.  
  46.         sleep(delay)
  47.  
  48.  
  49. def write_txt_file(text):
  50.     with open(file_dir, 'w') as fd:
  51.         fd.write(text)
  52.  
  53.  
  54. def check_matches(account_id, amount_matches, API_KEY):
  55.     match_url = 'https://euw1.api.riotgames.com/lol/match/v4/matchlists/by-account/' + account_id + '?endIndex=' + \
  56.                 str(amount_matches) + '&api_key=' + API_KEY
  57.  
  58.     response = requests.get(match_url)
  59.  
  60.     if not response.status_code == 200:
  61.         raise ValueError("Couldn't read total match data")
  62.  
  63.     match_data = json.loads(response.content.decode('utf-8'))
  64.  
  65.     global total_games
  66.     prev_total = total_games
  67.     total_games = match_data['totalGames']
  68.  
  69.     if prev_total == match_data['totalGames']:
  70.         return False
  71.  
  72.     previous_timestamp = datetime.now()
  73.  
  74.     statistics = []
  75.  
  76.     for matches in match_data['matches']:
  77.         timestamp_unix = matches['timestamp'] / 1000    # It's in milliseconds..
  78.         timestamp = datetime.fromtimestamp(timestamp_unix)
  79.  
  80.         time_diff = previous_timestamp - timestamp
  81.         if time_diff < timedelta(hours=5):
  82.             game_id = matches['gameId']
  83.             statistics.append(check_win_of_game(game_id, account_id, API_KEY))
  84.  
  85.         previous_timestamp = timestamp  # So we can compare with latest timestamp
  86.  
  87.     wins = statistics.count(True)
  88.     lose = len(statistics) - wins
  89.  
  90.     msg = 'Win/Loss:\n' + str(wins) + ' W - ' + str(lose) + ' L'
  91.     return msg
  92.  
  93.  
  94. def check_win_of_game(game_id, account_id, API_KEY):
  95.     current_game_url = 'https://euw1.api.riotgames.com/lol/match/v4/matches/' + str(game_id) + '?api_key=' + API_KEY
  96.  
  97.     response = requests.get(current_game_url)
  98.  
  99.     if not response.status_code == 200:
  100.         raise ValueError("Couldn't read total match data")
  101.  
  102.     game_data = json.loads(response.content.decode('utf-8'))
  103.  
  104.     participants = game_data['participantIdentities']
  105.  
  106.     for players in participants:
  107.         player_id = players['player']['accountId']
  108.         if account_id == player_id:
  109.             participant_id = players['participantId']
  110.             win = game_data['participants'][int(participant_id) - 1]['stats']['win']
  111.             return win
  112.  
  113.     raise ValueError('No data for match was found..')
  114.  
  115.  
  116. if __name__ == '__main__':
  117.     win_lose()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement