# -*- coding: utf-8 -*-
import urllib.request
import json
import operator
import os
from collections import defaultdict
from multiprocessing.dummy import Pool
import threading
class Counter:
def __init__(self, i=0):
self.i = i
# create a lock
self.lock = threading.Lock()
def __iter__(self):
return self
def next(self):
# acquire/release the lock when updating self.i
with self.lock:
self.i += 1
return self.i
def current(self):
return self.i
os.system('cls')
worker_pool_size = 20
worker_pool = Pool(worker_pool_size)
games = 5000
game_range = [860000, 970000]
counter = Counter()
chunk_size = 3
common_words = defaultdict(int)
common_champions = defaultdict(int)
exclude = ['a', 'i', 'me', 'why', 'to', 'and', 'my', 'you', 'dont', 'with', 'what', 'in',
'are', 'more', 'so', 'have', 'not', 'for', 'no', 'is', 'yes', 'im', 'u', '?', 'the', 'or',
'do', 'this', 'your', 'on', 'we', 'they', 'our', 'it', 'just', 'go', 'he', 'all', 'that',
'ur', 'who', 'of', 'can', 'its', 'get', 'cant', 'it\'s', 'can\'t', 'don\'t', 'i\'m', 'now',
'if', 'got', 'ss']
def loading_logic(i):
if counter.current() >= games:
return
try:
request = urllib.request.urlopen('http://euw.leagueoflegends.com/tribunal/en/get_reform_game/' + str(i) + '/1/')
except:
return
data = request.read()
data = data.decode('utf-8')
if (data == ''):
return
data = json.loads(data)
for message in data['chat_log']:
if message['association_to_offender'] == 'offender':
words = message['message'].split()
for word in words:
word = word.lower()
if not word.isdigit() and word not in exclude:
common_words[word] += 1
for player in data['players']:
if player['association_to_offender'] == 'offender':
common_champions[player['champion_name']] += 1
break
counter.next()
for i in range(game_range[0], game_range[1], worker_pool_size*chunk_size):
os.system('cls')
print('%s/%s games analyzed' % (counter.current(), games))
worker_pool.map(loading_logic, range(i, i+worker_pool_size*chunk_size))
if counter.current() >= games:
break
common_words = sorted(common_words.items(), key=operator.itemgetter(1), reverse=True)
common_champions = sorted(common_champions.items(), key=operator.itemgetter(1), reverse=True)
print('Words used: ', len(common_words))
for i in range(30):
print(common_words[i][0], ' - ', common_words[i][1])
print()
print()
for i in range(30):
print(common_champions[i][0], ' - ', common_champions[i][1])