<?php   
use Stichoza\GoogleTranslate\GoogleTranslate;
global $opt_themes;  		
require_once get_template_directory().'/libs/addons/inc/ssl.php';
ini_set('display_errors', ERRORS);   
@ini_set('user_agent', 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)');   
@ini_set('user_agent', 'My-Application/2.5');

$Go_Curl		= new Go_Curl();		
$allowed_status	= [200, 201]; // or whatever indicates success
		
$language		= $opt_themes['apk_language'];  
$gp			= "https://play.google.com/store/apps/details?id=".$titleId."&hl=".$language;
$gp_en_usa		= "https://play.google.com/store/apps/details?id=".$titleId."&hl=en-US";
//$gp1		= $Go_Curl->geturl($gp);
//$gp_en_us		= $Go_Curl->geturl($gp_en_usa); 
//$get_infos	= scrape_site($gp);
//$get_infos	= file_get_html($gp); 
//$get_infos	= file_get_contents($gp);
//$get_infos	= file_get_contents_curl($gp);
//$gp_en_us		= scrape_site($gp_en_usa); 
//$gp_en_us		= file_get_html($gp_en_usa);   
//$gp_en_us		= file_get_contents($gp_en_usa); 
//$gp_en_us		= get_linkz($gp_en_usa); 
$get_infos		= get_linkz($gp); 
$gp_en_us		= file_get_contents_curl($gp_en_usa); 

		
if($opt_themes['bahasa_on']){

$tr			= new GoogleTranslate(); 
$bahasa		= $opt_themes['bahasa'];  

}

//$links		= scrape_site($url);	
//$links		= file_get_html($url);	
//$links		= file_get_contents($url, false, stream_context_create($ssl));
//$links		= $Go_Curl->geturl($url);	 
		
/*
https://stackoverflow.com/a/76784298
Using PHP curl how does one get the response body for a 400 response

<base href="https://play.google.com/">

*/ 
 
##### ID ##### 
$arr['apk_id']			= $Go_Curl->match('/<link rel="alternate" href=".*?\?id=(.*?)" hreflang="x-default">/', $get_infos, 1);
$arr['base']			= $Go_Curl->match('/<base href="(.*?)">/', $get_infos, 1);
 
