import datetime import gzip import uuid import cProfile pstats io impo

  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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import datetime
import gzip
import uuid
# import cProfile, pstats, io
import uwsgi
from urllib.parse import parse_qs
import errors as exceptions
from handlers import bid, impression, install, click, win
import timers
import settings
from logger import log
def application(env, start_response):
#pr = cProfile.Profile()
#pr.enable()
# ... do something ...
uwsgi.set_logvar('request_id', str(uuid.uuid4()))
now = datetime.datetime.utcnow()
response = ""
path = env['PATH_INFO']
payload = env['wsgi.input'].read()
if env.get("HTTP_CONTENT_ENCODING", "") == "gzip":
payload = gzip.decompress(payload)
# log.debug(payload.decode("utf8"))
if path.startswith("/bidrequest/"):
try:
# if settings.SLOWDOWN:
# slowdown()
ssp_name = path[len("/bidrequest/"):]
response = bid.handle_request(ssp_name, payload, now)
start_response('200 OK', [('Content-Type','application/json; charset=utf-8'), ('Content-Length', str(len(response)))])
except UnicodeDecodeError:
start_response('204 No Content', [('Content-Length', '0')])
except exceptions.NoBid:
start_response('204 No Content', [('Content-Length', '0')])
except exceptions.NoBidBase:
start_response('204 No Content', [('Content-Length', '0')])
except ValueError as e:
log.warn(f"Invalid JSON: {e}")
start_response('204 No Content', [('Content-Length', '0')])
elif path.startswith("/trk/v2/win"):
try:
params = parse_qs(env['QUERY_STRING'])
auction_id = params.get("auction_id")[0]
price = params.get("auction_price")[0]
timestamp = params.get("time")[0]
win.handle_request(auction_id, price, timestamp)
start_response('200 Ok', [('Content-Length', '0')])
except KeyError:
log.warn(f"Bad event: {path} - {env['QUERY_STRING']}")
start_response('200 Ok', [('Content-Length', '0')])
response = ""
elif path.startswith("/trk/v3/impression"):
try:
params = parse_qs(env['QUERY_STRING'])
price = params.get("auction_price")[0]
auction_id = params.get("auction_id")[0]
timestamp = params.get("time")[0]
impression.handle_request(auction_id, price, timestamp)
start_response('200 Ok', [('Content-Length', '0')])
except KeyError:
try:
params = parse_qs(env['QUERY_STRING'])
timestamp = params.get("time")[0]
delta = datetime.datetime.utcnow() - datetime.datetime.utcfromtimestamp(int(timestamp))
log.warn(f"Bad event: {path} {auction_id} days: {delta.days}")
start_response('200 Ok', [('Content-Length', '0')])
except TypeError:
log.warn(f"Bad event: {path}")
start_response('200 Ok', [('Content-Length', '0')])
response = ""
elif path.startswith("/trk/v3/click"):
try:
params = parse_qs(env['QUERY_STRING'])
auction_id = params.get("auction_id")[0]
timestamp = params.get("time")[0]
market_link = click.handle_request(auction_id, timestamp)
log.debug(f"Redirected to: {market_link}")
start_response('302 Found', [('Content-Length', '0'), ('Location', market_link)])
except KeyError:
try:
params = parse_qs(env['QUERY_STRING'])
auction_id = params.get("auction_id")[0]
timestamp = params.get("time")[0]
delta = datetime.datetime.utcnow() - datetime.datetime.utcfromtimestamp(int(timestamp))
log.warn(f"Bad event: {path} {auction_id} days: {delta.days}")
start_response('200 Ok', [('Content-Length', '0')])
except TypeError:
log.warn(f"Bad event: {path}")
start_response('200 Ok', [('Content-Length', '0')])
response = ""
elif path.startswith("/trk/v4/click"):
try:
params = parse_qs(env['QUERY_STRING'])
auction_id = params.get("auction_id")[0]
timestamp = params.get("time")[0]
market_link = click.handle_request(auction_id, timestamp)
start_response('200 Ok', [('Content-Length', '0')])
except KeyError:
try:
params = parse_qs(env['QUERY_STRING'])
auction_id = params.get("auction_id")[0]
timestamp = params.get("time")[0]
delta = datetime.datetime.utcnow() - datetime.datetime.utcfromtimestamp(int(timestamp))
log.warn(f"Bad event: {path} {auction_id} days: {delta.days}")
start_response('200 Ok', [('Content-Length', '0')])
except TypeError:
log.warn(f"Bad event: {path}")
start_response('200 Ok', [('Content-Length', '0')])
response = ""
elif path.startswith("/install") or path.startswith("//install"):
try:
params = parse_qs(env['QUERY_STRING'])
revenue = params.get("revenue")[0]
auction_id = params.get("postback-parameter")[0]
install.handle_request(auction_id, revenue)
start_response('200 Ok', [('Content-Length', '0')])
except KeyError:
log.warn(f"Bad event: {path}")
start_response('200 Ok', [('Content-Length', '0')])
response = ""
else:
start_response('200 Ok', [('Content-Length', '0')])
#pr.disable()
#s = io.StringIO()
#sortby = 'cumulative'
#ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
#ps.print_stats()
#print(s.getvalue())
yield response