-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
executable file
·134 lines (106 loc) · 4.24 KB
/
app.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#!/usr/bin/env python
import os, sys, argparse, logging, json, time
import requests
def backfill():
# First, we get the latest round
latest = get_latest_round()
# Now, we get the range of all rounds from 0
ranges = range(1, latest)
for i in ranges:
log.info("Processing round: {}".format(i))
# Download the round
d = get_round(i)
# Publish it, but only for a non-error state
if d != None:
if args.cf:
sendto_cf(d)
# Update the pointer
l = d['round']
else:
log.info("Error downloading round {}. Attempting again in {} seconds".format(r, args.delay))
# And wait for the next round
time.sleep(int(args.delay))
def get_round(i):
try:
r = requests.get('https://{}/public/{}'.format(args.drand, i), timeout=2)
if r.status_code == 200:
r = r.json()
if r['round'] == i:
return r
else:
log.debug("{} did not match the expected output, this round may not exist".format(i))
return None
else:
return None
except requests.exceptions.ReadTimeout:
return None
def get_latest_round():
try:
r = requests.get('https://{}/public/latest'.format(args.drand), timeout=2).json()
return r['round']
except:
return None
def sendto_cf(d):
try:
r = requests.put(
"https://api.cloudflare.com/client/v4/accounts/{}/storage/kv/namespaces/{}/values/{}".format(
args.cf_account,
args.cf_namespace,
"{}.{}".format(args.drand.replace(".", ""), d['round'])
),
headers={
'Authorization': "Bearer {}".format(args.cf_token)
},
data=json.dumps(d)
)
except:
return None
if __name__ == '__main__':
parser = argparse.ArgumentParser()
# drand server
parser.add_argument("-d", "--drand", help="drand server to archive", default=os.getenv("DRAND_SERVER", "drand.cloudflare.com"))
# modes
parser.add_argument("-b", "--backfill", help="backfill workers KV with the results of all rounds", action="store_true")
parser.add_argument("-sv", "--server", help="run a server to update the archive as live", action="store_true")
# config
parser.add_argument("-dl", "--delay", help="pause between each check", default=os.getenv("DELAY", "5"))
# storage
## cloudflare (workers kv)
parser.add_argument("--cf", help="store data using Cloudflare Workers KV", action="store_true")
parser.add_argument("--cf-account", help="cloudflare account id", default=os.getenv("CLOUDFLARE_ACCOUNT", ""))
parser.add_argument("--cf-token", help="cloudflare api token", default=os.getenv("CLOUDFLARE_TOKEN", ""))
parser.add_argument("--cf-namespace", help="cloudflare kv namespace", default=os.getenv("CLOUDFLARE_NAMESPACE", ""))
## s3
# Verbose mode
parser.add_argument("--verbose", "-v", help="increase output verbosity", action="store_true")
args = parser.parse_args()
if args.verbose:
logging.basicConfig(level=logging.DEBUG)
else:
logging.basicConfig(level=logging.INFO)
log = logging.getLogger(__name__)
if args.backfill:
backfill()
exit()
if args.server:
l = get_latest_round()
while True:
# Grab the latest round
r = get_latest_round()
if r > l:
# We have a new round
log.info("Downloading next round: {}".format(r))
# Download it
d = get_round(r)
# Publish it, but only for a non-error state
if d != None:
if args.cf:
sendto_cf(d)
# Update the pointer
l = d['round']
else:
log.info("Error downloading round {}. Attempting again in {} seconds".format(r, args.delay))
else:
log.info("Round has not proceeded yet, still at round {}".format(r))
# And wait for the next round
time.sleep(int(args.delay))