if ( !empty($arr['base']) ) {
	
##### download apkgk ##### 
//$apkgk_dw_url			= "https://apkgk.com/".$titleId."/download";
//$apkgk_dw_urls			= scrape_site($apkgk_dw_url); 
//$apkgk_dw_urls			= file_get_contents($apkgk_dw_url, false, stream_context_create($ssl));
//$apkgk_dw_urls			= scrape_site($apkgk_dw_url);

/* 
$arr['link_download_gps']	= $Go_Curl->$Go_Curl->match_all('/<a.*?href="(.*?)"><i.*?><\/i>.*?<\/a>/ms', $Go_Curl->match('/<div class="c-download">(.*?)<\/div>/ms', $apkgk_dw_urls, 1), 1);  

$arr['name_download_gps']	= $Go_Curl->$Go_Curl->match_all('/<a.*?title="Download(.*?)".*?>.*?<\/a>/ms', $Go_Curl->match('/<div class="c-download">(.*?)<\/div>/ms', $apkgk_dw_urls, 1), 1);

$arr['size_download_gps']	= $Go_Curl->$Go_Curl->match_all('/<a.*?><i.*?<span>.*?\((.*?)\).*?<\/span><\/a>/ms', $Go_Curl->match('/<div class="c-download">(.*?)<\/div>/ms', $apkgk_dw_urls, 1), 1);

##### download aptoide ##### 
$aptoide_dw			= "https://en.aptoide.com/download?package_name=".$titleId."&store_name=aptoide-web"; 
$aptoide_dl			= file_get_html($aptoide_dw); 
//$aptoide_dl		= scrape_site($aptoide_dw);
 
$arr['link_download_aptoide']	= $Go_Curl->match('/<div.*?><span.*?>If the download didn.*?, <a.*?href="(.*?)">click here.<\/a><\/span><\/div>/ms', $aptoide_dl, 1) ;

$arr['link_download_aptoide1']	= $Go_Curl->match('/<meta http-equiv="refresh" content=".*?\;(.*?)"\/>/ms', $aptoide_dl, 1) ;
https://exthem.es/apk.php?pkg=
*/

$url2 			= 'https://apks.exthem.es/?pkgid='.$titleId; 
$url4 			= 'https://demos.web.id/?pkgid='.$titleId; 
$url 				= 'https://a-a.my.id/gps.php?pkgid='.$titleId; 
$url3 			= 'https://zplay.demos.web.id//gps/?pkgid='.$titleId; 
//$g_data			= file_get_contents_curl($url);
//$g_data			= get_linkz($url);
//$g_data			= scrape_site($url);
//$g_data			= scraperapi($url);
//$g_data			= get_url($url);
$g_data			= @file_get_html($url);

$l_apkcombo 		= 'https://apkcombo.com/genericApp/'.$titleId;  
$g_apkcombo			= @file_get_html($l_apkcombo);  
$apkcombo			= new simple_html_dom();
$apkcombo->load($g_apkcombo);	


$arr['AppCategory']	= $Go_Curl->match('/<script type="application.*?>.*?"@type": "MobileApplication".*?"applicationCategory": "(.*?)".*?<\/script>/', $apkcombo, 1);


$arr['SubCategory']	= $Go_Curl->match('/<script type="application.*?>.*?"@type": "MobileApplication".*?"applicationSubCategory": "(.*?)".*?<\/script>/', $apkcombo, 1);
    
$judul			= $Go_Curl->match('/<title>(.*?)<\/title>/ms', $g_data, 1); 
$arr['new_version']	= $Go_Curl->match('/<div class="version">(.*?)<\/div>/ms', $g_data, 1);  
$arr['new_size']		= $Go_Curl->match('/<div class="size">(.*?)<\/div>/ms', $g_data, 1);  
$arr['new_youtube']	= $Go_Curl->match('/<div class="youtube">(.*?)<\/div>/ms', $g_data, 1);  
$arr['new_desck_GP']	= $Go_Curl->match('/<div class="description">(.*?)<\/div>/ms', $g_data, 1);  
$arr['new_youtube2']	= $Go_Curl->match('/<video.*?poster="https:\/\/i.ytimg.com\/vi\/(.*?)\/hqdefault.jpg".*?>/', $get_infos, 1); 

$tagsx			= $Go_Curl->match_all('/<div.*?data-idom-class="Rj2Mlf OLiIxf PDpWxe P62QJc LQeN7 LMoCf">.*?<a.*?aria-label="(.*?)".*?><\/a>/ms', $Go_Curl->match('/<div class="Uc6QCc">(.*?)<\/section>/ms', $get_infos, 1), 1);
$tags				= implode(", ", $tagsx);
$arr['tag'] 		= $tags;

//print_r($tags);

//print_r($arr['new_version']);

//echo cst_sanitize_title_with_dashes('War Machines：Tanks Battle Game');
//echo '<pre>';
//print_r($arr['new_version']);
//print_r($arr['new_size']);
//print_r($arr['new_youtube']);
//print_r($arr['new_desck_GP']);
//echo '</pre>';

require_once get_template_directory().'/libs/addons/includes/nelaxa.php';
}

$arr['apk_name']	= $Go_Curl->match('/<h1><span.*?itemprop="name">(.*?)<\/span><\/h1>/', $get_infos, 1);

//if ( !empty($arr['apk_name']) ) {
//$arr['title_GP']	= $arr['apk_name'];
//} else {
//$arr['title_GP']	= $arr['app_name'];
//}
$arr['title_GP']   	= $arr['apk_name'] ? $arr['apk_name'] : $arr['name'];
		
$arr['apk_poster']	= $Go_Curl->match('/<meta property="og:image".*?content="(.*?)".*?>/', $get_infos, 1);

//if ( !empty($arr['apk_poster']) ) {
//$arr['poster_GP']	= $arr['apk_poster'];	 	
//} else {
//$arr['poster_GP']	= $arr['poster_sc'];
//} 

$arr['poster_GP']   	= $arr['geticon'] ? $arr['geticon'] : $arr['apk_poster'];

//if ( !empty($arr['appversion'] )) {
//$arr['version_GP']	= $arr['appversion']; 
//} elseif ( $arr['version_sc'] ) {
//$arr['version_GP']	= $arr['version_sc'];
//} 
$arr['version_GP']   	= $arr['appversion'] ? $arr['appversion'] : $arr['new_version'];


//$arr['version_GP'] = (empty($_POST['avg_rating']) ? 5 : $_POST['avg_rating'] );
//print_r($arr['appversion']);
//print_r($arr['new_size']);

//if ( !empty($arr['developer']) ) {
//$arr['developers_GP']	= $arr['developer']; 
//} else {
//$arr['developers_GP']	= $arr['developer_sc'];
//} 
$arr['developers_GP']   = $arr['developer'] ? $arr['developer'] : $arr['developer_sc'];

//if ( !empty($arr['android']) ) {
//$arr['requires_GP']	= $arr['android']; 
//} else {
//$arr['requires_GP']	= $arr['android_sc'];
//}  
$arr['requires_GP']   	= $arr['android'] ? $arr['android'] : $arr['android_sc']; 

//if ( !empty($arr['youtube']) ) {
//$arr['youtube_GP'] 	= $arr['youtube']; 
//} elseif ( !empty($arr['new_youtube2']) ) {
//$arr['youtube_GP']	= $arr['new_youtube2'];
//} else {
//$arr['youtube_GP']	= $arr['new_youtube'];
//} 
$arr['youtube_GP']   	= $arr['youtube'] ? $arr['youtube'] : $arr['new_youtube2']; 
  
//if ( !empty($arr['installs']) ) {
//$arr['installs_GP']	= $arr['installs'];
//} else {
//$arr['installs_GP']	= '';
//} 
$arr['installs_GP']   	= $arr['installs'] ? $arr['installs'] : ''; 

if ( !empty($arr['base']) ) {
//$arr['rated_GP']	= trim(strip_tags($Go_Curl->match('/<div class="jILTFe">(.*?)<\/div>/msi', $get_infos, 1)));
//$arr['rated_GP']	= str_replace(',', '.', $arr['rated_GP']);

$arr['rated_GP']		= $arr['numberrates'];

/* $arr['ratings_GP']	= trim(strip_tags($Go_Curl->match('/<div class="T4LgNb ".*?>.*?<script.*?,"ratingCount":"(.*?)"},.*?<\/script>/msi', $get_infos, 1)));  */

$arr['ratings_GP']	= $arr['numbervoters']; 

}

$arr['apk_updates']	= trim(strip_tags($Go_Curl->match('/<div><div class="lXlx5">.*?<\/div><div class="xg1aie">(.*?)<\/div><\/div>/msi', $get_infos, 1)));
$arr['apk_updates']	= date("M d, Y", strtotime($arr['apk_updates']));

//if ( !empty($arr['apk_updates']) ) {
//$arr['updates_GP']	= $arr['apk_updates'];
//$arr['updates_GP'] 	= date("M d, Y", strtotime($arr['apk_updates']));
//} else {
//$arr['updates_GP']	= $arr['updated_sc'];
//} 
//$arr['updates_GP']   = $arr['getUpdated'] ? $arr['getUpdated'] : $arr['updated_sc']; 
$arr['updates_GP']   	= $arr['getUpdated'] ?: $arr['getUpdated'] ?: $arr['apk_updates'] ?: $arr['updated_sc'];
 
 
//if ( !empty($arr['getsize']) ) {
//$arr['sizes_GP']	= $arr['getsize']; 
//} elseif ( !empty($arr['size_sc'] )) {
//$arr['sizes_GP']	= $arr['size_sc'];
//} else {
//$arr['sizes_GP']	= $arr['new_size'];	
//}
$arr['sizes_GP']   	= $arr['getsize'] ? $arr['getsize'] : $arr['new_size']; 

/* 
print_r($arr['getsize']);
print_r($arr['size_sc']);
print_r($arr['new_size']);
*/

if ( !empty($arr['cover']) ) {
$arr['backgrounds_GP']	= $arr['cover'];
}

if ( !empty($arr['base']) ) {
/* $arr['images_GP']	= $Go_Curl->$Go_Curl->match_all('/<img src="(.*?)".*?>/ms', $Go_Curl->match('/<div class="aoJE7e qwPPwf" jsname="K9a4Re" role="list" jsslot>(.*?)<c-data.*?/ms', $get_infos, 1), 1); */

$arr['images_GP']		= $arr['getScreenshots'];

}

if ( !empty($arr['category']) ) {
$arr['genres_GP']	= $arr['category'];
//$arr['genres_GP']	= str_replace('&', '',  $arr['genres_GP']); 
}

if ( !empty($arr['base']) ) {
$arr['paid_GP']		= trim(strip_tags($Go_Curl->match('/<span class="oocvOe">.*?<button aria-label=".*?Buy".*?">(.*?)<\/button>.*?<\/span>/msi', $get_infos, 1)));
$arr['paid_GP']		= preg_replace('/\D/', '', $arr['paid_GP']);
$arr['paid_GP1']		= trim(strip_tags($Go_Curl->match('/<button aria-label=".*?Buy".*?">(.*?)<\/button>/msi', $gp_en_us, 1)));
$arr['paid_GP2']		= trim(strip_tags($Go_Curl->match('/<button aria-label=".*?Buy".*?">(.*?)<\/button>/msi', $gp_en_us, 1)));
$arr['paid_GP2']		= preg_replace('/.*?Buy/is', 'Paid',  $arr['paid_GP2']);
$arr['paid_GP3']		= trim(strip_tags($Go_Curl->match('/<span class="oocvOe">.*?<button aria-label=".*?Buy".*?">(.*?)<\/button>.*?<\/span>/msi', $gp_en_us, 1)));
}


if ( !empty($arr['base']) ) {
$arr['articlebody_GPs']	= $Go_Curl->match('/<div class="bARER".*?>(.*?)<\/div>.*?<div class="TKjAsc">/msi', $get_infos, 1);
$arr['articlebody_GPs']	= preg_replace('/<a.*?">(.*?)<\/a>/is', '$1',  $arr['articlebody_GPs']);
$arr['articlebody_GPs']	= str_replace(array('<b>', '</b>', '<div>', '</div>'), '', $arr['articlebody_GPs']);
$arr['articlebody_GPs']	= preg_replace('/\shref=[\'|"][^\'"]+[\'|"]/', '', $arr['articlebody_GPs']);
$arr['articlebody_GPs']	= preg_replace('/\starget=[\'|"][^\'"]+[\'|"]/', '', $arr['articlebody_GPs']);
$arr['articlebody_GPs']	= preg_replace('/\sclass=[\'|"][^\'"]+[\'|"]/', '', $arr['articlebody_GPs']);
$arr['articlebody_GPs']	= preg_replace('/\sid=[\'|"][^\'"]+[\'|"]/', '', $arr['articlebody_GPs']);
$arr['articlebody_GPs']	= preg_replace('/\sstyle=[\'|"][^\'"]+[\'|"]/', '', $arr['articlebody_GPs']);
$arr['articlebody_GPs']	= preg_replace('/<script.*?\/script>/', '', $arr['articlebody_GPs']); 
$arr['articlebody_GPs']	= preg_replace('/<[^\/>]*>([\s]?)*<\/[^>]*>/', '', $arr['articlebody_GPs']);
$arr['articlebody_GPs']	= preg_replace('/<p[^>]*><\\/p[^>]*>/', '', $arr['articlebody_GPs']);

$arr['articlebody_GP']	= $arr['articlebody_GPs'] ? $arr['articlebody_GPs'] : $arr['description']; ;

/* if ( $arr['articlebody_GP'] === FALSE or $arr['articlebody_GP'] == '' ) $arr['articlebody_GP'] = $arr['description']; */

##### Desc summary #####
$arr['desck_GP2']		= substr(trim(strip_tags($Go_Curl->match('/<meta.*?itemprop="description".*?content="(.*?)">/msi', $get_infos, 1))),0,160);
//$arr['desck_GP']	= $arr['new_desck_GP'];
$arr['desck_GP']   	= $arr['summary'] ? $arr['summary'] : $arr['desck_GP2']; 
}


if ( !empty($arr['base']) ) {
if($opt_themes['bahasa_on']){
if ( !empty($arr['articlebody_GP']) ) {
$arr['articlebody_GP']	= $tr->setSource()->setTarget($bahasa)->translate($arr['articlebody_GP']); 
$arr['articlebody_GP']	= str_replace('< ', '<', $arr['articlebody_GP']); 
$arr['articlebody_GP']	= str_replace('< /', '</', $arr['articlebody_GP']); 
$arr['articlebody_GP']	= str_replace('</ ', '</', $arr['articlebody_GP']); 
$arr['articlebody_GP']	= str_replace(' >', '>', $arr['articlebody_GP']); 
}

if ( !empty($arr['desck_GP']) ) {
$arr['desck_GP']		= $tr->setSource()->setTarget($bahasa)->translate($arr['desck_GP']); 
$arr['desck_GP']		= str_replace('< ', '<', $arr['desck_GP']); 
$arr['desck_GP']		= str_replace('< /', '</', $arr['desck_GP']); 
$arr['desck_GP']		= str_replace('</ ', '</', $arr['desck_GP']); 
$arr['desck_GP']		= str_replace(' >', '>', $arr['desck_GP']); 
}

if ( !empty($arr['summary']) ) {
$arr['summary']		= $tr->setSource()->setTarget($bahasa)->translate($arr['summary']); 
$arr['summary']		= str_replace('< ', '<', $arr['summary']); 
$arr['summary']		= str_replace('< /', '</', $arr['summary']); 
$arr['summary']		= str_replace('</ ', '</', $arr['summary']); 
$arr['summary']		= str_replace(' >', '>', $arr['summary']); 
}

} 
 
$arr['whatnews_GP2']	= $Go_Curl->match('/What.*?new<\/h2>.*?<div itemprop="description">(.*?)<\/div>.*?<\/div>.*?<\/section>/msi', $get_infos, 1); 

$arr['whatnews_GP']   	= $arr['whatnews_GP2'] ? $arr['whatnews_GP2'] : $arr['recentchanges']; 

if($opt_themes['bahasa_on']){
if ( !empty($arr['whatnews_GP']) ) {
$arr['whatnews_GP']	= $tr->setSource()->setTarget($bahasa)->translate($arr['whatnews_GP']); 
$arr['whatnews_GP']	= str_replace('< ', '<', $arr['whatnews_GP']); 
$arr['whatnews_GP']	= str_replace('< /', '</', $arr['whatnews_GP']); 
$arr['whatnews_GP']	= str_replace('</ ', '</', $arr['whatnews_GP']); 
$arr['whatnews_GP']	= str_replace(' >', '>', $arr['whatnews_GP']); 
}

if ( !empty($arr['recentchanges']) ) {
$arr['recentchanges']	= $tr->setSource()->setTarget($bahasa)->translate($arr['recentchanges']); 
$arr['recentchanges']	= str_replace('< ', '<', $arr['recentchanges']); 
$arr['recentchanges']	= str_replace('< /', '</', $arr['recentchanges']); 
$arr['recentchanges']	= str_replace('</ ', '</', $arr['recentchanges']); 
$arr['recentchanges']	= str_replace(' >', '>', $arr['recentchanges']); 
}

}

}