func saveSubtitle(content []byte, subtitlePath string) { err := ioutil.WriteFile(subtitlePath, content, 0644) if err != nil { if config.Verbose { fmt.Fprintln(os.Stderr, err) } utils.Exit("Can't save the file %v", subtitlePath) } }
// Subtitles get the subtitles from the hash of a video func Subtitles(hash string) []byte { // Build request req := fluent.New() req.Get(buildURL(hash, "en")). SetHeader("User-Agent", userAgent). InitialInterval(time.Duration(time.Millisecond)). Retry(3) // Execute the request res, err := req.Send() if err != nil { if config.Verbose { fmt.Fprintln(os.Stderr, err) } utils.Exit("Can't reach the SubDB Web API. Are you connected to the Internet ?") } if res.StatusCode != 200 { if config.Verbose { fmt.Println(fmt.Sprintf("Response : %v", res)) } utils.Exit("Subtitle not found with SubDB Web API. Try with another language (See 'subify dl -h') You may contribute to the community by updating their database (see 'subify upload -h')") } // Extract the subtitles from the response defer res.Body.Close() content, err := ioutil.ReadAll(res.Body) if err != nil { if config.Verbose { fmt.Fprintln(os.Stderr, err) } utils.Exit("Can't read the content of the subtitle donwload from Subdb") } return content }
var openVideo bool // dlCmd represents the dl command var dlCmd = &cobra.Command{ Use: "dl <video-path>", Aliases: []string{"download"}, Short: "Download the subtitles for your video - 'subify dl --help'", Long: `Download the subtitles for your video (movie or TV Shows) Give the path of your video as first parameter and let's go !`, Run: func(cmd *cobra.Command, args []string) { // Assertions if config.Verbose { fmt.Println("Downloading command called with following parameters : " + strings.Join(args, " ")) } if len(args) != 1 { utils.Exit("Video file needed. See usage : 'subify help' or 'subify dl --help'") } videoPath := args[0] subtitles.Download(videoPath) if openVideo { open.Run(videoPath) } }, } func init() { dlCmd.Flags().StringVarP(&language, "language", "l", "en", "Language of the subtitle") viper.BindPFlag("language", dlCmd.Flags().Lookup("language")) dlCmd.Flags().BoolVarP(&openVideo, "open", "o", false, "Once the subtitle is donwloaded, open the video with your default video player (OSX: \"open\", Windows: \"start\", Linux/Other: \"xdg-open\")")