-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
132 lines (112 loc) · 3.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
const wiki = require("wikijs").default;
const generateImage = require("./generateImage");
const sendTweet = require('./twit');
const TARGET_SYLLABLE = 6;
const vocal = ["a", "i", "u", "e", "o"];
const alphaRegex = /[a-zA-Z]/;
const numRegex = /[0-9]/;
function isAlphabet(char) {
return alphaRegex.test(char);
}
function isVocal(char) {
return vocal.includes(char.toLowerCase());
}
function getArticleUrl(title) {
try {
return wiki({ apiUrl: "https://id.wikipedia.org/w/api.php" })
.page(title)
.then((page) => page.url())
.then((url) => url)
.catch(() => '')
} catch (err) {
return '';
}
}
async function isNotScientificName(title) {
try {
return wiki({ apiUrl: "https://id.wikipedia.org/w/api.php" })
.page(title)
.then((page) => page.fullInfo())
.then((info) => {
const isLatin = info.hasOwnProperty('general') && info.general.hasOwnProperty('regnum');
return !isLatin;
})
.catch(() => true)
} catch (err) {
return true;
}
}
function isMatchTargetSyllable(word) {
if (numRegex.test(word)) {
return false;
}
// clean up diacritics
word.normalize("NFD").replace(/[\u0300-\u036f]/g, "");
const wordLength = word.length;
let syllableCount = 0;
let syllable = "";
for (let i = 0; i < wordLength; i++) {
// if more than target, return early
if (syllableCount > TARGET_SYLLABLE) {
return false;
}
// if current counted syllable is empty, fill and continue for
if (syllable.length < 1) {
syllable = word[i];
continue;
}
const lastCharSyllable = syllable[syllable.length - 1];
// if encounter space or comma, reset current syllable
if (!isAlphabet(word[i])) {
if (isVocal(lastCharSyllable)) {
syllableCount += 1;
}
syllable = "";
continue;
}
if (!isVocal(lastCharSyllable) && !isVocal(word[i])) {
syllable += word[i];
} else if (!isVocal(lastCharSyllable) && isVocal(word[i])) {
syllable += word[i];
} else if (lastCharSyllable === "a" && word[i] === "a") {
syllable = "";
syllableCount += 2;
} else {
syllable = "";
syllableCount += 1;
}
}
if (isVocal(word[word.length - 1])) {
syllableCount += 1;
}
return syllableCount === TARGET_SYLLABLE;
}
async function findMatchedTitle() {
return wiki({ apiUrl: "https://id.wikipedia.org/w/api.php" })
.random(10)
.then(async (page) => {
const matched = page.filter((p) => isMatchTargetSyllable(p));
let matchedTitle;
for (const title of matched) {
const isNotLatinSpeciesName = await isNotScientificName(title);
if (isNotLatinSpeciesName) {
matchedTitle = title;
break;
}
}
return matchedTitle;
})
.catch(() => null)
}
async function findTitle() {
let matched ;
let attempt = 1;
while (!matched) {
matched = await findMatchedTitle();
console.log(matched, attempt++);
}
await generateImage(matched);
const articleUrl = await getArticleUrl(matched);
sendTweet(matched, articleUrl);
}
findTitle();