Loading... > 动漫也好、小说也好、网络也好,不论在哪里,我们总会看到有那么一两个句子能穿透你的心。我们把这些句子汇聚起来,形成一言网络,以传递更多的感动。如果可以,我们希望我们没有停止服务的那一天。 > 简单来说,一言指的就是一句话,可以是动漫中的台词,也可以是网络上的各种小段子。 或是感动,或是开心,有或是单纯的回忆。来到这里,留下你所喜欢的那一句句话,与大家分享,这就是一言存在的目的[一言官方](https://developer.hitokoto.cn) <div class="tip inlineBlock warning"> **请求说明** </div> ## **接口地址** 地址 | 协议 | 方法 --- | --- | --- https://v1.itggg.cn | HTTPS | Any ## **返回格式** 参数名称 | 描述 --- | --- id | 标识 hitokoto | 正文。编码方式 unicode。使用 utf-8。 type 类型 | 请参考第三节参数的表格 from | 出处 from_who | 作者 creator | 添加者 creator_uid | 添加者用户标识 reviewer | 审核员标识 uuid | 唯一标识 commit_from | 提交方式 created_at | 添加时间 length | 句子长度 ## **请求参数** 参数 | 值 | 可选 | 说明 --- | --- | --- | --- c | 见后表 | 是 | 句子类型 encode | 见后表 | 是 | 返回编码 charset | 见后表 | 是 | 字符集 callback | 如moe | 是 | 调用的异步函数 select | 默认: | 是 | 选择器。配合 encode=js 使用 min_length | 默认:0 | 是 | 返回句子的最小长度(包含) max_length | 默认:30 | 是 | 返回句子的最大长度(包含) ## **句子类型参数** 参数 | 描述 --- | --- a | 动画 b | 漫画 c | 游戏 d | 文学 e | 原创 f | 来自网络 g | 其他 h | 影视 i | 诗词 j | 网易云 k | 哲学 l | 抖机灵 ## **返回编码参数** 参数 | 描述 --- | --- text | 返回纯洁文本 json | 返回格式化后的 JSON 文本 js | 返回指定选择器的同步调用函数。默认选择器为:.hitokoto 其他 | 返回格式化后的 JSON 文本 ## **返回字符集参数** ## **使用示例** https://v1.itggg.cn/ (从7种分类中随机抽取) https://v1.itggg.cn/?c=b (请求获得一个分类是漫画的句子) https://v1.itggg.cn/?c=f&encode=text (请求获得一个来自网络的句子,并以纯文本格式输出) ## **网页使用** ``` <!-- 下面两个标签任意一种都可以,把这个标签加到任意你想要显示的位置,样式可以自己CSS修改 --> <p id="hitokoto_text">酝酿中...</p> <span id="hitokoto_text">『少女祈祷中…』</span> <!-- 以下写法,选取一种即可 --> <!-- 现代写法,推荐(不支持 IE) --> <script> fetch('https://v1.itggg.cn') .then(response => response.json()) .then(data => { const hitokoto = document.getElementById('hitokoto_text') hitokoto.href = 'https://hitokoto.cn/?uuid=' + data.uuid hitokoto.innerText = data.hitokoto }) .catch(console.error) </script> <!-- 如果你配置了 axios --> <script> axios.get('https://v1.itggg.cn') .then(({ data }) => { const hitokoto = document.getElementById('hitokoto_text') hitokoto.href = 'https://hitokoto.cn/?uuid=' + data.uuid hitokoto.innerText = data.hitokoto }) .catch(console.error) </script> <!-- 如果你的站点使用了 jQuery(如果是 JQ 3.x 以及更新的版本, 你得使用完整版的 JQ), 那么你可以... --> <script> $.ajax({ type: 'GET', url: 'https://v1.itggg.cn', dataType: 'jsonp', jsonp: 'callback', jsonpCallback: 'hitokoto', success (data) { $('#hitokoto_text').attr('href', 'https://hitokoto.cn/?uuid=' + data.uuid) $('#hitokoto_text').text(data.hitokoto) }, error (jqXHR, textStatus, errorThrown) { // 错误信息处理 console.error(textStatus, errorThrown) } }) </script> <!-- P.S 我们不推荐使用 jQuery Ajax。 推荐使用 fetch api 或者 axios.js--> <!-- 老式写法,兼容性最好; 支持 IE --> <script> var xhr = new XMLHttpRequest(); xhr.open('get', 'https://v1.itggg.cn'); xhr.onreadystatechange = function () { if (xhr.readyState === 4) { var data = JSON.parse(xhr.responseText); var hitokoto = document.getElementById('hitokoto_text'); hitokoto.href = 'https://hitokoto.cn/?uuid=' + data.uuid hitokoto.innerText = data.hitokoto; } } xhr.send(); </script> <!-- 新 API 方法, 十分简洁 --> <script src="https://v1.itggg.cn/?encode=js&select=%23hitokoto" defer></script> ``` ## **JavaScript** ``` // 本示例需要浏览器支持 Promise,fetch 以及 ES6 语法。 function fetch163Playlist(playlistId) { return new Promise((ok, err) => { fetch(`https://v1.itggg.cn/nm/playlist/${playlistId}`) .then(response => response.json()) .then(data => { const arr = []; data.playlist.tracks.map(function (value) { arr.push(value.id); }); return arr; }) .then(fetch163Songs) .then(ok) .catch(err); }); } function fetch163Songs(Ids) { return new Promise(function (ok, err) { let ids; switch (typeof Ids) { case 'number': ids = [Ids]; break; case 'object': if (!Array.isArray(Ids)) { err(new Error('Please enter array or number')); return; } ids = Ids; break; default: err(new Error('Please enter array or number')); return; break; } fetch(`https://v1.itggg.cn/nm/summary/${ids.join(',')}?lyric=true&common=true`) .then(response => response.json()) .then(data => { var songs = []; data.songs.map(function (song) { songs.push({ name: song.name, url: song.url, artist: song.artists.join('/'), album: song.album.name, pic: song.album.picture, lrc: song.lyric }); }); return songs; }) .then(ok) .catch(err); }); } // 使用测试 fetch163Playlist(2158283120) .then(console.log) .catch(console.error); fetch163Songs([28391863, 22640061]) .then(console.log) .catch(console.error); ``` Last modification:October 19th, 2020 at 06:26 pm © 禁止转载 Support 如果你想请我喝奶茶的话 ×Close Appreciate the author Sweeping payments Pay by AliPay Pay by WeChat
阿杜你好
学习了
赞~!
哈哈,谢谢点赞