blob: f7f3a2db290072a65d16b69516a4d855ab5231c3 [file] [log] [blame]
avm99963b5ab76c2020-12-27 02:18:49 +01001package main
2
3import (
4 "encoding/json"
5 "fmt"
6 "io/ioutil"
7 "log"
8 "net/http"
9 "os"
Adrià Vilanova Martínezd378fe72022-05-23 17:47:29 +020010 "regexp"
avm999630f961162020-12-27 13:12:27 +010011 "strings"
avm99963b5ab76c2020-12-27 02:18:49 +010012)
13
14const isoLangsFileName = "isoLangs.json"
15
Adrià Vilanova Martínezd378fe72022-05-23 17:47:29 +020016var initDataRe = regexp.MustCompile(`AF_initDataCallback\(.*data: ?(.+), ?sideChannel`)
17
avm99963b5ab76c2020-12-27 02:18:49 +010018type Language struct {
19 CodeName string `json:"-"`
20 Name string `json:"name"`
21 NativeName string `json:"nativeName"`
22}
23
24func getLanguage(isoLangs map[string]Language, lang string) (Language, error) {
25 for currLangCode, currLang := range isoLangs {
26 if currLangCode == lang {
27 currLang.CodeName = lang
avm999630f961162020-12-27 13:12:27 +010028
29 // Only consider the first entry of the list (e.g. "a, b, c" --> "a")
30 if index := strings.Index(currLang.NativeName, ","); index > 0 {
31 currLang.NativeName = currLang.NativeName[:index]
32 }
33
avm99963b5ab76c2020-12-27 02:18:49 +010034 return currLang, nil
35 }
36 }
37
38 return Language{}, fmt.Errorf("Didn't find language '%v' in isoLangs", lang)
39}
40
41func main() {
42 log.SetPrefix("[langs] ")
43 log.SetFlags(0)
44
45 isoLangsFile, err := os.Open(isoLangsFileName)
46 if err != nil {
47 log.Fatalf("Couldn't open file %v, error: %v", isoLangsFileName, err)
48 }
49 defer isoLangsFile.Close()
50
51 isoLangsRawData, err := ioutil.ReadAll(isoLangsFile)
52 if err != nil {
53 log.Fatalf("Couldn't read file %v, error: %v", isoLangsFileName, err)
54 }
55
56 var isoLangs map[string]Language
57 if err := json.Unmarshal(isoLangsRawData, &isoLangs); err != nil {
58 log.Fatalf("Couldn't unmarshal JSON file %v, error: %v", isoLangsFileName, err)
59 }
60
Adrià Vilanova Martínezd378fe72022-05-23 17:47:29 +020061 resp, err := http.Get("http://translate.google.com/")
avm99963b5ab76c2020-12-27 02:18:49 +010062 if err != nil {
Adrià Vilanova Martínezd378fe72022-05-23 17:47:29 +020063 log.Fatalf("Couldn't get current Google Translate page from server, error: %v", err)
avm99963b5ab76c2020-12-27 02:18:49 +010064 }
65 defer resp.Body.Close()
66
67 gTranslateRawData, err := ioutil.ReadAll(resp.Body)
68 if err != nil {
Adrià Vilanova Martínezd378fe72022-05-23 17:47:29 +020069 log.Fatalf("Couldn't read body data from Google Translate request, error: %v", err)
avm99963b5ab76c2020-12-27 02:18:49 +010070 }
71
Adrià Vilanova Martínezd378fe72022-05-23 17:47:29 +020072 initDataMatches := initDataRe.FindSubmatch(gTranslateRawData)
73 if len(initDataMatches) < 2 {
74 log.Fatalln("Couldn't find languages information in Google Translate homepage.")
75 }
76 initDataRaw := initDataMatches[1]
77
78 var gTranslateJson []interface{}
79 if err := json.Unmarshal(initDataRaw, &gTranslateJson); err != nil {
avm99963b5ab76c2020-12-27 02:18:49 +010080 log.Fatalf("Couldn't unmarshal JSON data from the Google Translate languages request, error: %v", err)
81 }
82
Adrià Vilanova Martínezd378fe72022-05-23 17:47:29 +020083 gTranslateLangs := gTranslateJson[1].([]interface{})
avm99963b5ab76c2020-12-27 02:18:49 +010084 langs := make(map[string]Language, len(gTranslateLangs))
85
Adrià Vilanova Martínezd378fe72022-05-23 17:47:29 +020086 for _, lang := range gTranslateLangs {
87 langSlice := lang.([]interface{})
88 if len(langSlice) < 2 {
89 log.Fatalln("A Google Translate language entry is malformed.")
90 }
91 langCode := langSlice[0].(string)
92 name := langSlice[1].(string)
avm99963b5ab76c2020-12-27 02:18:49 +010093 isoLang, err := getLanguage(isoLangs, langCode)
94 if err != nil {
95 log.Fatalf("Didn't find language '%v' in isoLangs, error: %v", langCode, err)
96 }
Adrià Vilanova Martínezd378fe72022-05-23 17:47:29 +020097 isoLang.Name = name
avm99963b5ab76c2020-12-27 02:18:49 +010098 langs[langCode] = isoLang
99 }
100
101 result, err := json.MarshalIndent(langs, "", " ")
102 if err != nil {
103 log.Fatal("Couldn't marshal the final JSON, error: %v", err)
104 }
105
106 fmt.Printf("%v", string(result))
107}