├── LOLHelper.py └── README.md /LOLHelper.py: -------------------------------------------------------------------------------- 1 | import psutil 2 | import base64 3 | import requests 4 | 5 | 6 | class LoLHelper: 7 | port = str 8 | token = str 9 | author = dict 10 | 11 | def __init__(self, port: str, token: str): 12 | self.port = port 13 | self.token = token 14 | # 添加身份验证 15 | tokens = base64.b64encode(("riot:%s" % token).encode()) 16 | self.author = { "Authorization":"Basic %s" % tokens.decode() } 17 | 18 | def get(self, api: str): 19 | result = requests.get( 20 | url = ("https://127.0.0.1:%s%s" % (self.port, correct(api))), 21 | headers = self.author, 22 | verify = False 23 | ) 24 | if result.status_code == 200: 25 | try: 26 | result.encoding = "utf-8" 27 | return result.text 28 | finally: 29 | result.close() 30 | else: 31 | return "" 32 | 33 | def post(self, api: str, data: str): 34 | result = requests.post( 35 | url = "https://127.0.0.1:%s%s" % (self.port, correct(api)), 36 | headers = self.author, 37 | verify = False, 38 | json = data 39 | ) 40 | if result.status_code == 200: 41 | try: 42 | result.encoding = "utf-8" 43 | return result.text 44 | finally: 45 | result.close() 46 | else: 47 | return "" 48 | 49 | def patch(self, api: str, data: str): 50 | result = requests.patch( 51 | url = "https://127.0.0.1:%s%s" % (self.port, correct(api)), 52 | headers = self.author, 53 | verify = False, 54 | json = data 55 | ) 56 | if result.status_code == 200: 57 | try: 58 | result.encoding = "utf-8" 59 | return result.text 60 | finally: 61 | result.close() 62 | else: 63 | return "" 64 | 65 | def put(self, api: str, data: str): 66 | result = requests.put( 67 | url = "https://127.0.0.1:%s%s" % (self.port, correct(api)), 68 | headers = self.author, 69 | verify = False, 70 | json = data 71 | ) 72 | if result.status_code == 200: 73 | try: 74 | result.encoding = "utf-8" 75 | return result.text 76 | finally: 77 | result.close() 78 | else: 79 | return "" 80 | 81 | def delete(self, api: str): 82 | return requests.delete( 83 | url = "https://127.0.0.1:%s%s" % (self.port, correct(api)), 84 | headers = self.author, 85 | verify = False 86 | ).status_code 87 | 88 | 89 | def correct(api:str): 90 | if api[0] != "/": 91 | return "/%s" % api 92 | return api 93 | 94 | 95 | def checkProcessAlive(processName: str): 96 | for x in psutil.process_iter(): 97 | if x.name().removesuffix(".exe") == processName: 98 | return True 99 | return False 100 | 101 | 102 | def init(): 103 | port = str 104 | token = str 105 | cmds = list[str] 106 | for process in psutil.process_iter(): 107 | if process.name().removesuffix(".exe") == "LeagueClientUx": 108 | cmds = process.cmdline() 109 | break 110 | for cmd in cmds: 111 | ary = cmd.split("=") 112 | if ary[0] == "--remoting-auth-token": 113 | token = ary[1] 114 | if ary[0] == "--app-port": 115 | port = ary[1] 116 | return LoLHelper(port, token) 117 | 118 | 119 | if checkProcessAlive("LeagueClient"): 120 | # 初始化LOLHelper类 121 | lHelper = init() 122 | # 获取当前召唤师信息 123 | print(lHelper.get("lol-summoner/v1/current-summoner")) 124 | # 获取当前召唤师生涯信息 125 | # print(lHelper.get("lol-summoner/v1/current-summoner/summoner-profile")) 126 | # 设置生涯背景图片 127 | # lHelper.post("lol-summoner/v1/current-summoner/summoner-profile",{"key":"backgroundSkinId","value":"1000"}) 128 | else: 129 | print("未检测到游戏进程,请先运行游戏") 130 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # LOLHelper 2 | League Of Legends LCU(LegueClientUx) API usage example. 3 | 英雄联盟客户端API调用示例,调用API时需要先运行游戏并保证RiotClientServices通讯正常。 4 | 你可以通过以下方式确定: 5 | ```python 6 | # lHelper = LoLHelper(port,token); 7 | lHelper.get("riot-messaging-service/v1/state") # 如果返回 "Connected" 则说明 RiotClientServices 通讯正常。 8 | ``` 9 | 此外,还需要确定账号的登录状态: 10 | ```python 11 | # lHelper = LoLHelper(port,token); 12 | lHelper.get("lol-summoner/v1/status") # 如果返回 { "ready":true } 则说明登录成功;否则可能登录失败或正在服务器排队队列中。 13 | ``` 14 | 在此之后你可以正常调用LCU API(当然上述情况是先运行程序后运行游戏的情况下要做的,如果是后运行程序你完全可以爱搞不搞),另外你可以自己扩展一些API调用实现的功能。 15 | 16 | 你还可以在以下文档中了解到更多的LCU API: 17 | [LCU API Document](https://lcu.vivide.re/) 18 | Riot官方的开发者(LOL)文档: 19 | [Riot Developer Document](https://developer.riotgames.com/docs/lol) 20 | 21 | 你可以在官方开发者文档里的Data Dragon(数据龙)中找到游戏中基本能看到的所有资源,例如召唤师技能,装备和英雄的图片资源等,Data Dragon会随着游戏版本的更新而更新(也可能提前或延后),Data Dragon是静态的数据,获取Data Dragon数据需要指定游戏版本,你可以通过 (https://ddragon.leagueoflegends.com/api/versions.json) 中第[1]个项、下标[0]来获取当前游戏的的版本。 22 | 23 | 还有一件事:用到的第三方库需要自己去下载:requests 和 pustil,嗯、当然你也可以选择其他的。 24 | --------------------------------------------------------------------------------