golang基础:并行翻译 | 青训营笔记

275 阅读4分钟

这是我参与「第三届青训营 -后端场」笔记创作活动的的第1篇笔记

题目简述:

实现并行请求两个翻译引擎来提高响应速度

工具介绍

01 devtools

入口

浏览器中按f12或者右键鼠标检查打开或者右上角展开面板选择打开

使用

image.png

如图所示选择 "网络",右键复制选择复制cURL(bash) 得到curl

curl 'https://api.interpreter.caiyunai.com/v1/dict' \
  -H 'Accept: application/json, text/plain, */*' \
  -H 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6' \
  -H 'Connection: keep-alive' \
  -H 'Content-Type: application/json;charset=UTF-8' \
  -H 'Origin: https://fanyi.caiyunapp.com' \
  -H 'Referer: https://fanyi.caiyunapp.com/' \
  -H 'Sec-Fetch-Dest: empty' \
  -H 'Sec-Fetch-Mode: cors' \
  -H 'Sec-Fetch-Site: cross-site' \
  -H '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 Edg/100.0.1185.44' \
  -H 'X-Authorization: token:qgemv4jr1y38jyq6vhvi' \
  -H 'app-name: xy' \
  -H 'device-id: ' \
  -H 'os-type: web' \
  -H 'os-version: ' \
  -H 'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="100", "Microsoft Edge";v="100"' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'sec-ch-ua-platform: "Windows"' \
  --data-raw '{"trans_type":"en2zh","source":"hello"}' \
  --compressed

02 curl2code

入口

Convert curl commands to code (curlconverter.com)

使用

image.png 复制到编辑器里面就可以正常运行了

03 oktools 在线转换工具

入口

JSON转Golang Struct - 在线工具 - OKTools (里面还有其它一些很有用的工具)

使用

image.png

直接把返回的json数据复制过去就可以得到结构体了

代码实现

package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"strings"
	"github.com/holy-func/async"
)

type Query struct {
	TransType string `json:"trans_type"`
	Source    string `json:"source"`
}
type CY 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      []string      `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 (d *CY) output() {
	fmt.Println("UK:", d.Dictionary.Prons.En, "US:", d.Dictionary.Prons.EnUs)
	for _, item := range d.Dictionary.Explanations {
		fmt.Println(strings.TrimSpace(item))
	}
}
func (y *YouDao) output() {
	for _, item := range strings.Split(y.Data.Entries[0].Explain, ";") {
		fmt.Println(item)
	}
}

type YouDao struct {
	Result struct {
		Msg  string `json:"msg"`
		Code int    `json:"code"`
	} `json:"result"`
	Data struct {
		Entries []struct {
			Explain string `json:"explain"`
			Entry   string `json:"entry"`
		} `json:"entries"`
		Query    string `json:"query"`
		Language string `json:"language"`
		Type     string `json:"type"`
	} `json:"data"`
}

func queryWordYD(word string) *async.GoPromise {
	return async.Promise(func(resolve, reject async.Handler) {
		client := &http.Client{}
		req, err := http.NewRequest("GET", fmt.Sprintf("https://dict.youdao.com/suggest?num=5&ver=3.0&doctype=json&cache=false&le=en&q=%s", word), nil)
		if err != nil {
			reject(err)
		}
		req.Header.Set("Accept", "application/json, text/plain, */*")
		req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
		req.Header.Set("Connection", "keep-alive")
		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 Edg/100.0.1185.44")
		req.Header.Set("sec-ch-ua", `" Not A;Brand";v="99", "Chromium";v="100", "Microsoft Edge";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 {
			reject(err)
		}
		defer resp.Body.Close()
		bodyText, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			reject(err)
		}
		var yd YouDao
		json.Unmarshal(bodyText, &yd)
		resolve(&yd)
	})
}

type queryResponse interface {
	output()
}

func output(ret queryResponse, word string) {
	fmt.Println(word)
	ret.output()
}
func queryWordCY(word string) *async.GoPromise {
	return async.Promise(func(resolve, reject async.Handler) {
		client := &http.Client{}
		query, _ := json.Marshal(Query{TransType: "en2zh", Source: word})
		var data = bytes.NewReader(query)
		req, err := http.NewRequest("POST", "https://api.interpreter.caiyunai.com/v1/dict", data)
		if err != nil {
			reject(err)
		}
		req.Header.Set("Accept", "application/json, text/plain, */*")
		req.Header.Set("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6")
		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 Edg/100.0.1185.44")
		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", "Microsoft Edge";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 {
			reject(err)
		}
		defer resp.Body.Close()
		bodyText, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			reject(err)
		}
		var cy CY
		json.Unmarshal(bodyText, &cy)
		resolve(&cy)
	})
}
func main() {
	if len(os.Args) != 2 {
		fmt.Println("hi~,try with any word")
	} else {
		word := os.Args[1]
		ret, err := async.Any(queryWordCY(word), queryWordYD(word)).UnsafeAwait()
		if err == nil {
			switch v := ret.(type) {
			case *CY:
				output(v, word)
			case *YouDao:
				output(v, word)
			}
		} else {
			fmt.Println("please try again!")
		}
	}
}

details

使用的两个翻译工具

彩云小译 - 在线翻译 (caiyunapp.com)

在线翻译_有道 (youdao.com)

特别说明:其中彩云小译为课堂示例,有道翻译调用的不是最终的翻译url 而是建议项,就是说获取到的是翻译结果的缩略,如果翻译的内容项过长得到的是不完整的翻译结果,这里重点在于过程的实现

代码思路:彩云小译根据课堂教学很容易实现翻译流程(通过命令行参数获取所要翻译的单词,然后调用翻译函数根据api返回结果通过结构体反序列化json字符串再按要求输出结果)有道翻译同理不过就是自己一步步去通过上面的三个工具来构成翻译代码,然后把两种翻译方法封装成函数,这里使用了之前写的一个go包holy-func/async: 一个使用同步风格书写并发代码的包 (github.com) ,分别把那两个翻译函数改写成一个返回 *async.GoPromis 的函数然后在执行翻译逻辑的时候调用async.Any方法以及UnsafeWait方法去等待两个请求中的任意一个resolved之后通过对返回值进行断言然后调用output方法进行输出