-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdoublecode.py
More file actions
executable file
·48 lines (35 loc) · 1.3 KB
/
doublecode.py
File metadata and controls
executable file
·48 lines (35 loc) · 1.3 KB
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
#!/usr/bin/env python
import json
from csv import reader as csvreader
from random import randint
import responder
import httpx
import environ
import redis
SRCDIR = environ.Path(__file__) - 1 # ./
env = environ.Env()
env.read_env(SRCDIR('env-local'))
db = redis.Redis.from_url(env('REDIS_URL'))
if 'CSV_PAIRS_URL' in env:
response = httpx.get(env('CSV_PAIRS_URL').strip(), timeout=25)
csv = csvreader(response.text.splitlines(), delimiter=',')
codes = dict((key.replace('-', ''), val) for key, val in csv)
#codes = dict(csvreader('a,b\nc,d'.splitlines(), delimiter=','))
app = responder.API()
@app.route("/")
async def home(req, resp):
resp.content = app.template('home.html')
@app.route("/getvalue/{key}")
async def get(req, resp, *, key: str):
key = key.replace('-', '')
if key == 'test':
resp.media = {'value': str(randint(0, 9999))}
elif db.get(key) == b'obtained':
resp.media = {'error': 'Этот код уже был использован'}
else:
value = codes.get(key)
if value:
db.set(key, 'obtained') # Отметить как использованый.
resp.media = {'value': value}
if __name__ == '__main__':
app.run(debug=env.bool('WEB_DEBUG', default=False), port=env('PORT', default=8000))