-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.py
71 lines (54 loc) · 1.73 KB
/
worker.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import requests
import json
import pprint
import pdb
import datetime
import simplejson
import socket
import struct
import logging
import coloredlogs
API_URL = "http://api.ipstack.com/"
API_KEY = "-PUT-YOUR-KEY-HERE-" # get it free at https://ipstack.com
LIMIT = 2
LISTS = ["gray_list", "white_list"]
OK = "OK"
TRUNC = False # Truncate peers list to LIMIT len
DAEMON = "107.150.28.134:12561" # Monero RPC compatible public node
logging.basicConfig(level=logging.INFO)
coloredlogs.install(fmt="%(message)s")
def ip(addr):
return socket.inet_ntoa(struct.pack("!I", addr))
def geo(ip):
return requests.get("http://api.ipstack.com/%s?access_key=%s" % (ip, API_KEY)).json()
def visit(peer):
return {
"ip": ip(peer["ip"]),
"geo": geo(ip(peer["ip"]))
}
dump = lambda name, data: simplejson.dump(
data, open(name, "w"), default=str, indent=2)
def main():
data = requests.post(
f"{DAEMON}/get_peer_list",
data=json.dumps({}),
headers={"content-type": "application/json"}
).json()
data["updated_at"] = datetime.datetime.utcnow()
if data["status"] == OK:
data["updated_at"] = datetime.datetime.utcnow()
for color in LISTS:
if TRUNC:
logging.warning("Truncating down to %s elements", LIMIT)
data[color] = data[color][0:LIMIT]
logging.info(f"Entering {color} group, {len(data[color])} peers")
group = 0
for peer in data[color]:
group += 1
logging.info(f"{group} / {len(data[color])}")
peer["info"] = visit(peer)
logging.info("Saving data")
dump("data.json", data)
else:
logging.error("Status is not OK")
main()