Advertisement
g6man

tv_cast.php

Aug 31st, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.71 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.         $content = curl_exec($curl);
  11.         curl_close($curl);
  12.  
  13.         if (empty($content)) {
  14.             throw new Exception("Can't get content, url: " . $url);
  15.         }
  16.  
  17.         return $content;
  18.     }
  19.  
  20.     function getMovieDetail($content)
  21.     {
  22.         $find = '<div class="movie_detail">';
  23.         $start = strpos($content, $find);
  24.         if ($start === false) {
  25.             throw new Exception("Can't find movie_detail start point.");
  26.         }
  27.  
  28.         $start += strlen($find);
  29.         $end = strpos($content, '<div class="wrap_bnr">', $start);
  30.         if ($end === false) {
  31.             throw new Exception("Can't find movie_detail end point.");
  32.         }
  33.  
  34.         return substr($content, $start, $end - $start);
  35.     }
  36.  
  37.     function getTitle($content)
  38.     {
  39.         $find = '<strong class="tit_movie">';
  40.         $start = strpos($content, $find);
  41.         if ($start === false) {
  42.             throw new Exception("Can't find title start point.");
  43.         }
  44.  
  45.         $start += strlen($find);
  46.         $end = strpos($content, '</strong>', $start);
  47.         if ($end === false) {
  48.             throw new Exception("Can't find title end point.");
  49.         }
  50.  
  51.         return substr($content, $start, $end - $start);
  52.     }
  53.  
  54.     function getName($content)
  55.     {
  56.         $find = '<em class="emph_point">';
  57.         $start = strpos($content, $find);
  58.         if ($start === false) {
  59.             return null;
  60.         }
  61.  
  62.         $start += strlen($find);
  63.         $end = strpos($content, '</em>', $start);
  64.         if ($end === false) {
  65.             return null;
  66.         }
  67.  
  68.         return substr($content, $start, $end - $start);
  69.  
  70.     }
  71.  
  72.     function getRole($content)
  73.     {
  74.         $find = '<span class="txt_join">';
  75.         $start = strpos($content, $find);
  76.         if ($start === false) {
  77.             return null;
  78.         }
  79.  
  80.         $start += strlen($find);
  81.         $end = strpos($content, '</span>', $start);
  82.         if ($end === false) {
  83.             return null;
  84.         }
  85.  
  86.         $span = trim(substr($content, $start, $end - $start));
  87.         if (mb_substr($span, -2) == ' 역') {
  88.             $span = mb_substr($span, 0, -2);
  89.         }
  90.  
  91.         return $span;
  92.     }
  93.  
  94.     function getPhoto($content)
  95.     {
  96.         $find = '<img src="';
  97.         $start = strpos($content, $find);
  98.         if ($start === false) {
  99.             return null;
  100.         }
  101.  
  102.         $start += strlen($find);
  103.         $end = strpos($content, '"', $start);
  104.         if ($end === false) {
  105.             return null;
  106.         }
  107.  
  108.         return substr($content, $start, $end - $start);
  109.  
  110.     }
  111.  
  112.     function getCast($content)
  113.     {
  114.         $result = [];
  115.         $roleCount = [];
  116.         $list = explode('class="link_join">', $content);
  117.         foreach ($list as $item) {
  118.             $name = getName($item);
  119.             if (is_null($name)) {
  120.                 continue;
  121.             }
  122.             $role = getRole($item);
  123.             if (is_null($role)) {
  124.                 continue;
  125.             }
  126.  
  127.             if (array_key_exists($role, $roleCount)) {
  128.                 $roleCount[$role] += 1;
  129.                 $role = $role . ' ' . $roleCount[$role];
  130.             } else {
  131.                 $roleCount[$role] = 1;
  132.             }
  133.  
  134.             $photo = getPhoto($item);
  135.  
  136.             $result[] = [
  137.                 'name' => $name,
  138.                 'role' => $role,
  139.                 'photo' => $photo,
  140.             ];
  141.         }
  142.  
  143.         return $result;
  144.     }
  145.  
  146.     function getDirectors($content, &$result)
  147.     {
  148.         $directors = getCast($content);
  149.         foreach ($directors as $item) {
  150.             $role = $item['role'];
  151.             if (mb_strpos($role, '감독') !== false or mb_strpos($role, '연출') !== false or mb_strpos($role, '기획') !== false) {
  152.                 $result['directors'][] = $item;
  153.             } elseif (mb_strpos($role, '제작') !== false or mb_strpos($role, '프로듀서') !== false) {
  154.                 $result['producers'][] = $item;
  155.             } elseif (mb_strpos($role, '극본') !== false or mb_strpos($role, '각본') !== false) {
  156.                 $result['writers'][] = $item;
  157.             } else {
  158.                 if (!array_key_exists('ohers')) {
  159.                     $result['others'] = [];
  160.                 }
  161.                
  162.                 $result['others'][] = $item;
  163.             }
  164.         }
  165.     }
  166.  
  167.     function getJson($content, &$result)
  168.     {
  169.         $result = [
  170.             'roles' => [],
  171.             'directors' => [],
  172.             'producers' => [],
  173.             'writers' => [],
  174.         ];
  175.  
  176.         $movieDetail = getMovieDetail($content);
  177.        
  178.         list($castContent, $directorContent) = explode('<h4 class="tit_movie">제작진</h4>', $movieDetail);
  179.         $result['roles'] = getCast($castContent);
  180.         getDirectors($directorContent, $result);
  181.  
  182.         return $result;
  183.     }
  184.  
  185.     function main($programId)
  186.     {try {
  187.             $content = curl('https://movie.daum.net/tv/crew?tvProgramId=' . $programId);
  188.             $json = [
  189.                 'data' => getJson($content, $result),
  190.             ];
  191.         } catch (Exception $e) {
  192.             $json = [
  193.                 'error' => (string)$e,
  194.             ];
  195.         }
  196.        
  197.         header('Content-Type: application/json');
  198.         $result = json_encode($json, JSON_UNESCAPED_UNICODE);
  199.         echo $result;
  200.     }
  201.  
  202.     error_reporting(E_ALL);
  203.     main(array_key_exists('id', $_GET) ? (int)$_GET['id'] : 0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement