-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
48 lines (37 loc) · 1.3 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
const express = require("express");
const axios = require("axios");
var request = require("request");
const app = express();
const YABLA_URI =
"https://chinese.yabla.com/chinese-english-pinyin-dictionary.php";
app.get("/:pinyin", (req, res) => {
const word = req.param("pinyin");
res.setHeader("Cache-Control", "max-age=0, s-maxage=31536000");
axios.get(`${YABLA_URI}?define=${encodeURI(word)}&limit=1`).then((_res) => {
let URI = (_res.data + "").match(/https?.*\.mp3/)[0];
let file = URI.match(/[0-9]*\.mp3/)[0];
req
.pipe(request(`https://yabla.vo.llnwd.net/media.yabla.com/audio/${file}`))
.pipe(res);
});
});
app.get("/pod/:pinyin", (req, res) => {
const word = req.param("pinyin");
res.setHeader("Cache-Control", "max-age=0, s-maxage=31536000");
axios
.get(`https://chinesepod.com/dictionary/english-chinese/${encodeURI(word)}`)
.then((_res) => {
let URI = (_res.data + "").match(/https?.*\.mp3/)[0];
req.pipe(request(unescape(URI))).pipe(res);
});
});
var { segment } = require("hanzi-tools");
app.post("/segment", (req, res) => {
console.log(req.body);
const text = req.body.text;
res.setHeader("Cache-Control", "max-age=0, s-maxage=31536000");
res.json({ segment: segment(text) });
});
app.listen(8889, () => {
console.log("running");
});