localizeMainJson.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package utils
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "github.com/charmbracelet/log"
  6. "io"
  7. "net/http"
  8. "os"
  9. "path/filepath"
  10. "strings"
  11. )
  12. func CovertJsonAndDownload(path string, dest string, ossUrl string) (err error) {
  13. log.Info("CovertJsonAndDownload", "url", fmt.Sprintf("%s", path))
  14. dataFile := dest + "/someData.json"
  15. // Get the data
  16. resp, err := http.Get(path)
  17. if err != nil {
  18. return err
  19. }
  20. defer resp.Body.Close()
  21. bodyBytes, err := io.ReadAll(resp.Body)
  22. content := string(bodyBytes)
  23. //以字串过滤所有url到本地
  24. if strings.Contains(content, ossUrl+"/720yun_fd_manage/") {
  25. content = strings.ReplaceAll(content, "https://4dkk.4dage.com/720yun_fd_manage/", "data/")
  26. }
  27. if strings.Contains(content, ossUrl+"/scene_edit_data/") {
  28. content = strings.ReplaceAll(content, "https://4dkk.4dage.com/", "data/")
  29. }
  30. target := &ScenesDataType{}
  31. err = json.NewDecoder(strings.NewReader(content)).Decode(&target)
  32. if err != nil {
  33. fmt.Println(err)
  34. return err
  35. }
  36. //补数据default
  37. for _, Scenes := range target.Scenes {
  38. if Scenes.InitVisual.Vlookatmin == nil {
  39. d := json.Number("-90")
  40. Scenes.InitVisual.Vlookatmin = &d
  41. }
  42. if Scenes.InitVisual.Vlookatmax == nil {
  43. m := json.Number("90")
  44. Scenes.InitVisual.Vlookatmax = &m
  45. }
  46. //fmt.Println("InitVisual %s\n", Scenes.InitVisual)
  47. }
  48. file, err := json.Marshal(target)
  49. if err != nil {
  50. panic(err)
  51. }
  52. //log.Info("data json:", string(file))
  53. // Writer the body to file
  54. err = os.WriteFile(dataFile, file, 0644)
  55. if err != nil {
  56. return err
  57. }
  58. return nil
  59. }
  60. func DownloadMainXml(path string, dest string) (err error) {
  61. log.Info("DownloadMainXml", "url", fmt.Sprintf("%s", path))
  62. dataFile := dest + "/tour.xml"
  63. dir := filepath.Dir(dataFile)
  64. err = os.MkdirAll(dir, 0644)
  65. if err != nil {
  66. return err
  67. }
  68. // Get the data
  69. file, err := os.Create(dataFile)
  70. if err != nil {
  71. return err
  72. }
  73. resp, err := http.Get(path)
  74. if err != nil {
  75. return err
  76. }
  77. defer resp.Body.Close()
  78. _, err = io.Copy(file, resp.Body)
  79. if err != nil {
  80. return err
  81. }
  82. return nil
  83. }