Advertisement
g6man

tv_search.php

Aug 31st, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.64 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 getTvpColl($content)
  21.     {
  22.         $find = '<div id="tvpColl">';
  23.         $start = strpos($content, $find);
  24.         if ($start === false) {
  25.             throw new Exception("Can't find tvp_coll start point.");
  26.         }
  27.  
  28.         $start += strlen($find);
  29.         $end = strpos($content, '<div class="line"></div><hr/>', $start);
  30.         if ($end === false) {
  31.             throw new Exception("Can't find tvp_coll end point.");
  32.         }
  33.  
  34.         return substr($content, $start, $end - $start);
  35.     }
  36.  
  37.     function getMainTitleId($content, $mediaName)
  38.     {
  39.         $find = '<span class="inner_cont">';
  40.         $start = strpos($content, $find);
  41.         if ($start === false) {
  42.             throw new Exception("Can't find main_title <span> start point.");
  43.         }
  44.  
  45.         $start += strlen($find);
  46.         $end = strpos($content, '</span>', $start);
  47.         if ($end === false) {
  48.             throw new Exception("Can't find main_title <span> end point.");
  49.         }
  50.  
  51.         $span = substr($content, $start, $end - $start);
  52.  
  53.         $end = 0;
  54.         $find_id = '&irk=';
  55.         $find_title = 'event);">';
  56.         while (true) {
  57.             $start = strpos($span, $find_id, $end);
  58.             if ($start === false) {
  59.                 throw new Exception("Can't find main_id start point.");
  60.             }
  61.  
  62.             $start += strlen($find_id);
  63.             $end = strpos($span, '&', $start);
  64.             if ($end === false) {
  65.                 throw new Exception("Can't find main_id end point.");
  66.             }
  67.  
  68.             $id = substr($span, $start, $end - $start);
  69.  
  70.             $start = strpos($span, $find_title, $start);
  71.             if ($start === false) {
  72.                 throw new Exception("Can't find main_title start point.");
  73.             }
  74.  
  75.             $start += strlen($find_title);
  76.             $end = strpos($span, '</a>', $start);
  77.             if ($end === false) {
  78.                 throw new Exception("Can't find main_title end point.");
  79.             }
  80.  
  81.             $title = substr($span, $start, $end - $start);
  82.  
  83.             if (strpos(str_replace(' ', '', $title), $mediaName) === false) {
  84.                 continue;
  85.             }
  86.  
  87.             return [$title, $id];
  88.         }
  89.  
  90.         return implode('', $title_piece);
  91.     }
  92.  
  93.     function getMainYear($content)
  94.     {
  95.         $find = '<span class="txt_summary">';
  96.         $start = strrpos($content, $find);
  97.         if ($start === false) {
  98.             throw new Exception("Can't find main_year start point.");
  99.         }
  100.  
  101.         $start += strlen($find);
  102.         $end = strpos($content, '</span>', $start);
  103.         if ($end === false) {
  104.             throw new Exception("Can't find main_year end point.");
  105.         }
  106.  
  107.         $span = substr($content, $start, $end - $start);
  108.         if (preg_match('/(\d{4})\.\d{1,2}\.\d{1,2}/', $span, $matches) == 0) {
  109.             throw new Exception("Can't fond main_year.");
  110.         }
  111.  
  112.         return $matches[1];
  113.     }
  114.  
  115.     function getMainContents($content, &$result, $mediaName)
  116.     {
  117.         list($title, $id) = getMainTitleId($content, $mediaName);
  118.         $year = getMainYear($content);
  119.  
  120.         $result[] = [
  121.             'title' => $title,
  122.             'id' => $id,
  123.             'year' => $year,
  124.         ];
  125.     }
  126.  
  127.     function getAltContents($content, &$result)
  128.     {
  129.         $find = '동명 콘텐츠';
  130.         $start = strpos($content, $find);
  131.         if ($start === false) {
  132.             return;
  133.         }
  134.  
  135.         $end = $start;
  136.         while (true) {
  137.             $find = '&irk=';
  138.             $start = strpos($content, $find, $end);
  139.             if ($start === false) {
  140.                 return;
  141.             }
  142.  
  143.             $start += strlen($find);
  144.             $end = strpos($content, '&', $start);
  145.             if ($end === false) {
  146.                 return;
  147.             }
  148.  
  149.             $id = substr($content, $start, $end - $start);
  150.  
  151.             $find = 'event);">';
  152.             $start = strpos($content, $find, $end);
  153.             if ($start === false) {
  154.                 continue;
  155.             }
  156.  
  157.             $start += strlen($find);
  158.             $end = strpos($content, '</a>', $start);
  159.             if ($end === false) {
  160.                 continue;
  161.             }
  162.  
  163.             $title = substr($content, $start, $end - $start);
  164.  
  165.             $find = '<span class="f_eb">';
  166.             $start = strpos($content, $find, $end);
  167.             if ($start === false) {
  168.                 continue;
  169.             }
  170.  
  171.             $start += strlen($find);
  172.             $end = strpos($content, '</span>', $start);
  173.             if ($end === false) {
  174.                 continue;
  175.             }
  176.  
  177.             $span = substr($content, $start, $end - $start);
  178.             if (preg_match('/(\d{4})\)/', $span, $matches) == 0) {
  179.                 continue;
  180.             }
  181.  
  182.             $year = $matches[1];
  183.  
  184.             $result[] = [
  185.                 'title' => $title,
  186.                 'id' => $id,
  187.                 'year' => $year,
  188.             ];
  189.         }
  190.     }
  191.  
  192.     function getSeries($content, &$result)
  193.     {
  194.         $find = '<div data-tab="tv_series"';
  195.         $start = strpos($content, $find);
  196.         if ($start === false) {
  197.             return;
  198.         }
  199.  
  200.         $end = strpos($content, '<script', $start);
  201.         if ($end === false) {
  202.             return;
  203.         }
  204.  
  205.         $content = substr($content, $start, $end - $start);
  206.         $end = 0;
  207.  
  208.         while (true) {
  209.             $find = '&irk=';
  210.             $start = strpos($content, $find, $end);
  211.             if ($start === false) {
  212.                 return;
  213.             }
  214.  
  215.             $start += strlen($find);
  216.             $end = strpos($content, '&', $start);
  217.             if ($end === false) {
  218.                 return;
  219.             }
  220.  
  221.             $id = substr($content, $start, $end - $start);
  222.  
  223.             $find = 'class="f_link_b"';
  224.             $end = strpos($content, $find, $end);
  225.             if ($end === false) {
  226.                 return;
  227.             }
  228.  
  229.             $find = 'event);">';
  230.             $start = strpos($content, $find, $end);
  231.             if ($start === false) {
  232.                 continue;
  233.             }
  234.  
  235.             $start += strlen($find);
  236.             $end = strpos($content, '</a>', $start);
  237.             if ($end === false) {
  238.                 continue;
  239.             }
  240.  
  241.             $title = substr($content, $start, $end - $start);
  242.  
  243.             $find = '<span class="f_nb">';
  244.             $start = strpos($content, $find, $end);
  245.             if ($start === false) {
  246.                 continue;
  247.             }
  248.  
  249.             $start += strlen($find);
  250.             $end = strpos($content, '.', $start);
  251.             if ($end === false) {
  252.                 continue;
  253.             }
  254.  
  255.             $year = substr($content, $start, $end - $start);
  256.  
  257.             $result[] = [
  258.                 'title' => $title,
  259.                 'id' => $id,
  260.                 'year' => $year,
  261.             ];
  262.         }
  263.     }
  264.  
  265.     function getJson($content, $mediaName)
  266.     {
  267.         $result = [];
  268.         $tvpColl = getTvpColl($content);
  269.         getMainContents($tvpColl, $result, str_replace(' ', '', $mediaName));
  270.         getAltContents($tvpColl, $result);
  271.         getSeries($tvpColl, $result);
  272.  
  273.         return $result;
  274.     }
  275.  
  276.     function main($mediaName)
  277.     {
  278.         try {
  279.             $content = curl('https://search.daum.net/search?w=tot&q=' . urlencode($mediaName));
  280.             $json = [
  281.                 'data' => getJson($content, $mediaName)
  282.             ];
  283.         } catch(Exception $e) {
  284.             $json = [
  285.                 'error' => (string)$e,
  286.             ];
  287.         }
  288.        
  289.         header('Content-Type: application/json');
  290.         $result = json_encode($json, JSON_UNESCAPED_UNICODE);
  291.         echo $result;
  292.     }
  293.  
  294.     main(array_key_exists('name', $_GET) ? $_GET['name'] : '');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement