此时此刻在访问网站的你应该有留意到左下角的Pio酱,点击后她后会随机的说出一句二刺螈台词,以下就来说说在这个功能背后的aniPhase
api
资源
找了半天发现萌百居然专门有一整页整理了ACG的那些经典语录
所以就恭敬不如从命啦,先用在线爬虫把数据爬下来
然后再简单处理一下就能用了
# encode: gbk
with open("2.txt", 'r', encoding='UTF-8') as f:
r = f.read().split(" • ")
f.close()
print("Before: " + str(len(r)))
f = open("result.txt", "wb")
for i in range(len(r) - 1, -1, -1):
if "o" in r[i]:
r.pop(i)
continue
f.write(r[i].encode("gbk", "ignore") + "!\n".encode("gbk", "ignore"))
print("After: " + str(len(r)))
搭建
下一步就是API的搭建,最终实现效果类似于随机语录API,读取内容后随机输出一条就行了
代码来源于网络,都在用的rbq代码
<?php
$path = dirname(__FILE__);
$file = file($path."/content.txt");
$arr = mt_rand( 0, count( $file ) - 1 );
$content = trim($file[$arr]);
if (isset($_GET['charset']) && !emptyempty($_GET['charset'])) {
$charset = $_GET['charset'];
if (strcasecmp($charset,"gbk") == 0 ) {
$content = mb_convert_encoding($content,'gbk', 'utf-8');
}
} else {
$charset = 'utf-8';
}
header("Content-Type: text/html; charset=$charset");
if ($_GET['format'] === 'js') {
echo "function hitokoto(){document.write(‘" . $content ."‘);}";
} else {
echo $content;
}
博主自建API:https://api.cirno.me/aniphase/
配置Pio
点击Pio后说话是通过 Pio.js
实现的,所以可以通过 xmlHttp
实现访问api,具体如下
touch: function () {
function httpGet(theUrl)
{
var xmlHttp = new XMLHttpRequest();
xmlHttp.open( "GET", theUrl, false ); // false for synchronous request
xmlHttp.send( null );
return xmlHttp.responseText;
}
current.canvas.onclick = function () {
modules.render(httpGet("https://api.cirno.me/aniphase/"));
};
},