-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver-main.py
193 lines (159 loc) · 6.97 KB
/
server-main.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import sys
import random
import time
import hashlib
import re
import json
import datetime
import base64
import os
import thread
import socket
import csv
import operator
import tornado.ioloop
import tornado.web
import tornado.httpclient
import tornado.options
import pygeoip
import urllib
from pyDes import *
from urlparse import urlparse
from tornado.web import asynchronous
from collections import defaultdict
from tornado.options import define, options
#Address of the forecasting server
UDP_IP = "180.179.212.15"
UDP_PORT = 5006
class MainHandler(tornado.web.RequestHandler):
def get(self):
global campaignData
global bidCountIndex
global gi4
start = time.time()
adHeight= self.get_argument('adHeight', True)
requestId= self.get_argument('requestId', True)
adWidth=self.get_argument('adWidth',True)
ip=self.get_argument('ip',True)
pageurl=self.get_argument('pageurl',True)
self.add_header("Content-Type", "text/plain")
bid = False
bidCpm = 0
code = ""
try:
domain = re.sub('www.',r'',str(urlparse(urllib.unquote(pageurl)).netloc))
except:
print "parse exception"
country = gi4.country_code_by_addr(ip).lower()
try:
ronCampaigns = campaignData['display:roe']
except KeyError:
ronCampaigns = list()
try:
black = campaignData['display:roe:black:'+domain]
except KeyError:
black = list()
ronCampaigns = list(set(ronCampaigns) - set(black))
try:
whiteCampaigns = campaignData['display:white:'+domain]
except KeyError:
whiteCampaigns = list()
campaigns = list(set(ronCampaigns+whiteCampaigns))
try:
geoCampaigns = campaignData['display:geo:'+country]
except KeyError:
geoCampaigns = list()
campaigns = list(set(geoCampaigns) & set(campaigns))
size=str(adWidth)+"x"+str(adHeight)
try:
sizeCampaigns = campaignData['display:size:'+size]
except KeyError:
sizeCampaigns = list()
campaigns = list(set(sizeCampaigns) & set(campaigns))
if(len(campaigns)>0):
camplist=[]
for camp in campaigns:
l = [camp, campaignData["display:campaign:"+str(camp)+":bid"],campaignData["display:campaign:"+str(camp)+":pacing"]]
camplist.append(l)
camplist.sort(key=operator.itemgetter(1), reverse=True) # sorts the list in place decending by bids
finalCampaign=0
for camp in camplist:
r=random.randrange(1,100)
if r<camp[2]:
finalCampaign=camp[0]
finalBid=camp[1]
break
if finalCampaign>0:
bid = True
banners = campaignData['display:campaign:'+str(finalCampaign)+':'+str(adWidth)+'x'+str(adHeight)]
randomBannerId = random.choice(banners)
landingPageURL = campaignData['display:campaign:'+str(finalCampaign)+':url']
buyerId=campaignData['display:campaign:'+str(finalCampaign)+':advertiserId']
bidCpm = finalBid
info = base64.b64encode(json.dumps({'e':'pubmatic','d':domain,'bid':randomBannerId,'cid':finalCampaign, 'b':finalBid,"country":country}))
info = info.replace("+","-").replace("/","_").replace("=","")
code="http://rtbidder.impulse01.com/serve?info="+info+"&p={PUBMATIC_SECOND_PRICE}&r=#PCACHEBUSTER&red="
if bid == False :
self.write("requestId="+requestId+"\n")
self.write("bid="+str(bidCpm)+"\n")
else:
self.write("id="+str(random.randrange(1000000,9999999))+"\n")
self.write("bid="+str(bidCpm)+"\n")
self.write("buyer="+str(buyerId)+"\n")
self.write("creativeId="+str(randomBannerId)+"\n")
self.write("creativeHTMLURL="+code+"\n")
self.write("landingPageURL="+landingPageURL+"\n")
self.write("landingPageTLD="+re.sub('www.',r'',str(urlparse(landingPageURL).netloc))+"\n")
self.write("requestId="+requestId+"\n")
timeTaken = time.time() - start
bidCountIndex["Pubmatic"][domain]["DesktopDisplay"][country.upper()][str(adWidth)+'x'+str(adHeight)]["Impressions"] += 1
if int(time.time() - bidCountIndex["Pubmatic"][domain]["DesktopDisplay"][country.upper()][str(adWidth)+'x'+str(adHeight)]["Lastupdate"])>120:
i = bidCountIndex["Pubmatic"][domain]["DesktopDisplay"][country.upper()][str(adWidth)+'x'+str(adHeight)]["Impressions"]
message = json.dumps({"messageType":"Forecast", "message":{"e":"Pubmatic", "d":domain , "c":"DesktopDisplay" ,"geo":country.upper(),
"size":str(adWidth)+'x'+str(adHeight) , "i":i}})
bidCountIndex["Pubmatic"][domain]["DesktopDisplay"][country.upper()][str(adWidth)+'x'+str(adHeight)]["Impressions"]=0
bidCountIndex["Pubmatic"][domain]["DesktopDisplay"][country.upper()][str(adWidth)+'x'+str(adHeight)]["Lastupdate"]=time.time()
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
sock.sendto(message, (UDP_IP, UDP_PORT))
print "\n\n\nInventory Forecast Transmitted\n"+message+"\n\n"
def autovivify(levels=1, final=dict):
return (defaultdict(final) if levels < 2 else defaultdict(lambda: autovivify(levels - 1, final)))
#---------------------Refresh Campaign Index------------------------------------------------
def refreshCache():
global campaignData
http_client = tornado.httpclient.HTTPClient()
try:
response = http_client.fetch("http://terminal.impulse01.com:5003/index?channel=1")
invertedIndex=json.loads(response.body)
except:
invertedIndex=dict()
campaignData=invertedIndex
print options.name+" Refreshed campaign inverted index from http://terminal.impulse01.com:5003/index?channel=1"
#-----------------------------------------------------------------------------------------------
#----------------------Initialize the Tornado Server --------------------------------
define("port", default=8888, help="run on the given port", type=int)
define("name", default="noname", help="name of the server")
define("refreshCache", default=10000, help="millisecond interval between cache refresh", type=int)
#sredisClient = tornadoredis.Client('cookie-tokyo.impulse01.com')
#redisClient.connect()
application = tornado.web.Application([(r".*", MainHandler),])
gi4 = pygeoip.GeoIP('/home/GeoLiteCity.dat', pygeoip.MEMORY_CACHE)
#-----------------------------------------------------------------------------------------------
#---------------------Construct Campaign Index------------------------------------------------
campaignData=dict()
http_client = tornado.httpclient.HTTPClient()
try:
response = http_client.fetch("http://terminal.impulse01.com:5003/index?channel=1")
invertedIndex=json.loads(response.body)
except:
invertedIndex=dict()
campaignData=invertedIndex
print options.name+" Loaded campaign inverted index from http://terminal.impulse01.com:5003/index?channel=1"
#-----------------------------------------------------------------------------------------------
bidCountIndex = autovivify(6, int)
if __name__ == "__main__":
tornado.options.parse_command_line()
application.listen(options.port)
tornado.ioloop.PeriodicCallback(refreshCache, options.refreshCache).start()
tornado.ioloop.IOLoop.instance().start()