Subversion Repositories cheapmusic

Rev

Rev 57 | Rev 66 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

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

error_reporting(E_ALL);

// Get itunes listings
function get_itunes($query, $searchType) {
    $vendors = Vendors::getInstance();
    $config = $vendors->getVendor(Vendors::ITUNES);
    $safequery = urlencode($query);
    $arrMusic = [];
    $arrEbooks = [];
    $arrAudiobooks = [];

    // Music (Albums)
    if ($_SESSION["filterMediaType"]["Digital"]) {
        $arrMusic = get_itunesCategory($config, $safequery, $searchType, "music", "album");
    }

    if ($_SESSION["filterMediaType"]["Book"]) {
        // ebooks
        $arrEbooks = get_itunesCategory($config, $safequery, $searchType, "ebook", "ebook");

        // audiobooks
        $arrAudiobooks = get_itunesCategory($config, $safequery, $searchType, "audiobook", "audiobook");
    }

    return (array_merge($arrMusic, $arrEbooks, $arrAudiobooks));
}

// Get linkshare listings by advertiser id
function get_itunesCategory($config, $safequery, $searchType, $mediaType, $entityType) {
    // API request variables
    $numResults = $config['numResults'];

    $url = "https://itunes.apple.com/";

    if (empty($_SESSION["barcode"]['Type'])) {
        $url .= "search";
        $url .= "?term=" . $safequery;
        $url .= "&media=" . $mediaType;
        $url .= "&entity=" . $entityType;
    }
    else {
        $url .= "lookup";
        $url .= "?upc=" . $safequery;
    }

    $url .= "&limit=" . $numResults;

    $result = getUrl($url);
    //$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->resultCount)) {
        if ($result->resultCount <= 0) {
            return [];
        }

        // If the response was loaded, parse it and store in array
        foreach ($result->results as $item) {
            $merchantName = "iTunes";
            $barcode = "";
            $barcodeType = "";
            $price = 0.00;

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

            $category = "Digital";

            if (!empty($item->wrapperType)) {
                $title = (string)$item->artistName . " - " . (string)$item->collectionName;
                $url = str_replace('http://', 'https://', (string)$item->collectionViewUrl);
                if (isset($item->collectionPrice)) {
                    $price = number_format(floatval($item->collectionPrice) , 2, '.', '');
                }
                if ($item->wrapperType == "collection") {
                    $category = "Digital";
                }
                else if ($item->wrapperType == "audiobook") {
                    $category = "Book";
                }
            }
            else if (!empty($item->kind) && $item->kind == "ebook") {
                $category = "Book";
                $title = (string)$item->artistName . " - " . (string)$item->trackName;
                $url = str_replace('http://', 'https://', (string)$item->trackViewUrl);
                if (isset($item->price)) {
                    $price = number_format(floatval($item->price) , 2, '.', '');
                }
            }

            if (empty($url)) {
                continue;
            }

            if ($price <= "0.00") {
                continue;
            }

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

            $currency = $item->currency;
            $timeLeft = 0;
            $listingType = 'Fixed';
            $location = 'US';
            $zip = '';
            $feedbackScore = - 1;
            $feedbackPercent = - 1;
            $sellerName = "";
            $handlingTime = - 1;
            $shippingCost = 0.00;
            $shippingCurrency = 'USD';
            $freeShippingCap = 0;

            $arr[] = array(
                "Merchant" => $merchantName,
                "Type" => "$type",
                "Title" => "$title",
                "Barcode" => "$barcode",
                "BarcodeType" => "$barcodeType",
                "Image" => "$pic",
                "URL" => "$url",
                "Category" => "$category",
                "Condition" => "$condition",
                "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",
                "ShippingCurrency" => "$shippingCurrency",
                "FreeShippingCap" => "$freeShippingCap",
                "Show" => true
            );
        }
    }
    // If the response does not indicate 'Success,' log the error(s)
    else {
        error_log($url);
        error_log($result->errorMessage);
        error_log($result->queryParameters);
    }

    return $arr;
}