martysmarty

GetEpisodes

Jan 2nd, 2019
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.91 KB | None | 0 0
  1. public function handle()
  2.     {
  3.         Log::useFiles(storage_path("/logs/iTunes_episodes.log"));
  4.         Log::info('Debug');
  5.  
  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.         while(1) {
  11.  
  12.             $predis = new Client();
  13.  
  14.             $job = $predis->blpop("queue.itunes.get_episodes_from_show", 10);
  15.             if($job){
  16.  
  17.  
  18.                 $show = json_decode($job[1]);
  19.  
  20.  
  21.                 try {
  22.                     $raw = file_get_contents($show->rss_url, 0, $context);
  23.                     $xml = str_get_html($raw);
  24.  
  25.                 } catch (\ErrorException $e) {
  26.  
  27.                     print_r($e->getMessage());
  28.                     unset ($e);
  29.                     continue;
  30.  
  31.                 }
  32.                 echo $show->rss_url . "\n";
  33.                 try {
  34.                     $xml = $xml->find("channel")[0];
  35.                 } catch (\Error $e) {
  36.  
  37.  
  38.                     if (str_contains($e->getMessage(), "Call to a member function find() on boolean")) {
  39.                         unset($e);
  40.                         continue;
  41.  
  42.                     }
  43.  
  44.                 } catch (\ErrorException $e) {
  45.  
  46.                     if (str_contains($e->getMessage(), "Undefined offset: 0")) {
  47.                         unset($e);
  48.                         continue;
  49.                     }
  50.  
  51.                 }
  52.  
  53.                 foreach ($xml->find("item") as $item) {
  54.  
  55.                     if (count($item->find("guid")) == 0) {
  56.                         $uuid = $this->strip_cdata($item->find("enclosure")[0]->innertext);
  57.                     } else {
  58.                         $uuid = $this->strip_cdata($item->find("guid")[0]->innertext);
  59.                     }
  60.  
  61.                     if (!itunesepisodes::where("uuid", "=", $uuid)->where("shows_id", "=", $show->id)->exists()) {
  62.  
  63.                         if (count($item->find("enclosure")) > 0) {
  64.  
  65.  
  66.                             $x = new itunesepisodes();
  67.  
  68.                             $x->shows_id = $show->id;
  69.                             $x->sites_id = 26;
  70.                             $x->download_url = $this->strip_cdata($item->find("enclosure")[0]->url);
  71.  
  72.  
  73.                             $item2 = str_replace("itunes:summary", "itunes_summary", $item->innertext);
  74.  
  75.                             $item2 = str_get_html($item2);
  76.  
  77.                             if (count($item2->find("itunes_summary")) == 0) {
  78.                                 if (count($item2->find("description")) == 0) {
  79.                                     $item2 = str_replace("itunes:subtitle", "itunes_subtitle", $item->innertext);
  80.  
  81.                                     $item2 = str_get_html($item2);
  82.                                     try {
  83.                                         $x->description = utf8_encode($this->strip_cdata($item2->find("itunes_subtitle")[0]->innertext));
  84.                                     } catch (\ErrorException $e) {
  85.                                         unset($e);
  86.                                     }
  87.                                 } else {
  88.                                     $x->description = utf8_encode($this->strip_cdata($item2->find("description")[0]->innertext));
  89.                                 }
  90.  
  91.                             } else {
  92.  
  93.                                 $x->description = utf8_encode($this->strip_cdata($item2->find("itunes_summary")[0]->innertext));
  94.                             }
  95.  
  96.                             $x->title = utf8_encode($this->strip_cdata($item->find("title")[0]->innertext));
  97.                             $x->slug = str_slug($x->title);
  98.  
  99.                             if (count($item->find("pubDate")) > 0) {
  100.                                 try {
  101.                                     $x->published = empty($this->strip_cdata($item->find("pubDate")[0]->innertext)) ? null : $this->strip_cdata($item->find("pubDate")[0]->innertext);
  102.                                 } catch (\Exception $e) {
  103.                                     unset($e);
  104.                                 }
  105.  
  106.                             }
  107.  
  108.                             $x->uuid = $uuid;
  109.  
  110.  
  111.                             $item2 = str_replace("itunes:episode", "itunes_episode", $item->innertext);
  112.  
  113.                             $item2 = str_get_html($item2);
  114.                             if (count($item2->find("itunes_episode")) > 0) {
  115.                                 $x->episode_number = utf8_encode($this->strip_cdata($item2->find("itunes_episode")[0]->innertext));
  116.                                 if ($x->episode_number == "") {
  117.                                     $x->episode_number = 0;
  118.                                 }
  119.                             }
  120.  
  121.                             $item2 = str_replace("itunes:season", "itunes_season", $item->innertext);
  122.  
  123.                             $item2 = str_get_html($item2);
  124.                             if (count($item2->find("itunes_season")) > 0) {
  125.                                 $x->season_number = utf8_encode($this->strip_cdata($item2->find("itunes_season")[0]->innertext));
  126.                                 if ($x->season_number == "") {
  127.                                     $x->season_number = 0;
  128.                                 }
  129.                             }
  130.                             try{
  131.                                 $x->save();
  132.  
  133.                                 $predis->rPush("queue.download_episode", json_encode($x));
  134.  
  135.                             }catch(QueryException $e){
  136.                                 unset($e);
  137.                                 continue;
  138.  
  139.                             }
  140.  
  141.                         }
  142.  
  143.  
  144.                     }
  145.                 }
  146.  
  147.             }
  148.  
  149.         }
  150.  
  151.  
  152.     }
  153.  
  154.  
  155.     function strip_cdata($string)
  156.  
  157.     {    preg_match_all('/<!\[cdata\[(.*?)\]\]>/is', $string, $matches);
  158.  
  159.         return str_replace($matches[0], $matches[1], $string);
  160.  
  161.     }
Advertisement
Add Comment
Please, Sign In to add comment