Example #1
0
func Search(dbPath, term string, options repo.SearchOptions) ([]*repo.RecipeLink, error) {
	fmt.Println("Loading database...")
	cache, err := repo.OpenBleveCache(dbPath)
	if err != nil {
		return nil, err
	}
	defer cache.Close()

	ctx := context.Background()
	repo := allrecipes.New(cache)

	return repo.Search(ctx, term, options)
}
Example #2
0
func Refresh(dbPath string) error {
	// cache, err := repo.OpenJsonFileCache(cacheFileName)
	fmt.Println("Loading database...")
	cache, err := repo.OpenBleveCache(dbPath)
	if err != nil {
		return err
	}
	defer cache.Close()

	ctx := context.Background()
	repo := allrecipes.New(cache)
	fmt.Println("Refreshing...")
	return repo.Refresh(ctx)
}
Example #3
0
func Get(dbPath, term string) {
	fmt.Println("Loading database...")
	cache, err := repo.OpenBleveCache(dbPath)
	if err != nil {
		fmt.Println(err)
		return
	}
	defer cache.Close()

	ctx := context.Background()
	r := allrecipes.New(cache)

	fmt.Println("Searching...")
	options := repo.SearchOptions{
		Title: true,
		URL:   true,
	}
	recipeLinks, err := r.Search(ctx, term, options)
	if err != nil {
		fmt.Println(err)
		return
	}

	for i, link := range recipeLinks {
		fmt.Printf("Downloading %d/%d...\n”", i+1, len(recipeLinks))
		url := "http://allrecipes.com" + link.URL // TODO This should not contain indexer specific code
		recipes, err := r.Get(ctx, url)
		if err != nil {
			fmt.Println(err)
			return
		}
		// r.Get should currently only return one recipe but just in case go over all
		for _, recipe := range recipes {
			if err != nil {
				fmt.Printf("%s skipping...\n", err)
				continue
			}
			// TODO Don't use the recipe name, in future use an ID
			fileName := recipe.Name
			err = saveRecipe(fileName, recipe)
			if err != nil {
				fmt.Printf("%s skipping...\n", err)
				continue
			}
		}
	}
}