martysmarty

iTunesGetShows

Jan 2nd, 2019
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.86 KB | None | 0 0
  1. public function handle()
  2.     {
  3.         $countries = [
  4.             "SE",
  5.             "NL",
  6.             "GB",
  7.             "US"
  8.         ];
  9.  
  10.         foreach($countries as $country){
  11.             foreach(array_merge(range(0, 9), range('A', 'Z')) as $char) {
  12.  
  13.                 echo $country . " - " . $char . "\n";
  14.                 $offset = 0;
  15.                 $resultCount = 1;
  16.                 while($resultCount >0){
  17.                     $time = time();
  18.  
  19.  
  20.                     try{
  21.  
  22.                         //This sometimes fails. If so, continue with next loop, we'll catch it next run
  23.                         $results = json_decode(\Httpful\Request::get("https://itunes.apple.com/search?limit=200&media=podcast&term=$char&country=$country&offset=" . $offset)->expectsJson()
  24.                             ->send());
  25.  
  26.                     } catch(\ErrorException $e){
  27.                         unset($e);
  28.                         continue;
  29.                     }
  30.  
  31.  
  32.                     $offset = $offset + $results->resultCount;
  33.                     $resultCount = $results->resultCount;
  34.  
  35.                     foreach($results->results as $result){
  36.  
  37.                         try{
  38.                             if (!itunesshows::where("sitesid", "=", 26)->where("show_site_id", "=", $result->trackId)->exists()) {
  39.  
  40.                                 $x = new itunesshows();
  41.  
  42.                                 $x->sitesid = 26;
  43.                                 $x->showname = $result->trackName;
  44.  
  45.                                 $x->show_site_id = $result->trackId;
  46.                                 if(property_exists($result, "feedUrl")){
  47.                                     $x->rss_url =$result->feedUrl;
  48.                                 }else{
  49.                                     $x->rss_url = "https://itunesu.itunes.apple.com/feed/id" . $result->trackId;
  50.                                 }
  51.  
  52.                                 $x->podcast_channel = $result->artistName;
  53.  
  54.                                 $x->image_url = $result->artworkUrl600;
  55.                                 $x->save();
  56.                                 $predis = new Client();
  57.                                 $predis->rPush("queue.itunes.get_episodes_from_show", json_encode($x));
  58.  
  59.                             }
  60.                         } catch (\ErrorException $e){
  61.  
  62.                             print_r($result);
  63.                             exit;
  64.                         }
  65.  
  66.                     }
  67.  
  68.                     print_r($resultCount . "\n");
  69.  
  70.                     if(time() - $time < 3){
  71.                         sleep(1);
  72.                     }
  73.                     if(time() - $time < 3){
  74.                         sleep(1);
  75.                     }
  76.                     if(time() - $time < 3){
  77.                         sleep(1);
  78.                     }
  79.  
  80.                 }
  81.  
  82.  
  83.  
  84.  
  85.             }
  86.         }
  87.  
  88.  
  89.     }
Add Comment
Please, Sign In to add comment