Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.22 KB | None | 0 0
  1. import sys
  2. sys.path.append("/var/pantheon")
  3.  
  4. import sortedcontainers
  5. import asyncio
  6.  
  7. from pantheon import Pantheon
  8. import utils.exceptions as exc
  9.  
  10. import time, datetime, json
  11.  
  12. timeBegin = time.mktime(datetime.datetime.utcnow().timetuple())
  13.  
  14. server = "euw1"
  15. key = "RGAPI-key"
  16.  
  17. pantheon = Pantheon(server, key, True)
  18.  
  19. listMatchId = sortedcontainers.SortedSet()
  20. listAccountId = sortedcontainers.SortedSet()
  21. listSummonerId = sortedcontainers.SortedSet()
  22. listLeagueId = sortedcontainers.SortedSet()
  23.  
  24. countLeaguePositionCalls=0
  25.  
  26. async def getMatchIds(data):
  27. #If at least one match in the list
  28. if data['totalGames'] > 0:
  29. #Get all the matchIds of the list
  30. matchIds = []
  31. for match in data['matches']:
  32. if match['platformId'].lower()==server:
  33. matchIds.append(match['gameId'])
  34.  
  35. #Lock the global matchlist, find the new matchIds and add them to the global list
  36. with await lockMatchlist:
  37. matchlist = [x for x in matchIds if x not in listMatchId]
  38. for m in matchlist:
  39. listMatchId.add(m)
  40.  
  41. #Put the new matchIds in the queue
  42. for i in matchlist:
  43. await queueMatchId.put(i)
  44.  
  45. async def getAccountIds(data):
  46.  
  47. participants = []
  48.  
  49. for p in (data['participantIdentities']):
  50. if p['player']['currentPlatformId'].lower()==server:
  51. if 'summonerId' in p['player']:
  52. participants.append({"accountId":p['player']['currentAccountId'],"summonerId":p['player']['summonerId'],"platformId":p['player']['currentPlatformId']})
  53. else:
  54. participants.append({"accountId":p['player']['currentAccountId'],"summonerId":0,"platformId":p['player']['currentPlatformId']})
  55.  
  56. with await lockAccountlist:
  57. participantslist = [x for x in participants if x['accountId'] not in listAccountId]
  58. for p in participantslist:
  59. listAccountId.add(p['accountId'])
  60.  
  61. #Put the new accountIds in the queue
  62. for i in participantslist:
  63. await queueAccountId.put(i['accountId'])
  64. #queueAccountSave.put(i)
  65.  
  66. with await lockSummonerlist:
  67. participantslist = [x for x in participants if x['summonerId'] not in listSummonerId]
  68. for s in participantslist:
  69. listSummonerId.add(s['summonerId'])
  70.  
  71. #Put the new summonerIds in the queue
  72. for i in participantslist:
  73. await queueSummonerId.put(i['summonerId'])
  74. #queueAccountSave.put(i)
  75.  
  76. async def getSummonerIds(data):
  77. summoners = []
  78.  
  79. #Get all summoners in the league
  80. for entry in data['entries']:
  81. summoners.append(entry['playerOrTeamId'])
  82.  
  83. #Add all summoners in the list of checked summoners
  84. with await lockSummonerlist:
  85. summonerslist = [int(x) for x in summoners if x not in listSummonerId]
  86. for s in summonerslist:
  87. listSummonerId.add(s)
  88.  
  89. async def getLeagueId(data):
  90.  
  91. for league in data:
  92. if league['queueType'] == "RANKED_SOLO_5x5":
  93.  
  94. #Add all summoners in the list of checked summoners
  95. with await lockLeaguelist:
  96. if league['leagueId'] not in listLeagueId:
  97. listLeagueId.add(league['leagueId'])
  98. await queueLeagueId.put(league['leagueId'])
  99. break
  100.  
  101.  
  102. async def matchlistCaller():
  103. while True:
  104. #Get accountId from queue
  105. accountId = await queueAccountId.get()
  106.  
  107. #Leave if no accountId left
  108. if accountId is None:
  109. await queueAccountId.put(None)
  110. break
  111.  
  112. try:
  113. data = await pantheon.getMatchlist(accountId, {"beginTime":1523743200000,"queue":420})
  114. await getMatchIds(data)
  115. except exc.NotFound as e:
  116. pass
  117. except (exc.RateLimit, exc.ServerError, exc.Timeout) as e:
  118. print(e)
  119. queueAccountId.put(accountId)
  120. except Exception as e:
  121. print(e)
  122.  
  123. async def matchCaller():
  124. while True:
  125.  
  126. #Get accountId from queue
  127. matchId = await queueMatchId.get()
  128.  
  129. #Leave if no accountId left
  130. if matchId is None:
  131. await queueMatchId.put(None)
  132. break
  133.  
  134. try:
  135. data = await pantheon.getMatch(matchId)
  136. await getAccountIds(data)
  137. except exc.NotFound as e:
  138. pass
  139. except (exc.RateLimit, exc.ServerError, exc.Timeout) as e:
  140. print(e)
  141. queueMatchId.put(matchId)
  142. except Exception as e:
  143. print(e)
  144.  
  145.  
  146. async def leagueCaller():
  147. while True:
  148.  
  149. #Get accountId from queue
  150. leagueId = await queueLeagueId.get()
  151.  
  152. #Leave if no accountId left
  153. if leagueId is None:
  154. await queueLeagueId.put(None)
  155. break
  156.  
  157. try:
  158. data = await pantheon.getLeagueById(leagueId)
  159. await getSummonerIds(data)
  160. except exc.NotFound as e:
  161. pass
  162. except (exc.RateLimit, exc.ServerError, exc.Timeout) as e:
  163. print(e)
  164. queueLeagueId.put(leagueId)
  165. except Exception as e:
  166. print(e)
  167.  
  168.  
  169. async def leaguePositionCaller():
  170. global countLeaguePositionCalls
  171. while True:
  172.  
  173. while len(queueLeagueId._queue) > 0:
  174. await asyncio.sleep(0.05)
  175.  
  176. #Get accountId from queue
  177. summonerId = await queueSummonerId.get()
  178.  
  179. #Leave if no accountId left
  180. if summonerId is None:
  181. await queueSummonerId.put(None)
  182. break
  183.  
  184. with await lockSummonerlist:
  185. if summonerId in listSummonerId:
  186. pass
  187.  
  188. try:
  189. countLeaguePositionCalls+=1
  190. data = await pantheon.getLeaguePosition(summonerId)
  191. await getLeagueId(data)
  192. except exc.NotFound as e:
  193. pass
  194. except (exc.RateLimit, exc.ServerError, exc.Timeout) as e:
  195. print(e)
  196. queueSummonerId.put(summonerId)
  197. except Exception as e:
  198. print(e)
  199.  
  200. async def summonerQueueCleaner():
  201. while True:
  202. summonerId = await queueSummonerId.get()
  203.  
  204. if summonerId is None:
  205. await queueSummonerId.put(None)
  206. break
  207.  
  208. with await lockSummonerlist:
  209. if summonerId not in listSummonerId:
  210. queueSummonerId.put(summonerId)
  211.  
  212. async def master():
  213. while True:
  214. await asyncio.sleep(15)
  215. print("Matchs : " + str(len(listMatchId)) + " / Accounts : " + str(len(listAccountId)) + " / Summoners : " + str(len(listSummonerId)) + " / Leagues : " + str(len(listLeagueId)))
  216. 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)))
  217.  
  218.  
  219. if len(queueMatchId._queue) == 0 and len(queueAccountId._queue) == 0 and len(queueLeagueId._queue) == 0 and len(queueSummonerId._queue) == 0:
  220. await asyncio.sleep(15)
  221. if len(queueMatchId._queue) == 0 and len(queueAccountId._queue) == 0 and len(queueLeagueId._queue) == 0 and len(queueSummonerId._queue) == 0:
  222. print("end master")
  223. await queueAccountId.put(None)
  224. await queueMatchId.put(None)
  225. await queueLeagueId.put(None)
  226. await queueSummonerId.put(None)
  227. return
  228.  
  229. #Init loop
  230. loop = asyncio.get_event_loop()
  231. asyncio.set_event_loop(loop)
  232.  
  233. #Init queues
  234. queueAccountId = asyncio.Queue(loop=loop, maxsize=0)
  235. queueMatchId = asyncio.Queue(loop=loop, maxsize=0)
  236. queueLeagueId = asyncio.Queue(loop=loop, maxsize=0)
  237. queueSummonerId = asyncio.Queue(loop=loop, maxsize=0)
  238.  
  239. lockMatchlist = asyncio.Lock()
  240. lockAccountlist = asyncio.Lock()
  241. lockLeaguelist = asyncio.Lock()
  242. lockSummonerlist = asyncio.Lock()
  243.  
  244. for i in [25350231,24372110,39675394,232555858,27970585,31268936]:
  245. queueAccountId.put_nowait(i)
  246.  
  247. tasks = [asyncio.ensure_future(matchlistCaller()) for _ in range(0,100)]
  248. tasks += [asyncio.ensure_future(matchCaller()) for _ in range(0,50)]
  249. tasks += [asyncio.ensure_future(master())]
  250. tasks += [asyncio.ensure_future(leagueCaller()) for _ in range(0,50)]
  251. tasks += [asyncio.ensure_future(leaguePositionCaller()) for _ in range(0,5)]
  252. tasks += [asyncio.ensure_future(summonerQueueCleaner()) for _ in range(0,5)]
  253. loop.run_until_complete(asyncio.gather(*tasks))
  254. loop.run_until_complete(asyncio.sleep(0))
  255. loop.call_soon_threadsafe(loop.stop)
  256. loop.stop()
  257.  
  258. print("end")
  259. print(timeBegin)
  260. print(time.mktime(datetime.datetime.utcnow().timetuple()) )
  261. print(timeBegin - time.mktime(datetime.datetime.utcnow().timetuple()) )
  262. print("Calls to leaguePosition : "+str(countLeaguePositionCalls))
  263. #print(listMatchId)
  264. print("Number of leagueId : "+str(len(listLeagueId)))
  265. leagueIdListForJson = []
  266. for i in listLeagueId:
  267. leagueIdListForJson.append(i)
  268. print(json.dumps(leagueIdListForJson))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement