Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- sys.path.append("/var/pantheon")
- import sortedcontainers
- import asyncio
- from pantheon import Pantheon
- import utils.exceptions as exc
- import time, datetime, json
- timeBegin = time.mktime(datetime.datetime.utcnow().timetuple())
- server = "euw1"
- key = "RGAPI-key"
- pantheon = Pantheon(server, key, True)
- listMatchId = sortedcontainers.SortedSet()
- listAccountId = sortedcontainers.SortedSet()
- listSummonerId = sortedcontainers.SortedSet()
- listLeagueId = sortedcontainers.SortedSet()
- countLeaguePositionCalls=0
- async def getMatchIds(data):
- #If at least one match in the list
- if data['totalGames'] > 0:
- #Get all the matchIds of the list
- matchIds = []
- for match in data['matches']:
- if match['platformId'].lower()==server:
- matchIds.append(match['gameId'])
- #Lock the global matchlist, find the new matchIds and add them to the global list
- with await lockMatchlist:
- matchlist = [x for x in matchIds if x not in listMatchId]
- for m in matchlist:
- listMatchId.add(m)
- #Put the new matchIds in the queue
- for i in matchlist:
- await queueMatchId.put(i)
- async def getAccountIds(data):
- participants = []
- for p in (data['participantIdentities']):
- if p['player']['currentPlatformId'].lower()==server:
- if 'summonerId' in p['player']:
- participants.append({"accountId":p['player']['currentAccountId'],"summonerId":p['player']['summonerId'],"platformId":p['player']['currentPlatformId']})
- else:
- participants.append({"accountId":p['player']['currentAccountId'],"summonerId":0,"platformId":p['player']['currentPlatformId']})
- with await lockAccountlist:
- participantslist = [x for x in participants if x['accountId'] not in listAccountId]
- for p in participantslist:
- listAccountId.add(p['accountId'])
- #Put the new accountIds in the queue
- for i in participantslist:
- await queueAccountId.put(i['accountId'])
- #queueAccountSave.put(i)
- with await lockSummonerlist:
- participantslist = [x for x in participants if x['summonerId'] not in listSummonerId]
- for s in participantslist:
- listSummonerId.add(s['summonerId'])
- #Put the new summonerIds in the queue
- for i in participantslist:
- await queueSummonerId.put(i['summonerId'])
- #queueAccountSave.put(i)
- async def getSummonerIds(data):
- summoners = []
- #Get all summoners in the league
- for entry in data['entries']:
- summoners.append(entry['playerOrTeamId'])
- #Add all summoners in the list of checked summoners
- with await lockSummonerlist:
- summonerslist = [int(x) for x in summoners if x not in listSummonerId]
- for s in summonerslist:
- listSummonerId.add(s)
- async def getLeagueId(data):
- for league in data:
- if league['queueType'] == "RANKED_SOLO_5x5":
- #Add all summoners in the list of checked summoners
- with await lockLeaguelist:
- if league['leagueId'] not in listLeagueId:
- listLeagueId.add(league['leagueId'])
- await queueLeagueId.put(league['leagueId'])
- break
- async def matchlistCaller():
- while True:
- #Get accountId from queue
- accountId = await queueAccountId.get()
- #Leave if no accountId left
- if accountId is None:
- await queueAccountId.put(None)
- break
- try:
- data = await pantheon.getMatchlist(accountId, {"beginTime":1523743200000,"queue":420})
- await getMatchIds(data)
- except exc.NotFound as e:
- pass
- except (exc.RateLimit, exc.ServerError, exc.Timeout) as e:
- print(e)
- queueAccountId.put(accountId)
- except Exception as e:
- print(e)
- async def matchCaller():
- while True:
- #Get accountId from queue
- matchId = await queueMatchId.get()
- #Leave if no accountId left
- if matchId is None:
- await queueMatchId.put(None)
- break
- try:
- data = await pantheon.getMatch(matchId)
- await getAccountIds(data)
- except exc.NotFound as e:
- pass
- except (exc.RateLimit, exc.ServerError, exc.Timeout) as e:
- print(e)
- queueMatchId.put(matchId)
- except Exception as e:
- print(e)
- async def leagueCaller():
- while True:
- #Get accountId from queue
- leagueId = await queueLeagueId.get()
- #Leave if no accountId left
- if leagueId is None:
- await queueLeagueId.put(None)
- break
- try:
- data = await pantheon.getLeagueById(leagueId)
- await getSummonerIds(data)
- except exc.NotFound as e:
- pass
- except (exc.RateLimit, exc.ServerError, exc.Timeout) as e:
- print(e)
- queueLeagueId.put(leagueId)
- except Exception as e:
- print(e)
- async def leaguePositionCaller():
- global countLeaguePositionCalls
- while True:
- while len(queueLeagueId._queue) > 0:
- await asyncio.sleep(0.05)
- #Get accountId from queue
- summonerId = await queueSummonerId.get()
- #Leave if no accountId left
- if summonerId is None:
- await queueSummonerId.put(None)
- break
- with await lockSummonerlist:
- if summonerId in listSummonerId:
- pass
- try:
- countLeaguePositionCalls+=1
- data = await pantheon.getLeaguePosition(summonerId)
- await getLeagueId(data)
- except exc.NotFound as e:
- pass
- except (exc.RateLimit, exc.ServerError, exc.Timeout) as e:
- print(e)
- queueSummonerId.put(summonerId)
- except Exception as e:
- print(e)
- async def summonerQueueCleaner():
- while True:
- summonerId = await queueSummonerId.get()
- if summonerId is None:
- await queueSummonerId.put(None)
- break
- with await lockSummonerlist:
- if summonerId not in listSummonerId:
- queueSummonerId.put(summonerId)
- async def master():
- while True:
- await asyncio.sleep(15)
- print("Matchs : " + str(len(listMatchId)) + " / Accounts : " + str(len(listAccountId)) + " / Summoners : " + str(len(listSummonerId)) + " / Leagues : " + str(len(listLeagueId)))
- print("Matchs queue : " + str(len(queueMatchId._queue)) + " / Account queue : " + str(len(queueAccountId._queue)) + " / League queue : " + str(len(queueLeagueId._queue)) + " / Summoner queue : " + str(len(queueSummonerId._queue)))
- if len(queueMatchId._queue) == 0 and len(queueAccountId._queue) == 0 and len(queueLeagueId._queue) == 0 and len(queueSummonerId._queue) == 0:
- await asyncio.sleep(15)
- if len(queueMatchId._queue) == 0 and len(queueAccountId._queue) == 0 and len(queueLeagueId._queue) == 0 and len(queueSummonerId._queue) == 0:
- print("end master")
- await queueAccountId.put(None)
- await queueMatchId.put(None)
- await queueLeagueId.put(None)
- await queueSummonerId.put(None)
- return
- #Init loop
- loop = asyncio.get_event_loop()
- asyncio.set_event_loop(loop)
- #Init queues
- queueAccountId = asyncio.Queue(loop=loop, maxsize=0)
- queueMatchId = asyncio.Queue(loop=loop, maxsize=0)
- queueLeagueId = asyncio.Queue(loop=loop, maxsize=0)
- queueSummonerId = asyncio.Queue(loop=loop, maxsize=0)
- lockMatchlist = asyncio.Lock()
- lockAccountlist = asyncio.Lock()
- lockLeaguelist = asyncio.Lock()
- lockSummonerlist = asyncio.Lock()
- for i in [25350231,24372110,39675394,232555858,27970585,31268936]:
- queueAccountId.put_nowait(i)
- tasks = [asyncio.ensure_future(matchlistCaller()) for _ in range(0,100)]
- tasks += [asyncio.ensure_future(matchCaller()) for _ in range(0,50)]
- tasks += [asyncio.ensure_future(master())]
- tasks += [asyncio.ensure_future(leagueCaller()) for _ in range(0,50)]
- tasks += [asyncio.ensure_future(leaguePositionCaller()) for _ in range(0,5)]
- tasks += [asyncio.ensure_future(summonerQueueCleaner()) for _ in range(0,5)]
- loop.run_until_complete(asyncio.gather(*tasks))
- loop.run_until_complete(asyncio.sleep(0))
- loop.call_soon_threadsafe(loop.stop)
- loop.stop()
- print("end")
- print(timeBegin)
- print(time.mktime(datetime.datetime.utcnow().timetuple()) )
- print(timeBegin - time.mktime(datetime.datetime.utcnow().timetuple()) )
- print("Calls to leaguePosition : "+str(countLeaguePositionCalls))
- #print(listMatchId)
- print("Number of leagueId : "+str(len(listLeagueId)))
- leagueIdListForJson = []
- for i in listLeagueId:
- leagueIdListForJson.append(i)
- print(json.dumps(leagueIdListForJson))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement