├── .gitignore ├── riot.txt ├── README.md └── RitoMongo.py /.gitignore: -------------------------------------------------------------------------------- 1 | RitoMongo.conf -------------------------------------------------------------------------------- /riot.txt: -------------------------------------------------------------------------------- 1 | c77b0a9a-820d-4949-916b-af4d506ec49a -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | This a python script that gets you started with Riot league of legends API to interface with MongoDB 2 | This script requires that you have a mongodb instance running at your local host (127.0.0.1) 3 | The script will take your Summoner Name, Region and your API key. 4 | The script will help you get started by creating a database "RitoMongoDB" and collection "SummonerID", 5 | which will have your current SummonerID parsed in a manner of 6 | { 7 | "_id" : 24754800, 8 | "profileIconId" : 529, 9 | "name" : "GedyHD", 10 | "summonerLevel" : 30, 11 | "revisionDate" : NumberLong(1436402086000) 12 | } 13 | This parsing was edited from the original JSON response replied from the riot API, I believe this could be easier to get you started 14 | -------------------------------------------------------------------------------- /RitoMongo.py: -------------------------------------------------------------------------------- 1 | """ 2 | Created on Tue Jun 30 01:09:30 2015 3 | 4 | @author: GedyHD 5 | """ 6 | import json; 7 | import urllib2 8 | import os.path 9 | import re; 10 | from pymongo import MongoClient; 11 | 12 | #================================= end of includes ================================================================== 13 | 14 | def getJSONReply(URL): 15 | response = urllib2.urlopen(URL); 16 | html = response.read(); 17 | data = json.loads(html); 18 | return data; 19 | 20 | 21 | def getUserInput(): 22 | FileExists=os.path.isfile('RitoMongo.conf') ; 23 | res=[]; 24 | if (FileExists): 25 | with open('RitoMongo.conf') as f: 26 | for line in f: 27 | res.append(line.rstrip('\n')); 28 | print line.rstrip('\n'); 29 | elif (not FileExists): 30 | SummonerName= raw_input('Enter your Summoner name: '); 31 | Region = (raw_input('Enter your region: ')).upper(); 32 | Key = raw_input('Enter your API Key which you retrieved from Riot website: '); 33 | f = open('RitoMongo.conf','w'); 34 | f.write(SummonerName+'\n'+Region+'\n'+Key); 35 | f.close(); 36 | return res; 37 | 38 | 39 | def getMatchHistory(SummonerID): 40 | 41 | mhURL="https://eune.api.pvp.net/api/lol/" + Region.lower() + "/v2.2/matchhistory/" + `SummonerID`+ "?api_key=" + Key; 42 | print mhURL; 43 | mh_data=getJSONReply(mhURL); 44 | print mh_data; 45 | 46 | return mh_data; 47 | 48 | def getRecentHistory(SummonerID): 49 | rURL="https://eune.api.pvp.net/api/lol/" + Region.lower()+ "/v1.3/game/by-summoner/" + `SummonerID`+ "/recent?api_key=" + Key; 50 | print rURL; 51 | r_data=getJSONReply(rURL); 52 | print r_data; 53 | r_data['_id']=r_data['summonerId']; 54 | r_data.pop('summonerId'); 55 | return r_data; 56 | def ReformatJSON(SummonerName,Region,Key): 57 | idURL = "https://na.api.pvp.net/api/lol/" + Region+ "/v1.4/summoner/by-name/" + SummonerName+ "?api_key=" + Key; 58 | id_data = getJSONReply(idURL); 59 | idRes=id_data[SummonerName.lower()]; 60 | idRes['_id'] = idRes['id']; 61 | idRes.pop('id'); 62 | print idRes; 63 | return idRes,id_data; 64 | 65 | #================================= Main ============================================================================= 66 | _InputFields= getUserInput(); 67 | SummonerName=_InputFields[0]; 68 | Region=_InputFields[1]; 69 | Key=_InputFields[2]; 70 | print "----------------------------------------------------------------------"; 71 | print "If you've inputted wrong data, please delete or modify RitoMongo.conf" ; 72 | print "----------------------------------------------------------------------"; 73 | # Retrieving the SummonerID; 74 | idURL,id_data=ReformatJSON(SummonerName,Region,Key); 75 | SummonerID = id_data[SummonerName.lower()]["_id"]; 76 | 77 | mh_data=getMatchHistory(SummonerID); 78 | rdata=getRecentHistory(SummonerID); 79 | # Connecting to mongoDB database 80 | client = MongoClient(); 81 | db = client['RitoMongoDB']; 82 | SummonerID_Collection = db["SummonerID"]; 83 | MatchHistory_Collection = db["MatchHistory"]; 84 | RecentHistory_Collection = db["RecentHistory"]; 85 | entryID = SummonerID_Collection.insert_one(idURL).inserted_id; 86 | entryID = MatchHistory_Collection.insert_one(mh_data).inserted_id; 87 | entryID = RecentHistory_Collection.insert_one(rdata).inserted_id; 88 | client.close(); --------------------------------------------------------------------------------