-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistVPS.py
221 lines (172 loc) · 6.53 KB
/
listVPS.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
214
215
216
217
218
219
220
221
import os
import sys
import paramiko
import time
import stat
import configparser
from pick import Picker
from urllib import parse
from enum import Enum
import faststream
configp = configparser.ConfigParser()
if configp.read('config.ini'):
config = configp['DEFAULT']
else:
print("config file not found")
sys.exit()
cakeBase = config['directURL']
sftpURL = config['sftpURL']
sftpUser = config['sftpUser']
sftpPass = config['sftpPass']
sftpPrivateKeyPath = config['sftpPrivateKeyPath']
sftpPort = int(config['sftpPort'])
sftpFolder = config['sftpFolder']
wantedFiles = [e.strip() for e in config['wantedFiles'].split(',')]
unwantedFolders = ['_gsdata_']
ssh = paramiko.SSHClient()
# automatically add keys without requiring human intervention
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(sftpURL, username=sftpUser, password=sftpPass, port=sftpPort, key_filename=sftpPrivateKeyPath)
ftp = ssh.open_sftp()
class FileManager():
def __init__(self):
self.path = ['files', 'tvshows']
def isDir(self, filename):
fullPath = self.getCurrentPath(filename)
statResult = ftp.lstat(fullPath)
return stat.S_ISDIR(statResult.st_mode)
def filterExtension(self, items):
result = []
for item in items:
filename, fileextension = os.path.splitext(item)
if fileextension in wantedFiles:
result += [item]
return result
def getCurrentPath(self, file=None):
if file is None:
return '/'.join(self.path) # TODO: handle case empty self.path
else:
fullpath = self.path + [file]
return '/'.join(fullpath)
def select(self, selectedItem):
itemName = os.path.basename(str(selectedItem))
if self.isDir(itemName):
self.path += [itemName]
return None
else:
return cakeBase + '/'.join(self.path[1:]) + '/' + itemName
def remove(self, filePath):
try:
ftp.stat(filePath)
except IOError as e:
if e.errno == errno.ENOENT:
print('Could not delete {}. File does not exist.'.format(filePath))
else:
print(str(e))
return False
else:
ftp.remove(filePath)
return True
def goUp(self):
if (len(self.path) > 1):
self.path.pop()
return True
else:
return False
def getItems(self, func = None):
items = ftp.listdir(self.getCurrentPath())
files = []
folders = []
for item in items:
if self.isDir(item) and item not in unwantedFolders:
dirFiles = ftp.listdir(self.getCurrentPath(item))
if any(self.isDir(item + '/' + x) for x in dirFiles): # Show if has subfolders
folders += [item]
continue
dirFiles = self.filterExtension(dirFiles)
if len(dirFiles) > 0 or len(self.path) == 1: # Show folders in root folder
folders += [item]
else:
filename, fileextension = os.path.splitext(item)
if fileextension in wantedFiles:
files += [item]
if func is not None:
files = sorted(files, key=func)
else:
files = sorted(files)
folders = sorted(folders)
return folders + files
class SelectionType(Enum):
Manual = 1
Picker = 2
class FileSelection():
def __init__(self, fileManager, orderBy=None, selectionType=SelectionType.Picker):
self.orderBy = orderBy
self.selectionType = selectionType
self.fileManager = fileManager
def switchOrderBy(self, test):
if self.orderBy is None:
self.orderBy = lambda x: ftp.stat(self.fileManager.getCurrentPath(x)).st_size
else:
self.orderBy = None
return None, -1
def askSelection(self, choices):
selectedItem, selectedIndex = ("", -1)
currentPath = self.fileManager.getCurrentPath()
if self.selectionType is SelectionType.Picker:
picker = Picker(choices, currentPath + " ('q' to quit)")
picker.register_custom_handler(ord('q'), sys.exit)
picker.register_custom_handler(ord('o'), self.switchOrderBy)
selectedItem, selectedIndex = picker.start()
else:
while True:
print(currentPath + " ('q' to quit)")
for i, choice in enumerate(choices):
print("["+str(i)+"] " + choice)
try:
selectedIndex = int(input("Select your choice between 0-" + str(len(choices)-1) + ": "))
if selectedIndex < 0 or selectedIndex >= len(choices):
print("Invalid choice. Must be between 0-" + str(len(choices)-1) + ".")
else:
break
except ValueError:
print("Please enter a number.")
return selectedItem, selectedIndex
fileManager = FileManager()
fileSelection = FileSelection(fileManager)
def browser():
while True:
items = fileManager.getItems(fileSelection.orderBy)
itemsName = []
for item in items:
if fileManager.isDir(item):
itemsName += [os.path.basename(str(item)) + "/"]
else:
raw_size = ftp.stat(fileManager.getCurrentPath(item)).st_size
size = str(round(raw_size / 1024 / 1024)) # Bytes to Megabytes
itemsName += [os.path.basename(str(item)) + " [" + size + " Mo]"]
choices = [".."] + itemsName
selectedItem, selectedIndex = fileSelection.askSelection(choices)
if selectedItem is None:
continue
if selectedIndex == 0:
fileManager.goUp()
continue
# - 1 for added ".." choice
selected = fileManager.select(items[selectedIndex - 1])
if selected:
return selected, items[selectedIndex - 1]
url, fileName = browser()
fastStreamRet = faststream.main(url)
while True:
res = input("Delete file server-side? (default 'n') [y/n] ")
if res == "y":
removeVideoRet = fileManager.remove(fileManager.getCurrentPath(fileName))
if removeVideoRet:
subName = os.path.splitext(fileName)[0] + '.srt'
removeSubRet = fileManager.remove(fileManager.getCurrentPath(subName))
sys.exit(0)
elif res == "n" or res == "":
sys.exit(0)
else:
print("Please enter 'y' or 'n'.")