/*
* @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 = 1,
period_month = prompt('収集期間: n(ヶ月) (※nヶ月前~昨日までの測定データが対象)', DEFAULT_PERIOD_MONTH);
if (period_month === null) {
return;
}
period_month = parseInt(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',
data_url = (w.URL || w.webkitURL).createObjectURL(blob);
download_link
.attr('download', file_name)
.attr('href', data_url);
if (w.navigator.msSaveOrOpenBlob) {
function open_dialog() {
w.navigator.msSaveOrOpenBlob(blob, file_name);
}
download_link.click(open_dialog).click();
}
else {
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);