martysmarty

GetDescription

Jan 2nd, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.30 KB | None | 0 0
  1.   public function handle()
  2.     {
  3.  
  4.         Log::useFiles(storage_path("/logs/iTunes_description.log"));
  5.         Log::info('Debug');
  6.  
  7.         $context = stream_context_create();
  8.         stream_context_set_params($context, array('user_agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0'));
  9.  
  10.         $shows = itunesshows::where("sitesid", "=", 26)->whereNull("description")->inRandomOrder()->take(1000)->get();
  11.  
  12.         foreach($shows as $show){
  13.  
  14.             try{
  15.                 $raw = file_get_contents($show->rss_url,0, $context);
  16.                 $xml = str_get_html($raw);
  17.  
  18.             }catch(\ErrorException $e){
  19.  
  20.                 print_r($e->getMessage());
  21.                 unset ($e);
  22.                 continue;
  23.  
  24.             }
  25.             echo $show->rss_url . "\n";
  26.             try{
  27.                 $xml = $xml->find("channel")[0];
  28.             }catch(\Error $e){
  29.  
  30.  
  31.                 if (str_contains($e->getMessage(), "Call to a member function find() on boolean")){
  32.                     unset($e);
  33.                     continue;
  34.  
  35.                 }
  36.  
  37.             }catch(\ErrorException $e){
  38.  
  39.                 if (str_contains($e->getMessage(), "Undefined offset: 0")){
  40.                     unset($e);
  41.                     continue;
  42.                 }
  43.  
  44.             }
  45.  
  46.             if(count($xml->find("description")) == 0){
  47.                 $xml2 = str_replace("itunes:summary", "itunes_summary", $xml);
  48.  
  49.                 $xml2 = str_get_html($xml2);
  50.  
  51.                 if(count($xml2->find("itunes_summary"))==0){
  52.  
  53.                 }else{
  54.                     $show->description = utf8_encode($this->strip_cdata($xml2->find("itunes_summary")[0]->innertext));
  55.                 }
  56.  
  57.  
  58.             }else{
  59.                 $show->description = utf8_encode($this->strip_cdata($xml->find("description")[0]->innertext));
  60.             }
  61.  
  62.             if(count($xml->find("language")) == 0) {
  63.                 $show->country = "Unknown";
  64.             }else{
  65.                 $show->country = $this->strip_cdata($xml->find("language")[0]->innertext);
  66.             }
  67.             $show->save();
  68.  
  69.         }
  70.     }
  71.  
  72.  
  73.     function strip_cdata($string)
  74.  
  75.       {    preg_match_all('/<!\[cdata\[(.*?)\]\]>/is', $string, $matches);
  76.  
  77.       return str_replace($matches[0], $matches[1], $string);
  78.  
  79.       }
Add Comment
Please, Sign In to add comment