Skip to content

Commit

Permalink
Fix pediy wrong timezone issue (#873)
Browse files Browse the repository at this point in the history
  • Loading branch information
renzhexigua authored and DIYgod committed Oct 14, 2018
1 parent c0c7817 commit 7881264
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 29 deletions.
60 changes: 31 additions & 29 deletions routes/pediy/topic.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const axios = require('../../utils/axios');
const dateHelper = require('../../utils/date');
const pediyUtils = require('./utils');
const cheerio = require('cheerio');

const baseUrl = 'https://bbs.pediy.com/';
Expand Down Expand Up @@ -56,40 +56,42 @@ module.exports = async (ctx) => {
},
});

const data = response.data;
const $ = cheerio.load(data);
const $ = cheerio.load(response.data);
const list = $('.thread');

ctx.state.data = {
title: `${title}`,
link: baseUrl + path,
item: $('.thread')
.map((_, elem) => {
const subject = $('.subject a', elem).eq(1);
const author = $('.username', elem).eq(0);
item:
list &&
list
.map((_, elem) => {
const subject = $('.subject a', elem).eq(1);
const author = $('.username', elem).eq(0);

let pubDate = $('.date', elem).eq(0);
if (pubDate.text().indexOf('前') !== -1) {
pubDate = dateHelper(pubDate.text(), 8);
} else {
pubDate = pubDate.text();
}
const pubDate = pediyUtils.dateParser(
$('.date', elem)
.eq(0)
.text(),
8
);

let topic;
if (isSpecific) {
topic = categoryId[category][1];
} else {
topic = $('.subject a.small', elem)
.eq(0)
.text();
}
let topic;
if (isSpecific) {
topic = categoryId[category][1];
} else {
topic = $('.subject a.small', elem)
.eq(0)
.text();
}

return {
title: subject.text(),
link: baseUrl + subject.attr('href'),
pubDate: pubDate,
description: `δ½œθ€…: ${author.text()} η‰ˆε—: ${topic}`,
};
})
.get(),
return {
title: subject.text(),
link: baseUrl + subject.attr('href'),
pubDate: `${pubDate}`,
description: `δ½œθ€…: ${author.text()} η‰ˆε—: ${topic}`,
};
})
.get(),
};
};
27 changes: 27 additions & 0 deletions routes/pediy/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const pediyUtils = {
dateParser: (html, timeZone) => {
let math;
let date = new Date();
if (/(\d+)εˆ†ι’Ÿε‰/.exec(html)) {
math = /(\d+)εˆ†ι’Ÿε‰/.exec(html);
date.setMinutes(date.getMinutes() - math[1]);
return date.toUTCString();
} else if (/(\d+)小既前/.exec(html)) {
math = /(\d+)小既前/.exec(html);
date.setHours(date.getHours() - math[1]);
return date.toUTCString();
} else if (/(\d+)倩前/.exec(html)) {
math = /(\d+)倩前/.exec(html);
date.setDate(date.getDate() - math[1]);
return date.toUTCString();
} else if (/(\d+)-(\d+)-(\d+) (\d+):(\d+)/.exec(html)) {
math = /(\d+)-(\d+)-(\d+) (\d+):(\d+)/.exec(html);
date = new Date(math[1], parseInt(math[2]) - 1, math[3], math[4], math[5]);
const serverOffset = new Date().getTimezoneOffset() / 60;
return new Date(date.getTime() - 60 * 60 * 1000 * (timeZone + serverOffset)).toUTCString();
}
return html;
},
};

module.exports = pediyUtils;

0 comments on commit 7881264

Please sign in to comment.