Advertisement
g6man

tv_detail.php

Aug 31st, 2018
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.97 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 getDetailSummarize($content)
  21.     {
  22.         $find = '<div class="detail_summarize">';
  23.         $start = strpos($content, $find);
  24.         if ($start === false) {
  25.             throw new Exception("Can't find detail_summarize start point.");
  26.         }
  27.  
  28.         $start += strlen($find);
  29.         $end = strpos($content, 'class="episode_info">', $start);
  30.         if ($end === false) {
  31.             throw new Exception("Can't find detail_summarize 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 trim(substr($content, $start, $end - $start));
  52.     }
  53.  
  54.     function getRating($content)
  55.     {
  56.         $find = '<em class="emph_grade">';
  57.         $start = strpos($content, $find);
  58.         if ($start === false) {
  59.             throw new Exception("Can't find rating start point.");
  60.         }
  61.  
  62.         $start += strlen($find);
  63.         $end = strpos($content, '</em>', $start);
  64.         if ($end === false) {
  65.             throw new Exception("Can't find rating end point.");
  66.         }
  67.  
  68.         return (float)substr($content, $start, $end - $start);
  69.     }
  70.  
  71.     function getGenres($content)
  72.     {
  73.         $find = '<dd class="f_l">';
  74.         $start = strpos($content, $find);
  75.         if ($start === false) {
  76.             throw new Exception("Can't find genres start point.");
  77.         }
  78.  
  79.         $start += strlen($find);
  80.         $end = strpos($content, '</dd>', $start);
  81.         if ($end === false) {
  82.             throw new Exception("Can't find genres end point.");
  83.         }
  84.  
  85.         return explode('/', str_replace(' ', '', substr($content, $start, $end - $start)));
  86.     }
  87.  
  88.     function getStudio($content)
  89.     {
  90.         $find = '<em class="emph_g">';
  91.         $start = strpos($content, $find);
  92.         if ($start === false) {
  93.             throw new Exception("Can't find studio start point.");
  94.         }
  95.  
  96.         $start += strlen($find);
  97.         $end = strpos($content, '</em>', $start);
  98.         if ($end === false) {
  99.             throw new Exception("Can't find studio end point.");
  100.         }
  101.  
  102.         return trim(substr($content, $start, $end - $start));
  103.     }
  104.  
  105.     function getOriginallyAvailableAt($content)
  106.     {
  107.         if (preg_match('/(\d{4}\.\d{2}\.\d{2})~/', $content, $matches) == 0) {
  108.             // throw new Exception("Can't find originally_available_at point.");
  109.             return null;
  110.         }
  111.  
  112.         return $matches[1];
  113.     }
  114.  
  115.     function getSummary($content)
  116.     {
  117.         $find = '<div class="desc_movie">';
  118.         $start = strpos($content, $find);
  119.         if ($start === false) {
  120.             throw new Exception("Can't find summary start point.");
  121.         }
  122.  
  123.         $start += strlen($find);
  124.         $end = strpos($content, '<div', $start);
  125.         if ($end === false) {
  126.             throw new Exception("Can't find summary end point.");
  127.         }
  128.  
  129.         return trim(str_replace(["\r\n", "\r", "\n\n"], "\n", strip_tags(substr($content, $start, $end - $start))));
  130.     }
  131.  
  132.     function getPosterUrl($content)
  133.     {
  134.         $find = '<img src="';
  135.         $start = strpos($content, $find, $prepos);
  136.         if ($start === false) {
  137.             throw new Exception("Can't find poster start point.");
  138.         }
  139.  
  140.         $start += strlen($find);
  141.         $end = strpos($content, '"', $start);
  142.         if ($end === false) {
  143.             throw new Exception("Can't find poster end point.");
  144.         }
  145.  
  146.         return substr($content, $start, $end - $start);
  147.     }
  148.  
  149.     function getJson($content)
  150.     {
  151.         $detailSummarize = getDetailSummarize($content);
  152.         $title = getTitle($detailSummarize);
  153.         $rating = getRating($detailSummarize);     
  154.         $genres = getGenres($detailSummarize);
  155.         $studio = getStudio($detailSummarize);
  156.         $originallyAvailableAt = getOriginallyAvailableAt($detailSummarize);
  157.         $summary = getSummary($detailSummarize);
  158.         $posterUrl = getPosterUrl($detailSummarize);
  159.  
  160.         $result = [
  161.             'title' => $title,
  162.             'original_title' => '',
  163.             'rating' => $rating,
  164.             'genres' => $genres,
  165.             'studio' => $studio,
  166.             'originally_available_at' => $originallyAvailableAt,
  167.             'summary' => $summary,
  168.             'poster_url' => $posterUrl,
  169.         ];
  170.  
  171.         return $result;
  172.     }
  173.  
  174.     function main($programId)
  175.     {
  176.         try {
  177.             $content = curl('https://movie.daum.net/tv/main?tvProgramId=' . $programId);
  178.             $json = [
  179.                 'data' => getJson($content),
  180.             ];
  181.         } catch (Exception $e) {
  182.             $json = [
  183.                 'error' => (string)$e,
  184.             ];
  185.         }
  186.        
  187.         header('Content-Type: application/json');
  188.         $result = json_encode($json, JSON_UNESCAPED_UNICODE);
  189.         echo $result;
  190.     }
  191.  
  192.     main(array_key_exists('id', $_GET) ? (int)$_GET['id'] : 0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement