Ejemplo n.º 1
0
// Download downloads the OpenSubtitles subtitle from a video
func (s API) Download(videoPath string, language lang.Language) (subtitlePath string, err error) {
	c, err := osdb.NewClient()
	if err != nil {
		return "", err
	}
	c.UserAgent = userAgent

	// Anonymous login
	if err = c.LogIn("", "", ""); err != nil {
		return "", err
	}
	if language.OpenSubtitles == "" {
		return "", errors.New("Language exists but is not available for OpenSubtitles")
	}
	languages := []string{language.OpenSubtitles}

	// Search file
	subs, err := c.FileSearch(videoPath, languages)
	if err != nil {
		return "", err
	}

	// Keep best one
	best := subs.Best()
	if best == nil {
		return "", errors.New("Did not find best subtitle for this video")
	}

	// Saving to disk
	subtitlePath = videoPath[0:len(videoPath)-len(path.Ext(videoPath))] + ".srt"
	if err := c.DownloadTo(best, subtitlePath); err != nil {
		return "", err
	}

	logger.INFO.Println("Original name of subtitle :", best.SubFileName)

	return subtitlePath, nil
}
Ejemplo n.º 2
0
// UpdateFromOpenSubtitle updates the guess with the OpenSubtitle informations
func (g *Guesser) UpdateFromOpenSubtitle() error {
	// Base path of the filename
	basePath := filepath.Base(g.FilePath)

	// OpenSubtitle client
	client, err := osdb.NewClient()
	if err != nil {
		return err
	}

	// Log in
	if err := client.LogIn("", "", "eng"); err != nil {
		return err
	}

	// Set the languages
	languages := []string{"eng"}

	// Hash movie file, and search
	subtitles, err := client.FileSearch(g.FilePath, languages)
	if err != nil {
		return err
	}

	// If nothing found, search by filename
	if len(subtitles) == 0 {
		client, err := osdb.NewClient()
		if err != nil {
			return err
		}

		err = client.LogIn("", "", "eng")
		if err != nil {
			return err
		}

		params := []interface{}{
			client.Token,
			[]map[string]string{
				{
					"query":         basePath,
					"sublanguageid": "en",
				},
			},
		}

		subtitles, err = client.SearchSubtitles(&params)
		if err != nil {
			return err
		}
	}

	// No subtitles found
	if len(subtitles) == 0 {
		return nil
	}

	for _, sub := range subtitles {
		switch sub.MovieKind {
		case MovieType:
			g.OpenSubtitle = append(g.OpenSubtitle, &OpenSubtitle{
				ImdbID:    fmt.Sprintf("tt%07s", sub.IDMovieImdb),
				Type:      MovieType,
				MovieName: sub.MovieFileName,
				Year:      sub.MovieYear,
				LevDist:   levenshtein.Distance(sub.SubFileName, basePath),
			})
		case ShowType:
			// The MovieFileName field returned by openSubtitles contains the
			// show name and the episode name.
			// e.g. `"Show Title" Show episode title`
			// Only the title is relevant
			showName := sub.MovieFileName
			s := strings.Split(sub.MovieFileName, `"`)
			if len(s) >= 2 {
				showName = s[1]
			}

			g.OpenSubtitle = append(g.OpenSubtitle, &OpenSubtitle{
				ImdbID:   fmt.Sprintf("tt%07s", sub.SeriesIMDBParent),
				Type:     ShowType,
				ShowName: showName,
				Season:   sub.SeriesSeason,
				Episode:  sub.SeriesEpisode,
				Year:     sub.MovieYear,
				LevDist:  levenshtein.Distance(sub.SubFileName, basePath),
			})
		default:
			return fmt.Errorf("Invalid movie kind: %q", sub.MovieKind)
		}
	}

	return nil
}
Ejemplo n.º 3
0
type osProxy struct {
	client   *osdb.Client
	language string
	user     string
	password string
}

// Name implements the Module interface
func (osp *osProxy) Name() string {
	return moduleName
}

// Function to get a new client
var newOsdbClient = func() (*osdb.Client, error) {
	return osdb.NewClient()
}

// Function to check the client
var checkOsdbClient = func(c *osdb.Client) error {
	return c.Noop()
}

// Function to log in the client
var logInOsdbClient = func(c *osdb.Client, user, password, language string) error {
	return c.LogIn(user, password, language)
}

// Function to search subtitles via params
var searchOsdbSubtitles = func(c *osdb.Client, params []interface{}) (osdb.Subtitles, error) {
	return c.SearchSubtitles(&params)