示例#1
0
文件: amazon.go 项目: vichuda/PiScan
// The Lookup function first looks for the given barcode in the barcodes
// database. If not found there, it tries the Amazon Product API, and save
// all those results into the barcodes database for future reference. It
// returns the json (a list of API structs, one per product) and error.
func Lookup(barcode string, asinLookup, asinInsert *sql.Stmt) ([]*commerce.API, error) {
	results := make([]*commerce.API, 0)
	var resultErr error

	// see if the barcode already exists in the db
	products, err := barcodes.LookupAsin(asinLookup, barcode)
	resultErr = err
	if err == nil && len(products) > 0 {
		for _, product := range products {
			result := new(commerce.API)
			result.SKU = product.Asin
			result.ProductName = product.ProductName
			result.ProductType = product.ProductType
			result.Vendor = strings.Join([]string{"AMZN", product.Locale}, ":")
			results = append(results, result)
		}
	} else {
		// if not, use the API instead, and save any results to the barcodes db
		api, aerr := apiLookup(barcode)
		resultErr = aerr
		if aerr == nil {
			// convert the api result string into a json object
			var apiList []commerce.API
			jerr := json.Unmarshal([]byte(api), &apiList)
			if jerr == nil {
				for _, apiResult := range apiList {
					// save each result for re-marshalling into json
					results = append(results, &apiResult)

					// and save it in the db, for the future
					prod := new(barcodes.AMAZON)
					prod.Barcode = barcode
					prod.Asin = apiResult.SKU
					prod.ProductName = apiResult.ProductName
					prod.ProductType = apiResult.ProductType
					vendor := strings.Split(apiResult.Vendor, ":")
					if len(vendor) > 0 {
						prod.Locale = vendor[1]
					} else {
						// use the default
						prod.Locale = "us"
					}
					_ = barcodes.InsertAsin(asinInsert, *prod)
				}
			}
		}
	}

	return results, resultErr
}
示例#2
0
文件: scan.go 项目: vichuda/PiScan
// Lookup the barcode, using both the barcodes database, and the Amazon API
func LookupBarcode(r *http.Request, db DBConnection) string {
	// the result is a json representation of the list of found products
	products := make([]*commerce.API, 0)

	// this function only responds to POST requests
	if "POST" == r.Method {
		r.ParseForm()

		barcodeVal, barcodeExists := r.PostForm["barcode"]
		if barcodeExists {
			queryFn := func(statements map[string]*sql.Stmt) {
				barcode := strings.Join(barcodeVal, "")

				// lookup the barcode versus the regular POD db
				podLookup, podLookupExists := statements[barcodes.GTIN_LOOKUP]
				if podLookupExists {
					podMatches, podMatchErr := barcodes.LookupGtin(podLookup, barcode)
					if podMatchErr == nil {
						for _, podMatch := range podMatches {
							// convert each podMatch GTIN struct to a commerce.API struct
							m := new(commerce.API)
							m.SKU = barcode
							m.ProductName = podMatch.ProductName
							products = append(products, m)
						}
					}
				}

				// lookup the barcode versus the Amazon db table/API
				asinLookup, asinLookupExists := statements[barcodes.ASIN_LOOKUP]
				asinInsert, asinInsertExists := statements[barcodes.ASIN_INSERT]
				if asinLookupExists && asinInsertExists {
					prods, prodErr := amazon.Lookup(barcode, asinLookup, asinInsert)
					if prodErr == nil {
						for _, prod := range prods {
							products = append(products, prod)
						}
					}
				}

				// supplement the list of results by looking at the user contributions
				contribLookup, contribLookupExists := statements[barcodes.BARCODE_LOOKUP]
				if contribLookupExists {
					contribMatches, contribMatchErr := barcodes.LookupContributedBarcode(contribLookup, barcode)
					if contribMatchErr == nil {
						for _, contrib := range contribMatches {
							// convert each contribMatch BARCODE struct to a commerce.API struct
							c := new(commerce.API)
							c.SKU = barcode
							c.ProductName = contrib.ProductName
							if contrib.ProductDesc != "" {
								c.ProductType = contrib.ProductDesc
							}
							products = append(products, c)
						}
					}
				}
			}
			WithServerDatabase(db, queryFn)
		}
	}

	result, err := json.Marshal(products)
	if err != nil {
		fmt.Println(err)
	}
	return string(result)
}