Skip to content

Instantly share code, notes, and snippets.

@dorukcan
Last active April 22, 2016 12:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dorukcan/8b5babe5cfc80ed2ff4b0d302e700946 to your computer and use it in GitHub Desktop.
Save dorukcan/8b5babe5cfc80ed2ff4b0d302e700946 to your computer and use it in GitHub Desktop.
import urllib2, json, sqlite3
from peewee import *
def getSummonerId(name, api_key):
url = "https://euw.api.pvp.net/api/lol/euw/v1.4/summoner/by-name/" + name + "?api_key=" + api_key
data = urllib2.urlopen(url).read()
jsonData = json.loads(data)
return jsonData[name.lower()]["id"]
def getMatches(s_id, region, api_key):
url = "https://euw.api.pvp.net/api/lol/" + region + "/v2.2/matchlist/by-summoner/" + str(s_id) + "?api_key=" + api_key
data = urllib2.urlopen(url).read()
jsonData = json.loads(data)
result = []
for match in jsonData["matches"]:
result.append(match["matchId"])
return result
def getParticipants(s_id, m_id, region, api_key):
url = "https://euw.api.pvp.net/api/lol/" + region + "/v2.2/match/" + str(m_id) + "?api_key=" + api_key
data = urllib2.urlopen(url).read()
jsonData = json.loads(data)
result = []
for participant in jsonData["participantIdentities"]:
if participant["player"]["summonerId"] != s_id:
result.append([ jsonData["matchId"],
jsonData["matchCreation"],
(0, 1)[participant["participantId"] > 5],
participant["player"]["summonerName"],
participant["player"]["summonerId"] ])
return result
def getChampionId(name):
url = "https://ddragon.leagueoflegends.com/cdn/6.7.1/data/en_US/champion/" + name.title() + ".json"
data = urllib2.urlopen(url).read()
jsonData = json.loads(data)
return jsonData["data"][name.title()]["key"]
def saveParticipants(name, region, participantList):
conn = sqlite3.connect('lol.db')
tableName = name + "_" + region
conn.execute("CREATE TABLE IF NOT EXISTS `" + tableName + "` (`id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, `summonerId` TEXT NOT NULL, `summonerName` TEXT NOT NULL, `team` INTEGER NOT NULL, `matchTime` TEXT NOT NULL, `matchId` TEXT NOT NULL);")
for row in participantList:
conn.execute("INSERT INTO `" + tableName + "` (`summonerId`, `summonerName`, `team`, `matchTime`, `matchId`) VALUES ('" + str(row[4]) + "', '" + str(row[3].encode("utf-8")) + "', " + str(row[2]) + ", '" + str(row[1]) + "', '" + str(row[0]) + "')")
conn.commit()
conn.close()
def getStatistics(name, region):
conn = sqlite3.connect('lol.db')
tableName = name + "_" + region
result = []
cursor = conn.execute("SELECT `summonerName` FROM `" + tableName + "`")
for row in cursor:
result.append(row[0])
conn.close()
return dict((i, result.count(i)) for i in result)
def main():
API_KEY = ""
summonerName = "TrillianPrak"
region = "EUW".lower()
print summonerName + "_" + region + " is starting"
summonerId = getSummonerId(summonerName, API_KEY)
print "ID of " + summonerName + " is " + str(summonerId)
matchList = getMatches(summonerId, region, API_KEY)
print "matchList is done"
for matchId in matchList:
participantsData = getParticipants(summonerId, matchId, region, API_KEY)
print "getParticipants is done for matchId=" + str(matchId)
saveParticipants(summonerName, region, participantsData)
print "saveParticipants is done for matchId=" + str(matchId)
print ("\n")
print getStatistics(summonerName, region)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment