如果看不懂代码里的部分内容,建议先阅读一下我博客写的LCU通信原理简介:https://alphaft.me/tags/LeagueClient/
原理
在LOL的商城里,好友之间赠送礼物有这么一个条件
超过14天的好友能赠送礼物
要判断是否超过14天,必定在某个地方储存了成为好友的日期。幸运的是,LCU的 REST API中提供了查询方法
GET /lol-store/v1/giftablefriends
返回样例:
[
{
"friendsSince": "2015-06-06 19:51:10",
"nick": "53263468",
"oldFriends": true,
"summonerId": 4012653853
},
{
...
...
},
....
]
数据注释:
key |
value |
friendSince |
成为好友的时间,精确到秒 |
nick |
好友昵称 |
oldFriends |
到目前时间是否够14天 |
summonerId |
summonerId |
有这些就很好搞了,接下来写个DEMO试试
DEMO
代码效果放在哔哩哔哩了
https://www.bilibili.com/video/BV1eK4y147od/
代码如下
因为要获取程序的命令行参数,所以需要管理员权限
import requests
import subprocess
from datetime import datetime
class LCU:
def __init__(self):
'''
init params
'''
self.port = None
self.host = '127.0.0.1'
self.token = None
self.protocol = 'https'
self.url = None
self.pid = None
def getParam(self):
'''
From https://github.com/minhluu2000/Project-Lego and changed a little
Get necessary for client-specific info through CommandLine
'''
raw_output = subprocess.check_output(
['wmic', 'PROCESS', 'WHERE', "name='LeagueClientUx.exe'", 'GET', 'commandline']).decode('gbk')
# Use GBK in case of Chinese character in path
app_port = raw_output.split('--app-port=')[-1].split(' ')[0].strip('\"')
auth_token = raw_output.split('--remoting-auth-token=')[-1].split(' ')[0].strip('\"')
url = self.protocol + '://' + 'riot:' + auth_token + '@' + self.host + ':' + app_port
return app_port, auth_token, url
def init(self):
'''
Init the instance
'''
while True:
if not self.checkProcess():
print('未检测到LeagueClient.exe')
else:
self.pid = self.checkProcess()
self.port, self.token, self.url = self.getParam()
break
def giftablefriends(self):
'''
/lol-store/v1/giftablefriends
:return:
[
{
"friendsSince": "2015-06-06 19:51:10",
"nick": "53263468",
"oldFriends": true,
"summonerId": 4012653853
},
....
]
'''
rest = "/lol-store/v1/giftablefriends"
request = requests.get(self.url + rest, verify=False)
infolist = request.json()
return infolist
def getFriendDate(self, reverse=False):
infolist = self.giftablefriends()
date_now_tuple = datetime.now().timetuple()
date_now = datetime(date_now_tuple.tm_year, date_now_tuple.tm_mon, date_now_tuple.tm_mday)
for infodict in infolist:
date = infodict['friendsSince']
date_old_tuple = datetime.strptime(date, '%Y-%m-%d %H:%M:%S').timetuple()
date_old = datetime(date_old_tuple.tm_year, date_old_tuple.tm_mon, date_old_tuple.tm_mday)
interval = (date_now-date_old).days
infodict['interval'] = interval
#infolist_sorted = sorted(infolist, key=lambda hey:hey['interval']) 排序后新建列表
infolist.sort(key=lambda profile:profile['interval'], reverse=False)
print(infolist)
for infodict in infolist:
date = infodict['friendsSince']
nick = infodict['nick']
interval = infodict['interval']
print(f'你和 {nick} 在 {date} 成为了好友, 距今已经 {interval}天')
lcu = LCU()
lcu.init()
lcu.getFriendDate()