download.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. package cmd
  2. import (
  3. "downloadQJ/utils"
  4. "fmt"
  5. "github.com/charmbracelet/log"
  6. "github.com/mitchellh/go-homedir"
  7. "github.com/spf13/cobra"
  8. "github.com/spf13/viper"
  9. "os"
  10. "os/exec"
  11. "runtime"
  12. "strconv"
  13. "strings"
  14. "time"
  15. )
  16. var code string
  17. var outputFile string
  18. var isProduction bool = true
  19. var cfgFile string
  20. var rootCmd = &cobra.Command{
  21. Use: "downloadQJ -code <code> -output-file <output-file>",
  22. Short: " QJkankan panorama data offline downloader,This command will get QJkankan image tiles",
  23. ValidArgs: []string{"code", "output-file"},
  24. Args: cobra.MinimumNArgs(2),
  25. //Args: cobra.MatchAll(cobra.ExactArgs(2)),
  26. Long: `This command will get QJkankan image tiles`,
  27. Version: "0.0.1",
  28. Run: RunCmd,
  29. }
  30. func RunCmd(cmd *cobra.Command, args []string) {
  31. timeUnix := time.Now().Unix()
  32. var mainTourUrl string
  33. var mainJsonUrl string
  34. var ossUrl string
  35. var bucket string
  36. code = args[0]
  37. dest := args[1]
  38. if isProduction {
  39. mainJsonUrl = "https://4dkk.4dage.com/720yun_fd_manage/" + code + "/someData.json?rnd=" + strconv.FormatInt(timeUnix, 10)
  40. mainTourUrl = "https://4dkk.4dage.com/720yun_fd_manage/" + code + "/tour.xml?rnd=" + strconv.FormatInt(timeUnix, 10)
  41. ossUrl = "https://4dkk.4dage.com/"
  42. bucket = "oss://4dkankan"
  43. } else {
  44. mainJsonUrl = "https://ossxiaoan.4dage.com/720yun_fd_manage/" + code + "/someData.json?rnd=" + strconv.FormatInt(timeUnix, 10)
  45. mainTourUrl = "https://ossxiaoan.4dage.com/720yun_fd_manage/" + code + "/tour.xml?rnd=" + strconv.FormatInt(timeUnix, 10)
  46. ossUrl = "https://ossxiaoan.4dage.com/"
  47. bucket = "oss://oss-xiaoan"
  48. }
  49. data := &utils.ScenesDataType{}
  50. if _, err := os.Stat(dest); os.IsNotExist(err) {
  51. err = os.Mkdir(dest, 0644)
  52. }
  53. log.Info("start downloading")
  54. err := utils.GetJson(mainJsonUrl, data)
  55. if err != nil {
  56. fmt.Println("Error1: ", err)
  57. return
  58. }
  59. err = utils.DownloadMainXml(mainTourUrl, dest+"/tour/")
  60. if err != nil {
  61. fmt.Println("Error2: ", err)
  62. return
  63. }
  64. err = utils.CovertJsonAndDownload(mainJsonUrl, dest, ossUrl)
  65. if err != nil {
  66. fmt.Println("Error3: ", err)
  67. return
  68. }
  69. excludeArray := []string{"*.exe", "tour_testingserver_macos", "tour.html", "*.js", "vtourskin*.xml", "webvr.xml", "combobox.xml", "vtourskin*.png", "vtourskin.png", "rotate_device.png"}
  70. exclString := " --exclude " + strings.Join(excludeArray, " --exclude ")
  71. log.Info("oss copy", "flag", fmt.Sprintf("%s", exclString))
  72. if len(data.Scenes) > 0 {
  73. for _, v := range data.Scenes {
  74. // 场景封面图
  75. if len(v.Icon) > 0 {
  76. log.Info("Downloading Scenes-cover", "url", fmt.Sprintf("%s", v.Icon))
  77. url := utils.DetectSource(v.Icon, dest, ossUrl)
  78. err = utils.DownloadFile(url, v.Icon)
  79. log.Info("Save Scenes-cover", "url", fmt.Sprintf("%s", url))
  80. }
  81. if v.Type == "pano" {
  82. // 主Tiles图
  83. if utils.IsCommandAvailable("ossutil") {
  84. log.Info("current", "bucket", fmt.Sprintf("%s", bucket))
  85. runMainTileDownload(v.SceneCode, dest, exclString, bucket)
  86. }
  87. // 场景封面图
  88. Hotspots := v.SomeData.Hotspots
  89. if Hotspots != nil {
  90. for _, Hotspot := range Hotspots {
  91. // 热点封面图
  92. if len(Hotspot.Secne.Icon) > 0 {
  93. log.Info("Downloading Hotspots-cover", "url", fmt.Sprintf("%s", Hotspot.Secne.Icon))
  94. url := utils.DetectSource(Hotspot.Secne.Icon, dest, ossUrl)
  95. err = utils.DownloadFile(url, Hotspot.Secne.Icon)
  96. log.Info("Save Hotspots-cover", "url", fmt.Sprintf("%s", url))
  97. }
  98. // 热点图集
  99. if len(Hotspot.ImageTextInfo.ImageList) > 0 {
  100. for _, img := range Hotspot.ImageTextInfo.ImageList {
  101. log.Info("Downloading Hotspots-ImageList", "url", fmt.Sprintf("%s", img.OssPath))
  102. url := utils.DetectSource(img.OssPath, dest, ossUrl)
  103. err = utils.DownloadFile(url, img.OssPath)
  104. log.Info("Save Hotspots-ImageList", "url", fmt.Sprintf("%s", url))
  105. }
  106. }
  107. // customIconInfo
  108. if len(Hotspot.CustomIconInfo.Img) > 0 {
  109. log.Info("Downloading Hotspots-CustomIconInfo", "url", fmt.Sprintf("%s", Hotspot.CustomIconInfo.Img))
  110. url := utils.DetectSource(Hotspot.CustomIconInfo.Img, dest, ossUrl)
  111. err = utils.DownloadFile(url, Hotspot.CustomIconInfo.Img)
  112. log.Info("Save Hotspots-CustomIconInfo", "url", fmt.Sprintf("%s", url))
  113. }
  114. }
  115. }
  116. if len(v.CustomMask.Sky.Icon) > 0 {
  117. log.Info("Downloading CustomMask Sky", "url", fmt.Sprintf("%s", v.CustomMask.Sky.Icon))
  118. url := utils.DetectSource(v.CustomMask.Sky.Icon, dest, ossUrl)
  119. err = utils.DownloadFile(url, v.CustomMask.Sky.Icon)
  120. log.Info("Save CustomMask Sky", "url", fmt.Sprintf("%s", url))
  121. }
  122. if len(v.CustomMask.Earth.Icon) > 0 {
  123. log.Info("Downloading CustomMask Earth", "url", fmt.Sprintf("%s", v.CustomMask.Earth.Icon))
  124. url := utils.DetectSource(v.CustomMask.Earth.Icon, dest, ossUrl)
  125. err = utils.DownloadFile(url, v.CustomMask.Earth.Icon)
  126. log.Info("Save CustomMask Earth", "url", fmt.Sprintf("%s", url))
  127. }
  128. }
  129. }
  130. }
  131. log.Info("end downloading")
  132. }
  133. func runMainTileDownload(sceneCode string, dest string, exclString string, bucket string) {
  134. log.Info("Downloading Scenes", "code", fmt.Sprintf("%s", sceneCode))
  135. command := "ossutil cp -r -f " + bucket + "/720yun_fd_manage/" + sceneCode + " " + dest + " " + exclString
  136. var cmd *exec.Cmd
  137. if runtime.GOOS == "windows" {
  138. cmd = exec.Command("powershell", command)
  139. // windows specific code here...
  140. } else {
  141. cmd = exec.Command("bash", command)
  142. }
  143. log.Info("execute ", "command", fmt.Sprintf("%s", command))
  144. if err := cmd.Run(); err != nil {
  145. fmt.Println("Error: ", err)
  146. }
  147. }
  148. func init() {
  149. cobra.OnInitialize(initConfig)
  150. rootCmd.PersistentFlags().StringVarP(&code, "code", "c", os.Getenv("HOME"), " QJkankan online url")
  151. rootCmd.PersistentFlags().StringVarP(&outputFile, "output-file", "o", os.Getenv("HOME"), "source output file path")
  152. rootCmd.PersistentFlags().BoolVar(&isProduction, "pro", true, "production or test env")
  153. //rootCmd.SetArgs([]string{"code", "output-file", "pro"})
  154. }
  155. // initConfig reads in config file and ENV variables if set.
  156. func initConfig() {
  157. if cfgFile != "" {
  158. // Use config file from the flag.
  159. log.Info("%v", cfgFile)
  160. viper.SetConfigFile(cfgFile)
  161. } else {
  162. // Find home directory.
  163. home, err := homedir.Dir()
  164. if err != nil {
  165. fmt.Println(err)
  166. os.Exit(1)
  167. }
  168. // Search config in home directory with name ".dadjoke" (without extension).
  169. viper.AddConfigPath(home)
  170. viper.SetConfigName(".config")
  171. }
  172. viper.AutomaticEnv() // read in environment variables that match
  173. // If a config file is found, read it in.
  174. if err := viper.ReadInConfig(); err == nil {
  175. fmt.Println("Using config file:", viper.ConfigFileUsed())
  176. }
  177. }
  178. func Execute() {
  179. err := rootCmd.Execute()
  180. if err != nil {
  181. os.Exit(1)
  182. }
  183. }