5
5
import logging
6
6
from urlparse import urlparse
7
7
8
- # CONFIG (see to put this in any other part; aka not global)
9
- from render_utils import incrementor
10
- RENDER_MAP = {
11
- 'auction_id' : incrementor (12345678 )}
12
- BODY_TEMPLATES = ['plugin/mopub/mopub_body1.tmpl' ,
13
- 'plugin/mopub/mopub_body2.tmpl' ]
14
- HEH_ENDPOINT_TMPL = "http://localhost:8080/impression/${exchange}/${AUCTION_ID}/${AUCTION_PRICE}?impid=${AUCTION_IMP_ID}"
15
- AD_SERVER_ENDPOINT_TMPL = "http://localhost:8080/events?ev=imp&aid=${AUCTION_ID}&apr=${AUCTION_PRICE}&sptid=${AUCTION_IMP_ID}"
16
- USE_HEH_ENDPOINT = False
17
- HTTP_RESOURCE = 'mopub'
18
-
19
8
20
9
class OpenRTBPlugin (ParameterPlugin ):
21
10
'''
@@ -24,34 +13,42 @@ class OpenRTBPlugin(ParameterPlugin):
24
13
def __init__ (self ):
25
14
self .request_body_templates = []
26
15
self .render_map = {}
27
- self .tmpl_notif = ''
16
+ self .tmpl_imp_notif = ''
28
17
29
- def initialize (self , adserver ):
18
+ def initialize (self , adserver , config ):
30
19
31
20
self .adserver = adserver
21
+ self .http_resource = config ['http_resource' ]
22
+ self .exchange = config ['exchange' ]
32
23
33
- self . render_map = RENDER_MAP
34
- self .request_body_templates_files = BODY_TEMPLATES
24
+ # Map to render the bid request body templates
25
+ self .render_map = config [ 'render_map' ]
35
26
36
- # Create templates...
27
+ # Create templates fot the bid requests...
28
+ self .request_body_templates_files = config ['req_body_templates' ]
37
29
for filename in self .request_body_templates_files :
38
30
with open (filename ) as f :
39
31
logging .info ('Using file template %s' % filename )
40
32
tmpl = Template ('' .join (f .readlines ()))
41
33
self .request_body_templates .append (tmpl )
42
34
43
- # Template for notification endpoint
44
- if USE_HEH_ENDPOINT :
45
- self .tmpl_notif_file = HEH_ENDPOINT_TMPL
35
+ # Templates for notification endpoint
36
+ if config ['use_heh_endpoint' ] :
37
+ self .tmpl_imp_notif_file = config ['heh_endpt_imp_tmpl' ]
38
+ self .tmpl_click_notif_file = config ['heh_endpt_click_tmpl' ]
46
39
else :
47
- self .tmpl_notif_file = AD_SERVER_ENDPOINT_TMPL
48
- self .tmpl_notif = Template (self .tmpl_notif_file )
40
+ self .tmpl_imp_notif_file = config ['adserver_endpt_imp_tmpl' ]
41
+ self .tmpl_click_notif_file = config ['adserver_endpt_click_tmpl' ]
42
+
43
+ # Create the templates for the notifications
44
+ self .tmpl_imp_notif = Template (self .tmpl_imp_notif_file )
45
+ self .tmpl_click_notif = Template (self .tmpl_click_notif_file )
49
46
50
47
def get_request (self ):
51
- # We need to return a request line, a map of headers and a body string
48
+ # We need to return a request line, a map of headers and a body
52
49
53
50
# Create the request line
54
- req_line = 'POST /%s HTTP/1.1' % HTTP_RESOURCE
51
+ req_line = 'POST /%s HTTP/1.1' % self . http_resource
55
52
56
53
# Set the headers
57
54
headers = {}
@@ -81,19 +78,53 @@ def receive_response(self, status_code, headers, body):
81
78
js = json .loads (body )
82
79
logging .debug ("Response received :" )
83
80
logging .debug (str (js ))
84
- price = js [ 'seatbid' ][ 0 ][ 'bid' ][ 0 ][ 'price' ]
85
- auction_id = js [ 'id' ]
86
- spot_id = js [ 'seatbid' ][ 0 ][ 'bid' ][ 0 ][ 'impid' ]
81
+ price = self . get_auction_price ( js )
82
+ auction_id = self . get_auction_id ( js )
83
+ spot_id = self . get_auction_impression_id ( js )
87
84
88
85
# With that data, create the notification...
89
86
notif_render = { 'AUCTION_PRICE' : price ,
90
87
'AUCTION_ID' : auction_id ,
91
88
'AUCTION_IMP_ID' : spot_id ,
92
- 'exchange' : 'mopub' }
93
- url = self .tmpl_notif .substitute (notif_render )
89
+ 'exchange' : self .exchange }
90
+
91
+ # Win notification...
92
+ url = self .tmpl_imp_notif .substitute (notif_render )
94
93
self .__send_impression_notification (url )
94
+
95
+ # Click notification...
96
+ click_url = self .tmpl_click_notif .substitute (notif_render )
97
+ self .__send_click_notification (click_url )
98
+
95
99
return (False , '' , {}, '' )
96
100
101
+ def get_auction_price (self , json_response ):
102
+ return json_response ['seatbid' ][0 ]['bid' ][0 ]['price' ]
103
+
104
+ def get_auction_id (self , json_response ):
105
+ return json_response ['id' ]
106
+
107
+ def get_auction_impression_id (self , json_response ):
108
+ return json_response ['seatbid' ][0 ]['bid' ][0 ]['impid' ]
109
+
110
+ def __send_click_notification (self , url ):
111
+
112
+ # Only generate clicks for the 2% of the cases
113
+ if random .random < 0.98 :
114
+ return
115
+
116
+ parsed_url = urlparse (url )
117
+ req_line = 'GET %s?%s HTTP/1.1' % (parsed_url .path ,
118
+ parsed_url .query )
119
+ headers = {}
120
+ headers ['Host' ] = 'localhost'
121
+ heads = self .__headers_to_str (headers )
122
+ buf = '%s\r \n %s\r \n ' % (req_line , heads )
123
+
124
+ # send the impression event in 0.1 secs
125
+ logging .debug ("Sending click notification :" )
126
+ logging .debug (buf )
127
+ self .adserver .send_event (buf , 0.1 )
97
128
98
129
def __send_impression_notification (self , url ):
99
130
parsed_url = urlparse (url )
@@ -105,7 +136,7 @@ def __send_impression_notification(self, url):
105
136
buf = '%s\r \n %s\r \n ' % (req_line , heads )
106
137
107
138
# send the impression event in 0.1 secs
108
- logging .debug ("Sending notification :" )
139
+ logging .debug ("Sending impression notification :" )
109
140
logging .debug (buf )
110
141
self .adserver .send_event (buf , 0.1 )
111
142
@@ -115,9 +146,8 @@ def __headers_to_str(self, headers):
115
146
heads += '%s: %s\r \n ' % (k , v )
116
147
return heads
117
148
118
-
119
149
def receive_win_response (self , status_code , headers , body ):
120
150
logging .debug ('received_win_response' )
121
151
122
152
def do (self , watcher , revents ):
123
- logging .info ('doing...' )
153
+ logging .debug ('doing...' )
0 commit comments