Radish Network Speed Testing の測定結果をダウンロード

    @@ -177,7 +177,8 @@ var speed_info_list = [], until_date = new Date(), since_date, - download_link = $('<a/>'), + download_link_id = 'data-to-csv', + download_link = $('a#' + download_link_id), page = 1, link_text = '【#PAGE#ページ目データ: #COUNT# 件 (合計:#TOTAL# 件)】'; @@ -185,11 +186,17 @@ since_date = new Date(until_date.getTime()) since_date.setMonth(until_date.getMonth() - period_month); - download_link.css({ - 'margin-left' : '32px' - , 'font-size' : '16px' - , 'background-color' : 'yellow' - }); + if (0 < download_link.size()) { + download_link.remove(); + } + download_link = $('<a/>') + .attr('id', download_link_id) + .css({ + 'margin-left' : '32px' + , 'font-size' : '16px' + , 'background-color' : 'yellow' + }); + $('div.title1').append(download_link); function get_csv_line(csv_columns) { @@ -245,11 +252,9 @@ download_link .attr('download', file_name) .attr('href', data_url) - .css({'display' : 'none'}); download_link.get(0).click(); } - download_link.remove(); } function load_page(page_info) {
  • /*
     * @title Radish Network Speed Testing の測定結果をダウンロード
     * @description 「みんなの測定結果」の絞り込み検索後に実行するとCSVファイルがダウンロードできる
     * @include http://netspeed.studio-radish.com/cgi-bin/netspeed/openresult/openresult.cgi?*
     * @license MIT License
     * @private
     */
    
    (function(w, d) {
    
    var DEFAULT_PERIOD_MONTH = 3,
        period_month = parseInt( prompt('対象月数(MM) (※MMヶ月前~昨日までの測定データが対象)', DEFAULT_PERIOD_MONTH), 10 );
    
    if (period_month <= 0) {
        period_month = DEFAULT_PERIOD_MONTH;
    }
    
    var script = d.createElement('script');
    script.src = '//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js';
    d.body.appendChild(script);
    function check() {
        if (!w.jQuery) {
            setTimeout(check, 100);
            return;
        }
        var $ = w.jQuery;
        
        function change_speed_to_Mbps(speed) {
            if (!$.trim(speed).match(/([\d.]+)\s*(\w?)bps/i)) {
                return speed;
            }
            var value = RegExp.$1,
                unit = RegExp.$2;
            
            switch (unit.toUpperCase()) {
                case 'K':
                    value /= 1000.0;
                    break;
                case 'M':
                    break;
                case 'G':
                    value *= 1000.0;
                    break;
                case 'T':
                    value *= 1000000.0;
                    break;
                default:
                    value /= 1000000.0;
                    break;
            }
            return value;
        }
        
        function get_filter_info() {
            var option_text_list = [],
                filter_setting = $('div#filter_setting');
            
            filter_setting.find('select').each(function() {
                var select = $(this),
                    value = select.val();
                
                select.find('option[value=' + value + ']').each(function() {
                    var option = $(this),
                        text = option.text().replace(/[─│┌┐┘└├┬┤┴┼]/g, '');
                    
                    option_text_list.push(text);
                });
            });
            
            var zipcode = '';
            
            filter_setting.find('input').each(function() {
                var input = $(this),
                    type = input.attr('type'),
                    name = input.attr('name'),
                    value = input.val();
                
                switch (type) {
                    case 'checkbox':
                        option_text_list.push( value + '=' + (input.prop('checked') ? 'ON' : 'OFF') );
                        break;
                    case 'text':
                        if (name == 'zipcode1') {
                            zipcode += value;
                        }
                        else if (name == 'zipcode2') {
                            zipcode += '-' + value;
                            option_text_list.push(zipcode);
                        }
                        break;
                }
            });
            
            return option_text_list.join('・');
        }
    
        function get_date_string(date) {
            return date.getFullYear() + '/' + (parseInt(date.getMonth(),10) + 1) + '/' + date.getDate();
        }
        
        function get_time_string(date) {
            return date.getHours() + ':' + date.getMinutes();
        }
        
        function get_date(speed_data) {
            if (!speed_data.match(/(\d{4})\/(\d{1,2})\/(\d{1,2})[\s()a-zA-Z]+(\d{1,2}):(\d{1,2})/)) {
                return null;
            }
            return new Date(RegExp.$1, RegExp.$2 - 1, RegExp.$3, RegExp.$4, RegExp.$5);
        }
        
        function get_speed_info(table) {
            var downlink_speed = change_speed_to_Mbps(table.find('span.graphmarker').first().text()),
                speed_data = table.find('div.speeddata').first().text(),
                date = get_date(speed_data),
                topic_id = $.trim(table.find('a[href *= "openresult.cgi?topic"]').first().text());
            
            if (table.find('nobr:contains(下り)').size() < 1) {
                downlink_speed = -1;
            }
            
            return {
                downlink_speed : downlink_speed
            ,   speed_data : speed_data
            ,   date : date
            ,   topic_id : topic_id
            };
        }
        
        var get_page_info = (function() {
            var next_url_dict = {}, topic_id_dict = {};
            
            return function(page_root, since_date, until_date) {
                var speed_info_list = [],
                    next_url = null,
                    first_date = new Date();
                
                page_root.find('table.graphscale').each(function() {
                    var table = $(this).parents('table').eq(1),
                        speed_info = get_speed_info(table),
                        target_date = speed_info.date,
                        topic_id = speed_info.topic_id;
                    
                    if (topic_id_dict[topic_id]) {
                        return;
                    }
                    topic_id_dict[topic_id] = true;
                    
                    if (target_date < first_date) {
                        first_date = target_date;
                    }
                    if (target_date < since_date || until_date <= target_date) {
                        return;
                    }
                    if (speed_info.downlink_speed < 0) {
                        return;
                    }
                    speed_info_list.push(speed_info);
                });
                
                if (since_date <= first_date) {
                    next_url = page_root.find('a[href *= "openresult.cgi"]:contains(過去のデータへ)').first().attr('href');
                    if (next_url_dict[next_url]) {
                        next_url = null;
                    }
                    next_url_dict[next_url] = true;
                }
                
                return {
                    speed_info_list : speed_info_list
                ,   next_url : next_url
                }
            };
        })();
        
        (function() {
            var speed_info_list = [],
                until_date = new Date(),
                since_date,
                download_link_id = 'data-to-csv',
                download_link = $('a#' + download_link_id),
                page = 1,
                link_text = '【#PAGE#ページ目データ: #COUNT# 件 (合計:#TOTAL# 件)】';
            
            until_date = new Date(until_date.getFullYear(), until_date.getMonth(), until_date.getDate());
            since_date = new Date(until_date.getTime())
            since_date.setMonth(until_date.getMonth() - period_month);
            
            if (0 < download_link.size()) {
                download_link.remove();
            }
            download_link = $('<a/>')
                .attr('id', download_link_id)
                .css({
                    'margin-left' : '32px'
                ,   'font-size' : '16px'
                ,   'background-color' : 'yellow'
                });
            
            $('div.title1').append(download_link);
            
            function get_csv_line(csv_columns) {
                var output_columns = [];
                $.each(csv_columns, function(index, csv_column) {
                    output_columns.push('"' + csv_column + '"');
                });
                return output_columns.join(',');
            }
            
            function finish() {
                var csv_lines = [],
                    max_date = new Date(until_date.getTime());
                
                max_date.setDate(until_date.getDate() - 1);
                
                csv_lines.push(get_csv_line([
                    get_date_string(since_date)
                ,   '~'
                ,   get_date_string(max_date)
                ,   get_filter_info()
                ]));
                
                csv_lines.push(get_csv_line([
                    'Topic ID'
                ,   '測定日'
                ,   '時刻'
                ,   '下り速度(Mbps)'
                ]));
            
                $.each(speed_info_list, function(index, speed_info) {
                    var date = speed_info.date;
                    
                    csv_lines.push(get_csv_line([
                        speed_info.topic_id
                    ,   get_date_string(date)
                    ,   get_time_string(date)
                    ,   speed_info.downlink_speed
                    ]));
                });
                
                var csv = csv_lines.join('\n'),
                    bom = new Uint8Array([0xEF, 0xBB, 0xBF]),
                    blob = new Blob([bom, csv], {'type' : 'text/csv'}),
                    file_name = 'netspeed.studio-radish.com_' + period_month + 'months_' + (new Date().getTime()) + '.csv';
                
                if (w.navigator.msSaveOrOpenBlob) {
                    w.navigator.msSaveOrOpenBlob(blob, file_name);
                }
                else {
                    var data_url = (w.URL || w.webkitURL).createObjectURL(blob);
                    
                    download_link
                        .attr('download', file_name)
                        .attr('href', data_url)
                    
                    download_link.get(0).click();
                }
            }
            
            function load_page(page_info) {
                $.merge(speed_info_list, page_info.speed_info_list);
                
                download_link.text(
                    link_text
                        .replace(/#PAGE#/g, page)
                        .replace(/#COUNT#/g, page_info.speed_info_list.length)
                        .replace(/#TOTAL#/g, speed_info_list.length)
                );
                
                if (!page_info.next_url) {
                    finish(since_date, until_date);
                    return;
                }
                $.get(page_info.next_url, function(html){
                    var html = html.replace(/(?:^[\s\S]*?<body.*?>|<\/body>[\s\S]*$)/gi, '');
                    load_page( get_page_info($('<div/>').html(html), since_date, until_date) );
                }, 'html');
                
                page ++;
            }
            
            load_page( get_page_info($('body'), since_date, until_date) );
        })();
    }
    check();
    })(window, document);
    
  • Permalink
    このページへの個別リンクです。
    RAW
    書かれたコードへの直接のリンクです。
    Packed
    文字列が圧縮された書かれたコードへのリンクです。
    Userscript
    Greasemonkey 等で利用する場合の .user.js へのリンクです。
    Loader
    @require やソースコードが長い場合に多段ロードする Loader コミのコードへのリンクです。
    Metadata
    コード中にコメントで @xxx と書かれたメタデータの JSON です。

History

  1. 2015/08/17 15:49:47 - 2015-08-17
  2. 2015/08/17 15:49:34 - 2015-08-17
  3. 2015/08/17 12:09:43 - 2015-08-17
  4. 2015/08/17 12:09:29 - 2015-08-17
  5. 2015/08/15 20:55:31 - 2015-08-15
  6. 2015/08/14 08:22:03 - 2015-08-14
  7. 2015/08/14 06:44:15 - 2015-08-14
  8. 2015/08/14 06:35:46 - 2015-08-14
  9. 2015/08/14 06:32:02 - 2015-08-14
  10. 2015/08/14 01:06:45 - 2015-08-14
  11. 2015/08/14 00:36:27 - 2015-08-14
  12. 2015/08/13 22:55:36 - 2015-08-13
  13. 2015/08/13 22:44:49 - 2015-08-13
  14. 2015/08/13 22:37:43 - 2015-08-13
  15. 2015/08/13 21:58:37 - 2015-08-13
  16. 2015/08/13 21:57:14 - 2015-08-13
  17. 2015/08/13 21:56:34 - 2015-08-13
  18. 2015/08/13 21:55:38 - 2015-08-13