Browse Source

Add hitokoto

pull/6/head
Menci 8 years ago
parent
commit
1b0cc45cff
  1. 9
      modules/api_v2.js
  2. 11
      modules/index.js
  3. 1
      package.json
  4. 12
      utility.js
  5. 7
      views/index.ejs

9
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 });
}
});

11
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);

1
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",

12
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
});
}
};

7
views/index.ejs

@ -50,6 +50,13 @@
</div>
</div>
<div class="right floated five wide column">
<% if (hitokoto) { %>
<h4 class="ui top attached block header">一言(ヒトコト)</h4>
<div class="ui bottom attached center aligned segment">
<div style="margin-top: 5px; font-size: 1em; line-height: 1.5em; "><%= hitokoto.hitokoto %></div>
<% if (hitokoto.source) { %><div style="text-align: right; margin-top: 15px; font-size: 0.9em; color: #666; ">——<%= hitokoto.source %></div><% } %>
</div>
<% } %>
<%
if (fortune) {
let color;

Loading…
Cancel
Save