"""Loud Detective is an aptly named program that collects basic information about a URL.
leverages nmap to do a simple port scan"""

import sys
import socket
import json
import requests
import nmap

TARGET = str(sys.argv[1])
scan_v = nmap.PortScanner()
ports = [21, 22, 23, 80, 137, 139, 443, 445, 8089, 8080]
r = requests.get("https://"+TARGET)
gethostby_ = socket.gethostbyname(sys.argv[1])
req_two = requests.get("https://ipinfo.io/"+gethostby_+"/json")
resp_ = json.loads(req_two.text)
print("Gathering basic information about: "+TARGET)
portscan = scan_v.scan(resp_['ip'], str(ports))
print("Host", TARGET, " is ", portscan['scan'][resp_['ip']]['status']['state'])
print("IP Addresss: "+resp_['ip'])
print("Hostname: "+resp_['hostname'])
print('Organization: '+resp_['org'])
print("Location: "+resp_['loc'])
print("Region: "+resp_['region'])
print("City: "+resp_['city'])
print("Country: "+resp_['country'])
print("Timezone: "+resp_['timezone'])
print("Open Ports (and in the future, services) for "+TARGET)
print("Scanning", TARGET, "on ports 21, 22, 80, 137, 139, 443, 445, 8080, 8089")
for port in ports:
    portscan = scan_v.scan(resp_['ip'], str(port))
    print("Port ", port, "is ", portscan['scan'][resp_['ip']]['tcp'][port]['state'])
