#!/usr/bin/env python

import requests, sys
import paho.mqtt.publish as publish


#print(sys.argv[1])
res = requests.get('http://ipwho.is/'+ sys.argv[1]).json()
# print(res)
print(res['ip'])
print(res['latitude'])
print(res['longitude'])

msgs = [ {"topic": "name", "payload": res['ip'] },
    {"topic": "lat", "payload": res['latitude'] },
    {"topic": "lon", "payload": res['longitude'] } ]

print(msgs)
publish.multiple(msgs, hostname="mqtt.33co.de", port=8883, tls={'ca_certs':"/etc/ssl/certs/ca-certificates.crt"})

# in case of problem check SELinux
# many t=runs to release ports, access
# audit2allow -w -a
# audit2allow  -a
# audit2allow  -a -M fail2ban_t
# semodule -i fail2ban_t.pp

# or just run setsebool 0


# to get the location  curl http://ipwho.is/197.26.19.254
# curl http://ipwho.is/197.26.19.254 |  jq -r '.latitude,  .longitude'
# guide to create custom action
# https://webcp.io/custom-fail2ban-action/
