|
| 1 | +# The following code is written by Rohit Saxena. STUDY PURPOSE ONLY. Any unauthorized use or modification is prohibited. |
| 2 | +import ctypes |
| 3 | +import datetime |
| 4 | +import ftplib |
| 5 | +import glob |
| 6 | +import os |
| 7 | +import shutil |
| 8 | +import string |
| 9 | +from urllib.error import URLError |
| 10 | +from urllib.request import * |
| 11 | + |
| 12 | +import docx |
| 13 | +import openpyxl |
| 14 | + |
| 15 | +# import PyPDF2 |
| 16 | +# import re |
| 17 | + |
| 18 | +errorCount, i, j = 0, 0, 0 |
| 19 | +rvs = [] |
| 20 | +host = "" # FTP Host URL |
| 21 | +ftp_id = "" # FTP CLIENT ID |
| 22 | +ftp_pw = "" # FTP CLIENT PASSWORD |
| 23 | +_ftp_root_folder_ = 'htdocs' # FTP root folder |
| 24 | +newdir = os.environ['COMPUTERNAME'] # FTP Main folder name |
| 25 | +ft = [] |
| 26 | +todaysDate = str(datetime.datetime.today()) # FTP sub-folder name |
| 27 | +ftp = ftplib.FTP() |
| 28 | + |
| 29 | + |
| 30 | +def _rS_sign(): |
| 31 | + print("\n><----------------------------------------------><\n") |
| 32 | + print("|""---------"" ""------------") |
| 33 | + print("|"" ""|"" "" |") |
| 34 | + print("|"" ""|"" "" |") |
| 35 | + print("|""---------"" ""|") |
| 36 | + print("|""-"" ""------------") |
| 37 | + print("|"" ""-"" ""|") |
| 38 | + print("|"" ""-"" ""|") |
| 39 | + print("|"" ""-"" "" ------------") |
| 40 | + print("The following code is written by Rohit Saxena. STUDY PURPOSE ONLY. Any unauthorized use or modification is " |
| 41 | + "prohibited.") |
| 42 | + print("\n><----------------------------------------------><\n") |
| 43 | + return True |
| 44 | + |
| 45 | + |
| 46 | +def _rSAdmin_Check(): |
| 47 | + try: |
| 48 | + is_admin = (os.getuid() == 0) |
| 49 | + except AttributeError: |
| 50 | + is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0 |
| 51 | + return is_admin |
| 52 | + |
| 53 | + |
| 54 | +def _rSInternet_Check_(): # Internet Tester |
| 55 | + while True: |
| 56 | + try: |
| 57 | + urlopen('https://www.central16.in', timeout=1) |
| 58 | + return |
| 59 | + except URLError as e: |
| 60 | + print('Internet Connectivity Check --> URL Error: ', e.reason) # remove print is building exe |
| 61 | + pass |
| 62 | + |
| 63 | + |
| 64 | +def _RsBackupWriter(rrs, pp): # local backup (unfinished) |
| 65 | + global j |
| 66 | + print('BACKUP FOLDER STARTED /N rvs len : ', len(rrs)) |
| 67 | + trs = open(pp, "a") |
| 68 | + for item in rrs: |
| 69 | + trs.write("%s\n" % item) |
| 70 | + print('Backup File Created') |
| 71 | + file1 = open(pp, "r") |
| 72 | + print(file1.read()) |
| 73 | + file1.close() |
| 74 | + |
| 75 | + |
| 76 | +def _RsXlsx_(SS, CC): # XLSX Reader |
| 77 | + rs = openpyxl.load_workbook(CC, read_only=True, data_only=True) |
| 78 | + ws = rs.active |
| 79 | + for i in range(1, ws.max_row + 1): |
| 80 | + for j in range(1, ws.max_column + 1): |
| 81 | + if SS == ws.cell(i, j).value: |
| 82 | + print("Found in cell :", ws.cell(i, j), "File Location :", CC) |
| 83 | + rvs.append(CC) |
| 84 | + |
| 85 | + |
| 86 | +''' |
| 87 | +def _rsPDF_(SS, CC): # PDF reader |
| 88 | +
|
| 89 | + filepdf = PyPDF2.PdfFileReader(CC) |
| 90 | + NumPages = filepdf.getNumPages() |
| 91 | + for i in range(0, NumPages): |
| 92 | + PageObj = filepdf.getPage(i) |
| 93 | + Text = PageObj.extractText() |
| 94 | + ResSearch = re.search(SS, Text) |
| 95 | + print("-------------> ", ResSearch) |
| 96 | + if ResSearch in filepdf: |
| 97 | + print('String', ResSearch, 'File Path: ', CC) |
| 98 | + rvs.append() |
| 99 | +''' |
| 100 | + |
| 101 | + |
| 102 | +def _RsTxt_(SS, CC): # TXT Reader |
| 103 | + file1 = open(CC, 'r', encoding='UTF8', errors='ignore') |
| 104 | + flag = 0 |
| 105 | + index = 0 |
| 106 | + for line in file1: |
| 107 | + index += 1 |
| 108 | + if SS in line.lower(): |
| 109 | + flag = 1 |
| 110 | + break |
| 111 | + if flag != 0: |
| 112 | + print('String', SS, 'Found In Line: ', index, 'File Path: ', CC) |
| 113 | + rvs.append(CC) |
| 114 | + file1.close() |
| 115 | + |
| 116 | + |
| 117 | +def _RsDocx_(SS, CC): # DOCX Reader |
| 118 | + flag = 0 |
| 119 | + index = 0 |
| 120 | + doc = docx.Document(CC) |
| 121 | + for para in doc.paragraphs: |
| 122 | + index += 1 |
| 123 | + if SS in para.text: |
| 124 | + flag = 1 |
| 125 | + break |
| 126 | + if flag != 0: |
| 127 | + print('String', SS, 'Found In Line: ', index, 'File Path: ', CC) |
| 128 | + rvs.append(CC) |
| 129 | + |
| 130 | + |
| 131 | +def _RsDirCheck_(rsdc): # FTP Main Folder Checker |
| 132 | + x = 0 |
| 133 | + for g in rsdc: |
| 134 | + if newdir in g: |
| 135 | + x = 1 |
| 136 | + if x == 0: |
| 137 | + print(" New Directory created ---> ", newdir) |
| 138 | + ftp.mkd(newdir) |
| 139 | + |
| 140 | + |
| 141 | +def _RsTodayFolderCheck_(rstfc): # FTP Sub-Folder Checker |
| 142 | + x = 0 |
| 143 | + for g in rstfc: |
| 144 | + if todaysDate in g: |
| 145 | + x = 1 |
| 146 | + if x == 0: |
| 147 | + ftp.mkd(todaysDate) |
| 148 | + print("New Today's date folder created ---> ", todaysDate) |
| 149 | + |
| 150 | + |
| 151 | +def _Rs_ftp_COPY_(): # FTP Client connector |
| 152 | + print('uploading data...') |
| 153 | + port = 21 |
| 154 | + ftp.connect(host, port) |
| 155 | + print(ftp.getwelcome()) |
| 156 | + print("Logging in...") |
| 157 | + ftp.login(ftp_id, ftp_pw) |
| 158 | + ftp.cwd(_ftp_root_folder_) |
| 159 | + ftp.retrlines('LIST', ft.append) |
| 160 | + _RsDirCheck_(ft) |
| 161 | + ftp.cwd(newdir) |
| 162 | + ft.clear() |
| 163 | + ftp.retrlines('LIST', ft.append) |
| 164 | + _RsTodayFolderCheck_(ft) |
| 165 | + ftp.cwd(todaysDate) |
| 166 | + |
| 167 | + |
| 168 | +def _rS_FTP_file_transf(rvs): # FTP File uploader |
| 169 | + try: |
| 170 | + rs = 0 |
| 171 | + print('Uploading Data...') |
| 172 | + while rs != len(rvs): |
| 173 | + filename = rvs[rs] |
| 174 | + file_name, file_extension = os.path.splitext(filename) |
| 175 | + _FTP_File_Name_ = os.path.basename(file_name) + file_extension |
| 176 | + _ftpNewFileNMCmd_ = "STOR %s" % _FTP_File_Name_ |
| 177 | + with open(filename, "rb") as file: |
| 178 | + ftp.storbinary(_ftpNewFileNMCmd_, file) |
| 179 | + rs += 1 |
| 180 | + print('Upload Complete !') |
| 181 | + ftp.dir() |
| 182 | + except Exception: |
| 183 | + print(Exception) |
| 184 | + |
| 185 | + |
| 186 | +def main(): |
| 187 | + global errorCount |
| 188 | + if _rSAdmin_Check(): |
| 189 | + os.system('color 2') |
| 190 | + print("<-----Admin Access----->") |
| 191 | + else: |
| 192 | + os.system('color 4') |
| 193 | + print("<-----No Admin Access----->") |
| 194 | + global i, j |
| 195 | + global rvs |
| 196 | + # val = input("Enter extension : ") #If specific extension only required uncheck and add val in _Rel_Path |
| 197 | + ff = input("Enter string : ").lower() |
| 198 | + |
| 199 | + rs = ['%s:\\' % d for d in string.ascii_uppercase if os.path.exists('%s:\\' % d)] |
| 200 | + print('\n><----------------------------------------------><\nGENERAL DATA\nActive local drives ---> ', rs) |
| 201 | + FtypeXLSX = '.xlsx' |
| 202 | + FtypeTXT = '.txt' |
| 203 | + FtypeDOCX = '.docx' |
| 204 | + FtypePDF = '.pdf' |
| 205 | + _Rel_Exp_ = '**/*' |
| 206 | + _Rel_Path_ = _Rel_Exp_ |
| 207 | + # print('Selected file type = ', val) |
| 208 | + rss = [] |
| 209 | + |
| 210 | + bck_dir = "rrs" # LINE 154 - 166 REFERS LOCAL BACKUP (UNFINISHED) |
| 211 | + bck_file = "rrs.txt" |
| 212 | + print('Hidden folder named rss is created in first local drive from available drive list') |
| 213 | + pth = os.path.join(rs[0], bck_dir) |
| 214 | + if os.path.exists(pth): # this if will flush the old backup folder and re-create new backup folder |
| 215 | + shutil.rmtree(pth) |
| 216 | + os.makedirs(pth) |
| 217 | + print('Hidden folder path: ', pth) |
| 218 | + print('Hidden file text.txt is created in hidden folder rss') |
| 219 | + CN = os.path.join(pth, bck_file) |
| 220 | + print('Hidden file location: ', CN) |
| 221 | + file1 = open(CN, "a") |
| 222 | + file1.close() |
| 223 | + if _rS_sign(): |
| 224 | + print('Below files are present in system: '"\n") |
| 225 | + while i != len(rs): |
| 226 | + os.chdir(rs[i]) |
| 227 | + for file in glob.glob(_Rel_Path_, recursive=True): |
| 228 | + # print(file) |
| 229 | + completeName = os.path.join(rs[i], file) |
| 230 | + # print('File path: ', completeName) |
| 231 | + file_name, file_extension = os.path.splitext(completeName) |
| 232 | + if file_extension == FtypeXLSX: |
| 233 | + try: |
| 234 | + _RsXlsx_(ff, completeName) |
| 235 | + except Exception: |
| 236 | + errorCount += 1 |
| 237 | + print('Error in reading file, File Path: ', completeName) |
| 238 | + if file_extension == FtypeTXT: |
| 239 | + try: |
| 240 | + _RsTxt_(ff, completeName) |
| 241 | + except PermissionError: |
| 242 | + errorCount += 1 |
| 243 | + print('Admin Permission Required File Path: ', completeName) |
| 244 | + if file_extension == FtypeDOCX: |
| 245 | + try: |
| 246 | + _RsDocx_(ff, completeName) |
| 247 | + except Exception: |
| 248 | + errorCount += 1 |
| 249 | + print('Error in reading file, File Path: ', completeName) |
| 250 | + ''' |
| 251 | + if file_extension == FtypePDF: |
| 252 | + try: |
| 253 | + _rsPDF_(ff, completeName) |
| 254 | + except Exception: |
| 255 | + errorCount += 1 |
| 256 | + print('Error in reading file, File Path: ', completeName) |
| 257 | + ''' |
| 258 | + rss.append(completeName) |
| 259 | + i += 1 |
| 260 | + else: |
| 261 | + exit() |
| 262 | + print('\nSystem scan completed\n><----------------------------------------------><\nSCAN RESULTS') |
| 263 | + # print(rss) |
| 264 | + print('Searched Text:', ff) |
| 265 | + print('Total File Scanned :', len(rss)) |
| 266 | + print("Total file found: ", len(rvs)) |
| 267 | + print("Total No of unreachable files (ACTION REQUIRED)", errorCount) |
| 268 | + if len(rvs) == 0: |
| 269 | + input("Press enter to close program") |
| 270 | + exit() |
| 271 | + print("Below are the file paths \n", rvs) |
| 272 | + ''' |
| 273 | + _Rs_ftp_COPY_() |
| 274 | + _rS_FTP_file_transf(rvs) #ONLY FOR FTP ''' |
| 275 | + print('calling backup') |
| 276 | + _RsBackupWriter(rvs, CN) # LOCAL BACKUP CREATOR |
| 277 | + |
| 278 | + |
| 279 | +_rSInternet_Check_() |
| 280 | + |
| 281 | +if _rS_sign(): |
| 282 | + print("Other generic information\n><----------------------------------------------><\n1.Backup folders are " |
| 283 | + "ENABLED\n2.FTP options are DISABLED ") |
| 284 | + main() |
| 285 | + |
| 286 | +else: |
| 287 | + print("Author sign missing") |
| 288 | + |
| 289 | +input("Press enter to close program") |
0 commit comments