· 10 years ago · Mar 12, 2016, 02:39 PM
1<?php
2
3/*echo $start_time = time();
4echo "<br>";*/
5error_reporting(E_ALL);
6set_time_limit(0);
7
8//server config file
9include_once 'configure.php';
10
11//mongo db select file
12include_once 'MongoDbConnection.php';
13
14//app file
15include_once 'AppService.php';
16$app_obj = new AppService();
17
18
19$mongo_db_conn = new MongoDbConnection();
20$m = $mongo_db_conn->SetDBConn();
21
22// select a database
23$db = $m->SalesFigure;
24$collection_ads = $db->AdsRevenue;
25$collection_apps = $db->Apps;
26$collection_linked_account = $db->Accounts;
27$collection_cron_master = $db->CronMaster;
28$collection_country_master = $db->CountryMaster;
29
30$unmatch_country_arr = array();
31
32$country_temp_result = $collection_country_master->find(array(), array('_id' => 1, 'country_name' => 1, 'country_code' => 1, 'inmobi_country_id' => 1));
33$country_result = iterator_to_array($country_temp_result);
34$country_master = array();
35foreach ($country_result as $country_values) {
36 $country_name = strtolower($country_values['country_name']);
37 $country_code = strtolower($country_values['country_code']);
38 $country_master[$country_name] = $country_values['_id'];
39 $country_master[$country_values['inmobi_country_id']] = $country_code;
40}
41
42
43
44$app_temp_result = $collection_apps->find(array(), array('_id' => 1, 'app_name' => 1, 'platform' => 1, 'inmobi_interstitial_site_id' => 1, 'inmobi_banner_site_id' => 1, 'inmobi_video_site_id' => 1, 'platform' => 1));
45$app_result = iterator_to_array($app_temp_result);
46$app_master = array();
47
48
49$inmobi_interstitial_site_id_arr = array();
50$inmobi_banner_site_id_arr = array();
51$inmobi_video_site_id_arr = array();
52
53foreach ($app_temp_result as $value) {
54 if (!empty($value['inmobi_interstitial_site_id'])) {
55 foreach ($value['inmobi_interstitial_site_id'] as $interstitial_val) {
56 //$inmobi_interstitial_site_id_arr[$interstitial_val] = (string)$value['_id'];
57 $inmobi[$interstitial_val] = $value;
58 $inmobi[$interstitial_val]['adtype'] = 'interstitial';
59 }
60 }
61 if (!empty($value['inmobi_banner_site_id'])) {
62 foreach ($value['inmobi_banner_site_id'] as $banner_val) {
63 // $inmobi_banner_site_id_arr[$banner_val] = (string)$value['_id'];
64 $inmobi[$banner_val] = $value;
65 $inmobi[$banner_val]['adtype'] = 'banner';
66 }
67 }
68 if (!empty($value['inmobi_video_site_id'])) {
69 foreach ($value['inmobi_video_site_id'] as $video_val) {
70 //$inmobi_video_site_id_arr[$video_val] = (string)$value['_id'];
71 $inmobi[$video_val] = $value;
72 $inmobi[$video_val]['adtype'] = 'video';
73 }
74 }
75}
76
77$collection_linked_account = $db->Accounts;
78$check_provider_name = 'inmobi';
79
80$link_account_data = $collection_linked_account->find(array('check_provider_name' => $check_provider_name, 'status' => 'active'), array('_id' => 1, 'store_id' => 1, 'store_name' => 1, 'store_password' => 1, 'nick_name' => 1,'store_secretkey'=>1));
81$link_count = $link_account_data->count();
82
83
84if (!empty($link_account_data)) {
85 foreach ($link_account_data as $acct) {
86 $username = strtolower($acct['store_id']);
87 $password = strtolower($acct['store_password']);
88 $secretkey = $acct['store_secretkey'];
89
90 $ch = curl_init();
91 curl_setopt($ch, CURLOPT_URL, "https://api.inmobi.com/v1.0/generatesession/generate");
92 curl_setopt($ch, CURLOPT_HEADER, true);
93 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
94 $data = array(
95 'username:' . $username,
96 'password:' . $password,
97 'secretKey:' . $secretkey
98 );
99 curl_setopt($ch, CURLOPT_HTTPHEADER, $data);
100 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
101 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
102 $output = curl_exec($ch);
103 curl_close($ch);
104
105 $arr = explode('sessionId', $output);
106 $arr2 = explode(',', $arr['1']);
107
108 $session_id = trim(preg_replace("/[^a-zA-Z0-9]+/", "", $arr2['0']));
109 $account_id_arr = explode("accountId", $arr2['1']);
110 $account_id = trim(preg_replace("/[^a-zA-Z0-9]+/", "", $account_id_arr['1']));
111
112 $ch1 = curl_init();
113 $headers = array(
114 'Content-type:application/json',
115 'accountId:' . $account_id,
116 'secretKey:' . $secretkey,
117 'sessionId:' . $session_id
118 );
119
120
121 $start_date = date('Y-m-d', strtotime('-3 days'));
122
123
124 $request_arr['reportRequest']['metrics'] = array("clicks", "earnings", "adImpressions", "adRequests");
125 $request_arr['reportRequest']['groupBy'] = array("country", "site", "date");
126 $request_arr['reportRequest']['timeFrame'] = "{$start_date}:{$start_date}";
127 $request_arr['reportRequest']['orderBy'] = array('date');
128 $request_arr['reportRequest']['orderType'] = 'asc';
129
130
131
132 $request_data = json_encode($request_arr);
133 curl_setopt($ch1, CURLOPT_URL, 'https://api.inmobi.com/v1.1/reporting/publisher.json');
134 curl_setopt($ch1, CURLOPT_HTTPHEADER, $headers);
135 curl_setopt($ch1, CURLOPT_POSTFIELDS, $request_data);
136 curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 0);
137 curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, 0);
138 curl_setopt($ch1, CURLOPT_RETURNTRANSFER, TRUE);
139 $res = curl_exec($ch1) or exit(curl_error($ch1));
140 $response_arr = json_decode($res, true);
141 $inmobi_result = $response_arr['respList'];
142
143
144
145 if (count($inmobi_result) > 0) {
146 $update_counter = 0;
147 $unmatched_records_count = 0;
148 foreach ($inmobi_result as $inmobi_app_values) {
149 $check_country_name = strtolower($inmobi_app_values['country']);
150 //$check_country_name = $inmobi_app_values['country'];
151 if (empty($country_master[$check_country_name])) {
152 if (!in_array($check_country_name, $unmatch_country_arr)) {
153 $unmatch_country_arr[$inmobi_app_values['countryId']] = $check_country_name;
154 $unmatched_records_count++;
155 }
156 } else {
157 $collection_country_master->update(array('_id' => $country_master[$check_country_name]), array('$set' => array('inmobi_country_id' => $inmobi_app_values['countryId'])));
158 $update_counter++;
159 }
160 }
161 }
162
163 $adsrevenue = array();
164 $adsrevenue['adprovider'] = 'inmobi';
165 $adsrevenue['provider'] = 'inmobi';
166 $adsrevenue['provider_id'] = (string) $acct['_id'];
167
168
169 if (!empty($inmobi_result)) {
170 foreach ($inmobi_result as $result) {
171 $adsrevenue['_id'] = new MongoId();
172 $adsrevenue['adrevenue'] = $result['earnings'];
173 $adsrevenue['date'] = new MongoDate(strtotime($result['date']));
174 $adsrevenue['request'] = $result['adRequests'];
175 $adsrevenue['clicks'] = $result['clicks'];
176 $adsrevenue['fill_rate'] = 0;
177 $adsrevenue['impressions'] = $result['adImpressions'];
178 $adsrevenue['ecpm'] = 0;
179 $adsrevenue['ctr'] = 0;
180 $adsrevenue['rpi'] = 0;
181 $adsrevenue['sync_date'] = new MongoDate(strtotime(date('Y-m-d')));
182 $adsrevenue['country'] = $country_master[$result['countryId']];
183 if (array_key_exists($result['siteId'], $inmobi)) {
184
185 echo "Success=" . $result['siteId'] . "\n";
186 $adsrevenue['app_id'] = $inmobi[$result['siteId']]['_id'];
187 $adsrevenue['platform'] = $inmobi[$result['siteId']]['platform'];
188 $adsrevenue['adtype'] = $inmobi[$result['siteId']]['adtype'];
189 $collection_ads->insert($adsrevenue);
190 } else {
191 $fp = fopen('boombit.records.log', 'w+');
192 fwrite($fp, 'Site Id not found==' . $result['siteId'] . "\n");
193 //echo "Not found ".$result['siteId']."\n";
194 }
195
196
197
198 // print_r($adsrevenue);
199 //$collection_ads->insert($adsrevenue);
200 }
201 }
202 }
203}
204
205
206
207
208
209
210
211
212
213
214//$request_data='{"reportRequest": {"metrics":["clicks","earnings","adImpressions","adRequests"],"timeFrame":"2016-02-15:2016-02-16","groupBy":["country","site","date"],"orderBy":["date"],"orderType": "asc"}}';
215
216
217
218
219
220
221
222//echo "<pre>";
223/*echo "Records updated =".$update_counter."\n";
224echo "Records unmatched =".$unmatched_records_count."\n";
225print_r($unmatch_country_arr);
226exit;*/
227
228
229
230/********* Import Ads revenue ****************/