Advertisement
g6man

tv_episode.php

Aug 31st, 2018
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.83 KB | None | 0 0
  1. <?php
  2.     function curl($url)
  3.     {
  4.         $ua = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36';
  5.         $curl = curl_init($url);
  6.         curl_setopt($curl, CURLOPT_USERAGENT, $ua);
  7.         curl_setopt($curl, CURLOPT_FAILONERROR, true);
  8.         curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  9.         curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
  10.         $output = curl_exec($curl);
  11.         curl_close($curl);
  12.  
  13.         return $output;
  14.     }
  15.  
  16.     function toJson($content, $programId)
  17.     {
  18.         $find = 'MoreView.init(' . $programId . ', ';
  19.         $start = strpos($content, $find);
  20.         if ($start === false) {
  21.             throw new Exception("Can't find json data start point.");
  22.         }
  23.        
  24.         $start += strlen($find);
  25.         $end = strpos($content, ']);', $start);
  26.         if ($end === false) {
  27.             throw new Exception("Can't find json data end point.");
  28.         }
  29.  
  30.         $moreView = substr($content, $start, $end - $start + 1);
  31.  
  32.         return json_decode($moreView, true);
  33.     }
  34.  
  35.     function getJson($content, $programId)
  36.     {
  37.         $data = toJson($content, $programId);
  38.  
  39.         $result = [];
  40.         $normal_episodes = 0;
  41.         $special_eposodes = 0;
  42.         $old_name = '';
  43.         for ($i = count($data); $i != 0; ) {
  44.             --$i;
  45.             $item = $data[$i];
  46.             $name = trim($item['name']);
  47.  
  48.             if (empty($name) or $name == $old_name) {
  49.                 $seasons = '0';
  50.                 $episodes = (string)(++$special_eposodes);
  51.             } else {
  52.                 $seasons = '1';
  53.                 $episodes = (string)(++$normal_episodes);
  54.             }
  55.  
  56.             $old_name = $name;
  57.  
  58.             $title = trim($item['title']);
  59.             $replace_search = ["\r\n", "\n\n", '<br>', '<BR>', '!|'];
  60.             $summary = trim(str_replace($replace_search, "\n", $item['introduceDescription']));
  61.             $lines = [];
  62.             foreach (explode("\n", $summary) as $line) {
  63.                 $line = trim($line);
  64.                 if (empty($line)) {
  65.                     continue;
  66.                 }
  67.  
  68.                 $lines[] = $line;
  69.             }
  70.             $summary = implode("\n", $lines);
  71.             $originallyAvailableAt = null;
  72.             foreach ($item['channels'] as $channel) {
  73.                 if (array_key_exists('broadcastDate', $channel) and strlen($channel['broadcastDate']) == 8) {
  74.                     $originallyAvailableAt = $channel['broadcastDate'];
  75.                     break;
  76.                 }
  77.             }
  78.             $rating = (float)$item['rate'];
  79.  
  80.             $result[] = [
  81.                 'seasons' => $seasons,
  82.                 'episodes' => $episodes,
  83.                 'title' => $title,
  84.                 'summary' => $summary,
  85.                 'originally_available_at' => $originallyAvailableAt,
  86.                 'rating' => $rating,
  87.             ];
  88.         }
  89.  
  90.         return $result;
  91.     }
  92.  
  93.     function main($programId)
  94.     {
  95.         try {
  96.             $content = curl('https://movie.daum.net/tv/episode?tvProgramId=' . $programId);
  97.             $json = [
  98.                 'data' => getJson($content, $programId),
  99.             ];
  100.         } catch (Exception $e) {
  101.             $json = [
  102.                 'error' => (string)$e,
  103.             ];
  104.         }
  105.        
  106.         header('Content-Type: application/json');
  107.         $result = json_encode($json, JSON_UNESCAPED_UNICODE);
  108.         echo $result;
  109.     }
  110.  
  111.     main(array_key_exists('id', $_GET) ? (int)$_GET['id'] : 0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement