疑问?
- 首先,并行应该是为了更高的效率
- 既然这样利用sync.waitGroup就应该AAd(1),而不是AAD(2)
// 使用两种搜索引擎
// 一种是 彩云小译
// 一种是 火山翻译
// 两个并发执行
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"sync"
"time"
)
func main() {
if len(os.Args) != 2 {
log.Fatal("input is wrong.")
os.Exit(1)
}
startTime := time.Now()
var wg sync.WaitGroup
wg.Add(1)
word := os.Args[1]
go query1(word, &wg)
go query2(word, &wg)
wg.Wait()
totalTime := time.Since(startTime)
fmt.Println(totalTime)
}
type RequestWord struct {
TransType string `json:"trans_type"`
Source string `json:"source"`
}
type BodyWord struct {
Rc int `json:"rc"`
Wiki struct {
KnownInLaguages int `json:"known_in_laguages"`
Description struct {
Source string `json:"source"`
Target interface{} `json:"target"`
} `json:"description"`
ID string `json:"id"`
Item struct {
Source string `json:"source"`
Target string `json:"target"`
} `json:"item"`
ImageURL string `json:"image_url"`
IsSubject string `json:"is_subject"`
Sitelink string `json:"sitelink"`
} `json:"wiki"`
Dictionary struct {
Prons struct {
EnUs string `json:"en-us"`
En string `json:"en"`
} `json:"prons"`
Explanations []string `json:"explanations"`
Synonym []string `json:"synonym"`
Antonym []interface{} `json:"antonym"`
WqxExample [][]string `json:"wqx_example"`
Entry string `json:"entry"`
Type string `json:"type"`
Related []interface{} `json:"related"`
Source string `json:"source"`
} `json:"dictionary"`
}
func query1(word string, wg *sync.WaitGroup) {
defer wg.Done()
client := &http.Client{}
data := RequestWord{TransType: "en2zh", Source: word}
byteData, err := json.Marshal(data)
if err != nil {
log.Fatal("can not json 序列化")
}
readerData := bytes.NewReader(byteData)
//var data = strings.NewReader(`{"trans_type":"en2zh","source":"hello"}`)
req, err := http.NewRequest("POST", "https://api.interpreter.caiyunai.com/v1/dict", readerData)
if err != nil {
log.Fatal(err)
}
req.Header.Set("Accept", "application/json, text/plain, */*")
req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9")
req.Header.Set("Connection", "keep-alive")
req.Header.Set("Content-Type", "application/json;charset=UTF-8")
req.Header.Set("Origin", "https://fanyi.caiyunapp.com")
req.Header.Set("Referer", "https://fanyi.caiyunapp.com/")
req.Header.Set("Sec-Fetch-Dest", "empty")
req.Header.Set("Sec-Fetch-Mode", "cors")
req.Header.Set("Sec-Fetch-Site", "cross-site")
req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36")
req.Header.Set("X-Authorization", "token:qgemv4jr1y38jyq6vhvi")
req.Header.Set("app-name", "xy")
req.Header.Set("os-type", "web")
req.Header.Set("sec-ch-ua", `" Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"`)
req.Header.Set("sec-ch-ua-mobile", "?0")
req.Header.Set("sec-ch-ua-platform", `"Windows"`)
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
if resp.StatusCode != 200 {
log.Fatal("wrong status", resp.StatusCode)
}
defer resp.Body.Close()
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var bodyWord BodyWord
err = json.Unmarshal(bodyText, &bodyWord)
if err != nil {
log.Fatal("wrong json 反序列化")
}
for _, it := range bodyWord.Dictionary.Explanations {
fmt.Println(it)
}
}
type HuoRequestWord struct {
Text string `json:"text"`
Language string `json:"language"`
}
type HuoBodyWord struct {
Words []struct {
Source int `json:"source"`
Text string `json:"text"`
PosList []struct {
Type int `json:"type"`
Phonetics []struct {
Type int `json:"type"`
Text string `json:"text"`
} `json:"phonetics"`
Explanations []struct {
Text string `json:"text"`
Examples []struct {
Type int `json:"type"`
Sentences []struct {
Text string `json:"text"`
TransText string `json:"trans_text"`
} `json:"sentences"`
} `json:"examples"`
Synonyms []interface{} `json:"synonyms"`
} `json:"explanations"`
Relevancys []interface{} `json:"relevancys"`
} `json:"pos_list"`
} `json:"words"`
Phrases []interface{} `json:"phrases"`
BaseResp struct {
StatusCode int `json:"status_code"`
StatusMessage string `json:"status_message"`
} `json:"base_resp"`
}
func query2(word string, wg *sync.WaitGroup) {
defer wg.Done()
client := &http.Client{}
huoRequestWord := HuoRequestWord{Text: word, Language: "en"}
buf, err := json.Marshal(huoRequestWord)
if err != nil {
log.Fatal("error")
}
data := bytes.NewReader(buf)
req, err := http.NewRequest("POST", "https://translate.volcengine.com/web/dict/match/v1/?msToken=&X-Bogus=DFSzswVLQDGtC/5eSW0l1M9WX7jd&_signature=_02B4Z6wo00001JD56XQAAIDAEPsTN6VE2riQ-e3AAEZO7TAlBiy9SBKN5sfihPhbmOy5.KrzHCtYQZl9DHefvgxT-PsOnS0AmjpaM7syiFygO1UxLeEBrGudOAh6M5dOQ3kOqY09WEPyeWtVb3", data)
if err != nil {
log.Fatal(err)
}
req.Header.Set("authority", "translate.volcengine.com")
req.Header.Set("accept", "application/json, text/plain, */*")
req.Header.Set("accept-language", "zh-CN,zh;q=0.9")
req.Header.Set("content-type", "application/json")
req.Header.Set("cookie", "x-jupiter-uuid=16520028946591388; s_v_web_id=verify_db5e5f9990cec7a39198a9448fd8f1b3; _tea_utm_cache_2018=undefined; ttcid=34d4f9520b114c1bb3ee7d9c82b2155035; i18next=translate; tt_scid=1I4BEIvX6As5q4-BKGbTFtBIsF3bsja4rqNyQvh.Z29elMd5EuCwJsy1--ZmfXVpddc0")
req.Header.Set("origin", "https://translate.volcengine.com")
req.Header.Set("referer", "https://translate.volcengine.com/translate?category=&home_language=zh&source_language=detect&target_language=zh&text=police")
req.Header.Set("sec-ch-ua", `" Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"`)
req.Header.Set("sec-ch-ua-mobile", "?0")
req.Header.Set("sec-ch-ua-platform", `"Windows"`)
req.Header.Set("sec-fetch-dest", "empty")
req.Header.Set("sec-fetch-mode", "cors")
req.Header.Set("sec-fetch-site", "same-origin")
req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36")
resp, err := client.Do(req)
if err != nil {
log.Fatal(err)
}
if resp.StatusCode != 200 {
log.Fatal("error")
}
defer resp.Body.Close()
bodyText, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatal(err)
}
var huoBodyWord HuoBodyWord
err = json.Unmarshal(bodyText, &huoBodyWord)
if err != nil {
log.Fatal("error")
}
for _, it := range huoBodyWord.Words {
for _, it2 := range it.PosList {
for _, it3 := range it2.Explanations {
fmt.Println(it3.Text)
}
}
}
}