-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
633 lines (466 loc) · 26.5 KB
/
main.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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
import platform
import time
import requests
from PyEnhance import WebTools, Stamps, Loading
from bs4 import BeautifulSoup
import re
from itertools import zip_longest
import os
import sys, locale
from rich.align import Align
from rich.console import Console
from rich.table import Table
import math
Stamp = Stamps.Stamp
class Main:
"""
Class that handles DNS record retrieval for a given URL.
Attributes:
WebTool (WebTools.WebTools): Instance of WebTools class.
CheckingIfWebsiteWillBlockRequestLoading (Loading.Loading): Instance of Loading class for indicating that the script is checking if dnshistory.com will block the request.
GettingRecordsLoading (Loading.Loading): Instance of Loading class for indicating the status of record retrieval.
Replace (list): List of strings to replace in HTML tags.
SubDomains (list): List of subdomains.
Records (dict): Dictionary to store DNS records. Keys represent record types and values represent record data.
Tags (list): List of HTML tags.
RecordTypes (list): List of record types.
WebHeaders (dict): Dictionary of HTTP headers used for web requests.
URL (str): The URL of the website used to fetch its DNS records.
ClearScreenCommand (str): Command to clear the screen based on user OS.
UserOS (str): Operating system of the user.
Methods:
grouper(sequence, n, fillvalue=None): Groups the sequence into tuples of size n.
GetBasePageRecords(URL): Retrieves the base page records for the given URL.
GetHistory(URL, Type): Retrieves historical DNS records for the given URL and record type.
Filter(): Filters the DNS records based on certain conditions.
"""
def __init__(self, URL):
"""
Initializes the object with the given URL.
:param URL: The URL of the website used to fetch its DNS records.
"""
self.URL = URL
self.WebTool = WebTools.WebTools()
self.CheckingIfWebsiteWillBlockRequestLoading = Loading.Loading()
self.GettingRecordsLoading = Loading.Loading()
self.Replace = []
self.SubDomains = []
self.Records = {'SOA':[], 'NS':[], 'MX':[], 'A':[],
'AAAA':[], 'CNAME':[], 'PTR':[], 'TXT':[]}
self.Tags = [
"a", "abbr", "address", "area", "article", "aside", "audio", "b", "base", "bdi",
"bdo", "blockquote", "body", "br", "button", "canvas", "caption", "cite", "code",
"col", "colgroup", "command", "datalist", "dd", "del", "details", "dfn", "dialog",
"div", "dl", "dt", "em", "embed", "fieldset", "figcaption", "figure", "footer",
"form", "h1", "h2", "h3", "h4", "h5", "h6", "head", "header", "hgroup", "html",
"i", "iframe", "img", "input", "ins", "kbd", "label", "legend", "li", "link",
"main", "map", "mark", "menu", "meta", "meter", "nav", "noscript", "object",
"ol", "optgroup", "option", "output", "p", "param", "picture", "pre", "progress",
"q", "rp", "rt", "ruby", "s", "samp", "section", "select", "slot", "small", "source",
"span", "strong", "style", "sub", "summary", "sup", "table", "tbody", "td", "textarea",
"tfoot", "th", "thead", "time", "title", "tr", "track", "u", "ul", "var", "video",
"wbr"
]
self.RecordTypes = ["SOA","NS","MX","A","AAAA","CNAME", "PTR", "TXT"]
self.WebHeaders = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.74 Safari/537.36",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Language": "en-US,en;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"Connection": "keep-alive",
"Upgrade-Insecure-Requests": "1",
"Sec-Fetch-Site": "none",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-User": "?1",
"Sec-Fetch-Dest": "document",
"Cache-Control": "max-age=0",
"Sec-CH-UA": "\" Not A;Brand\";v=\"99\", \"Chromium\";v=\"99\", \"Google Chrome\";v=\"99\"",
"Sec-CH-UA-Mobile": "?0",
"Sec-CH-UA-Platform": "\"Windows\"",
"Cookie": "sessionId=eyJpdiI6IklDVkZuQmFcL3NcLzN4Z1wvTmFcLzNnPT0iLCJ2YWx1ZSI6IlwvT1wvUnRQOHhcLzE2NGJcL1wvXC9cLzBcL1wvXC8wK3c9PSIsIm1hYyI6IjlkYzBmYWM1MjJlZjQ4MzI5YjFjNzg3MTQ0NjQ5ZDMwOTBiMjJhOWFjY2M2MzZiNTY5ZDgwN2E4YWE2NzE4YTEifQ%3D%3D; theme=light; csrftoken=ghPx4Dr56TjYVbnP5rSzG5hajkLQzN7b;"
}
for Tag in self.Tags:
self.Replace.append(f'<{Tag}>')
self.Replace.append(f'</{Tag}>')
self.Replace.append(f'<{Tag}/>')
self.ClearScreenCommand = None
if platform.system() == "Windows":
self.UserOS = "Windows"
self.ClearScreenCommand = "cls"
elif platform.system() == "Linux":
self.UserOS = "Linux"
self.ClearScreenCommand = "clear"
elif platform.system() == "Darwin":
self.UserOS = "MacOS"
self.ClearScreenCommand = "clear"
else:
self.UserOS = "Unknown"
self.ClearScreenCommand = None
if self.UserOS == "Windows":
WinTerm = self.ShellIsWinTerm()
if WinTerm is False:
self.UnicodeSupported = False
else:
self.UnicodeSupported = True
else:
self.UnicodeSupported = True
#Function Calls
self.CheckingIfWebsiteWillBlockRequestLoading.Spin(Text="Checking If Request Will Be Blocked") # Starts Loading indicator
self.GetBasePageRecords(URL=URL)
for RecordType in self.RecordTypes:
self.GetHistory(URL=URL, RecordType=RecordType.lower())
self.Filter()
def ShellIsWinTerm(self) -> bool: # Checks if users shell is the Windows Terminal. Need to do this due to cmd being unable to display the unicode arrow used for the output table.
is_windows_terminal = sys.platform == "win32" and os.environ.get("WT_SESSION") # Got this snippet from Stack Overflow (https://stackoverflow.com/questions/62878172/how-do-i-detect-if-my-python-script-is-running-in-the-windows-terminal)
if is_windows_terminal is None: # Will return none if shell is not Windows Terminal
return False
else:
return True
def grouper(self,sequence, n, fillvalue=None) -> list: # Coded by ChatGPT
"""
:param sequence: The input sequence to be grouped.
:param n: The number of elements in each group.
:param fillvalue: The value used to fill any missing elements in the last group if the input sequence is not divisible evenly by n.
:return: A list of grouped elements, where each group contains n elements from the input sequence. If the input sequence is not divisible evenly by n, the last group will be filled with fillvalue.
"""
args = [iter(sequence)] * n
return [list(group) for group in zip_longest(*args, fillvalue=fillvalue)]
def GetBasePageRecords(self, URL:str):
"""
:param URL: The URL of the website used to fetch its DNS records.
:return: None
"""
WebRequest = requests.get(f'https://dnshistory.org/dns-records/{URL}', headers=self.WebHeaders)
WebRequestSoup = BeautifulSoup(WebRequest.text, 'html.parser')
if WebRequest.status_code == 403: # Triggerd if a CloudFlare captcha is returned
self.CheckingIfWebsiteWillBlockRequestLoading.Stop()
os.system(self.ClearScreenCommand)
print(f"{Stamp.Error} Request Blocked, Status code: {WebRequest.status_code}")
exit()
self.CheckingIfWebsiteWillBlockRequestLoading.Stop() # Stops loading indicator so a new one can be made
os.system(self.ClearScreenCommand)
self.GettingRecordsLoading.Spin(Text="Getting Records") # Makes a new loading indicator
IndexesForSoups = []
ListForSoups = []
RecordsContainers = WebRequestSoup.find_all('h3')
for RecordType in RecordsContainers:
IndexesForSoups.append(WebRequest.text.index(str(RecordType)))
for Iter, Index in enumerate(IndexesForSoups):
if Iter+1 == len(IndexesForSoups): # Checks if current loop iteration is the last and if it is brakes the loop
break
else:
NextIndex = IndexesForSoups[Iter+1]
ListForSoups.append(WebRequest.text[Index:NextIndex])
DictGuide = {0:"SOA", 1:"NS", 2:"MX", 3:"A", 4:"AAAA", 5:"CNAME", 6:"PTR", 7:"TXT"} # Dict of DNS record types
for Iter, PreSoup in enumerate(ListForSoups):
IterSoup = BeautifulSoup(PreSoup, 'html.parser')
Items = IterSoup.find_all('p')
TagPattern = re.compile(r'</?\w*/?>') # Matches any HTML tag
self.Records[DictGuide.get(Iter)].append(''.join([TagPattern.sub('', str(Item).replace('\n', ' ')) for Item in Items]).strip())
def GetHistory(self, URL:str, RecordType:str):
"""
Get historical DNS records for a given URL and record type.
:param URL: The URL for which to retrieve the historical DNS records.
:type URL: str
:param RecordType: The type of DNS record to retrieve (e.g., A, CNAME, MX).
:type RecordType: str
:return: None
"""
DateRegex = re.compile("\d{4}-\d{2}-\d{2}\s->\s\d{4}-\d{2}-\d{2}") # Regex for finding dates in html code
WebRequest = requests.get(f'https://dnshistory.org/historical-dns-records/{RecordType}/{URL}', headers=self.WebTool.RequestHeaders)
WebRequestSoup = BeautifulSoup(WebRequest.text, 'html.parser')
Content = WebRequestSoup.find_all("p")
IndexesForRecords = re.findall(DateRegex, str(Content)) # Returns a list of indexes where the DateRegex was matched.
Content = str(Content)
TagPattern = re.compile(r'</?\w*/?>') # Matches any HTML tag
for Iter, DataForIndex in enumerate(IndexesForRecords):
if Iter+1 == len(IndexesForRecords): # Checks if current loop iteration is the last
Index1 = Content.index(DataForIndex)
Index2 = Content[Index1:].index('</p>')
Index2 = int(Index1)+int(Index2)
Record = Content[Index1:Index2]
self.Records[RecordType.upper()].append(TagPattern.sub('', str(Record).replace('\n', ' ')))
break
Index1 = Content.index(DataForIndex) # Index where DateRegex was matched
Index2 = Content.index(IndexesForRecords[Iter+1]) # Index of next DateRegex match
Record = Content[Index1:Index2] # This gets the full record. This works because
self.Records[RecordType.upper()].append(TagPattern.sub('', str(Record).replace('\n', ' ')))
def Filter(self):
self.RecordsToBeFiltered = {'SOA':[], 'NS':[], 'MX':[], 'A':[],
'AAAA':[], 'CNAME':[], 'PTR':[], 'TXT':[]}
self.RecordsFiltered = {'SOA':[], 'NS':[], 'MX':[], 'A':[],
'AAAA':[], 'CNAME':[], 'PTR':[], 'TXT':[]}
DateRegex = re.compile("(?=\d{4}-\d{2}-\d{2}\s->\s\d{4}-\d{2}-\d{2})")
for RecordType in self.RecordTypes:
self.RecordsToBeFiltered[RecordType] = re.split(DateRegex, ''.join(self.Records[RecordType]))
for RecordType in self.RecordTypes:
for IterCount, Record in enumerate(self.RecordsToBeFiltered[RecordType]):
if Record != "":
if self.UnicodeSupported is True:
Record = str(Record).replace('->', '⇒')
else:
Record = str(Record).replace('->', '->')
if RecordType == "SOA":
Record = Record.replace('<', "").replace('>', '')
NewLineList = ["MName:", "Serial:", "Refresh:", "Retry:", "Expire:"]
for NewLine in NewLineList:
NewLineIndex = Record.index(NewLine)
if NewLine == "MName:":
if IterCount == 0 or IterCount == 1:
Record = f"{Record[:NewLineIndex]}\n{Record[NewLineIndex:]}"
else:
Record = f"\n{Record[:NewLineIndex]}\n{Record[NewLineIndex:]}"
else:
Record = f"{Record[:NewLineIndex]}\n{Record[NewLineIndex:]}"
if RecordType == "NS" or RecordType == "A" or RecordType == "AAAA" or RecordType == "MX":
Index1 = Record.index('<')
Index2 = Record.rindex('>')
Record = f"{Record[:Index1]}{Record[Index2+1:]}"
self.RecordsFiltered[RecordType].append(str(Record))
self.Output()
def Output(self):
self.GettingRecordsLoading.Stop() # Stops loading indicator
os.system(self.ClearScreenCommand)
LongestList = max(self.RecordsFiltered['SOA'], self.RecordsFiltered['NS'], self.RecordsFiltered['MX'], self.RecordsFiltered['A'], self.RecordsFiltered['AAAA'], self.RecordsFiltered['CNAME'], self.RecordsFiltered['PTR'], self.RecordsFiltered['TXT'])
RealLenDict = { # Need this due to the code below that will even out the lists so that they are all of equal length this dictionary is used to compare list length post equalization
"SOA":len(self.RecordsFiltered['SOA']), "NS":len(self.RecordsFiltered['NS']), "MX":len(self.RecordsFiltered['MX']),
"A":len(self.RecordsFiltered['A']), "AAAA":len(self.RecordsFiltered['AAAA']), "CNAME":len(self.RecordsFiltered['CNAME']),
"PTR":len(self.RecordsFiltered['PTR']), "TXT":len(self.RecordsFiltered['TXT'])
}
self.NS_LessThen6 = False
for IterCount, RecordType in enumerate(self.RecordTypes):
if RecordType == "NS": # This code is used to group NS records so they take up the same amount of lines as the corresponding SOA record
if RealLenDict["NS"] < 6:
self.NS_LessThen6 = True
self.RecordsFiltered[RecordType].append('\n'.join(self.RecordsFiltered[RecordType]))
self.RecordsFiltered[RecordType] = self.RecordsFiltered[RecordType][-1:]
for _ in range(len(LongestList) - RealLenDict["NS"]):
self.RecordsFiltered[RecordType].append(" ")
else:
if IterCount == 0: # Need to check this because the first SOA record is only 6 lines
GroupedList = list(self.grouper(self.RecordsFiltered[RecordType], 6, fillvalue=''))
self.RecordsFiltered[RecordType] = GroupedList
else:
GroupedList = list(self.grouper(self.RecordsFiltered[RecordType], 7, fillvalue=''))
self.RecordsFiltered[RecordType] = GroupedList
else:
for _ in range(len(LongestList) - len(self.RecordsFiltered[RecordType])):
self.RecordsFiltered[RecordType] += [" "]
TermSize = os.get_terminal_size()
TermWidth = TermSize.columns
table = Table()
table.show_header = False
table.add_column(style="rgb(255,255,255)", no_wrap=True, width=math.floor(TermWidth/2))
table.add_column(style="rgb(255,255,255)", no_wrap=True, width=math.floor(TermWidth/2))
table.border_style = "rgb(255,255,255)"
table.title_style = "bold"
RecordTupsForTables = ('SOA','NS'),("A","AAAA"),("MX","CNAME"),("PTR","TXT")
for RecordType1, RecordType2 in RecordTupsForTables:
Section1HeaderText = f'{RecordType1} Records'
Section2HeaderText = f'{RecordType2} Records'
Section1HeaderTextLen = len(Section1HeaderText)
Section2HeaderTextLen = len(Section2HeaderText)
LeftColumnWidth = math.floor(TermWidth/2)
RightColumnWidth = math.floor(TermWidth/2)
SectionHeader1BufferInt = math.floor((LeftColumnWidth-Section2HeaderTextLen)/2)
SectionHeader2BufferInt = math.floor((RightColumnWidth-Section1HeaderTextLen)/2)
Section1HeaderSides = '═' * (SectionHeader1BufferInt-4) # Need to adjust
Section2HeaderSides = '═' * (SectionHeader2BufferInt-4) # Need to adjust
SectionHeader1 = f"{Section1HeaderSides} {Section1HeaderText} {Section1HeaderSides}"
SectionHeader2 = f"{Section2HeaderSides} {Section2HeaderText} {Section2HeaderSides}"
# ||| Start of code I will probably remove |||
if len(SectionHeader1) > LeftColumnWidth:
Diff = len(SectionHeader1) - LeftColumnWidth
if Diff == 1:
SectionHeader1 = f"{Section1HeaderSides} {Section1HeaderText} {Section1HeaderSides[:-1]}"
else:
Section1HeaderSides = Section1HeaderSides[:-int(Diff/2)]
SectionHeader1 = f"{Section1HeaderSides} {Section1HeaderText} {Section1HeaderSides}"
if len(SectionHeader2) > RightColumnWidth:
Diff = len(SectionHeader2) - RightColumnWidth
if Diff == 1:
SectionHeader2 = f"{Section2HeaderSides} {Section2HeaderText} {Section2HeaderSides[:-1]}"
else:
Section2HeaderSides = Section2HeaderSides[:-int(Diff/2)]
SectionHeader2 = f"{Section2HeaderSides} {Section2HeaderText} {Section2HeaderSides}"
# ||| End of code I will probably remove |||
table.add_row(Align(SectionHeader1, align="center"), Align(SectionHeader2, align="center"))
table.add_section()
LenRecordType1 = RealLenDict[RecordType1]
LenRecordType2 = RealLenDict[RecordType2]
if LenRecordType1 < 100 and LenRecordType2 < 100:
DisplayRange = max(LenRecordType1, LenRecordType2)
else:
DisplayRange = 100
if LenRecordType1 == 0:
self.RecordsFiltered[RecordType1].insert(0, f"No {RecordType1} Records Found")
if LenRecordType2 == 0:
self.RecordsFiltered[RecordType2].insert(0, f"No {RecordType2} Records Found")
table.add_row('', '') # Table Padding
for RecordType1Item, RecordType2Item in zip(self.RecordsFiltered[RecordType1][:DisplayRange], self.RecordsFiltered[RecordType2][:DisplayRange]):
if RecordType2 == "NS":
if self.NS_LessThen6 != True:
RecordType2Item = '\n'.join(RecordType2Item)
table.add_row(RecordType1Item, RecordType2Item)
else:
table.add_row(RecordType1Item, RecordType2Item)
else:
table.add_row(RecordType1Item, RecordType2Item)
table.add_row('', '')
if RealLenDict[RecordType1] > 100:
LeftRowText = f"There are {RealLenDict[RecordType1] - DisplayRange} More {RecordType1} records."
else:
LeftRowText = ''
if RealLenDict[RecordType2] > 100:
RightRowText = f"There are {RealLenDict[RecordType2] - DisplayRange} More {RecordType2} records."
else:
RightRowText = ''
if LeftRowText != '' or RightRowText != '' or LeftRowText != '' and RightRowText != '':
table.add_row(LeftRowText, RightRowText)
table.add_section() # Table Padding
console = Console()
console.print(table)
self.SaveResults()
def SaveResults(self):
def MakeTXT(SaveFileName:str):
RecordTypeList = [
"SOA",
"NS",
"MX",
"A",
"AAAA",
"CNAME",
"PTR",
"TXT"
]
BannerNameForTXT = self.URL
TableForTXT = Table()
TableForTXT.show_header = False
TableForTXT.add_section()
TableForTXT.add_row(Align(f"Historian DNS Results For {BannerNameForTXT}", align="center"))
TableForTXT.add_section()
for RecordType in RecordTypeList:
if RecordType != "SOA":
TableForTXT.add_section()
TableForTXT.add_row(Align(f'{RecordType} Records', align="center"))
TableForTXT.add_section()
if RecordType != "NS":
for Record in self.RecordsFiltered[RecordType]:
TableForTXT.add_row(f"{Record}")
else:
for RecordsList in self.RecordsFiltered[RecordType]:
for Record in RecordsList:
TableForTXT.add_row(f"{Record}")
from rich import print as rprint
with open(SaveFileName, 'x', encoding='utf-8') as f:
rprint(TableForTXT, file=f)
if self.UserOS == "Linux" or self.UserOS == "MacOS" or self.UserOS == "Unknown":
print('\n')
print(f"{Stamps.Stamp.Output} Saved Results to {os.path.dirname(os.path.abspath(__file__))}/{SaveFileName}")
print('\n')
print(f"{Stamps.Stamp.Info} Exiting")
else:
print('\n')
print(f"{Stamps.Stamp.Output} Saved Results to {os.path.dirname(os.path.abspath(__file__))}\\{SaveFileName}")
print('\n')
print(f"{Stamps.Stamp.Info} Exiting")
def SaveFileNameHandling():
SaveFileName = f"HistorianDNS-{str(self.URL)[:str(self.URL).rindex('.')]}.txt"
if os.path.exists(SaveFileName):
print("\n")
PathAlreadyExistChoice = input(f'{Stamps.Stamp.Error} A file with the name {SaveFileName} already exists. Over Write File [1] | Change Save File Name [2] | Exit [3]: ')
if PathAlreadyExistChoice == "1":
os.remove(SaveFileName)
MakeTXT(SaveFileName=SaveFileName)
elif PathAlreadyExistChoice == "2":
print("\n")
SaveFileName = input(f"{Stamps.Stamp.Input} New File Name: ")
if not SaveFileName.endswith(".txt"):
SaveFileName = f"{SaveFileName}.txt"
MakeTXT(SaveFileName=SaveFileName)
elif PathAlreadyExistChoice == "3":
print(f"{Stamp.Info} Exiting...")
exit()
else:
print('\n')
print(f"{Stamps.Stamp.Error} Invalid Choice")
print("\n")
PathAlreadyExistChoice = input(f'{Stamps.Stamp.Error} A file with the name {SaveFileName} already exists. Over Write File [1] | Change Save File Name [2] | Exit [3]: ')
if PathAlreadyExistChoice == "1":
os.remove(SaveFileName)
MakeTXT(SaveFileName=SaveFileName)
if PathAlreadyExistChoice == "2":
print("\n")
SaveFileName = input(f"{Stamps.Stamp.Input} New File Name: ")
if not SaveFileName.endswith(".txt"):
SaveFileName = f"{SaveFileName}.txt"
MakeTXT(SaveFileName=SaveFileName)
if PathAlreadyExistChoice == "3":
print(f"{Stamp.Info} Exiting...")
exit()
else:
MakeTXT(SaveFileName=SaveFileName)
print('\n')
SaveResultsChoice = input(f"{Stamps.Stamp.Input} Save Results [y/n]: ")
if SaveResultsChoice == "y" or SaveResultsChoice == "Y" or SaveResultsChoice == "yes" or SaveResultsChoice == "Yes":
SaveFileNameHandling()
else:
print('\n')
print(f"{Stamps.Stamp.Info} Exiting...")
exit()
class UI:
"""
This class represents the user interface for a program.
It provides methods to check for an internet connection,
get user input and start the main program.
Attributes:
ClearScreenCommand (str): The command to clear the screen based on the user's operating system.
UserOS (str): The user's operating system.
"""
def __init__(self):
self.ClearScreenCommand = None
if platform.system() == "Windows":
self.UserOS = "Windows"
self.ClearScreenCommand = "cls"
elif platform.system() == "Linux":
self.UserOS = "Linux"
self.ClearScreenCommand = "clear"
elif platform.system() == "Darwin":
self.UserOS = "MacOS"
self.ClearScreenCommand = "clear"
else:
self.UserOS = "Unknown"
self.ClearScreenCommand = None
os.system(self.ClearScreenCommand)
print(f"{Stamp.Info} Checking for an internet connection")
if self.InternetConnection() == False:
RetryChoice = (f"{Stamp.Error} No internet connection try again? [y/n]: ")
if RetryChoice.lower() == "y" or RetryChoice.lower() == "yes":
print('\n')
if self.InternetConnection() == False:
print(f"{Stamp.Error} Retry failed exiting")
exit()
else:
print(f"{Stamp.Info} Exiting")
exit()
else:
os.system(self.ClearScreenCommand)
self.Input()
def InternetConnection(self) -> bool:
try:
requests.get('https://google.com')
return True
except:
return False
def Input(self):
URL = input(f"{Stamp.Input} Please Enter URL: ")
if URL.startswith('https://'):
URL = URL.replace("https://", "")
if URL.startswith("http://"):
URL = URL.replace("http://", "")
print('\n')
print(f"{Stamp.Info} Starting search on url: {URL}")
time.sleep(2)
os.system(self.ClearScreenCommand)
Main(URL=URL)
UI()