LeagueClient通信(1)-你和好友们认识了多久?

原理

在 LOL 的商城里,好友之间赠送礼物有这么一个条件

超过 14 天的好友能赠送礼物

要判断是否超过 14 天,必定在某个地方储存了成为好友的日期。幸运的是,LCU 的 REST API 中提供了查询方法

GET /lol-store/v1/giftablefriends

返回样例:

1
2
3
4
5
6
7
8
9
10
11
12
13
[
{
"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/

代码如下
因为要获取程序的命令行参数,所以需要管理员权限

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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()
Author

BakaFT

Posted on

2020-07-02

Updated on

2023-12-28

Licensed under

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×