set() provides uniquness, with open() automatically close()es

Use set() to get only unique elements of a list.
Use `or` in assignments to deal with empty values as in `x = s or "<no text!>"`
Use `with open() as` syntax to get close() to happen automatically.
Python had a built-in object called file so avoid using that as a variable name.
Avoid naked exceptions.  See https://realpython.com/blog/python/the-most-diabolical-python-antipattern/
This commit is contained in:
cclauss 2016-04-20 23:25:45 +02:00
parent aacf27ae36
commit cc4e9eb3d1

View file

@ -91,10 +91,7 @@ class myparser:
return emails return emails
def unique(self): def unique(self):
self.new = [] self.new = list(set(self.temp))
for x in self.temp:
if x not in self.new:
self.new.append(x)
return self.new return self.new
@ -160,15 +157,11 @@ def red(text):
return colored(text, 'red', attrs=['bold']) return colored(text, 'red', attrs=['bold'])
def unique(data): def unique(data):
unique = [] return list(set(data))
for x in data:
if x not in unique:
unique.append(x)
return unique
def checkProxyUrl(url): def checkProxyUrl(url):
url_checked = urlparse(url) url_checked = urlparse(url)
if ((url_checked.scheme != 'http') & (url_checked.scheme != 'https')) | (url_checked.netloc == ''): if (url_checked.scheme not in ('http', 'https')) | (url_checked.netloc == ''):
raise argparse.ArgumentTypeError('Invalid {} Proxy URL (example: http://127.0.0.1:8080).'.format(url)) raise argparse.ArgumentTypeError('Invalid {} Proxy URL (example: http://127.0.0.1:8080).'.format(url))
return url_checked return url_checked
@ -214,31 +207,24 @@ if __name__ == '__main__':
sys.exit() sys.exit()
args = parser.parse_args() args = parser.parse_args()
if not args.domain:
domain = ""
if(args.domain):
domain = args.domain
else:
print(red("[-] Please specify a domain name to search.")) print(red("[-] Please specify a domain name to search."))
sys.exit(2) sys.exit(2)
domain = args.domain
userAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1" userAgent = (args.uagent or
if(args.uagent): "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:40.0) Gecko/20100101 Firefox/40.1")
userAgent = args.uagent
print("User-Agent in use: {}".format(yellow(userAgent))) print("User-Agent in use: {}".format(yellow(userAgent)))
if(args.proxy): if args.proxy:
print("Proxy server in use: {}".format(yellow(args.proxy.scheme + "://" + args.proxy.netloc))) print("Proxy server in use: {}".format(yellow(args.proxy.scheme + "://" + args.proxy.netloc)))
filename = "" filename = args.filename or ""
if(args.filename):
filename = args.filename
limit = args.limit limit = args.limit
engine = args.engine engine = args.engine
googleUrl = "http://www.google.com/search?num=100&start={counter}&hl=en&q=%40\"{word}\"" googleUrl = 'http://www.google.com/search?num=100&start={counter}&hl=en&q=%40"{word}"'
bingUrl = "http://www.bing.com/search?q=%40{word}&count=50&first={counter}" bingUrl = "http://www.bing.com/search?q=%40{word}&count=50&first={counter}"
askUrl = "http://www.ask.com/web?q=%40{word}" askUrl = "http://www.ask.com/web?q=%40{word}"
yahooUrl = "http://search.yahoo.com/search?p=%40{word}&n=100&ei=UTF-8&va_vt=any&vo_vt=any&ve_vt=any&vp_vt=any&vd=all&vst=0&vf=all&vm=p&fl=0&fr=yfp-t-152&xargs=0&pstart=1&b={counter}" yahooUrl = "http://search.yahoo.com/search?p=%40{word}&n=100&ei=UTF-8&va_vt=any&vo_vt=any&ve_vt=any&vp_vt=any&vd=all&vst=0&vf=all&vm=p&fl=0&fr=yfp-t-152&xargs=0&pstart=1&b={counter}"
@ -289,38 +275,34 @@ if __name__ == '__main__':
all_emails = unique(all_emails) all_emails = unique(all_emails)
print(green("\n\n[+] Emails found:")) print(green("\n\n[+] Emails found:"))
print(green("------------------")) print(green("-" * 18))
if all_emails == []: if not all_emails:
print(red("No emails found")) print(red("No emails found"))
sys.exit(3) sys.exit(3)
else: else:
for emails in all_emails: for emails in all_emails:
print(emails) print(emails)
if filename != "": if filename:
try: try:
print(green("[+] Saving files...")) print(green("[+] Saving files..."))
file = open(filename, 'w') with open(filename, 'w') as out_file:
for email in all_emails: for email in all_emails:
try: try:
file.write(email + "\n") out_file.write(email + "\n")
except: except:
print(red("Exception " + email)) print(red("Exception " + email))
pass
file.close
except Exception as e: except Exception as e:
print(red("Error saving TXT file: " + e)) print(red("Error saving TXT file: " + e))
try: try:
filename = filename.split(".")[0] + ".xml" filename = filename.split(".")[0] + ".xml"
file = open(filename, 'w') with open(filename, 'w') as out_file:
file.write('<?xml version="1.0" encoding="UTF-8"?><EmailHarvester>') out_file.write('<?xml version="1.0" encoding="UTF-8"?><EmailHarvester>')
for x in all_emails: for x in all_emails:
file.write('<email>' + x + '</email>') out_file.write('<email>{}</email>'.format(x))
file.write('</EmailHarvester>') out_file.write('</EmailHarvester>')
file.flush()
file.close()
print(green("Files saved!")) print(green("Files saved!"))
except Exception as er: except Exception as er:
print(red("Error saving XML file: " + er)) print(red("Error saving XML file: " + er))