Created
December 11, 2020 09:25
-
-
Save book000/b9b8de68b8c5cd83f083fd4ba7ff8387 to your computer and use it in GitHub Desktop.
ドラゴンクエストタクトの新着ニュースをWebhookを通じてDiscordに通知する。
This file contains hidden or 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__ . "/tact_readed.json")) { | |
$readed = json_decode(file_get_contents(__DIR__ . "/tact_readed.json"), true); | |
} else { | |
$readed = []; | |
} | |
$html = file_get_contents("https://cache.sqex-bridge.jp/guest/information?game_id=238&list=no", false, stream_context_create([ | |
"http" => [ | |
"header" => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Firefox/78.0" | |
] | |
])); | |
preg_match_all("/<a href=\"\/guest\/information\/([0-9]+)\">(.+?)<\/a>/", $html, $m, PREG_SET_ORDER); | |
foreach ($m as $one) { | |
$id = $one[1]; | |
$title = $one[2]; | |
if (in_array($id, $sended)) { | |
continue; | |
} | |
$url = "https://cache.sqex-bridge.jp/guest/information/" . $id; | |
$article = file_get_contents($url, false, stream_context_create($context)); | |
preg_match("/<div class=\"outline\">([\s\S]+)<\/div>[\s\S]+?<p class=\"page-transitions\">/", $article, $m_article); | |
$content = strip_tags($m_article[1]); | |
$content = trim($content); | |
do { | |
$content = preg_replace("/^[ \t]+/mu", "", $content, -1, $count); | |
} while ($count != 0); | |
do { | |
$content = preg_replace("/[ \t]+$/mu", "", $content, -1, $count); | |
} while ($count != 0); | |
$content = str_replace("\r\n", "\n", $content); | |
do { | |
$content = str_replace("\n\n\n", "\n\n", $content, $count); | |
} while ($count != 0); | |
echo "$id - $title\n"; | |
echo $content . "\n"; | |
sendDiscordWebhook("**$title** - $id```$content```"); | |
$sended[] = $id; | |
} | |
file_put_contents(__DIR__ . "/tact_readed.json", json_encode($sended)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment