Browse Source

Request hitokoto on client side

master
Menci 8 years ago
parent
commit
f49e5f4790
  1. 9
      modules/api_v2.js
  2. 9
      modules/index.js
  3. 17
      utility.js
  4. 15
      views/index.ejs

9
modules/api_v2.js

@ -69,15 +69,6 @@ app.get('/api/v2/search/tags/:keyword*?', async (req, res) => {
}
});
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 });
}
});
app.post('/api/v2/markdown', async (req, res) => {
try {
let s = await syzoj.utils.markdown(req.body.s.toString(), null, req.body.noReplaceUI === 'true');

9
modules/index.js

@ -45,20 +45,11 @@ app.get('/', async (req, res) => {
else where = { is_public: true };
let contests = await Contest.query([1, 5], where, [['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,
hitokoto: hitokoto,
links: syzoj.config.links
});
} catch (e) {

17
utility.js

@ -325,23 +325,6 @@ module.exports = {
md5.update(data);
return md5.digest('hex');
},
async hitokoto() {
try {
let request = require('request-promise');
let res = await request({
uri: 'https://sslapi.hitokoto.cn',
timeout: 1500,
qs: {
c: 'a'
},
json: true
});
if (!res.hitokoto) return null;
else return res;
} catch (e) {
return null;
}
},
isValidUsername(s) {
return /^[a-zA-Z0-9\-\_]+$/.test(s);
},

15
views/index.ejs

@ -79,8 +79,19 @@
<% if (hitokoto) { %>
<h4 class="ui top attached block header">一言(ヒトコト)</h4>
<div class="ui bottom attached center aligned segment">
<div style="font-size: 1em; line-height: 1.5em; "><%= hitokoto.hitokoto %></div>
<% if (hitokoto.from) { %><div style="text-align: right; margin-top: 15px; font-size: 0.9em; color: #666; ">——<%= hitokoto.from %></div><% } %>
<div class="ui active centered inline loader" id="hitokoto-loader"></div>
<script>
$.get('https://sslapi.hitokoto.cn/?c=a', function (data) {
data = JSON.parse(data);
$('#hitokoto-loader').removeClass('active');
$('#hitokoto-content').css('display', '').text(data.hitokoto);
if (data.from) {
$('#hitokoto-from').css('display', '').text('——' + data.from);
}
});
</script>
<div style="font-size: 1em; line-height: 1.5em; display: none; " id="hitokoto-content"></div>
<div style="text-align: right; margin-top: 15px; font-size: 0.9em; color: #666; display: none; " id="hitokoto-from"></div>
</div>
<% } %>
<%

Loading…
Cancel
Save