コード例 #1
0
ファイル: search.go プロジェクト: matthistuff/amazon
// Search does a product search
func Search(c *cli.Context) {
	color.Allow(c)

	search := strings.Replace(strings.Join(c.Args(), "+"), " ", "+", -1)
	page := c.Int("page")
	sort := c.String("sort")
	searchIndex := c.String("index")
	api := api.Create(c.GlobalString("locale"))

	conf := config.GetConfig()
	defer conf.Flush()

	params := map[string]string{
		"Keywords":      search,
		"ResponseGroup": "ItemAttributes,Small,EditorialReview,OfferSummary,BrowseNodes",
		"ItemPage":      strconv.FormatInt(int64(page), 10),
	}
	if sort != "" {
		params["Sort"] = sort
	}
	result, err := api.ItemSearch(searchIndex, params)

	if err != nil {
		panic(err)
	}

	fmt.Printf("\nFound %d results matching query %s\n\n", result.Items.TotalResults, color.Header("'%s'", search))

	cache := make(map[string]string)
	for index, item := range result.Items.ItemList {
		price := item.ItemAttributes.ListPrice.FormattedPrice

		if price == "" {
			if lowestNew := item.OfferSummary.LowestNewPrice; lowestNew.Amount != 0 {
				price = fmt.Sprintf("%s (new)", lowestNew.FormattedPrice)
			} else if lowestUsed := item.OfferSummary.LowestUsedPrice; lowestUsed.Amount != 0 {
				price = fmt.Sprintf("%s (used)", lowestUsed.FormattedPrice)
			} else {
				price = "n/a"
			}
		}

		year := item.ItemAttributes.PublicationDate
		if year == "" {
			year = item.ItemAttributes.ReleaseDate
		}
		if year != "" {
			year = fmt.Sprintf(" (%4.4s)", year)
		}

		ratingFormatted := ""
		if !c.Bool("no-rating") {
			rating, err := helper.Rating(item.ASIN, api.Locale)
			if err != nil {
				panic(err)
			}

			ratingFormatted = fmt.Sprintf("%-5s ", helper.FormatRating(rating))
		}

		normalizedIndex := index + 1
		cache[strconv.Itoa(normalizedIndex)] = item.ASIN

		maxLen := math.Min(float64(52-len(year)), float64(len(item.ItemAttributes.Title)))
		fmt.Printf("(%s) %-52s %s%s [%s]\n",
			color.ShortID("%2d", normalizedIndex),
			fmt.Sprintf("%s%s", item.ItemAttributes.Title[:int(maxLen)], year),
			color.Faint(ratingFormatted),
			color.Bold("%9s", price),
			item.ItemAttributes.Binding)
	}
	conf.ResultCache["Products"] = cache

	fmt.Printf("\nPage %d of %d\n\n", page, result.Items.TotalPages)
}
コード例 #2
0
ファイル: info.go プロジェクト: matthistuff/amazon
// Info prints information about a product
func Info(c *cli.Context) {
	color.Allow(c)

	api := api.Create(c.GlobalString("locale"))
	config := config.GetConfig()

	asin, exists := config.ASINFromCache("Products", c.Args().First())
	if !exists {
		fmt.Fprintln(os.Stderr, "Cannot look up ASIN")
		os.Exit(1)
	}

	result, err := api.ItemLookup(asin, "ItemAttributes,Small,OfferFull,EditorialReview")
	if err != nil {
		panic(err)
	}

	item := result.Items.ItemList[0]
	rating, err := helper.Rating(item.ASIN, api.Locale)
	if err != nil {
		panic(err)
	}

	year := item.ItemAttributes.PublicationDate
	if year == "" {
		year = item.ItemAttributes.ReleaseDate
	}
	if year != "" {
		year = fmt.Sprintf(" - %4.4s", year)
	}

	fmt.Printf("%s %s\n", color.Header(item.ItemAttributes.Title), helper.FormatRating(rating))

	fmt.Println(color.Faint(fmt.Sprintf("(%s) %s%s",
		strings.Join(item.ItemAttributes.Languages.Languages(), ", "),
		item.ItemAttributes.Binding,
		year)))

	fmt.Println("")
	if item.ItemAttributes.ListPrice.FormattedPrice != "" {
		fmt.Printf("%s\n", color.Bold(item.ItemAttributes.ListPrice.FormattedPrice))
	}
	if item.OfferSummary.TotalNew > 0 {
		fmt.Printf("%d new from %s\t", item.OfferSummary.TotalNew, item.OfferSummary.LowestNewPrice.FormattedPrice)
	}
	if item.OfferSummary.TotalUsed > 0 {
		fmt.Printf("%d used from %s\t", item.OfferSummary.TotalUsed, item.OfferSummary.LowestUsedPrice.FormattedPrice)
	}
	fmt.Printf("\n\n")

	if len(item.ItemAttributes.Authors) > 0 {
		fmt.Println(color.Header("AUTHORS"))
		for _, author := range item.ItemAttributes.Authors {
			fmt.Printf("\t%s\n", author)
		}
	}

	if item.ItemAttributes.Publisher != "" {
		fmt.Printf("%s\n\t%s\n", color.Header("PUBLISHER"), item.ItemAttributes.Publisher)
	}

	if item.ItemAttributes.Edition != "" {
		fmt.Printf("%s\n\t%s\n", color.Header("EDITION"), item.ItemAttributes.Edition)
	}

	if item.ItemAttributes.ISBN != "" {
		fmt.Printf("%s\n\t%s\n", color.Header("ISBN"), item.ItemAttributes.ISBN)
	}

	if len(item.EditorialReviews.EditorialReviewList) > 0 {
		fmt.Println(color.Header(strings.ToUpper(item.EditorialReviews.EditorialReviewList[0].Source)))
		fmt.Println(helper.FormatText(item.EditorialReviews.EditorialReviewList[0].Content, 80, "\t"))
	}
}