Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function curl($url)
- {
- $ua = 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36';
- $curl = curl_init($url);
- curl_setopt($curl, CURLOPT_USERAGENT, $ua);
- curl_setopt($curl, CURLOPT_FAILONERROR, true);
- curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
- $content = curl_exec($curl);
- curl_close($curl);
- if (empty($content)) {
- throw new Exception("Can't get content, url: " . $url);
- }
- return $content;
- }
- function getTvpColl($content)
- {
- $find = '<div id="tvpColl">';
- $start = strpos($content, $find);
- if ($start === false) {
- throw new Exception("Can't find tvp_coll start point.");
- }
- $start += strlen($find);
- $end = strpos($content, '<div class="line"></div><hr/>', $start);
- if ($end === false) {
- throw new Exception("Can't find tvp_coll end point.");
- }
- return substr($content, $start, $end - $start);
- }
- function getMainTitleId($content, $mediaName)
- {
- $find = '<span class="inner_cont">';
- $start = strpos($content, $find);
- if ($start === false) {
- throw new Exception("Can't find main_title <span> start point.");
- }
- $start += strlen($find);
- $end = strpos($content, '</span>', $start);
- if ($end === false) {
- throw new Exception("Can't find main_title <span> end point.");
- }
- $span = substr($content, $start, $end - $start);
- $end = 0;
- $find_id = '&irk=';
- $find_title = 'event);">';
- while (true) {
- $start = strpos($span, $find_id, $end);
- if ($start === false) {
- throw new Exception("Can't find main_id start point.");
- }
- $start += strlen($find_id);
- $end = strpos($span, '&', $start);
- if ($end === false) {
- throw new Exception("Can't find main_id end point.");
- }
- $id = substr($span, $start, $end - $start);
- $start = strpos($span, $find_title, $start);
- if ($start === false) {
- throw new Exception("Can't find main_title start point.");
- }
- $start += strlen($find_title);
- $end = strpos($span, '</a>', $start);
- if ($end === false) {
- throw new Exception("Can't find main_title end point.");
- }
- $title = substr($span, $start, $end - $start);
- if (strpos(str_replace(' ', '', $title), $mediaName) === false) {
- continue;
- }
- return [$title, $id];
- }
- return implode('', $title_piece);
- }
- function getMainYear($content)
- {
- $find = '<span class="txt_summary">';
- $start = strrpos($content, $find);
- if ($start === false) {
- throw new Exception("Can't find main_year start point.");
- }
- $start += strlen($find);
- $end = strpos($content, '</span>', $start);
- if ($end === false) {
- throw new Exception("Can't find main_year end point.");
- }
- $span = substr($content, $start, $end - $start);
- if (preg_match('/(\d{4})\.\d{1,2}\.\d{1,2}/', $span, $matches) == 0) {
- throw new Exception("Can't fond main_year.");
- }
- return $matches[1];
- }
- function getMainContents($content, &$result, $mediaName)
- {
- list($title, $id) = getMainTitleId($content, $mediaName);
- $year = getMainYear($content);
- $result[] = [
- 'title' => $title,
- 'id' => $id,
- 'year' => $year,
- ];
- }
- function getAltContents($content, &$result)
- {
- $find = '동명 콘텐츠';
- $start = strpos($content, $find);
- if ($start === false) {
- return;
- }
- $end = $start;
- while (true) {
- $find = '&irk=';
- $start = strpos($content, $find, $end);
- if ($start === false) {
- return;
- }
- $start += strlen($find);
- $end = strpos($content, '&', $start);
- if ($end === false) {
- return;
- }
- $id = substr($content, $start, $end - $start);
- $find = 'event);">';
- $start = strpos($content, $find, $end);
- if ($start === false) {
- continue;
- }
- $start += strlen($find);
- $end = strpos($content, '</a>', $start);
- if ($end === false) {
- continue;
- }
- $title = substr($content, $start, $end - $start);
- $find = '<span class="f_eb">';
- $start = strpos($content, $find, $end);
- if ($start === false) {
- continue;
- }
- $start += strlen($find);
- $end = strpos($content, '</span>', $start);
- if ($end === false) {
- continue;
- }
- $span = substr($content, $start, $end - $start);
- if (preg_match('/(\d{4})\)/', $span, $matches) == 0) {
- continue;
- }
- $year = $matches[1];
- $result[] = [
- 'title' => $title,
- 'id' => $id,
- 'year' => $year,
- ];
- }
- }
- function getSeries($content, &$result)
- {
- $find = '<div data-tab="tv_series"';
- $start = strpos($content, $find);
- if ($start === false) {
- return;
- }
- $end = strpos($content, '<script', $start);
- if ($end === false) {
- return;
- }
- $content = substr($content, $start, $end - $start);
- $end = 0;
- while (true) {
- $find = '&irk=';
- $start = strpos($content, $find, $end);
- if ($start === false) {
- return;
- }
- $start += strlen($find);
- $end = strpos($content, '&', $start);
- if ($end === false) {
- return;
- }
- $id = substr($content, $start, $end - $start);
- $find = 'class="f_link_b"';
- $end = strpos($content, $find, $end);
- if ($end === false) {
- return;
- }
- $find = 'event);">';
- $start = strpos($content, $find, $end);
- if ($start === false) {
- continue;
- }
- $start += strlen($find);
- $end = strpos($content, '</a>', $start);
- if ($end === false) {
- continue;
- }
- $title = substr($content, $start, $end - $start);
- $find = '<span class="f_nb">';
- $start = strpos($content, $find, $end);
- if ($start === false) {
- continue;
- }
- $start += strlen($find);
- $end = strpos($content, '.', $start);
- if ($end === false) {
- continue;
- }
- $year = substr($content, $start, $end - $start);
- $result[] = [
- 'title' => $title,
- 'id' => $id,
- 'year' => $year,
- ];
- }
- }
- function getJson($content, $mediaName)
- {
- $result = [];
- $tvpColl = getTvpColl($content);
- getMainContents($tvpColl, $result, str_replace(' ', '', $mediaName));
- getAltContents($tvpColl, $result);
- getSeries($tvpColl, $result);
- return $result;
- }
- function main($mediaName)
- {
- try {
- $content = curl('https://search.daum.net/search?w=tot&q=' . urlencode($mediaName));
- $json = [
- 'data' => getJson($content, $mediaName)
- ];
- } catch(Exception $e) {
- $json = [
- 'error' => (string)$e,
- ];
- }
- header('Content-Type: application/json');
- $result = json_encode($json, JSON_UNESCAPED_UNICODE);
- echo $result;
- }
- main(array_key_exists('name', $_GET) ? $_GET['name'] : '');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement