Advertisement
g6man

movie_detail.php

Aug 31st, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.19 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, '<script', $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 getTitleYear($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.         $title_year = trim(substr($content, $start, $end - $start));
  52.  
  53.         if (preg_match('/(.*?) \((\d{4})\)/', $title_year, $matches) == 0) {
  54.             throw new Exception("Can't find regex point.");
  55.         }
  56.  
  57.         $title = $matches[1];
  58.         $year = (int)$matches[2];
  59.  
  60.         return [$title, $year];
  61.     }
  62.  
  63.     function getOriginalTitle($content)
  64.     {
  65.         $find = '<span class="txt_origin">';
  66.         $start = strpos($content, $find);
  67.         if ($start === false) {
  68.             throw new Exception("Can't find original_title start point.");
  69.         }
  70.  
  71.         $start += strlen($find);
  72.         $end = strpos($content, '</span>', $start);
  73.         if ($end === false) {
  74.             throw new Exception("Can't find original_title end point.");
  75.         }
  76.  
  77.         return trim(substr($content, $start, $end - $start));
  78.     }
  79.  
  80.     function getRating($content)
  81.     {
  82.         $find = '<em class="emph_grade">';
  83.         $start = strpos($content, $find);
  84.         if ($start === false) {
  85.             throw new Exception("Can't find rating start point.");
  86.         }
  87.  
  88.         $start += strlen($find);
  89.         $end = strpos($content, '</em>', $start);
  90.         if ($end === false) {
  91.             throw new Exception("Can't find rating end point.");
  92.         }
  93.  
  94.         return (float)substr($content, $start, $end - $start);
  95.     }
  96.  
  97.     function getTagline($content)
  98.     {
  99.         $find = '<strong class="tit_copy">';
  100.         $start = strpos($content, $find);
  101.         if ($start === false) {
  102.             // throw new Exception("Can't find tagline start point.");
  103.             return '';
  104.         }
  105.  
  106.         $start += strlen($find);
  107.         $end = strpos($content, '</strong', $start);
  108.         if ($end === false) {
  109.             // throw new Exception("Can't find tagline end point.");
  110.             return '';
  111.         }
  112.  
  113.         return trim(str_replace(["\r", "\n", "  "], " ", strip_tags(substr($content, $start, $end - $start))));
  114.     }
  115.  
  116.     function getListMovie($content)
  117.     {
  118.         $find = '<dl class="list_movie list_main">';
  119.         $start = strpos($content, $find);
  120.         if ($start === false) {
  121.             throw new Exception("Can't find list_movie start point.");
  122.         }
  123.  
  124.         $start += strlen($find);
  125.         $end = strpos($content, '</dl>', $start);
  126.         if ($end === false) {
  127.             throw new Exception("Can't find list_movie end point.");
  128.         }
  129.  
  130.         return substr($content, $start, $end - $start);
  131.     }
  132.  
  133.     function getGenresContries($content)
  134.     {
  135.         $find = '<dd class="txt_main">';
  136.         $start = strpos($content, $find);
  137.         if ($start === false) {
  138.             throw new Exception("Can't find genres start point.");
  139.         }
  140.  
  141.         $start += strlen($find);
  142.         $end = strpos($content, '</dd>', $start);
  143.         if ($end === false) {
  144.             throw new Exception("Can't find genres end point.");
  145.         }
  146.  
  147.         $genres = explode('/', str_replace(' ', '', substr($content, $start, $end - $start)));
  148.  
  149.         $find = '<dd>';
  150.         $start = strpos($content, $find);
  151.         if ($start === false) {
  152.             throw new Exception("Can't find countries start point.");
  153.         }
  154.  
  155.         $start += strlen($find);
  156.         $end = strpos($content, '</dd>', $start);
  157.         if ($end === false) {
  158.             throw new Exception("Can't find countries end point.");
  159.         }
  160.  
  161.         $countries = explode(',', str_replace(["\n", "\t", ' '], '', substr($content, $start, $end - $start)));
  162.  
  163.         return [$genres, $countries];
  164.     }
  165.  
  166.     function getOriginallyAvailableAt($content)
  167.     {
  168.         if (preg_match('/(\d{4}\.\d{2}\.\d{2})\s*개봉/u', $content, $matches) == 0) {
  169.             return null;
  170.         }
  171.  
  172.         return $matches[1];
  173.     }
  174.  
  175.     function getDurationContentRating($content)
  176.     {
  177.         if (preg_match('/(\d+)분,(.*)<\/dd>/u', $content, $matches) == 0) {
  178.             $duration = 0;
  179.             $contentRating = null;
  180.         } else {
  181.             $duration = (int)$matches[1];
  182.             $contentRating = trim($matches[2]);
  183.         }
  184.  
  185.         return [$duration, $contentRating];
  186.     }
  187.  
  188.     function getSummary($content)
  189.     {
  190.         $find = '<div class="desc_movie">';
  191.         $start = strpos($content, $find);
  192.         if ($start === false) {
  193.             throw new Exception("Can't find summary start point.");
  194.         }
  195.  
  196.         $start += strlen($find);
  197.         $end = strpos($content, '</div', $start);
  198.         if ($end === false) {
  199.             throw new Exception("Can't find summary end point.");
  200.         }
  201.  
  202.         return trim(str_replace(["\r\n", "\r", "\n\n"], "\n", strip_tags(substr($content, $start, $end - $start))));
  203.     }
  204.  
  205.     function getPosterUrl($content)
  206.     {
  207.         $find = '<img src="';
  208.         $start = strpos($content, $find, $prepos);
  209.         if ($start === false) {
  210.             throw new Exception("Can't find poster start point.");
  211.         }
  212.  
  213.         $start += strlen($find);
  214.         $end = strpos($content, '"', $start);
  215.         if ($end === false) {
  216.             throw new Exception("Can't find poster end point.");
  217.         }
  218.  
  219.         return substr($content, $start, $end - $start);
  220.     }
  221.  
  222.     function getJson($content)
  223.     {
  224.         $detailSummarize = getDetailSummarize($content);
  225.         list($title, $year) = getTitleYear($detailSummarize);
  226.         $originalTitle = getOriginalTitle($detailSummarize);
  227.         $rating = getRating($detailSummarize);
  228.         $tagline = getTagline($detailSummarize);
  229.         $summary = getSummary($detailSummarize);
  230.         $posterUrl = getPosterUrl($detailSummarize);
  231.         $listMovie = getListMovie($detailSummarize);
  232.         list($genres, $countries) = getGenresContries($listMovie);
  233.         $originallyAvailableAt = getOriginallyAvailableAt($listMovie);
  234.         list($duration, $contentRating) = getDurationContentRating($listMovie);
  235.  
  236.         $result = [
  237.             'title' => $title,
  238.             'year' => $year,
  239.             'original_title' => $originalTitle,
  240.             'rating' => $rating,
  241.             'tagline' => $tagline,
  242.             'genres' => $genres,
  243.             'countries' => $countries,
  244.             'originally_available_at' => $originallyAvailableAt,
  245.             'duration' => $duration,
  246.             'content_rating' => $contentRating,
  247.             'summary' => $summary,
  248.             'poster_url' => $posterUrl,
  249.         ];
  250.  
  251.         return $result;
  252.     }
  253.  
  254.     function main($movieId)
  255.     {
  256.         try {
  257.             $content = curl('https://movie.daum.net/moviedb/main?movieId=' . $movieId);
  258.             $json = [
  259.                 'data' => getJson($content),
  260.             ];
  261.         } catch (Exception $e) {
  262.             $json = [
  263.                 'error' => (string)$e,
  264.             ];
  265.         }
  266.        
  267.         header('Content-Type: application/json');
  268.         $result = json_encode($json, JSON_UNESCAPED_UNICODE);
  269.         echo $result;
  270.     }
  271.  
  272.     main(array_key_exists('id', $_GET) ? (int)$_GET['id'] : 0);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement