<?php 
use Stichoza\GoogleTranslate\GoogleTranslate;
class scrapes{	
	public function scrape_web_info($sources) {	
		require_once get_template_directory().'/libs/addons/inc/ssl.php';
		@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');
		@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');
		global $opt_themes;
		
		$Go_Curl		= new Go_Curl();	
		if($opt_themes['bahasa_on']){

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

		}
		
		$url			= $_POST['wp_url'];
		
		$arr			= array();
		
		//$links		= scrape_site($url);	
		$links			= @file_get_html($url);	
		//$links		= file_get_contents($url, false, stream_context_create($ssl));
		//$links		= $Go_Curl->geturl($url);	 
		
		 
		$arr['id_ps']			= $Go_Curl->match('/<span class="info__APKDetail.*?"><strong>Package.*?<\/strong> <!-- -->(.*?)<\/span>/ms', $links, 1);
		
		$arr['title_id']		= $Go_Curl->match('/<span class="info__APKDetail.*?"><strong>Package.*?<\/strong> <!-- -->(.*?)<\/span>/ms', $links, 1);
		$arr['title_id_alt']	= $Go_Curl->match('/<span class="info__APKDetail.*?"><strong>Package.*?<\/strong> <!-- -->(.*?)<\/span>/ms', $links, 1);
		
		$arr['GP_ID']			= $arr['title_id'];
		$arr['GP_ID_alt']		= $arr['title_id_alt'];
		
		$arr['GP_IDX']			= $arr['title_id'];
		$arr['GP_IDX_alt']		= $arr['title_id_alt'];
		
        if ($arr['id_ps'] === FALSE or $arr['id_ps'] == '') $arr['id_ps'] = $arr['title_id'];	
		if ($arr['GP_ID'] === FALSE or $arr['GP_ID'] == '') $arr['GP_ID'] = $arr['GP_ID_alt'];
        if ($arr['GP_ID'] === FALSE or $arr['GP_ID'] == '') $arr['GP_ID'] = $arr['GP_IDX'];	
		  
		if($arr['title_id']){
		$title_id				= $arr['title_id'];
		} else {
		$title_id				= $arr['title_id_alt'];
		}				 
  
		$titleId				= $title_id;
		
		$arr['title_GP']		= str_replace(array('&#8211;', '-', 'Download'), '', $Go_Curl->match('/<h2 class="info__APKHeaderTitle.*?">(.*?)\ - APK.*?<\/h2>/ms', $links, 1));
		$arr['title_GP']		= preg_replace('/\s+/', ' ', $arr['title_GP']);
		$arr['title']			= $arr['title_GP'];		
		 
		$arr['poster_GP']		= $Go_Curl->match('/<div class="app-informations__AppCard.*?"><img.*?src="(.*?)\?w.*?" class="app-informations__AppIcon.*?">.*?<\/div>/ms', $links, 1);
		$arr['poster_web']		= $arr['poster_GP'];		
		$poster_web				= $arr['poster_web'];		

		$arr['images_GP']		= $Go_Curl->match_all('/"url":"(.*?)"/ms', $Go_Curl->match('/<script id="__NEXT_DATA__".*?>.*?"screenshots":.*?{(.*?)},"stats":.*?<\/script>/ms', $links, 1), 1);

		$arr['backgrounds_GP']	= $Go_Curl->match('/<script id="__NEXT_DATA__".*?>.*?"pageProps":{"versions":.*?{"id":.*?,"uname":".*?","icon":".*?","graphic":"(.*?)\",.*?"date":".*?","name":".*?","store":"aptoide-web","vername":".*?","vercode":.*?,"downloads":.*?,"size":.*?,"trusted":.*?,"parsedName":".*?","appcoins":.*?}.*?<\/script>/msi', $links, 1);
		 
		
		$arr['articlebody_GP']	= $Go_Curl->match('/<div itemprop="description" class="details__DescriptionParagraphs-sc.*? csCwnn">(.*?)<div class="details__NewContainer-sc.*? iAkypZ"/msi', $links, 1); 
		$arr['articlebody_GP']	= preg_replace('/\shref=[\'|"][^\'"]+[\'|"]/', '', $arr['articlebody_GP']);
		$arr['articlebody_GP']	= preg_replace('/\starget=[\'|"][^\'"]+[\'|"]/', '', $arr['articlebody_GP']);
		$arr['articlebody_GP']	= preg_replace('/\sclass=[\'|"][^\'"]+[\'|"]/', '', $arr['articlebody_GP']);
		$arr['articlebody_GP']	= preg_replace('/\sid=[\'|"][^\'"]+[\'|"]/', '', $arr['articlebody_GP']);  
		$arr['articlebody_GP']	= preg_replace('/<([^<\/>]*)>([\s]*?|(?R))<\/\1>/', '', $arr['articlebody_GP']);
		$arr['articlebody_GP']	= preg_replace('/<script.*?\/script>/', '', $arr['articlebody_GP']);
		$arr['articlebody_GP']	= str_replace('<br/></p>', '</p>'.PHP_EOL, $arr['articlebody_GP']); 
		$arr['articlebody_GP']	= preg_replace('/<[^\/>]*>([\s]?)*<\/[^>]*>/', '', $arr['articlebody_GP']); //hilangkan <p> kosong
		$arr['articlebody_GP']	= preg_replace('/<p[^>]*><\\/p[^>]*>/', '', $arr['articlebody_GP']); //hilangkan <p> kosong

		$arr['articlebody_GP']	= str_replace(array('<div>', '</div>' ), ' ', $arr['articlebody_GP'] );
		
		
		$arr['desck_GP']		= substr($arr['articlebody_GP'].'.', 0, strpos($arr['articlebody_GP'], '.')); // stop setelah titik pertama
		 
		$arr['version_GP']		= $Go_Curl->match('/<span class="info__APKDetail.*?"><strong>APK Version<!-- -->:<\/strong> <!-- -->(.*?)<\/span>/msi', $links, 1); 
		$arr['version_GP_alt']	= $Go_Curl->match('/<script id="__NEXT_DATA__".*?>.*?"pageProps":{"versions".*?{.*?"vername":"(.*?)".*?"appcoins":true}.*?<\/script>/msi', $links, 1); 
		if ($arr['version_GP'] === FALSE or $arr['version_GP'] == '') $arr['version_GP'] = $arr['version_GP_alt'];
		
		$arr['version']			= $arr['version_GP']; 	
		$arr['genres_GP']			= $Go_Curl->match('/<script id="__NEXT_DATA__".*?>.*?"groups".*?{.*?"title":"(.*?)","parent":{.*?}}.*?<\/script>/msi', $links, 1); 
		
		$arr['genres_GP']		= unicode_conv($arr['genres_GP']); 	
		$arr['genres_web']		= $arr['genres_GP']; 	
		
		$arr['developers_GP']	= $Go_Curl->match('/<script id="__NEXT_DATA__".*?>.*?"developer":{"id".*?"name":"(.*?)","website".*?}.*?<\/script>/msi', $links, 1); 
		$arr['developer_web']	= $arr['developers_GP']; 	
		
		$arr['sizes_GP_alt']	= $Go_Curl->match('/<script id="__NEXT_DATA__".*?>.*?,"file":{"vername":".*?","vercode":.*?,"md5sum":".*?","filesize":(.*?),"added":.*?<\/script>/msi', $links, 1); 
		$arr['sizes_GP_alt']	= formatBytes($arr['sizes_GP_alt']);	
		$arr['sizes_GP']		= $Go_Curl->match('/<span itemprop="fileSize".*?>(.*?)<\/span>/msi', $links, 1);  
		
		$arr['updates_GP']		= $Go_Curl->match('/<script id="__NEXT_DATA__".*?>.*?"added":".*?","modified":"(.*?)\ .*?","updated":".*?","age".*?<\/script>/msi', $links, 1); 	
		 
		$arr['whatnews_GP']		= $Go_Curl->match('/<script id="__NEXT_DATA__".*?>.*?"description":".*?","summary":".*?","news":"(.*?)","screenshots".*?<\/script>/msi', $links, 1); 	
		$arr['whatnews_GP']		= str_replace('\n', '', $arr['whatnews_GP']);
		$arr['whatnews_GP']		= str_replace('\r', '', $arr['whatnews_GP']);
		  
		$arr['summary']			= $Go_Curl->match('/<script id="__NEXT_DATA__".*?>.*?"description":".*?","summary":"(.*?)","news":".*?","screenshots".*?<\/script>/msi', $links, 1); 	
		$arr['summary']			= unicode_conv($arr['summary']);
		$arr['summary']			= str_replace('\n', '', $arr['summary']);
		$arr['summary']			= str_replace('\r', '', $arr['summary']);
  	 
		if($opt_themes['bahasa_on']){
			
		$arr['articlebody_GP']	= $tr->setSource()->setTarget($bahasa)->translate($arr['articlebody_GP']);   
		$arr['desck_GP']		= $tr->setSource()->setTarget($bahasa)->translate($arr['desck_GP']);
		$arr['whatnews_GP']		= $tr->setSource()->setTarget($bahasa)->translate($arr['whatnews_GP']);
		$arr['summary']			= $tr->setSource()->setTarget($bahasa)->translate($arr['summary']);
		
		}
		
		$arr['rated_GP']		= $Go_Curl->match('/<script id="__NEXT_DATA__".*?>.*?"stats":{"rating":{.*?},"prating":{"avg":(.*?),"total":.*?,"votes":.*?}.*?<\/script>/msi', $links, 1); 	
		
		$arr['ratings_GP']		= $Go_Curl->match('/<script id="__NEXT_DATA__".*?>.*?"stats":{"rating":{.*?},"prating":{"avg":.*?,"total":(.*?),"votes":.*?}.*?<\/script>/msi', $links, 1); 	
		 
		$arr['installs_GP']		= $Go_Curl->match('/<script id="__NEXT_DATA__".*?>.*?"pageProps":{"versions":.*?{"id":.*?,"uname":".*?","icon":".*?","graphic":".*?","date":".*?","name":".*?","store":"aptoide-web","vername":".*?","vercode":.*?,"downloads":(.*?),"size":.*?,"trusted":.*?,"parsedName":".*?","appcoins":.*?}.*?<\/script>/msi', $links, 1); 	

		$arr['requires_GPs']	= $Go_Curl->match('/<dd itemprop="operatingSystem".*?><strong>.*?<\/strong> <!-- -->(.*?)<\/dd>/msi', $links, 1); 	
 
		$arr['requires_GP']		= $arr['requires_GPs'];
		$arr['youtube_GP']		= '';
		 	
		$aptoide_dw				= "https://en.aptoide.com/download?package_name=".$arr['id_ps']."&store_name=aptoide-web"; 
		$aptoide_dl				= $Go_Curl->geturl("${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) ;
 
		$apkgk_dw_url			= "https://apkgk.com/".$arr['id_ps']."/download";
		$apkgk_dw_urls			= file_get_html($apkgk_dw_url);
		$arr['link_download_gps']	= $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->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->match_all('/<a.*?><i.*?<span>.*?\((.*?)\).*?<\/span><\/a>/ms', $Go_Curl->match('/<div class="c-download">(.*?)<\/div>/ms', $apkgk_dw_urls, 1), 1);
 
				 
		if ( empty($titleId)) {
			$arr['error']			= '<h2 style="text-align: center;font-size: large;font-weight: 700;">Sorry, the requested URL <b style="color: blue;" >"'.$titleId.'"</b> was not found on <a style="color: red;" href="'.$sumbers.'" target="_blank">Aptoide</a>.</h2><h3 style="text-align: center;font-size: small;">Please Search Another or use Other Sources</h3>';
			echo $arr['error'];
		} 
		return $arr;
	}
		  
}