Subversion Repositories cheapmusic

Rev

Rev 136 | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
include_once ('php/constants.php');

error_reporting(E_ALL);

// Get Walmart listings
function get_walmart($query, $searchCondition) {
    $vendors = Vendors::getInstance();
    $config = $vendors->getVendor(Vendors::WALMART);

    // Music
    $arrMusic = get_walmartCategory($config, $query, $searchCondition, $config["musicCategory"]);

    // Books
    $arrBooks = get_walmartCategory($config, $query, $searchCondition, $config["bookCategory"], $config["bookSubCategory"]);

    return (array_merge($arrMusic, $arrBooks));
}

// Get linkshare listings by advertiser id
function get_walmartCategory($config, $query, $searchCondition, $categoryId, $subCategories = []) {
    // API request variables
    $numResults = $config['numResults'];
    $url = '[cached]';

    $result = getSearchCache("walmart", $query, $categoryId);
    if ($result === false) {
        // Construct the findItemsByKeywords HTTP GET call
        $params = [];
        $params["apiKey"] = $config["apiKey"];
        $params["lsPublisherId"] = $config["publisherId"];
        $params["numItems"] = $numResults;
        $params["start"] = "1";
        $params["sort"] = "price";
        $params["order"] = "asc";
        $params["categoryId"] = $categoryId;
        $params["query"] = $query;

        $pairs = array();
        foreach ($params as $key => $value) {
            array_push($pairs, rawurlencode($key)."=".rawurlencode($value));
        }
        $canonical_query_string = join("&", $pairs);

        $url = "https://api.walmartlabs.com/v1/search?" . $canonical_query_string;
        $result = getUrl($url);

        saveSearchCache("walmart", $query, $categoryId, $result);
    }

    $result = utf8_encode($result);
    $result = json_decode($result);

    $arr = [];

    //echo "$url<pre>";print_r($result);echo "</pre>";
    // Check to see if the request found any results
    if (isset($result->totalResults)) {
        if ($result->totalResults <= 0) {
            return [];
        }

        // If the response was loaded, parse it and store in array
        foreach ($result->items as $item) {
            if (!empty($subCategories) && !empty($item->categoryNode) && !in_array($item->categoryNode, $subCategories)) {
                continue;
            }
            $merchantName = "Walmart";
            if (isset($item->marketplace) && $item->marketplace) {
                $merchantName .= " Marketplace";
            }
            $barcode = (!empty($item->upc) ? $item->upc : "");
            $barcodeType = clsLibGTIN::GTINCheck($barcode, false, 1);

            $title = (string)$item->name;
            $pic = str_replace('http://', 'https://', (string)$item->thumbnailImage);
            if (empty($pic)) {
                continue;
            }

            if (!isset($item->productTrackingUrl)) {
                continue;
            }
            $url = str_replace('http://', 'https://', (string)$item->productUrl);
            if (empty($url)) {
                continue;
            }

            if ((string)$result->categoryId == $config["musicCategory"]) {
                if (!empty($item->categoryPath) && strpos((string)$item->categoryPath, "Vinyl Records") !== false) {
                    $mediaType = "Record";
                }
                else if (!empty($item->categoryPath) && strpos((string)$item->categoryPath, "Cassette Tapes") !== false) {
                    continue;
                }
                else { // mediaType "...on CD or Vinyl"
                    if (strpos($title, "Vinyl") !== false) {
                        $mediaType = "Record";
                    }
                    else {
                        $mediaType = "CD";
                    }
                }
            }
            else {
                $mediaType = "Book";
            }

            $condition = 'New';
            $detailCondition = 'Brand New';
            $country = 'US';
            $bestOffer = false;

            $price = number_format(floatval(empty($item->salePrice) ? 0 : $item->salePrice) , 2, '.', '');
            if ($price <= "0.00") {
                continue;
            }

            $currency = 'USD';
            $timeLeft = 0;
            $listingType = 'Fixed';
            $location = 'US';
            $zip = '';
            $feedbackScore = - 1;
            $feedbackPercent = - 1;
            $sellerName = (!empty($item->sellerInfo) ? (string)$item->sellerInfo : "");
            $handlingTime = - 1;
            if (!(isset($item->marketplace) && $item->marketplace)) {
                $handlingTime = 1;
            }
            $shippingCost = 0.00;
            $shippingEstimated = false;
            if (isset($item->standardShipRate)) {
                $shippingCost = number_format(floatval($item->standardShipRate) , 2, '.', '');
            }
            $shippingCurrency = 'USD';
            $freeShippingCap = 0;
            //                  if ($item->isTwoDayShippingEligible == "1" || $item->freeShippingOver35Dollars == "1") {
            if (!(isset($item->marketplace) && $item->marketplace)) {
                $freeShippingCap = 35.00;
            }

            $arr[] = array(
                "Merchant" => $merchantName,
                "Condition" => $condition,
                "Title" => $title,
                "Barcode" => $barcode,
                "BarcodeType" => $barcodeType,
                "Image" => $pic,
                "URL" => $url,
                "MediaType" => $mediaType,
                "DetailCondition" => $detailCondition,
                "Country" => $country,
                "BestOffer" => $bestOffer,
                "TimeLeft" => $timeLeft,
                "Price" => $price,
                "Currency" => $currency,
                "ListingType" => $listingType,
                "Location" => $location,
                "Zip" => $zip,
                "FeedbackScore" => $feedbackScore,
                "FeedbackPercent" => $feedbackPercent,
                "SellerName" => $sellerName,
                "HandlingTime" => $handlingTime,
                "ShippingCost" => $shippingCost,
                "ShippingEstimated" => $shippingEstimated,
                "ShippingCurrency" => $shippingCurrency,
                "FreeShippingCap" => $freeShippingCap,
                "Show" => true,
                "Details" => ""
            );
        }
    }
    // If the response does not indicate 'Success,' log the error(s)
    else {
        if (!empty($result->errors)) {
            foreach ($result->errors as $error) {
                my_error_log($url);
                my_error_log("$error->message ($error->code)");
            }
        }
    }

    return $arr;
}