avm99963 | b5ab76c | 2020-12-27 02:18:49 +0100 | [diff] [blame] | 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "io/ioutil" |
| 7 | "log" |
| 8 | "net/http" |
| 9 | "os" |
Adrià Vilanova Martínez | d378fe7 | 2022-05-23 17:47:29 +0200 | [diff] [blame^] | 10 | "regexp" |
avm99963 | 0f96116 | 2020-12-27 13:12:27 +0100 | [diff] [blame] | 11 | "strings" |
avm99963 | b5ab76c | 2020-12-27 02:18:49 +0100 | [diff] [blame] | 12 | ) |
| 13 | |
| 14 | const isoLangsFileName = "isoLangs.json" |
| 15 | |
Adrià Vilanova Martínez | d378fe7 | 2022-05-23 17:47:29 +0200 | [diff] [blame^] | 16 | var initDataRe = regexp.MustCompile(`AF_initDataCallback\(.*data: ?(.+), ?sideChannel`) |
| 17 | |
avm99963 | b5ab76c | 2020-12-27 02:18:49 +0100 | [diff] [blame] | 18 | type Language struct { |
| 19 | CodeName string `json:"-"` |
| 20 | Name string `json:"name"` |
| 21 | NativeName string `json:"nativeName"` |
| 22 | } |
| 23 | |
| 24 | func getLanguage(isoLangs map[string]Language, lang string) (Language, error) { |
| 25 | for currLangCode, currLang := range isoLangs { |
| 26 | if currLangCode == lang { |
| 27 | currLang.CodeName = lang |
avm99963 | 0f96116 | 2020-12-27 13:12:27 +0100 | [diff] [blame] | 28 | |
| 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 | |
avm99963 | b5ab76c | 2020-12-27 02:18:49 +0100 | [diff] [blame] | 34 | return currLang, nil |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | return Language{}, fmt.Errorf("Didn't find language '%v' in isoLangs", lang) |
| 39 | } |
| 40 | |
| 41 | func 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ínez | d378fe7 | 2022-05-23 17:47:29 +0200 | [diff] [blame^] | 61 | resp, err := http.Get("http://translate.google.com/") |
avm99963 | b5ab76c | 2020-12-27 02:18:49 +0100 | [diff] [blame] | 62 | if err != nil { |
Adrià Vilanova Martínez | d378fe7 | 2022-05-23 17:47:29 +0200 | [diff] [blame^] | 63 | log.Fatalf("Couldn't get current Google Translate page from server, error: %v", err) |
avm99963 | b5ab76c | 2020-12-27 02:18:49 +0100 | [diff] [blame] | 64 | } |
| 65 | defer resp.Body.Close() |
| 66 | |
| 67 | gTranslateRawData, err := ioutil.ReadAll(resp.Body) |
| 68 | if err != nil { |
Adrià Vilanova Martínez | d378fe7 | 2022-05-23 17:47:29 +0200 | [diff] [blame^] | 69 | log.Fatalf("Couldn't read body data from Google Translate request, error: %v", err) |
avm99963 | b5ab76c | 2020-12-27 02:18:49 +0100 | [diff] [blame] | 70 | } |
| 71 | |
Adrià Vilanova Martínez | d378fe7 | 2022-05-23 17:47:29 +0200 | [diff] [blame^] | 72 | 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 { |
avm99963 | b5ab76c | 2020-12-27 02:18:49 +0100 | [diff] [blame] | 80 | log.Fatalf("Couldn't unmarshal JSON data from the Google Translate languages request, error: %v", err) |
| 81 | } |
| 82 | |
Adrià Vilanova Martínez | d378fe7 | 2022-05-23 17:47:29 +0200 | [diff] [blame^] | 83 | gTranslateLangs := gTranslateJson[1].([]interface{}) |
avm99963 | b5ab76c | 2020-12-27 02:18:49 +0100 | [diff] [blame] | 84 | langs := make(map[string]Language, len(gTranslateLangs)) |
| 85 | |
Adrià Vilanova Martínez | d378fe7 | 2022-05-23 17:47:29 +0200 | [diff] [blame^] | 86 | 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) |
avm99963 | b5ab76c | 2020-12-27 02:18:49 +0100 | [diff] [blame] | 93 | 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ínez | d378fe7 | 2022-05-23 17:47:29 +0200 | [diff] [blame^] | 97 | isoLang.Name = name |
avm99963 | b5ab76c | 2020-12-27 02:18:49 +0100 | [diff] [blame] | 98 | 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 | } |