基于先前系统导出汉字库的拼音获取
正常来讲,一般拿到这个问题后,语言选型应该是Python吧,但是考虑到项目全栈JS,又考虑到后续的兼容性,所以就选了JavaScript,本来挺简单的,因为存在异步的问题,还是有一些小曲折的,代码逻辑极其简单,不多作注释了。
具体讲讲获取方法,找到一个提供异步提交获取汉字读音的网站上作循环提交,但是我根据Chrome Tools里的请求信息,实在构造不好请求(https请求),一直提交失败,好在chrome支持将请求复制为curl命令,又好在JavaScript天然支持执行bash或cmd的原生命令,所以技术路线已经很明显了,根据拿到的curl,替换掉查询部分值,构造好命令字符串后,JavaScript调用接口执行命令,获得返回值,提取Payload部分,判断是否多音字等,一系列处理后存入数据库,一个字和它每个读音为一条记录。
好在网站不校验我这种无良做法。所以不太需要人为干预获取过程。
字库2W+,最初直接获取全部结果集+一个循环读取+直接请求,电脑喜闻乐见地死掉了,后来改为使用LIMIT方式一条一条获取,外层一个Interval 500ms检查一次上次请求完事儿没,完事儿了就获取下一个字,再请求这个字的读音,这么做时间慢一些(乐观时间估计:20000 / 2 / 60 /60 = 2.78h),但起码不影响电脑的使用,而且也算得上无良届的良心做法了。
var mysql = require('mysql');
var exec = require('child_process').exec;
var trunk1 = 'curl "https://zhongwenzhuanpinyin.51240.com/web_system/51240_com_www/system/file/zhongwenzhuanpinyin/data/?ajaxtimestamp=1501983414997" -H "pragma: no-cache" -H "origin: https://zhongwenzhuanpinyin.51240.com" -H "accept-encoding: gzip, deflate" -H "accept-language: zh-CN,zh;q=0.8" -H "user-agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36" -H "content-type: application/x-www-form-urlencoded" -H "accept: */*" -H "cache-control: no-cache" -H "authority: zhongwenzhuanpinyin.51240.com" -H "cookie: zwzyp_wenzi=0; zwzyp_jiange=1; zwzyp_shengdiao=0; Hm_lvt_fbe0e02a7ffde424814bef2f6c9d36eb=1501837611,1501933953,1501933982,1501982487; Hm_lpvt_fbe0e02a7ffde424814bef2f6c9d36eb=1501982487" -H "referer: https://zhongwenzhuanpinyin.51240.com/" --data "zwzyp_zhongwen="';
var trunk2 = '&zwzyp_shengdiao=0&zwzyp_wenzi=0&zwzyp_jiange=1&zwzyp_duozhongduyin=1" --compressed';
var limitBase = -1;
var runFlag = true;
var conn = mysql.createConnection({
host: '192.168.1.240',
user: 'root',
password: 'root',
database:'db_test',
port: 3306
});
conn.connect();
setInterval(function(){
if(runFlag) {
runFlag = false;
limitBase = limitBase + 1;
var statement = 'SELECT * from xt_hanziku_copy LIMIT ' + limitBase + ', ' + 1;
// console.log(statement);
conn.query(statement, function(err, rows, fields) {
if (err) console.log(err);
if(rows.length !== 0) {
var _hanzi = rows[0].hanzi;
queryCallback(_hanzi);
console.log(limitBase + ' ' + _hanzi);
} else {
console.log('Finished!');
}
});
}
}, 500);
// conn.end();
function queryCallback (hanzi2) {
var payload = encodeURI(hanzi2);
var realPayload = trunk1 + payload.replace(/%/g,'"%"').substring(1) + trunk2;
// console.log(realPayload);
var child_suf = exec(realPayload,(error, stdout, stderr) => {
if (error) console.log(error);
handdler (hanzi2, stdout);
});
}
function handdler (hanzi3, stdout) {
stdout = stdout.substring(142, stdout.length - 18).trim();
var leftPos = stdout.indexOf('(');
var resultArray = [];
if(leftPos !== -1) {
resultArray.push(stdout.substring(0, leftPos));
var tmpArray = stdout.substring(leftPos + 1, stdout.length - 1).split('、');
for(var count = 0; count < tmpArray.length; ++count){
resultArray.push(tmpArray[count]);
if(count + 1 === tmpArray.length) {
getArray(resultArray, hanzi3);
}
}
} else {
conn.query('INSERT INTO db_result VALUES ("' + hanzi3 + '","' + stdout + '")', function(err, result) {
if(err) console.log(err);
runFlag = true;
})
}
}
function getArray(a, b) {
var hash = {},
len = a.length,
result = [];
for (var i = 0; i < len; i++){
if (!hash[a[i]]){
hash[a[i]] = true;
conn.query('INSERT INTO db_result VALUES ("' + b + '","' + a[i] + '")', function(err, result) {
if(err) throw err;
if(i === len) {
runFlag = true;
}
})
}
}
}