1 Star 0 Fork 0

开发者/SmartMirror

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
widgets.js 2.67 KB
一键复制 编辑 原始数据 按行查看 历史
Shinao 提交于 2016-06-17 15:06 . Take and upload photo widget
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));
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/qiqidie/SmartMirror.git
git@gitee.com:qiqidie/SmartMirror.git
qiqidie
SmartMirror
SmartMirror
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385