Example #1
0
func keywordHandler(params martini.Params, res http.ResponseWriter, req *http.Request) {
	word := params["word"]
	keyword, err := model.FindKeyword(word)
	if err != nil {
		if err == mgo.ErrNotFound {
			res.Write([]byte(fmt.Sprintf("stop entering keywords into the url manually")))
			return
		}
		res.Write([]byte(fmt.Sprintf("error: %v", err)))
		return
	}

	tmpl, err := template.ParseFiles(filepath.Join(frontEndRoot, "keyword.html"))
	if err != nil {
		res.Write([]byte(fmt.Sprintf("error: %v", err)))
		return
	}
	tmpl.Execute(res, keyword)
}
Example #2
0
func chooseHandler(params martini.Params, res http.ResponseWriter, req *http.Request) {
	word := params["word"]
	keyword, err := model.FindKeyword(word)
	if err != nil {
		if err == mgo.ErrNotFound {
			res.Write([]byte(fmt.Sprintf("stop entering keywords into the url manually")))
			return
		}
		res.Write([]byte(fmt.Sprintf("error: %v", err)))
		return
	}

	// the previous words
	prev := req.FormValue("previous")
	allPrev := strings.Split(prev, ",")
	if prev == "" {
		allPrev = []string{}
	}

	// find all movies matching the keyword
	movies, err := model.FindMovies(keyword.Movies)
	if err != nil {
		res.Write([]byte(fmt.Sprintf("error: %v", err)))
		return
	}

	// filter out to movies also matching the previous keywords
	allMatches := append(allPrev, word)
	moviesMatching := []model.Movie{}
	for _, movie := range movies {
		matches := true
		for _, w := range allMatches {
			if !util.SliceHasString(movie.Keywords, w) {
				matches = false
				break
			}
		}
		if matches {
			moviesMatching = append(moviesMatching, movie)
		}
	}

	// we narrowed it down!
	if len(moviesMatching) == 1 {
		tmpl, err := template.ParseFiles(filepath.Join(frontEndRoot, "final_choice.html"))
		if err != nil {
			res.Write([]byte(fmt.Sprintf("error: %v", err)))
			return
		}
		tmpl.Execute(res, moviesMatching[0])
		return
	}

	// we have to narrow it down farther.  get one keyword from each movie matching
	// all of them so far
	newKeywords := []model.Keyword{}
	for _, movie := range moviesMatching {
		n := rand.Int() % len(movie.Keywords)
		kw, err := model.FindKeyword(movie.Keywords[n])
		if err != nil {
			res.Write([]byte(fmt.Sprintf("error: %v", err)))
			return
		}
		kw.Movies = []string{}
		// don't repeat a word.  this could possibly, in a tiny circumstance,
		// cause newKeywords to be empty
		if !util.SliceHasString(allMatches, kw.Word) {
			newKeywords = append(newKeywords, *kw)
		}
	}

	tmpl, err := template.ParseFiles(filepath.Join(frontEndRoot, "choose_keyword.html"))
	if err != nil {
		res.Write([]byte(fmt.Sprintf("error: %v", err)))
		return
	}
	tmpl.Execute(res, &chooseInfo{Keywords: newKeywords, Previous: allMatches,
		Header: getHeader(false)})
}