Subversion Repositories cheapmusic

Rev

Rev 137 | 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, $searchCondition) {
    $vendors = Vendors::getInstance();
    $config = $vendors->getVendor(Vendors::ITUNES);
    $arrMusic = [];
    $arrEbooks = [];
    $arrAudiobooks = [];
    $url = '[cached]';

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

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

        // audiobooks
        $arrAudiobooks = get_itunesCategory($config, $query, $searchCondition, "audiobook", "audiobook");
    }

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

// Get iTunes listings by category
function get_itunesCategory($config, $query, $searchCondition, $mediaType, $entityType) {
    // API request variables
    $numResults = $config['numResults'];

    $result = getSearchCache("itunes", $query, $mediaType . $entityType);
    if ($result === false) {
        $url = "https://itunes.apple.com/";
        $params = [];
        
        if (empty($_SESSION["advSearch"]["Barcode"])) {
            $url .= "search";
            $params["term"] = $query;
            $params["media"] = $mediaType;
            $params["entity"] = $entityType;
        }
        else {
            $url .= "lookup";
            $params["upc"] = $_SESSION["advSearch"]["Barcode"];
        }

        $params["limit"] = $numResults;

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

        $result = getUrl($url);

        saveSearchCache("itunes", $query, $mediaType . $entityType, $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;
            }

            $mediaType = "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") {
                    $mediaType = "Digital";
                }
                else if ($item->wrapperType == "audiobook") {
                    $mediaType = "Book";
                }
            }
            else if (!empty($item->kind) && $item->kind == "ebook") {
                $mediaType = "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;
            }

            $condition = 'New';
            $detailCondition = '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;
            $shippingEstimated = false;
            $shippingCurrency = 'USD';
            $freeShippingCap = 0;

            $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 {
        my_error_log($url);
        if (!empty($result->errorMessage) && !empty($result->queryParameters)) {
            my_error_log($result->errorMessage);
            my_error_log($result->queryParameters);
        } else {
            my_error_log(print_r($result, 1));
        }
    }

    return $arr;
}