0

I have some code that I need to use to ingest a file containing emails, and interact with the 'haveibeenpwned' API to return dates of when they have been seen in public dumps.

So far this is what I have below and I am currently struggling with implementing the following:

1.) Each request must be delayed by 1 second, per the API usage guidelines.

2.) The script must take in a file which will contain the emails to be submitted (I tried invoking it with a for-loop on command line but that did not work).

3.) The email addresses submitted must be echoed to the output file for correlating with the breach data.

Essentially I am looking to run the script, supply an input file (probably with argparse) and then have the script output the following example contents to a file, being appended as it runs through the supplied list of emails...

email1@email.com

DATA - Breached on xxxx-xx-xx

email2@email.com

DATA - Breached on xxxx-xx-xx

Any help or suggestions with the required changes would be much appreciated. You can see its current functionality by running it with '-a' and supplying an email address.

import argparse
import json
import requests


def accCheck(acc):
    r = requests.get(
            'https://haveibeenpwned.com/api/v2/breachedaccount/%s?truncateResponse=false' % acc
        )
    data = json.loads(r.text)
    for i in data:
        print(
            i['Name'] +
            ' - Breached on ' +
            i['BreachDate']
        )

def list():
    r = requests.get(
            'https://haveibeenpwned.com/api/v2/breaches'
        )
    data = json.loads(r.text)
    for i in data:
        print(
            i['Name'] +
            ' - Breached on - ' +
            i['BreachDate']
        )

if __name__ == '__main__':
    ap = argparse.ArgumentParser()
    ap.add_argument(
        '-a',
        '--account'
    )
    ap.add_argument(
        '--list',
        action='store_true'
    )
    args = ap.parse_args()
    acc = args.account
    list = args.list
    if not list:
        if not acc:
            print(
                'Specify an account (-a <email>) to check or --list for site info'
            )
            exit(0)
        try:
            accCheck(acc)
        except ValueError:
            print(
                'No breach data found'
            )
    else:
        list()
Dronzil
  • 31
  • 1
  • 4
  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the [How to Ask](https://stackoverflow.com/help/how-to-ask) page for help clarifying this question. – Francesco Montesano Jul 02 '17 at 07:54
  • Answers to your questions: 1) https://stackoverflow.com/questions/510348/how-can-i-make-a-time-delay-in-python 2) https://stackoverflow.com/questions/3277503/how-do-i-read-a-file-line-by-line-into-a-list 3) https://stackoverflow.com/questions/4706499/how-do-you-append-to-a-file – lispsil Jul 02 '17 at 08:01
  • So to clarify, I am primarily trying to figure out how I would be able to use this script against a text file of email addresses. I want to loop through each of the email addresses and output all results to a file. When I have done this before It would replace the contents and not append, but for what I am trying to achieve I want to iterate through each email in the supplied text file and have each result appended to the output file. – Dronzil Jul 02 '17 at 08:10

1 Answers1

0

You could accept the input file and output file as command line arguments like this:

ap.add_argument(
    '-if',
    '--input_file'
    )

ap.add_argument(
    '-of',
    '--ouput_file'
    )
args = ap.parse_args()

input_file = args.input_file

ouput_file = args.ouput_file

Assuming that the file you specified as your input file contains an email account on each line , you could do the following :

with open(input_file,'r') as fl,RedirectStdoutTo(ouput_file):
        for acc in fl:
            try:
                accCheck(acc.rstrip())
            except ValueError:
                print(
                    'No breach data found'
                )

RedirectStdoutTo is a class you would write to allow you to redirect your print output to a file

class RedirectStdoutTo:
    def __init__(self, filename):
        self.out_new = open(filename,'w')

    def __enter__(self):
        self.out_old = sys.stdout
        sys.stdout = self.out_new

    def __exit__(self, *args):
        sys.stdout = self.out_old
        self.out_new.close()
Sanjucta
  • 127
  • 1
  • 6