示例#1
0
// Subtitles get the subtitles from the hash of a video
func subtitles(hash string, language string) ([]byte, error) {

	// Build request
	req := fluent.New()
	req.Get(buildURL(hash, language)).
		SetHeader("User-Agent", userAgent).
		InitialInterval(time.Duration(time.Millisecond)).
		Retry(3)

	// Execute the request
	res, err := req.Send()
	if err != nil {
		return []byte{}, fmt.Errorf("Can't reach the SubDB Web API. Are you connected to the Internet ? %v", err.Error())
	}
	if res.StatusCode != 200 {
		return []byte{}, fmt.Errorf(`Subtitle not stored by SubDB`)
	}

	// Extract the subtitles from the response
	defer res.Body.Close()
	content, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return []byte{}, fmt.Errorf("The content of the subtitles dowloaded from Subdb is corrupted")
	}

	return content, nil
}
示例#2
0
// This looks ugly as f**k
func decodeURL(url string, v interface{}) error {
	t := fluent.New()

	t.Get(url).Retry(3)
	if resp, err := t.Send(); err == nil {
		defer resp.Body.Close()
		if data, err := ioutil.ReadAll(resp.Body); err == nil {
			return decode(data, v)
		} else {
			return err
		}
	} else {
		return err
	}
	return nil
}
示例#3
0
// 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
}