From 1b0cc45cff541a82f857215959d732fac8fbd441 Mon Sep 17 00:00:00 2001 From: Menci Date: Sun, 15 Jan 2017 11:34:00 +0800 Subject: [PATCH] Add hitokoto --- modules/api_v2.js | 9 +++++++++ modules/index.js | 11 ++++++++++- package.json | 1 + utility.js | 12 +++++++++++- views/index.ejs | 7 +++++++ 5 files changed, 38 insertions(+), 2 deletions(-) diff --git a/modules/api_v2.js b/modules/api_v2.js index a8fe357..4c74818 100644 --- a/modules/api_v2.js +++ b/modules/api_v2.js @@ -50,3 +50,12 @@ app.get('/api/v2/search/problem/:keyword*?', async (req, res) => { res.send({ success: false }); } }); + +app.get('/api/v2/hitokoto', async (req, res) => { + try { + res.send(await syzoj.utils.hitokoto()); + } catch (e) { + syzoj.log(e); + res.status(500).send({ e: e }); + } +}); diff --git a/modules/index.js b/modules/index.js index 7900e2f..f74aa76 100644 --- a/modules/index.js +++ b/modules/index.js @@ -49,11 +49,20 @@ app.get('/', async (req, res) => { let contests = await Contest.query([1, 5], null, [['start_time', 'desc']]); + let hitokoto; + try { + hitokoto = await syzoj.utils.hitokoto(); + } catch (e) { + syzoj.log(e); + hitokoto = null; + } + res.render('index', { ranklist: ranklist, notices: notices, fortune: fortune, - contests: contests + contests: contests, + hitokoto: hitokoto }); } catch (e) { syzoj.log(e); diff --git a/package.json b/package.json index 843a66e..cc165ed 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "mysql": "^2.11.1", "pygmentize-bundled-cached": "^1.1.0", "request": "^2.74.0", + "request-promise": "^4.1.1", "sequelize": "^3.24.3", "session-file-store": "^1.0.0", "sqlite3": "^3.1.4", diff --git a/utility.js b/utility.js index d9b77fd..2e47c24 100644 --- a/utility.js +++ b/utility.js @@ -195,7 +195,7 @@ module.exports = { res = res.filter(x => x); } - + res.spj = list.includes('spj.js'); return res; }, @@ -219,5 +219,15 @@ module.exports = { }, removeTitleTag(s) { return s.replace(/「[\S\s]+?」/, ''); + }, + async hitokoto() { + let request = require('request-promise'); + return await request({ + uri: 'http://api.hitokoto.us/rand', + qs: { + encode: 'json' + }, + json: true + }); } }; diff --git a/views/index.ejs b/views/index.ejs index b25e90a..358ce55 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -50,6 +50,13 @@
+ <% if (hitokoto) { %> +

一言(ヒトコト)

+
+
<%= hitokoto.hitokoto %>
+ <% if (hitokoto.source) { %>
——<%= hitokoto.source %>
<% } %> +
+ <% } %> <% if (fortune) { let color;