Skip to content

Instantly share code, notes, and snippets.

@SansWord
Last active August 29, 2015 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SansWord/cdaf2bbeb8929594f05f to your computer and use it in GitHub Desktop.
Save SansWord/cdaf2bbeb8929594f05f to your computer and use it in GitHub Desktop.
Dump fortune of each day using javascript.
/*jslint indent: 4 */
/*global
jQuery: false,
alert: false,
window: false,
queryNext: false,
*/
/**
* dump fortune of each day using javascript.
*
* which I don't buy it. only for production purpose.
*
* you should use this function in
* http://tw.18dao.net/
* this website to avoid cross-access restriction.
*
* modify success to save result to a global object,
* modify complete to execute next query,
*
* finally, use saveAsText to download total result.
*/
var startTime,
TOTAL_RESULT = [],
FORCE_STOP = false,
END_YEAR = 2023;
function add0(value) {
return (value < 10 ? (value === 0 ? "00" : "0" + value) : value);
}
function extractContent(content, start, end) {
var resultAndNext = content.substring(content.indexOf(start));
var nextIndex = resultAndNext.indexOf(end);
return resultAndNext.substring(0, nextIndex).trim().substring(start.length + 1).trim();
}
function queryFortune(year, month, day) {
console.log("query:" + add0(year) + "_" + add0(month) + "_" + add0(day));
jQuery.ajax(
"http://tw.18dao.net/%E6%AF%8F%E6%97%A5%E9%BB%83%E6%9B%86/" + year + "%E5%B9%B4" + month + "%E6%9C%88" + day + "%E6%97%A5",
{
success: function (content) {
var good = "【宜】",
bad = "【忌】",
lunarDate = "【農曆】",
end = "</p>",
goodString = extractContent(content, good, end),
badString = extractContent(content, bad, end),
lunarString = extractContent(content, lunarDate, end),
fortune = {
good: goodString,
bad: badString,
lunarDate: lunarString,
date: add0(year) + "_" + add0(month) + "_" + add0(day)
};
// console.log(fortune);
TOTAL_RESULT.push(fortune);
},
error: function () {
console.log("error");
},
complete: function () {
setTimeout(function () {
queryNext(year, month, day);
}, 300);
}
}
);
}
function queryNext(year, month, day) {
if (FORCE_STOP) {
return;
}
if (day < 31) {
queryFortune(year, month, day + 1);
} else {
if (month < 12) {
queryFortune(year, month + 1, 1);
} else {
if (year < END_YEAR) {
queryFortune(year + 1, 1, 1);
} else {
alert(
"complete! last date is:"
+ add0(year) + "_" + add0(month) + "_" + add0(day)
+ ", it takes "
+ (new Date().getTime() - startTime.getTime())
+ "ms"
);
}
}
}
}
function saveAsText(obj) {
var src = 'data:text/html;charset=utf-8,<!DOCTYPE%20html><html%20lang%3D"en"><head><%2Fhead><body>' + encodeURIComponent(JSON.stringify(obj)) + '<%2Fbody><%2Fhtml>';
window.open(src, '_blank');
}
@tensionHuang
Copy link

so cool~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment