aboutsummaryrefslogtreecommitdiffstats
path: root/notifier/subject_weather_yahoo.js
blob: 648cc4f7763d88f05aa6b530354e6f4a81218e6e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// PLUGIN_INFO//{{
var PLUGIN_INFO =
<VimperatorPlugin>
    <name>{NAME}</name>
    <description>yahoo weather forecast notice.</description>
    <description lang="ja">ヤフー天気予報通知</description>
    <author mail="suvene@zeromemory.info" homepage="http://zeromemory.sblo.jp/">suVene</author>
    <version>0.1.2</version>
    <minVersion>2.0pre</minVersion>
    <maxVersion>2.0pre</maxVersion>
    <updateURL>http://svn.coderepos.org/share/lang/javascript/vimperator-plugins/trunk/notifier/subject_weather_yahoo.js</updateURL>
    <detail><![CDATA[
== Options ==
>||
liberator.globalVariables.subject_weather_yahoo_urls =  [url1, url2,]
||<
- @see http://weather.yahoo.co.jp/weather/
    ]]></detail>
</VimperatorPlugin>;
//}}}
(function() {

var notifier = liberator.plugins.notifier;
if (!notifier) return;

var libly = notifier.libly;
var $U = libly.$U;
var logger = $U.getLogger('subject_weather_yahoo');

var URLs = liberator.globalVariables.subject_weather_yahoo_urls || [
    'http://weather.yahoo.co.jp/weather/jp/27/6200/27127/5300001.html',
    'http://weather.yahoo.co.jp/weather/jp/13/4410/13113/1500001.html'
];

URLs.forEach(function(url) {
    notifier.subject.register(notifier.SubjectHttp, {
        interval: 10,
        options: {
            url: url,
            headers: null,
            extra: { encoding: 'euc-jp' }
        },
        parse: function(res) {
            var parsed = res.getHTMLDocument(
                            'id("cat-pass") | id("yjw_pinpoint_today") | id("yjw_pinpoint_tomorrow")'
                         );
            if (!parsed.length) return;

            var now = new Date();
            var hours = now.getHours();
            var start = Math.floor(hours / 3) + 1;
            var yahooToday = $U.getFirstNodeFromXPath('descendant::h3/span/text()', parsed[1]);
            yahooToday = yahooToday.textContent.replace(/.*\u6708(\d+)\u65E5.*/, '$1'); // 月($1)日
            var source, cloneTable;


            if (yahooToday == now.getDate()) {
                source = parsed[1];
            } else {
                source = parsed[2];
            }

            var table = $U.getFirstNodeFromXPath('descendant::table', source);
            table.style.width = '95%';
            table.style.color = '#222';
            var cloneTable = table.cloneNode(false);
            this.cloneTable(cloneTable, table, start, start + 3, true);

            // concat tommorow
            if (start > 6)
                this.cloneTable(cloneTable,
                        $U.getFirstNodeFromXPath('descendant::table', parsed[2]),
                        1, (3 - (8 - start)), false);

            var next = new Date();
            next.setHours(now.getHours() + 1)
            next.setMinutes(0);
            next.setSeconds(0);
            this.interval = (next.getTime() - now.getTime()) / 1000;

            var df = window.content.document.createDocumentFragment();
            df.appendChild(parsed[0]);
            df.appendChild(cloneTable);
            return df;
        },
        cloneTable: function(source, table, start, end, withHead) {
            var rows = table.getElementsByTagName('tr');
            for (let r = 0, len = rows.length; r < len; r++) {
                let row = withHead ? rows[r].cloneNode(false) : source.getElementsByTagName('tr')[r];
                let cols = rows[r].getElementsByTagName('td');
                if (withHead)
                    source.appendChild(row);
                for (let c = 0, len2 = cols.length; c < len2; c++) {
                    if ((withHead && c == 0) || (start <= c && c < end)) {
                        row.appendChild(cols[c].cloneNode(true));
                    }
                }
            }
            return source;
        },
        buildMessages: function(diff) {
            return new notifier.Message('Weather forecast by Yahoo!', $U.xmlSerialize(diff), url);
        }
    });
});

})();
// vim: set fdm=marker sw=4 ts=4 sts=0 et: