
import requests
import hashlib
import sys



def request_api_data(query_char):
  '''requests our data and gives us a response'''
  url = 'https://api.pwnedpasswords.com/range/' + query_char
  res = requests.get(url)
  if res.status_code != 200:    #check our response if the status is 200
    raise RuntimeError(f'Error fetching: {res.status_code}, check the api and try again')
  return res

def get_password_leaks_count(hashes, hash_to_check):
  '''loop through all the hashes for checking the parameter 'hash_to_check' and returns the count '''
  hashes = (line.split(':') for line in hashes.text.splitlines())
  for h, count in hashes:
    if h == hash_to_check:
      return count
  return 0

def pwned_api_check(password):
  '''check password if it exists in API'''
  sha1password = hashlib.sha1(password.encode('utf-8')).hexdigest().upper()   #hashing the password using SHA-1 algorithm and returning query char
  first5_char, tail = sha1password[:5], sha1password[5:]
  response = request_api_data(first5_char)
  return get_password_leaks_count(response, tail)

def main(args):
  '''recieves the arguments that we give in command line'''
  for password in args:
    count = pwned_api_check(password)
    if count:
      print(f'{password} was found {count} times... you should probably change your password!')
    else:
      print(f'{password} was NOT found. Carry on!')
  return '!!!-----Done----!!!'

if __name__ == '__main__':
  sys.exit(main(sys.argv[1:]))    #exiting out of the file 

