| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- package cmd
- import (
- "downloadQJ/utils"
- "fmt"
- "github.com/charmbracelet/log"
- "github.com/mitchellh/go-homedir"
- "github.com/spf13/cobra"
- "github.com/spf13/viper"
- "os"
- "os/exec"
- "runtime"
- "strconv"
- "strings"
- "time"
- )
- var code string
- var outputFile string
- var isProduction bool = true
- var cfgFile string
- var rootCmd = &cobra.Command{
- Use: "downloadQJ -code <code> -output-file <output-file>",
- Short: " QJkankan panorama data offline downloader,This command will get QJkankan image tiles",
- ValidArgs: []string{"code", "output-file"},
- Args: cobra.MinimumNArgs(2),
- //Args: cobra.MatchAll(cobra.ExactArgs(2)),
- Long: `This command will get QJkankan image tiles`,
- Version: "0.0.1",
- Run: RunCmd,
- }
- func RunCmd(cmd *cobra.Command, args []string) {
- timeUnix := time.Now().Unix()
- var mainTourUrl string
- var mainJsonUrl string
- var ossUrl string
- var bucket string
- code = args[0]
- dest := args[1]
- if isProduction {
- mainJsonUrl = "https://4dkk.4dage.com/720yun_fd_manage/" + code + "/someData.json?rnd=" + strconv.FormatInt(timeUnix, 10)
- mainTourUrl = "https://4dkk.4dage.com/720yun_fd_manage/" + code + "/tour.xml?rnd=" + strconv.FormatInt(timeUnix, 10)
- ossUrl = "https://4dkk.4dage.com/"
- bucket = "oss://4dkankan"
- } else {
- mainJsonUrl = "https://ossxiaoan.4dage.com/720yun_fd_manage/" + code + "/someData.json?rnd=" + strconv.FormatInt(timeUnix, 10)
- mainTourUrl = "https://ossxiaoan.4dage.com/720yun_fd_manage/" + code + "/tour.xml?rnd=" + strconv.FormatInt(timeUnix, 10)
- ossUrl = "https://ossxiaoan.4dage.com/"
- bucket = "oss://oss-xiaoan"
- }
- data := &utils.ScenesDataType{}
- if _, err := os.Stat(dest); os.IsNotExist(err) {
- err = os.Mkdir(dest, 0644)
- }
- log.Info("start downloading")
- err := utils.GetJson(mainJsonUrl, data)
- if err != nil {
- fmt.Println("Error1: ", err)
- return
- }
- err = utils.DownloadMainXml(mainTourUrl, dest+"/tour/")
- if err != nil {
- fmt.Println("Error2: ", err)
- return
- }
- err = utils.CovertJsonAndDownload(mainJsonUrl, dest, ossUrl)
- if err != nil {
- fmt.Println("Error3: ", err)
- return
- }
- excludeArray := []string{"*.exe", "tour_testingserver_macos", "tour.html", "*.js", "vtourskin*.xml", "webvr.xml", "combobox.xml", "vtourskin*.png", "vtourskin.png", "rotate_device.png"}
- exclString := " --exclude " + strings.Join(excludeArray, " --exclude ")
- log.Info("oss copy", "flag", fmt.Sprintf("%s", exclString))
- if len(data.Scenes) > 0 {
- for _, v := range data.Scenes {
- // 场景封面图
- if len(v.Icon) > 0 {
- log.Info("Downloading Scenes-cover", "url", fmt.Sprintf("%s", v.Icon))
- url := utils.DetectSource(v.Icon, dest, ossUrl)
- err = utils.DownloadFile(url, v.Icon)
- log.Info("Save Scenes-cover", "url", fmt.Sprintf("%s", url))
- }
- if v.Type == "pano" {
- // 主Tiles图
- if utils.IsCommandAvailable("ossutil") {
- log.Info("current", "bucket", fmt.Sprintf("%s", bucket))
- runMainTileDownload(v.SceneCode, dest, exclString, bucket)
- }
- // 场景封面图
- Hotspots := v.SomeData.Hotspots
- if Hotspots != nil {
- for _, Hotspot := range Hotspots {
- // 热点封面图
- if len(Hotspot.Secne.Icon) > 0 {
- log.Info("Downloading Hotspots-cover", "url", fmt.Sprintf("%s", Hotspot.Secne.Icon))
- url := utils.DetectSource(Hotspot.Secne.Icon, dest, ossUrl)
- err = utils.DownloadFile(url, Hotspot.Secne.Icon)
- log.Info("Save Hotspots-cover", "url", fmt.Sprintf("%s", url))
- }
- // 热点图集
- if len(Hotspot.ImageTextInfo.ImageList) > 0 {
- for _, img := range Hotspot.ImageTextInfo.ImageList {
- log.Info("Downloading Hotspots-ImageList", "url", fmt.Sprintf("%s", img.OssPath))
- url := utils.DetectSource(img.OssPath, dest, ossUrl)
- err = utils.DownloadFile(url, img.OssPath)
- log.Info("Save Hotspots-ImageList", "url", fmt.Sprintf("%s", url))
- }
- }
- // customIconInfo
- if len(Hotspot.CustomIconInfo.Img) > 0 {
- log.Info("Downloading Hotspots-CustomIconInfo", "url", fmt.Sprintf("%s", Hotspot.CustomIconInfo.Img))
- url := utils.DetectSource(Hotspot.CustomIconInfo.Img, dest, ossUrl)
- err = utils.DownloadFile(url, Hotspot.CustomIconInfo.Img)
- log.Info("Save Hotspots-CustomIconInfo", "url", fmt.Sprintf("%s", url))
- }
- }
- }
- if len(v.CustomMask.Sky.Icon) > 0 {
- log.Info("Downloading CustomMask Sky", "url", fmt.Sprintf("%s", v.CustomMask.Sky.Icon))
- url := utils.DetectSource(v.CustomMask.Sky.Icon, dest, ossUrl)
- err = utils.DownloadFile(url, v.CustomMask.Sky.Icon)
- log.Info("Save CustomMask Sky", "url", fmt.Sprintf("%s", url))
- }
- if len(v.CustomMask.Earth.Icon) > 0 {
- log.Info("Downloading CustomMask Earth", "url", fmt.Sprintf("%s", v.CustomMask.Earth.Icon))
- url := utils.DetectSource(v.CustomMask.Earth.Icon, dest, ossUrl)
- err = utils.DownloadFile(url, v.CustomMask.Earth.Icon)
- log.Info("Save CustomMask Earth", "url", fmt.Sprintf("%s", url))
- }
- }
- }
- }
- log.Info("end downloading")
- }
- func runMainTileDownload(sceneCode string, dest string, exclString string, bucket string) {
- log.Info("Downloading Scenes", "code", fmt.Sprintf("%s", sceneCode))
- command := "ossutil cp -r -f " + bucket + "/720yun_fd_manage/" + sceneCode + " " + dest + " " + exclString
- var cmd *exec.Cmd
- if runtime.GOOS == "windows" {
- cmd = exec.Command("powershell", command)
- // windows specific code here...
- } else {
- cmd = exec.Command("bash", command)
- }
- log.Info("execute ", "command", fmt.Sprintf("%s", command))
- if err := cmd.Run(); err != nil {
- fmt.Println("Error: ", err)
- }
- }
- func init() {
- cobra.OnInitialize(initConfig)
- rootCmd.PersistentFlags().StringVarP(&code, "code", "c", os.Getenv("HOME"), " QJkankan online url")
- rootCmd.PersistentFlags().StringVarP(&outputFile, "output-file", "o", os.Getenv("HOME"), "source output file path")
- rootCmd.PersistentFlags().BoolVar(&isProduction, "pro", true, "production or test env")
- //rootCmd.SetArgs([]string{"code", "output-file", "pro"})
- }
- // initConfig reads in config file and ENV variables if set.
- func initConfig() {
- if cfgFile != "" {
- // Use config file from the flag.
- log.Info("%v", cfgFile)
- viper.SetConfigFile(cfgFile)
- } else {
- // Find home directory.
- home, err := homedir.Dir()
- if err != nil {
- fmt.Println(err)
- os.Exit(1)
- }
- // Search config in home directory with name ".dadjoke" (without extension).
- viper.AddConfigPath(home)
- viper.SetConfigName(".config")
- }
- viper.AutomaticEnv() // read in environment variables that match
- // If a config file is found, read it in.
- if err := viper.ReadInConfig(); err == nil {
- fmt.Println("Using config file:", viper.ConfigFileUsed())
- }
- }
- func Execute() {
- err := rootCmd.Execute()
- if err != nil {
- os.Exit(1)
- }
- }
|