|
<?php |
|
function sendDiscordWebhook($message) |
|
{ |
|
$url = "<DISCORD WEBHOOK URL>"; |
|
|
|
$data = [ |
|
"content" => $message |
|
]; |
|
$header = array( |
|
"Content-Type: application/json", |
|
"Content-Length: ".strlen(json_encode($data)), |
|
"User-Agent: DiscordBot (http://example.com, v0.0.1)" |
|
); |
|
$context = array( |
|
"http" => array( |
|
"method" => "POST", |
|
"header" => implode("\r\n", $header), |
|
"content" => json_encode($data) |
|
) |
|
); |
|
$context = stream_context_create($context); |
|
file_get_contents($url, false, $context); |
|
} |
|
|
|
if (file_exists(__DIR__ . "/kirara_app_readed.json")) { |
|
$readed = json_decode(file_get_contents(__DIR__ . "/kirara_app_readed.json"), true); |
|
} else { |
|
$readed = []; |
|
} |
|
|
|
$xml = simplexml_load_file("https://krr-prd-web.star-api.com/news/feed/"); |
|
foreach ($xml->channel->item as $key => $item) { |
|
if ($key == 1) { |
|
continue; |
|
} |
|
//print_r($item); |
|
$title = (string)$item->title; |
|
$link = (string)$item->link; |
|
$pubDate = (string)$item->pubDate; |
|
|
|
$html = file_get_contents($link, false, $context); |
|
|
|
foreach ($http_response_header as $header) { |
|
$header = explode(": ", $header); |
|
if ($header[0] == "Content-Encoding" && $header[1] == "gzip") { |
|
$html = gzdecode($html); |
|
} |
|
} |
|
|
|
preg_match("/<div class=\"newsText\">([\s\S]+)<\/div>/", $html, $m); |
|
|
|
if (!isset($m[1])) { |
|
// 再取得 |
|
sleep(1); |
|
$html = file_get_contents($link, false, $context); |
|
|
|
foreach ($http_response_header as $header) { |
|
$header = explode(": ", $header); |
|
if ($header[0] == "Content-Encoding" && $header[1] == "gzip") { |
|
$html = gzdecode($html); |
|
} |
|
} |
|
|
|
preg_match("/<div class=\"newsText\">([\s\S]+)<\/div>/", $html, $m); |
|
|
|
if (!isset($m[1])) { |
|
// 取得失敗 |
|
continue; |
|
} |
|
} |
|
$text = $m[1]; |
|
|
|
$text = preg_replace("/<strong>(.+?)<\/strong>/", "**$1**", $text); |
|
$text = preg_replace("/<del.+?>(.+?)<\/del>/", " ~~$1~~ ", $text); |
|
$text = preg_replace("/<h1>(.+?)<\/h1>/", "__**$1**__\n", $text); |
|
|
|
$text = str_replace("<br>", "\n", $text); |
|
$text = str_replace("<br/>", "\n", $text); |
|
$text = str_replace("</br>", "\n", $text); |
|
$text = str_replace("<br />", "\n", $text); |
|
$text = str_replace(" ", "\n\n", $text); |
|
$text = str_replace("</p><p>", "\n", $text); |
|
|
|
$text = strip_tags($text); |
|
$text = trim($text); |
|
|
|
echo $text; |
|
if (!isset($readed[$link])) { |
|
sendDiscordWebhook(":new:__**" . $title . "**__ (" . date("Y/m/d H:i:s", strtotime($pubDate)) . ")\n\n" . mb_strimwidth($text, 0, 1950, "...") . "\n\n" . $link); |
|
$readed[$link] = $title; |
|
} elseif (isset($readed[$link]) && $readed[$link] != $title) { |
|
sendDiscordWebhook(":arrows_counterclockwise:__**" . $title . "**__ (" . date("Y/m/d H:i:s", strtotime($pubDate)) . ")\n\n" . mb_strimwidth($text, 0, 1950, "...") . "\n\n" . $link); |
|
$readed[$link] = $title; |
|
} |
|
sleep(1); |
|
} |
|
file_put_contents(__DIR__ . "/kirara_app_readed.json", json_encode($readed)); |