-
Notifications
You must be signed in to change notification settings - Fork 2
/
LuatableBot.py
213 lines (187 loc) · 8.44 KB
/
LuatableBot.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/env python3
import asyncio
import os
import shutil
import subprocess
import time
import traceback
from os import environ, path
from config import (AIRPOWER_TABLE, AKASHI_LIST_OUTPUT_LUA, BONUS_JS, DB_PATH,
ENTITIES_DB, ITEM_TYPES_DB, ITEMS_DATA, ITEMS_DATA_V2, ITEMS_DB, SHIP_REMODEL_EXTRA,
JSON_PATH, KCDATA_SHIP_ALL_JSON, KCDATA_SLOTITEM_ALL_JSON,
KCKIT_NAME, LUATABLE_PATH, OUPUT_PATH, SCRIPTS_PATH,
SEASONAL_PATH, SHINKAI_ITEMS_DATA, SHINKAI_SHIPS_DATA,
SHIP_CLASSES_DB, SHIP_NAMESUFFIX_DB, SHIP_SERIES_DB,
SHIP_TYPE_COLLECTIONS_DB, SHIP_TYPES_DB,
SHIPCLASSES_MAPPING_DATA, SHIPS_DATA, SHIPS_DB,
WCTF_DB_NAME)
from crawlers import (AkashiListCrawler, SeasonalCrawler,
WikiwikiCrawler)
from DBDownloader import DBDownloader
from ShinkaiLuatable import ShinkaiLuatable
from ShipClassMappingLuatable import ShipClassMappingLuatable
from ShipLuatable import ShipLuatable
from utils import nedb2json
from WikiBot import WikiBot
def LuatableBotTask(skip_fails = False):
def inner(fn):
async def wrapper(*args, **kw):
print('[{}]: Task starting...'.format(fn.__name__))
START = time.time()
try:
await fn(*args, **kw)
except Exception as e:
if not skip_fails:
raise e
else:
print('[{}]: Task skip'.format(fn.__name__))
traceback.print_exc()
END = time.time()
print('[{}]: Task total used {}s'.format(fn.__name__, round(END - START, 3)))
return wrapper
return inner
def Switch(name):
def inner(fn):
async def wrapper(*args, **kw):
if environ.get(name):
await fn(*args, **kw)
return wrapper
return inner
class LuatableBotException(Exception):
def __init__(self, message):
super().__init__(message)
def exec(args):
print('exec: ' + ' '.join(args))
try:
subprocess.run(args)
except Exception as e:
raise LuatableBotException(e)
class LuatableBot:
def __init__(self):
self.task_status = {}
if not path.isdir(DB_PATH):
os.mkdir(DB_PATH)
if not path.isdir(OUPUT_PATH):
os.mkdir(OUPUT_PATH)
if not path.isdir(OUPUT_PATH + LUATABLE_PATH):
os.mkdir(OUPUT_PATH + LUATABLE_PATH)
if not path.isdir(OUPUT_PATH + JSON_PATH):
os.mkdir(OUPUT_PATH + JSON_PATH)
if not path.isdir(OUPUT_PATH + SEASONAL_PATH):
os.mkdir(OUPUT_PATH + SEASONAL_PATH)
@LuatableBotTask()
async def ClonseDBS(self):
shutil.rmtree(KCKIT_NAME + '/', ignore_errors=True)
shutil.rmtree(WCTF_DB_NAME + '/', ignore_errors=True)
KCKIT_REPO_URL = environ.get('KCKIT_REPO_URL')
KCKIT_REPO_BRANCH = environ.get('KCKIT_REPO_BRANCH')
WCTF_DB_REPO_URL = environ.get('WCTF_DB_REPO_URL')
WCTF_DB_REPO_BRANCH = environ.get('WCTF_DB_REPO_BRANCH')
if not KCKIT_REPO_URL:
raise LuatableBotException('KCKIT_REPO_URL 未设置,请设置 KCKIT 的仓库地址,例如 https://github.com/TeamFleet/KCKit.git')
if not WCTF_DB_REPO_URL:
raise LuatableBotException('WCTF_DB_REPO_URL 未设置,请设置 WhoCallsTheFleet-DB 的仓库地址,例如 https://github.com/TeamFleet/WhoCallsTheFleet-DB.git')
self.__git_clone(KCKIT_REPO_URL, KCKIT_NAME, KCKIT_REPO_BRANCH)
self.__git_clone(WCTF_DB_REPO_URL, WCTF_DB_NAME, WCTF_DB_REPO_BRANCH)
def __git_clone(self, url, dest, branch = 'master'):
exec(['git', 'clone', '--depth=1', url, '-b', branch, dest])
@LuatableBotTask()
async def FetchDBS(self):
task_name = 'FetchDBS'
if task_name not in self.task_status:
dbDownloader = DBDownloader()
dbDownloader.appendTask('https://kcwikizh.github.io/kcdata/ship/all.json', DB_PATH + KCDATA_SHIP_ALL_JSON)
dbDownloader.appendTask('https://kcwikizh.github.io/kcdata/slotitem/all.json', DB_PATH + KCDATA_SLOTITEM_ALL_JSON)
dbDownloader.appendTask('https://raw.githubusercontent.com/kcwikizh/get_kaisou_data/master/kaisou_data.json', DB_PATH + SHIP_REMODEL_EXTRA)
await dbDownloader.start()
self.task_status[task_name] = True
@LuatableBotTask()
async def Nedb2json(self):
# 复制数据文件
nedbs = os.listdir('{}/db'.format(WCTF_DB_NAME))
for dbfile in nedbs:
shutil.copyfile('{}/db/{}'.format(WCTF_DB_NAME, dbfile), 'db/{}'.format(dbfile))
nedb2json(DB_PATH + ENTITIES_DB + '.nedb', DB_PATH + ENTITIES_DB + '.json')
nedb2json(DB_PATH + ITEM_TYPES_DB + '.nedb', DB_PATH + ITEM_TYPES_DB + '.json')
nedb2json(DB_PATH + ITEMS_DB + '.nedb', DB_PATH + ITEMS_DB + '.json')
nedb2json(DB_PATH + SHIP_CLASSES_DB + '.nedb', DB_PATH + SHIP_CLASSES_DB + '.json')
nedb2json(DB_PATH + SHIP_NAMESUFFIX_DB + '.nedb', DB_PATH + SHIP_NAMESUFFIX_DB + '.json')
nedb2json(DB_PATH + SHIP_SERIES_DB + '.nedb', DB_PATH + SHIP_SERIES_DB + '.json')
nedb2json(DB_PATH + SHIP_TYPES_DB + '.nedb', DB_PATH + SHIP_TYPES_DB + '.json')
nedb2json(DB_PATH + SHIP_TYPE_COLLECTIONS_DB + '.nedb', DB_PATH + SHIP_TYPE_COLLECTIONS_DB + '.json')
nedb2json(DB_PATH + SHIPS_DB + '.nedb', DB_PATH + SHIPS_DB + '.json')
@LuatableBotTask()
async def AkashiList(self):
task_name = 'AkashiList'
if task_name not in self.task_status:
akashiListCrawler = AkashiListCrawler()
await akashiListCrawler.start()
self.task_status[task_name] = True
@Switch('Wikiwiki')
@LuatableBotTask()
async def WikiwikiData(self):
wikiwikiCrawler = WikiwikiCrawler()
await wikiwikiCrawler.start()
@Switch('SeasonalSubtitles')
@LuatableBotTask()
async def SeasonalSubtitles(self):
seasonalCrawler = SeasonalCrawler()
await seasonalCrawler.start()
@Switch("ShipClassesMapping")
@LuatableBotTask()
async def ShipClassesMappingLuatable(self):
shipClassesMappingLuatable = ShipClassMappingLuatable()
shipClassesMappingLuatable.start()
@Switch('Ships')
@LuatableBotTask(True)
async def ShipLuatable(self):
await self.BonusJson()
await self.AkashiList()
shipLuatable = ShipLuatable()
shipLuatable.start()
@Switch('Shinkai')
@LuatableBotTask(True)
async def ShinkaiLuatable(self):
shinkaiLuatable = ShinkaiLuatable()
await shinkaiLuatable.start()
@Switch('KcwikiUpdate')
@LuatableBotTask()
async def WikiBotUpdate(self):
KCWIKI_ACCOUNT = environ.get('KCWIKI_ACCOUNT')
KCWIKI_PASSWORD = environ.get('KCWIKI_PASSWORD')
wikiBot = WikiBot(KCWIKI_ACCOUNT, KCWIKI_PASSWORD)
await wikiBot.start()
async def BonusJson(self):
self.__exec_js(SCRIPTS_PATH + BONUS_JS)
def __exec_lua(self, filename):
exec(['lua', filename])
def __exec_js(self, filename):
exec(['node', filename])
@Switch('Check')
@LuatableBotTask(True)
async def CheckLuatable(self):
self.__exec_lua(OUPUT_PATH + LUATABLE_PATH + SHIPS_DATA + '.lua')
self.__exec_lua(OUPUT_PATH + LUATABLE_PATH + ITEMS_DATA + '.lua')
self.__exec_lua(OUPUT_PATH + LUATABLE_PATH + ITEMS_DATA_V2 + '.lua')
self.__exec_lua(OUPUT_PATH + LUATABLE_PATH + SHINKAI_ITEMS_DATA + '.lua')
self.__exec_lua(OUPUT_PATH + LUATABLE_PATH + SHINKAI_SHIPS_DATA + '.lua')
self.__exec_lua(OUPUT_PATH + LUATABLE_PATH + SHIPCLASSES_MAPPING_DATA + '.lua')
self.__exec_lua(OUPUT_PATH + LUATABLE_PATH + AKASHI_LIST_OUTPUT_LUA)
self.__exec_lua(OUPUT_PATH + LUATABLE_PATH + AIRPOWER_TABLE)
self.__exec_lua(OUPUT_PATH + LUATABLE_PATH + SHIP_SERIES_DB + '.lua')
print('CheckLuatable: All the lua files is valid!')
async def main(self):
await self.FetchDBS()
await self.ClonseDBS()
await self.Nedb2json()
await self.SeasonalSubtitles()
await self.WikiwikiData()
await self.ShipLuatable()
await self.ShipClassesMappingLuatable()
await self.ShinkaiLuatable()
await self.CheckLuatable()
await self.WikiBotUpdate()
if __name__ == '__main__':
luatableBot = LuatableBot()
LOOP = asyncio.get_event_loop()
LOOP.run_until_complete(luatableBot.main())