-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathnvkdeamon.py
182 lines (161 loc) · 5.41 KB
/
nvkdeamon.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
#! python3
# -*- coding: utf-8 -*-
'''
自动监测生肉文件夹变化,如果有新文件进入自动启动压制
'''
import sys
def show_exception_and_exit(exc_type, exc_value, tb):
import traceback
traceback.print_exception(exc_type, exc_value, tb)
input('''脚本遇到错误,请截图此画面发送给Kilo19。5秒后继续
Error encountered, please send a screenshot of this error to Kilo19
Press Enter to Exit
''')
import time
time.sleep(5)
sys.excepthook = show_exception_and_exit
import subprocess
import codecs
import time
import sys
import os
import nvksupport
from settings import cloudRoot, cloudDir, customDownDir
scriptDir = os.path.dirname(os.path.realpath(sys.argv[0]))
def GrabArgs(lines, argLen):
returnVal = [''] * argLen
for x in range(len(returnVal)):
if len(lines) > x:
returnVal[x] = lines[x].strip()
return returnVal
def ListIngredients(dirPath, name, ext):
myIngredients = set()
if os.path.isdir(dirPath):
for root, dirs, files in os.walk(dirPath):
if root == dirPath:
for file in files:
if not name in file and file[-3:] == ext:
fileStr = os.path.join(root, file) + '\n'
myIngredients.add(fileStr)
return myIngredients
def ListFile(dirPath, name, ext):
candidates = set()
if os.path.isdir(dirPath):
for root, dirs, files in os.walk(dirPath):
for file in files:
if file[-3:] == ext and name in file:
fileStr = os.path.join(root, file) + '\n'
candidates.add(fileStr)
return candidates
def SafeReadandWriteHead(inName, fileHead = None):
lines = []
if not os.path.isfile(inName):
codecs.open(inName, 'w', 'utf-8').close()
else:
inf = codecs.open(inName, 'r', 'utf-8')
lines = inf.readlines()
inf.close()
lines = [line.strip() + '\n' for line in lines]
if fileHead:
fileHead = [l.strip() + '\n' for l in fileHead]
if len(lines) < len(fileHead) or lines[:len(fileHead)] != fileHead:
print("writehead: " + time.ctime())
inf = codecs.open(inName, 'w', 'utf-8')
inf.writelines(fileHead)
inf.close()
x = 0
while x < len(lines):
if lines[x][0] == '#':
lines.pop(x)
else:
x += 1
return lines
def DeamonHelper(inName, argLen, scriptName, fileHead):
inName = os.path.join(customDownDir, inName + '.txt')
lines = SafeReadandWriteHead(inName, fileHead)
if len(lines):
inf = codecs.open(inName, 'w+', 'utf-8')
if fileHead:
fileHead = [l.strip() + '\n' for l in fileHead]
inf.writelines(fileHead)
else:
inf.write('')
inf.close()
while lines:
args = GrabArgs(lines, argLen)
lines = lines[len(args):]
print(inName + ' command: ' + time.ctime())
for arg in args:
print(arg)
# For safety
time.sleep(0.5)
subprocess.Popen(
[
'py', '-3', os.path.join(scriptDir, scriptName + '.py')
] + args,
creationflags = subprocess.CREATE_NEW_CONSOLE
)
if __name__ == "__main__":
print("NVK Deamon online: " + time.ctime())
ingHead = ['#网盘高压下令文件',
'#不要删除开头带#的行,这些都是注释',
'#不要使用OC网页版编辑此文件',
'#此文件为UTF-8编码,用来下令压生肉',
'#下令前请先用everything搜索(任务栏那个放大镜)',
'#优先下载1080p',
'#搜索时按Alt+3然后Alt+4可以缩放everything窗口',
'#生肉速度大约视频1/2时长',
'#同步到网盘速度不等']
beHead = ['#压熟肉下令文件,带#的行都是注释',
'#请在BUG检测完成后再压制',
'#不要使用OC网页版编辑此文件',
'#教程https://docs.google.com/document/d/1CdyqEP7eanL6J6xUpbcOuq-uIBQkSEqhhHgeGFT94ik/edit',
'#此文件为UTF-8编码,用来下令压熟肉',
'#熟肉速度大约视频0.8-0.9倍',
'#请同时提供生肉路径与字幕所在网盘文件夹,然后保存',
'#同步到网盘速度不等']
cookedHead = ['#此文件专门记录熟肉路径,自动更新',
'#请优先从已有熟肉中选择上传',
'#上传完成后尽快删除']
while True:
DeamonHelper("ing", 1, "NixieCloud_Enc", ingHead)
DeamonHelper("BE", 3, "Bili_Enc", beHead)
ingredientsListPath = os.path.join(
scriptDir, 'ingredientsList' + '.txt'
)
ingredients = ListIngredients(customDownDir, '[BE_', 'mp4')
cookedPath = os.path.join(customDownDir, 'cookedList' + '.txt')
cooked = ListFile(os.path.join(cloudRoot, cloudDir), '[BE_', 'mp4')
oldIngList = set(SafeReadandWriteHead(ingredientsListPath, None))
oldcookedList = set(SafeReadandWriteHead(cookedPath, None))
if ingredients != oldIngList:
#set difference finds new ingredients to transcode
newIngs = list(ingredients - oldIngList)
print('Auto NCE: ' + time.ctime())
for newIng in newIngs:
print(newIng)
newIngStripped = newIng.strip()
newIngStripped = nvksupport.ReplaceNaughtyCharacters(
newIngStripped, nvksupport.tl
)
subprocess.Popen(
[
'py', '-3',
os.path.join(scriptDir, "NixieCloud_Enc" + '.py'),
newIngStripped
],
creationflags=subprocess.CREATE_NEW_CONSOLE
)
ingredients = ListIngredients(customDownDir, '[BE_', 'mp4')
print("writeIng: " + time.ctime())
outFile = codecs.open(ingredientsListPath, 'w', 'utf-8')
outFile.writelines(list(ingredients))
outFile.close()
if cooked != oldcookedList:
print("writeCooked: " + time.ctime())
codecs.open(cookedPath, 'w', 'utf-8').close()
SafeReadandWriteHead(cookedPath, cookedHead)
outFile = codecs.open(cookedPath, 'a+', 'utf-8')
outFile.writelines(list(cooked))
outFile.close()
time.sleep(5)