Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*echo $start_time = time();
- echo "<br>";*/
- error_reporting(E_ALL);
- set_time_limit(0);
- //server config file
- include_once 'configure.php';
- //mongo db select file
- include_once 'MongoDbConnection.php';
- //app file
- include_once 'AppService.php';
- $app_obj = new AppService();
- $mongo_db_conn = new MongoDbConnection();
- $m = $mongo_db_conn->SetDBConn();
- // select a database
- $db = $m->SalesFigure;
- $collection_ads = $db->AdsRevenue;
- $collection_apps = $db->Apps;
- $collection_linked_account = $db->Accounts;
- $collection_cron_master = $db->CronMaster;
- $collection_country_master = $db->CountryMaster;
- $unmatch_country_arr = array();
- $country_temp_result = $collection_country_master->find(array(), array('_id' => 1, 'country_name' => 1, 'country_code' => 1, 'inmobi_country_id' => 1));
- $country_result = iterator_to_array($country_temp_result);
- $country_master = array();
- foreach ($country_result as $country_values) {
- $country_name = strtolower($country_values['country_name']);
- $country_code = strtolower($country_values['country_code']);
- $country_master[$country_name] = $country_values['_id'];
- $country_master[$country_values['inmobi_country_id']] = $country_code;
- }
- $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));
- $app_result = iterator_to_array($app_temp_result);
- $app_master = array();
- $inmobi_interstitial_site_id_arr = array();
- $inmobi_banner_site_id_arr = array();
- $inmobi_video_site_id_arr = array();
- foreach ($app_temp_result as $value) {
- if (!empty($value['inmobi_interstitial_site_id'])) {
- foreach ($value['inmobi_interstitial_site_id'] as $interstitial_val) {
- //$inmobi_interstitial_site_id_arr[$interstitial_val] = (string)$value['_id'];
- $inmobi[$interstitial_val] = $value;
- $inmobi[$interstitial_val]['adtype'] = 'interstitial';
- }
- }
- if (!empty($value['inmobi_banner_site_id'])) {
- foreach ($value['inmobi_banner_site_id'] as $banner_val) {
- // $inmobi_banner_site_id_arr[$banner_val] = (string)$value['_id'];
- $inmobi[$banner_val] = $value;
- $inmobi[$banner_val]['adtype'] = 'banner';
- }
- }
- if (!empty($value['inmobi_video_site_id'])) {
- foreach ($value['inmobi_video_site_id'] as $video_val) {
- //$inmobi_video_site_id_arr[$video_val] = (string)$value['_id'];
- $inmobi[$video_val] = $value;
- $inmobi[$video_val]['adtype'] = 'video';
- }
- }
- }
- $collection_linked_account = $db->Accounts;
- $check_provider_name = 'inmobi';
- $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));
- $link_count = $link_account_data->count();
- if (!empty($link_account_data)) {
- foreach ($link_account_data as $acct) {
- $username = strtolower($acct['store_id']);
- $password = strtolower($acct['store_password']);
- $secretkey = $acct['store_secretkey'];
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, "https://api.inmobi.com/v1.0/generatesession/generate");
- curl_setopt($ch, CURLOPT_HEADER, true);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- $data = array(
- 'username:' . $username,
- 'password:' . $password,
- 'secretKey:' . $secretkey
- );
- curl_setopt($ch, CURLOPT_HTTPHEADER, $data);
- curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
- $output = curl_exec($ch);
- curl_close($ch);
- $arr = explode('sessionId', $output);
- $arr2 = explode(',', $arr['1']);
- $session_id = trim(preg_replace("/[^a-zA-Z0-9]+/", "", $arr2['0']));
- $account_id_arr = explode("accountId", $arr2['1']);
- $account_id = trim(preg_replace("/[^a-zA-Z0-9]+/", "", $account_id_arr['1']));
- $ch1 = curl_init();
- $headers = array(
- 'Content-type:application/json',
- 'accountId:' . $account_id,
- 'secretKey:' . $secretkey,
- 'sessionId:' . $session_id
- );
- $start_date = date('Y-m-d', strtotime('-3 days'));
- $request_arr['reportRequest']['metrics'] = array("clicks", "earnings", "adImpressions", "adRequests");
- $request_arr['reportRequest']['groupBy'] = array("country", "site", "date");
- $request_arr['reportRequest']['timeFrame'] = "{$start_date}:{$start_date}";
- $request_arr['reportRequest']['orderBy'] = array('date');
- $request_arr['reportRequest']['orderType'] = 'asc';
- $request_data = json_encode($request_arr);
- curl_setopt($ch1, CURLOPT_URL, 'https://api.inmobi.com/v1.1/reporting/publisher.json');
- curl_setopt($ch1, CURLOPT_HTTPHEADER, $headers);
- curl_setopt($ch1, CURLOPT_POSTFIELDS, $request_data);
- curl_setopt($ch1, CURLOPT_SSL_VERIFYHOST, 0);
- curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, 0);
- curl_setopt($ch1, CURLOPT_RETURNTRANSFER, TRUE);
- $res = curl_exec($ch1) or exit(curl_error($ch1));
- $response_arr = json_decode($res, true);
- $inmobi_result = $response_arr['respList'];
- if (count($inmobi_result) > 0) {
- $update_counter = 0;
- $unmatched_records_count = 0;
- foreach ($inmobi_result as $inmobi_app_values) {
- $check_country_name = strtolower($inmobi_app_values['country']);
- //$check_country_name = $inmobi_app_values['country'];
- if (empty($country_master[$check_country_name])) {
- if (!in_array($check_country_name, $unmatch_country_arr)) {
- $unmatch_country_arr[$inmobi_app_values['countryId']] = $check_country_name;
- $unmatched_records_count++;
- }
- } else {
- $collection_country_master->update(array('_id' => $country_master[$check_country_name]), array('$set' => array('inmobi_country_id' => $inmobi_app_values['countryId'])));
- $update_counter++;
- }
- }
- }
- $adsrevenue = array();
- $adsrevenue['adprovider'] = 'inmobi';
- $adsrevenue['provider'] = 'inmobi';
- $adsrevenue['provider_id'] = (string) $acct['_id'];
- if (!empty($inmobi_result)) {
- foreach ($inmobi_result as $result) {
- $adsrevenue['_id'] = new MongoId();
- $adsrevenue['adrevenue'] = $result['earnings'];
- $adsrevenue['date'] = new MongoDate(strtotime($result['date']));
- $adsrevenue['request'] = $result['adRequests'];
- $adsrevenue['clicks'] = $result['clicks'];
- $adsrevenue['fill_rate'] = 0;
- $adsrevenue['impressions'] = $result['adImpressions'];
- $adsrevenue['ecpm'] = 0;
- $adsrevenue['ctr'] = 0;
- $adsrevenue['rpi'] = 0;
- $adsrevenue['sync_date'] = new MongoDate(strtotime(date('Y-m-d')));
- $adsrevenue['country'] = $country_master[$result['countryId']];
- if (array_key_exists($result['siteId'], $inmobi)) {
- echo "Success=" . $result['siteId'] . "\n";
- $adsrevenue['app_id'] = $inmobi[$result['siteId']]['_id'];
- $adsrevenue['platform'] = $inmobi[$result['siteId']]['platform'];
- $adsrevenue['adtype'] = $inmobi[$result['siteId']]['adtype'];
- $collection_ads->insert($adsrevenue);
- } else {
- $fp = fopen('boombit.records.log', 'w+');
- fwrite($fp, 'Site Id not found==' . $result['siteId'] . "\n");
- //echo "Not found ".$result['siteId']."\n";
- }
- // print_r($adsrevenue);
- //$collection_ads->insert($adsrevenue);
- }
- }
- }
- }
- //$request_data='{"reportRequest": {"metrics":["clicks","earnings","adImpressions","adRequests"],"timeFrame":"2016-02-15:2016-02-16","groupBy":["country","site","date"],"orderBy":["date"],"orderType": "asc"}}';
- //echo "<pre>";
- /*echo "Records updated =".$update_counter."\n";
- echo "Records unmatched =".$unmatched_records_count."\n";
- print_r($unmatch_country_arr);
- exit;*/
- /********* Import Ads revenue ****************/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement