示例#1
0
// CartsList lists all active carts
func CartsList(c *cli.Context) {
	color.Allow(c)

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

	index := 1
	cache := make(map[string]string)
	for _, cart := range conf.Carts {
		fmt.Printf("(%s) %s\n", color.ShortID(strconv.Itoa(index)), cart.Name)
		cache[strconv.Itoa(index)] = cart.Name
		index++
	}
	conf.ResultCache["Carts"] = cache
}
示例#2
0
// CartInfo prints information about a cart
func CartInfo(c *cli.Context) {
	color.Allow(c)

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

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

	cartName := conf.CartNameFromCache(c.Args().First())

	if cart, exists := conf.Carts[cartName]; !exists {
		fmt.Fprintf(os.Stderr, "Cart %s is unknown\n", cartName)
		os.Exit(1)
	} else {
		fmt.Printf("\nCart %s\n\n", color.Header(cart.Name))

		getResponse, getErr := api.CartGet(cart.CartID, cart.HMAC)

		if getErr != nil {
			panic(getErr)
		}

		index := 1
		cache := make(map[string]string)
		for _, item := range getResponse.Cart.CartItems.CartItemList {
			fmt.Printf("(%s) %-45.45s %9s [×%d]\n",
				color.ShortID(strconv.Itoa(index)),
				item.Title,
				item.ItemTotal.FormattedPrice,
				item.Quantity)
			cache[strconv.Itoa(index)] = item.CartItemID
			index++
		}
		conf.ResultCache["Cart"+strings.Title(cartName)+"Items"] = cache

		if len(getResponse.Cart.CartItems.CartItemList) == 0 {
			fmt.Println("Cart is empty")
		} else {
			fmt.Printf("\nSubtotal %s\n\n", color.Bold(getResponse.Cart.SubTotal.FormattedPrice))
		}
	}
}
示例#3
0
// 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)
}