Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function handle()
- {
- Log::useFiles(storage_path("/logs/iTunes_episodes.log"));
- Log::info('Debug');
- $context = stream_context_create();
- stream_context_set_params($context, array('user_agent' => 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:64.0) Gecko/20100101 Firefox/64.0'));
- while(1) {
- $predis = new Client();
- $job = $predis->blpop("queue.itunes.get_episodes_from_show", 10);
- if($job){
- $show = json_decode($job[1]);
- try {
- $raw = file_get_contents($show->rss_url, 0, $context);
- $xml = str_get_html($raw);
- } catch (\ErrorException $e) {
- print_r($e->getMessage());
- unset ($e);
- continue;
- }
- echo $show->rss_url . "\n";
- try {
- $xml = $xml->find("channel")[0];
- } catch (\Error $e) {
- if (str_contains($e->getMessage(), "Call to a member function find() on boolean")) {
- unset($e);
- continue;
- }
- } catch (\ErrorException $e) {
- if (str_contains($e->getMessage(), "Undefined offset: 0")) {
- unset($e);
- continue;
- }
- }
- foreach ($xml->find("item") as $item) {
- if (count($item->find("guid")) == 0) {
- $uuid = $this->strip_cdata($item->find("enclosure")[0]->innertext);
- } else {
- $uuid = $this->strip_cdata($item->find("guid")[0]->innertext);
- }
- if (!itunesepisodes::where("uuid", "=", $uuid)->where("shows_id", "=", $show->id)->exists()) {
- if (count($item->find("enclosure")) > 0) {
- $x = new itunesepisodes();
- $x->shows_id = $show->id;
- $x->sites_id = 26;
- $x->download_url = $this->strip_cdata($item->find("enclosure")[0]->url);
- $item2 = str_replace("itunes:summary", "itunes_summary", $item->innertext);
- $item2 = str_get_html($item2);
- if (count($item2->find("itunes_summary")) == 0) {
- if (count($item2->find("description")) == 0) {
- $item2 = str_replace("itunes:subtitle", "itunes_subtitle", $item->innertext);
- $item2 = str_get_html($item2);
- try {
- $x->description = utf8_encode($this->strip_cdata($item2->find("itunes_subtitle")[0]->innertext));
- } catch (\ErrorException $e) {
- unset($e);
- }
- } else {
- $x->description = utf8_encode($this->strip_cdata($item2->find("description")[0]->innertext));
- }
- } else {
- $x->description = utf8_encode($this->strip_cdata($item2->find("itunes_summary")[0]->innertext));
- }
- $x->title = utf8_encode($this->strip_cdata($item->find("title")[0]->innertext));
- $x->slug = str_slug($x->title);
- if (count($item->find("pubDate")) > 0) {
- try {
- $x->published = empty($this->strip_cdata($item->find("pubDate")[0]->innertext)) ? null : $this->strip_cdata($item->find("pubDate")[0]->innertext);
- } catch (\Exception $e) {
- unset($e);
- }
- }
- $x->uuid = $uuid;
- $item2 = str_replace("itunes:episode", "itunes_episode", $item->innertext);
- $item2 = str_get_html($item2);
- if (count($item2->find("itunes_episode")) > 0) {
- $x->episode_number = utf8_encode($this->strip_cdata($item2->find("itunes_episode")[0]->innertext));
- if ($x->episode_number == "") {
- $x->episode_number = 0;
- }
- }
- $item2 = str_replace("itunes:season", "itunes_season", $item->innertext);
- $item2 = str_get_html($item2);
- if (count($item2->find("itunes_season")) > 0) {
- $x->season_number = utf8_encode($this->strip_cdata($item2->find("itunes_season")[0]->innertext));
- if ($x->season_number == "") {
- $x->season_number = 0;
- }
- }
- try{
- $x->save();
- $predis->rPush("queue.download_episode", json_encode($x));
- }catch(QueryException $e){
- unset($e);
- continue;
- }
- }
- }
- }
- }
- }
- }
- function strip_cdata($string)
- { preg_match_all('/<!\[cdata\[(.*?)\]\]>/is', $string, $matches);
- return str_replace($matches[0], $matches[1], $string);
- }
Advertisement
Add Comment
Please, Sign In to add comment