代码拉取完成,页面将自动刷新
var request = require('request');
var cheerio = require('cheerio');
var config = require('./config');
var news = [];
var refreshNews = function() {
request(config.widget.news.url, function (error, response, xml) {
if (error || response.statusCode !== 200)
return;
var parseString = require('xml2js').parseString;
parseString(xml, function (err, result) {
items = result.rss.channel[0].item;
items.forEach(function(item) {
var entry_news = new Object();
entry_news.title = item.title;
entry_news.description = item.description;
entry_news.category = item.category;
entry_news.img = item.thumbnail[0].$.url.replace("medium2", "bigger");
news.push(entry_news);
});
});
});
}
setInterval(refreshNews, 1000 * 60 * config.widget.news.refreshRateInMinutes);
refreshNews();
module.exports.news = function (req, res) {
res.send(JSON.stringify(news));
}
var weather = {};
var refreshWeather = function() {
request({url: config.widget.weather.url, json: true}, function (error, response, body) {
if (!error && response.statusCode === 200) {
weather = response;
}
});
}
setInterval(refreshWeather, 1000 * 60 * config.widget.weather.refreshRateInMinutes);
refreshWeather();
module.exports.weather = function (req, res) {
res.send(weather);
}
var movies = [];
var refreshMovies = function() {
request(config.widget.cinema.url, function (error, response, html) {
if (error || response.statusCode !== 200)
return;
var $ = cheerio.load(html);
$('.hred').each(function(i, elem) {
var movie = new Object();
movie.title = $(this).find('.meta-title-link').text().trim();
movie.img = $(this).find('.thumbnail-img').attr('data-src');
movie.duration = $(this).find('.meta-body-item').text();
var indexDuration = movie.duration.indexOf("|");
movie.duration = movie.duration.substring(indexDuration + 1, indexDuration + 10).trim().replace(" ", "").replace("min", "");
movie.pressRating = $(this).find('.stareval-note').first().text().trim();
movie.peopleRating = $(this).find('.stareval-note').eq(1).text().trim();
movie.totalRating = parseInt(movie.pressRating) + parseInt(movie.peopleRating);
movie.hours = [];
$(this).find('.showtimes-format').first().find('.hours-item-value').each(function(i, elem) {
movie.hours.push($(this).text());
});
if (!isNaN(movie.totalRating) && movie.totalRating > 1 && movie.hours.length > 0)
movies.push(movie);
});
//movies.sort(function(a, b) { return (a.totalRating > b.totalRating) ? -1 : 1;} );
});
}
setInterval(refreshMovies, 1000 * 60 * config.widget.cinema.refreshRateInMinutes);
refreshMovies();
module.exports.cinema = function (req, res) {
res.send(JSON.stringify(movies));
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。