Skip to content

Instantly share code, notes, and snippets.

@pengx17
Created September 26, 2019 12:36
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 pengx17/c7847206f3fff834306f97952b457f5f to your computer and use it in GitHub Desktop.
Save pengx17/c7847206f3fff834306f97952b457f5f to your computer and use it in GitHub Desktop.
肖富贵起名器
import { request } from 'gaxios';
import * as cheerio from 'cheerio';
import { promises } from 'fs';
const base = 'http://v.8s8s.com';
async function getNameList(): Promise<[string, string][]> {
const page = await request<string>({
url: base + '/qumingzi_5.php',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
body:
'xing=%E8%82%96&gender=1&submit=+%E5%85%8D%E8%B2%BB%E8%B5%B7%E5%90%8D%E6%9F%A5%E8%A9%A2+',
method: 'POST',
});
const $ = cheerio.load(page.data);
const list = $('.qiming_name li a').toArray();
return list.map(i => {
return [$(i).attr('href'), $(i).text()];
});
}
interface NameScore {
score: string;
nameSimplified?: string;
text?: string;
}
async function getNameScoreA(url: string): Promise<NameScore> {
const page = await request<string>({
url: base + url,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
method: 'GET',
});
const $ = cheerio.load(page.data);
return {
score: $('.pingfen_right_ass span').text(),
};
}
async function getNameScoreB(name: string): Promise<NameScore> {
const body = encodeURI(`xs=肖&mz=${name.substr(1)}&action=test`);
const page = await request<string>({
url: 'https://xmcs.buyiju.com/',
headers: {
'Content-Length': '' + body.length,
'Content-Type': 'application/x-www-form-urlencoded',
},
method: 'POST',
body: body,
});
const $ = cheerio.load(page.data);
const nameSimplified = $('td', $('tr')[2])
.toArray()
.slice(1)
.map(a =>
$(a)
.text()
.trim()
)
.join('');
return {
score: $('font[size="5"]').text(),
nameSimplified: nameSimplified,
};
}
async function iterate() {
for (let [url, name] of await getNameList()) {
try {
// const { scoreA } = await getNameScoreA(url);
const { score, nameSimplified } = await getNameScoreB(name);
const goodName = +score > 80;
const veryGoodName = +score > 94;
if (goodName) {
promises.appendFile(
'result.csv',
`${nameSimplified},${score}${veryGoodName ? ',⭐' : ''}\n`
);
}
console.log(nameSimplified, score, veryGoodName ? '⭐' : ' ');
} catch (err) {
console.log(err);
}
}
}
async function main() {
while (true) {
try {
await iterate();
} catch (err) {
console.log(err);
}
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment