Skip to content

Instantly share code, notes, and snippets.

@eban
Last active March 23, 2019 23:49
Show Gist options
  • Save eban/7754f8cf42c5ac480953 to your computer and use it in GitHub Desktop.
Save eban/7754f8cf42c5ac480953 to your computer and use it in GitHub Desktop.
Feed generator for tenki.jp
module['exports'] = function Tenkijp(hook) {
var md5 = require("md5");
if (md5(hook.params.magic) != "0f20e92d496b9b9a2ce77c0c5b0c24b9") {
console.log("md5 error", hook.req.headers['user-agent'], hook.req.headers['x-forwarded-for'], hook.params.magic);
return;
}
var cheerio = require("cheerio");
var req = require("request");
var rss = require("rss");
var url = hook.params.url || "http://www.tenki.jp/amedas/3/14/43241.html";
var ip = hook.req.headers['x-forwarded-for'];
var ua = hook.req.headers['user-agent'];
if (!ua.match(/curl|Mozilla/)) {
console.log("user-agent error", ua, "magic", hook.params.magic);
return;
}
req(url, function(error, res, body) {
if (!error && res.statusCode == 200) {
var $ = cheerio.load(body);
var date = $("td.bold").eq(1),
time = date.next(),
deg = time.next(),
rain = deg.next(),
wdir = rain.next(),
wind = wdir.next();
var rdate = $("h3 > time").attr("datetime");
var fragment = rdate.replace(/\D+/g, "");
var title = $("title").text();
// var imgurl = $("table.amedas-point-graph-table > tr:nth-child(1) > td > img").attr("src")
var imgurl = $(".amedas-point-graph-table > tbody:nth-child(1) > tr:nth-child(1) > td:nth-child(2) > img:nth-child(1)").attr("src")
var string = title.replace(/\(.*/, "") + " " +
date.text() + " " +
time.text() + " " +
"気温" + deg.text() + "度 " +
"降水" + "量" + rain.text() + "mm " +
wdir.text() + "の風" +
wind.text() + "m";
var feed = new rss({title: title, description: title});
feed.item({
title: string,
description: "<img src=" + imgurl + "></img>",
url: url + "?" + fragment
});
hook.debug(ua, ip, string);
//hook.debug(feed.xml());
hook.res.writeHead(200, { 'Content-Type': 'text/xml' });
hook.res.end(feed.xml());
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment