-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLSE.py
372 lines (274 loc) · 12.8 KB
/
LSE.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
'''-------------------------------------------
Link search engine
----------------------------------------------
A simple and lightweight search engine for
.csv link sheets.
Simply put your sheet inside the 'link sheets'
folder and run this script.
First update: 24/07/2022 18:27
Last update: 27/07/2022 18:33
Version: Beta 1.1
2022
-------------------------------------------'''
# ------------ Import modules --------------
import os, math
from time import sleep
# Tries to import the termcolor module or asks the user to install it
try:
from termcolor import colored
except ImportError:
print('termcolor module not found. Would you like to install it? (y/n)')
_install = input()
if _install in ['y','yes','Y','Yes','YES']:
os.system('python -m pip install termcolor')
from termcolor import colored
else:
print('You can install it manually by running: python -m pip install termcolor\nExiting...')
sleep(3)
exit()
# Tries to import the pandas module or asks the user to install it
try:
from pandas import read_csv
except ImportError:
print('pandas module not found. Would you like to install it? (y/n)')
_install = input()
if _install in ['y','yes','Y','Yes','YES']:
os.system('python -m pip install pandas')
from pandas import read_csv
else:
print('You can install it manually by running: python -m pip install pandas\nExiting...')
sleep(3)
exit()
# Tries to import the pyperclip module or asks the user to install it
try:
from pyperclip import copy
except ImportError:
print('pyperclip module not found. Would you like to install it? (y/n)')
_install = input()
if _install in ['y','yes','Y','Yes','YES']:
os.system('python -m pip install pyperclip')
from pyperclip import copy
else:
print('You can install it manually by running: python -m pip install pyperclip\nExiting...')
sleep(3)
exit()
# Tries to import the webbrowser module or asks the user to install it
try:
from webbrowser import open as openWeb
except ImportError:
print('webbrowser module not found. Would you like to install it? (y/n)')
_install = input()
if _install in ['y','yes','Y','Yes','YES']:
os.system('python -m pip install webbrowser')
from webbrowser import open as openWeb
else:
print('You can install it manually by running: python -m pip install webbrowser\nExiting...')
sleep(3)
exit()
# NB: You might need to install those last extensions using the 'pip install termcolor' command etc..
# --------------- Functions ----------------
# Clears the console
# Might need to be changed for other OS
def clearConsole():
# Windows
if 'nt' in os.name:
os.system('cls')
# Linux and probably Mac
else:
os.system('clear')
# Shows the search progress bar
def progressBar(_currentItem,_maxItems):
_currentProgress = round(_currentItem * 20 / _maxItems)
# Displays the progress only if different from last cycle
if _currentProgress != round((_currentItem - 1) * 20 / _maxItems):
clearConsole()
print(*[colored('━','green') for _tile in range(_currentProgress)],*[colored('━','red') for _tile in range(20 - _currentProgress)],' ',_currentItem,'/',_maxItems,' links searched',sep="")
# Converts
def convertSize(bytesSize):
if bytesSize == 0:
return "0B"
size_name = ("B", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB")
i = int(math.floor(math.log(bytesSize, 1024)))
p = math.pow(1024, i)
s = round(bytesSize / p, 2)
return "%s %s" % (s, size_name[i])
# Find titles from specific keyword(s)
def searchKeys(selectedFile,_keywords):
_titleList = []
_titleSizes = []
_titleLinks = []
for _filePath in selectedFile:
_file = read_csv('link sheets/' + _filePath)
_titleList += _file['title'].tolist()
_titleSizes += _file['size'].tolist()
_titleLinks += _file['link'].tolist()
_currentId = 0
foundIds, foundTitles, foundSizes, foundLinks = [], [], [], []
for _currentTitle in _titleList:
_currentId += 1
_matchKeywords = 0
progressBar(_currentId,len(_titleList))
for _currentKeyword in _keywords:
if _currentTitle.lower().find(_currentKeyword) != -1:
_matchKeywords +=1
if _matchKeywords == len(_keywords):
foundIds.append(_currentId)
foundTitles.append(_currentTitle)
foundSizes.append(_titleSizes[_currentId-1])
foundLinks.append(_titleLinks[_currentId-1])
# Make list of duplicate links index
_dupIndexList = [i for i, x in enumerate(foundLinks) if i != foundLinks.index(x)]
# Remove from the duplicate elements for each list
# We remove them in reverse so that the list is not modified after remove
for i in sorted(_dupIndexList, reverse=True):
del foundIds[i]
del foundTitles[i]
del foundSizes[i]
del foundLinks[i]
return foundIds, foundTitles, foundSizes, foundLinks
#searchKeys('link sheets/'+_filePath,'test')
#sleep(5)
# ------------ Get link sheet --------------
clearConsole()
linkSheetFolder = 'link sheets'
linkSheetFiles = []
# Lists .csv files in the sheet folder
for _filePath in os.listdir(linkSheetFolder):
# Check if current path is a .csv file
if os.path.isfile(os.path.join(linkSheetFolder, _filePath)) and _filePath[-4:] == '.csv':
linkSheetFiles.append(_filePath)
# Checks for the number of files found
if len(linkSheetFiles) != 0:
print(colored(str(len(linkSheetFiles)) + " file(s) found:\n", attrs=['bold']))
_id = 0
for _currentFile in linkSheetFiles:
_id += 1
print(colored(str(_id)+" - "+_currentFile[:-4],'blue'))
else:
print(colored("No files found.\n",'red') + colored("Please place your .csv files in the software 'link sheets' folder.",'cyan'))
# Ask the user for the desired file
while True:
selectedID = input("\nType the desired file Id (all will search through all the csv): ")
# Check if selectedID is all
if selectedID == 'all':
selectedFile = linkSheetFiles
break
# Check for valid file id
if not selectedID in [str(_id) for _id in range(1,len(linkSheetFiles)+1)]:
print(colored("Please choose a valid file id.",'red'))
else:
selectedFile = []
selectedFile.append(linkSheetFiles[int(selectedID)-1])
break
clearConsole()
previousKeywords = []
# Search for files containing keyword(s)
while True:
# Prints the found results and highlights keywords in titles
def printResults(_keywords):
for _Id in range(0,len(foundIds)):
_Title = foundTitles[_Id] # Full filename
_Link = colored(foundLinks[_Id], attrs=['bold']) # Link
_Size = colored(convertSize(foundSizes[_Id]),'red') # Size of file
_Name = colored(_Title[:- len(_Title.split('.')[-1])-1],'blue') # Filename without extension
_Extension = colored('.'+_Title.split('.')[-1],'cyan') # File extension
# Highlights keywords
_keyPos = []
for _keyword in _keywords:
_start = _Name.lower().find(_keyword.lower())
_end = _start + len(_keyword)
_Name = _Name[:_start] + colored(_Name[_start:_end], attrs=['reverse']) + colored(_Name[_end:],'blue')
# Prints all informations
print(colored(str(_Id+1)+' -','yellow'), colored('#' + str(foundIds[_Id]),'magenta'), _Name + _Extension, _Size, _Link)
# Ask user for keywords, uses previous keywords if no keywords are given
print("Please enter desired keywords:",colored("(syntax: word1,word2,...)", attrs=['bold']))
keywords = str(input("{} \r".format(colored(str(previousKeywords).replace('[','').replace(']','').replace("'",''),'cyan'))) or (str(previousKeywords).replace('[','').replace(']','').replace("'",''))).lower().split(',')
keywords += previousKeywords[len(keywords):]
previousKeywords = keywords
# Gets a list of ids corresponding to each title matching desired keywords
foundIds, foundTitles, foundSizes, foundLinks = searchKeys(selectedFile, keywords)
# Prints the found results and highlights keywords in titles
clearConsole()
print(colored("Found " + str(len(foundIds)) + " results.",'green'))
printResults(keywords)
# Ask user for next action
print(colored("\nAvailable actions:\n", attrs=['bold']) + "\n1 - Edit search\n2 - Copy all links to clipboard\n3 - Open all links in chrome\n4 - Copy desired links to clipboard\n5 - Open desired links in chrome\n6 - Exit\n" + colored("\nEnter the number of the desired action:",'cyan'))
# Get user input
while True:
userInput = input()
# Check for valid input
if userInput in ['1','2','3','4','5','6']:
if userInput == '1':
# Edit last keywords
clearConsole()
elif userInput == '2':
# Copy all links to clipboard
_toCopy = []
for _Id in range(0,len(foundIds)):
_toCopy.append(foundLinks[_Id]+'\n')
copy(''.join(_toCopy)) # Copy link(s) to clipboard
clearConsole()
print(colored("Link(s) copied to clipboard.\n",'green'))
elif userInput == '3':
# Open all links in browser
clearConsole()
if len(foundIds) > 10:
print(colored("Warning: opening too many links may cause your browser to crash or pages to load incorrectly.\n",'red') + colored("Continue? (y/n)",'cyan'))
while True:
_userInput = input()
if _userInput in ['y','yes','Y','Yes','YES']:
print(colored("\nPlease wait while links are opened in browser...\n",'cyan'))
for _Id in range(0,len(foundIds)):
openWeb(foundLinks[_Id])
break
elif _userInput in ['n','no','N','No','NO']:
break
else:
print(colored("Please choose a valid option.",'red'))
clearConsole()
else:
print(colored("\nPlease wait while links are opened in browser...\n",'cyan'))
for _Id in range(0,len(foundIds)):
openWeb(foundLinks[_Id]) # Open link(s) in browser
clearConsole()
elif userInput == '4':
# Copy desired links to clipboard
while True:
# Ask user for desired links ids separated by commas and turn them into a list
print("\nPlease enter desired link ids:",colored("(syntax: 1,3,7,...)", attrs=['bold']))
_userInput = input().split(',')
# Check for valid input
if not all([_id in [str(_id) for _id in range(1,len(foundIds)+1)] for _id in _userInput]):
print(colored("Please choose valid link(s) id(s).",'red'))
else:
break
# Copy desired links to clipboard
_toCopy = []
for _Id in _userInput:
_toCopy.append(foundLinks[int(_Id)-1]+'\n')
copy(''.join(_toCopy)) # Copy link(s) to clipboard
clearConsole()
print(colored("Link(s) copied to clipboard.\n",'green'))
elif userInput == '5':
# Open desired links in browser
while True:
# Ask user for desired links ids separated by commas and turn them into a list
print("\nPlease enter desired link ids:",colored("(syntax: 1,3,7,...)", attrs=['bold']))
_userInput = input().split(',')
# Check for valid input
if not all([_id in [str(_id) for _id in range(1,len(foundIds)+1)] for _id in _userInput]):
print(colored("Please choose valid link(s) id(s).",'red'))
else:
break
# Open desired links in browser
print(colored("\nPlease wait while links are opened in browser...\n",'cyan'))
for _Id in _userInput:
openWeb(foundLinks[int(_Id)-1]) # Open link(s) in browser
clearConsole()
elif userInput == '6':
# Close program
clearConsole()
exit()
break
else:
print(colored("Please choose a valid option.",'red'))