add exception for no connection to the password or mail api

This commit is contained in:
koksnuss 2019-07-14 17:07:47 +02:00
parent bfb35b2517
commit 78b38a25b9

View File

@ -23,6 +23,7 @@
#
#TODO
# - add feature: keepass integration? isnt there something like this already?
# - handle mail API exeption more precisely
from os import name
@ -31,7 +32,7 @@ from re import match
from argparse import ArgumentParser
from hashlib import sha1
from getpass import getpass
from requests import get
import requests
if name == 'nt': # disable colors for windows environment
@ -80,12 +81,23 @@ def check_mail_or_password(user_input):
query_password(user_input)
def no_connection_to(url):
''' Warn about no connection to *url* and exit '''
stdout.write(RED)
print('Could not connect to {}, please check your internet connection'.format(url))
stdout.write(RESET)
exit(1)
def query_password(password):
''' Query the first 5 digits of the sha1 hash of the user input
ti the haveibeenpwned.com api. '''
password_hash = sha1(password.encode('UTF-8')).hexdigest().upper()
request = password_hash[:5]
response = get(PASSWORD_API + request).text
try:
response = requests.get(PASSWORD_API + request).text
except requests.exceptions.ConnectionError:
no_connection_to(PASSWORD_API)
hash_searched = 'not yet'
for answer in response.splitlines():
data = answer.split(':')
@ -109,7 +121,7 @@ def query_password(password):
def query_mail(mail):
try:
response = get(MAIL_API + mail).json()
response = requests.get(MAIL_API + mail).json()
for breach in response:
print('Name: {}'.format(breach['Name']))
print('Title: {}'.format(breach['Title']))
@ -127,6 +139,8 @@ def query_mail(mail):
print('Sensitive: {}'.format('yes' if breach['IsSensitive'] == 1 else 'No'))
print('Retired: {}'.format('yes' if breach['IsRetired'] == 1 else 'No'))
print('Spam: {}'.format('yes' if breach['IsSpamList'] == 1 else 'No'))
except requests.exceptions.ConnectionError:
no_connection_to(MAIL_API)
except:
stdout.write(GREEN)
print('The mail adress {} is not in the leak database.'.format(mail))