Exemple #1
0
func searchTvshow(c *command, db *imdb.DB, query string) (*imdb.Tvshow, error) {
	tvsearch, err := search.Query(db, query)
	if err != nil {
		return nil, err
	}
	tvsearch.Chooser(c.chooser)
	tvsearch.Entity(imdb.EntityTvshow)
	tvsearch.Votes(flagVotes, -1)

	results, err := tvsearch.Results()
	if err != nil {
		return nil, err
	}
	if len(results) == 0 {
		return nil, ef("Could not find TV show.")
	}
	tv, err := tvsearch.Pick(results)
	if err != nil {
		return nil, err
	}
	if tv == nil {
		return nil, ef("No TV show results to pick from.")
	}
	if tv.Entity != imdb.EntityTvshow {
		return nil, ef("Expected TV show but got %s", tv.Entity)
	}
	ent, err := tv.GetEntity(db)
	if err != nil {
		return nil, err
	}
	return ent.(*imdb.Tvshow), nil
}
Exemple #2
0
func (c *command) results(db *imdb.DB, one bool) ([]search.Result, bool) {
	searcher, err := search.Query(db, strings.Join(c.flags.Args(), " "))
	if err != nil {
		pef("%s", err)
		return nil, false
	}
	searcher.Chooser(c.chooser)

	results, err := searcher.Results()
	if err != nil {
		pef("%s", err)
		return nil, false
	}
	if len(results) == 0 {
		pef("No results found.")
		return nil, false
	}
	if one {
		r, err := searcher.Pick(results)
		if err != nil {
			pef("%s", err)
			return nil, false
		}
		if r == nil {
			pef("No results to pick from.")
			return nil, false
		}
		return []search.Result{*r}, true
	}
	return results, true
}
Exemple #3
0
func guessEpisode(
	c *command,
	db *imdb.DB,
	fname string,
) (*imdb.Episode, error) {
	fname = path.Base(fname)
	s, e, start, _, err := episodeNumbers(fname, flagRenameRegexEpisode)
	if err != nil {
		return nil, ef("Could not find episode numbers: %s", err)
	}

	// A guess at where the TV show name is in the file name.
	title := strings.TrimSpace(fname[0:start])

	tvsub, err := search.Query(db, title)
	if err != nil {
		return nil, err
	}
	tvsub.Entity(imdb.EntityTvshow)
	tvsub.Votes(flagVotes, -1)

	esearch := search.New(db)
	esearch.Tvshow(tvsub)
	esearch.Entity(imdb.EntityEpisode)
	esearch.Seasons(s, s).Episodes(e, e)
	esearch.Chooser(c.chooser)

	results, err := esearch.Results()
	if err != nil {
		return nil, err
	}
	if len(results) == 0 {
		return nil, ef("Could not find episode.")
	}
	m, err := esearch.Pick(results)
	if err != nil {
		return nil, err
	}
	if m == nil {
		return nil, ef("No episode results to pick from.")
	}
	if m.Entity != imdb.EntityEpisode {
		return nil, ef("Expected episode but got %s", m.Entity)
	}
	ent, err := m.GetEntity(db)
	if err != nil {
		return nil, err
	}
	return ent.(*imdb.Episode), nil
}
Exemple #4
0
func cmd_rename(c *command) bool {
	if len(flagRenameTvshow) > 0 {
		return cmd_rename_tvshow(c, flagRenameTvshow)
	}
	firstArg := c.flags.Arg(0)
	if _, err := os.Stat(firstArg); err == nil {
		return cmd_rename_smart(c)
	}

	c.assertLeastNArg(2)
	db := openDb(c.dbinfo())
	defer closeDb(db)

	query := c.flags.Arg(0)
	files := fun.Map(path.Clean, c.flags.Args()[1:]).([]string)
	searcher, err := search.Query(db, query)
	if err != nil {
		pef("%s", err)
		return false
	}
	searcher.Chooser(c.chooser)
	searcher.Limit(len(files))

	results, err := searcher.Results()
	if err != nil {
		pef("%s", err)
		return false
	}
	if len(results) == 0 {
		pef("No search results.")
		return false
	}
	if len(results) < len(files) {
		pef("Omitting last %d file(s) since there are only %d search results.",
			len(files)-len(results), len(results))
		files = files[0:len(results)]
	}

	entities := make([]imdb.Entity, len(results))
	for i, r := range results {
		e, err := r.GetEntity(db)
		if err != nil {
			pef("Could not get entity for '%s': %s", r, err)
			return false
		}
		entities[i] = e
	}
	return doRename(c, db, files, entities)
}
Exemple #5
0
func guessMovie(c *command, db *imdb.DB, fname string) (*imdb.Movie, error) {
	fname = path.Base(fname)
	year, ystart, _, err := fileNameYear(fname, flagRenameRegexYear)
	if err != nil {
		return nil, ef("Could not find year for movie: %s", err)
	}

	// A guess at where the title is in the file name.
	title := strings.TrimSpace(fname[0:ystart])

	msearch, err := search.Query(db, title)
	if err != nil {
		return nil, err
	}
	msearch.Entity(imdb.EntityMovie)
	msearch.Years(year-1, year+1)
	msearch.Chooser(c.chooser)
	msearch.Votes(flagVotes, -1)

	results, err := msearch.Results()
	if err != nil {
		return nil, err
	}
	if len(results) == 0 {
		return nil, ef("Could not find movie.")
	}
	m, err := msearch.Pick(results)
	if err != nil {
		return nil, err
	}
	if m == nil {
		return nil, ef("No movie results to pick from.")
	}
	if m.Entity != imdb.EntityMovie {
		return nil, ef("Expected movie but got %s", m.Entity)
	}
	ent, err := m.GetEntity(db)
	if err != nil {
		return nil, err
	}
	return ent.(*imdb.Movie), nil
}