Created
December 11, 2020 09:40
-
-
Save book000/5ecd5a2da388eb1b2e3944c18025dae5 to your computer and use it in GitHub Desktop.
アイドルマスターシンデレラガールズ スターライトステージの新着ニュースをWebhookを通じてDiscordに通知する。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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__ . "/starlight-stage_readed.json")) { | |
$readed = json_decode(file_get_contents(__DIR__ . "/starlight-stage_readed.json"), true); | |
} else { | |
$readed = []; | |
} | |
$json = file_get_contents("https://apis.game.starlight-stage.jp/information/ajax_announce"); | |
$json = json_decode($json, true); | |
foreach ($json["announce_list"] as $one) { | |
$id = $one["announce_id"]; | |
$title = $one["title"]; | |
$link_num = $one["link_num"]; | |
$post_at = $one["post_at"]; | |
if (in_array($id, $readed)) { | |
continue; | |
} | |
$url = "https://apis.game.starlight-stage.jp/information/detail/" . $id . "/1/" . "10" . "/" . $link_num; | |
$html = file_get_contents($url); | |
preg_match("/<div class=\"text\">([\s\S]+)<\/div>/", $html, $m); | |
if (!isset($m[1])) { | |
continue; | |
} | |
$text = $m[1]; | |
$text = strip_tags($text); | |
$text = trim($text); | |
sendDiscordWebhook(":new:__**" . $title . "**__ (" . $post_at . ")\n```" . mb_strimwidth($text, 0, 1950, "...") . "```\n" . $url); | |
$readed[] = $id; | |
} | |
file_put_contents(__DIR__ . "/starlight-stage_readed.json", json_encode($readed)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment