Пример #1
0
func TestMovieList(t *testing.T) {
	s := createTestServer()
	defer s.Close()
	defer os.Remove(TEST_DATABASE)

	m1 := &Movie{Title: "Fight Club", Filename: "m1"}
	if err := database.SaveMovie(m1); err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	m2 := &Movie{Title: "Rio 2", Filename: "m2"}
	if err := database.SaveMovie(m2); err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	r, err := http.Get(s.URL + "/api/movies")
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	var response []Movie
	err = json.NewDecoder(r.Body).Decode(&response)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	if len(response) != 2 {
		t.Errorf("expected 2 results, got %d", len(response))
	}
}
Пример #2
0
func TestDeleteMovie(t *testing.T) {
	s := createTestServer()
	defer s.Close()
	defer os.Remove(TEST_DATABASE)

	m := testMovie()
	if err := database.SaveMovie(m); err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	r, err := Delete(s.URL + "/api/movies/" + strconv.FormatInt(m.Id, 10))
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	if r.StatusCode != http.StatusOK {
		t.Errorf("expected %d status code, got %d", http.StatusOK, r.StatusCode)
	}

	var rm Movie
	err = json.NewDecoder(r.Body).Decode(&rm)
	if err != nil {
		t.Errorf("unexpected error: %v", err)
	}

	if m.Id != rm.Id {
		t.Errorf("expected returned item id %d, got id %d", m.Id, rm.Id)
	}

	// Double check this doesn't exist in the database
	if movie, err := database.GetMovie(m.Id); err == nil {
		t.Errorf("expected error fetching deleted movie, got %v", movie)
	}
}
Пример #3
0
func FetchMetadataForMovie(movie *Movie) (err error) {
	var query string
	if movie.Title != "" {
		query = movie.Title
	} else {
		// Try to get the file name info from GuessIt
		meta, err := guessit.Guess(movie.Filename)
		if err != nil {
			log.Printf("Failed to guess movie info: %+v", err)

			// Fallback to simple filename guessing
			filename := filepath.Base(movie.Filename)
			extension := filepath.Ext(movie.Filename)
			query = filename[:len(filename)-len(extension)]
		} else {
			query = meta.Title
		}
	}
	log.Printf("Fetching metadata for %s", query)

	movies, err := con.SearchMovies(query)
	if err != nil {
		log.Printf("Error querying TheMovieDB for %s: %+v", query, err)
		return err
	}

	if len(movies) < 1 {
		log.Printf("No movies found for name: %s", query)
		return nil
	}

	// For now, we'll assume the first match is the winner
	match := movies[0]
	movie.Title = match.Title
	movie.BackdropPath = match.BackdropPath
	movie.PosterPath = match.PosterPath
	movie.Adult = match.Adult
	movie.Homepage = match.Homepage
	movie.ImdbId = match.ImdbID
	movie.Overview = match.Overview
	movie.Runtime = match.Runtime
	movie.Tagline = match.Tagline
	movie.UserRating = match.VoteAverage
	err = database.SaveMovie(movie)
	if err != nil {
		return err
	}

	return nil
}
Пример #4
0
func processMovie(path string, queue *Queue) error {
	movie, _ := database.GetMovieByFilename(path)
	if movie == nil || movie.Filename == "" {
		movie, _ = NewMovie(path)
	}

	err := database.SaveMovie(movie)
	if err != nil {
		log.Printf("Error saving movie: %+v", err)
		return err
	}

	queue.Add(&FetchMetadataTask{Movie: movie})
	return nil
}
Пример #5
0
// PUT /api/movies
// Creates a new movie
func MovieCreate(w http.ResponseWriter, r *http.Request) (interface{}, error) {
	var m Movie
	err := json.NewDecoder(r.Body).Decode(&m)
	if err != nil {
		return nil, err
	}

	if m.ID != 0 {
		return nil, badRequest{errors.New("Movie already has an ID")}
	}

	err = database.SaveMovie(&m)
	if err != nil {
		return nil, err
	}

	return m, nil
}