import time, requests
from datetime import datetime


def checkExonoraTor(ip,posix_time):
    """Check if an IP was a TOR exit node at a particular time by querying ExonoraTor.

    Sends the request via https and reads the content returned for a string
    indicating that the IP was in fact an exit node.

    Parameters:
        ip: The IP address being examined.
        posix_time: A POSIX/UNIX timestamp (seconds from epoch)

    Returns:
        True if the response contains 'Result is positive', false otherwise.
    """
    date = datetime.fromtimestamp(posix_time).strftime('%Y-%m-%d')
    url = 'https://exonerator.torproject.org/?ip=' + str(ip) + '&timestamp=' + date + '&lang=en'
    response = requests.get(url).content
    if str(response).find('Result is positive') > 0:
        return True
    elif str(response).find('Result is negative') > 0:
        return False
    print('Could not determine if {} is an exit node.'.format(ip))
    raise UserWarning