Subversion Repositories cheapmusic

Rev

Rev 137 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
57 - 1
<?php
65 - 2
include_once ('php/constants.php');
57 - 3
 
4
error_reporting(E_ALL);
5
 
6
// Get itunes listings
66 - 7
function get_itunes($query, $searchCondition) {
57 - 8
    $vendors = Vendors::getInstance();
9
    $config = $vendors->getVendor(Vendors::ITUNES);
65 - 10
    $arrMusic = [];
11
    $arrEbooks = [];
12
    $arrAudiobooks = [];
109 - 13
    $url = '[cached]';
57 - 14
 
15
    // Music (Albums)
16
    if ($_SESSION["filterMediaType"]["Digital"]) {
81 - 17
        $arrMusic = get_itunesCategory($config, $query, $searchCondition, "music", "album");
57 - 18
    }
19
 
20
    if ($_SESSION["filterMediaType"]["Book"]) {
21
        // ebooks
81 - 22
        $arrEbooks = get_itunesCategory($config, $query, $searchCondition, "ebook", "ebook");
57 - 23
 
24
        // audiobooks
81 - 25
        $arrAudiobooks = get_itunesCategory($config, $query, $searchCondition, "audiobook", "audiobook");
57 - 26
    }
27
 
65 - 28
    return (array_merge($arrMusic, $arrEbooks, $arrAudiobooks));
57 - 29
}
30
 
81 - 31
// Get iTunes listings by category
32
function get_itunesCategory($config, $query, $searchCondition, $mediaType, $entityType) {
65 - 33
    // API request variables
34
    $numResults = $config['numResults'];
57 - 35
 
99 - 36
    $result = getSearchCache("itunes", $query, $mediaType . $entityType);
37
    if ($result === false) {
38
        $url = "https://itunes.apple.com/";
39
        $params = [];
40
 
137 - 41
        if (empty($_SESSION["advSearch"]["Barcode"])) {
99 - 42
            $url .= "search";
43
            $params["term"] = $query;
44
            $params["media"] = $mediaType;
45
            $params["entity"] = $entityType;
46
        }
47
        else {
48
            $url .= "lookup";
137 - 49
            $params["upc"] = $_SESSION["advSearch"]["Barcode"];
99 - 50
        }
57 - 51
 
99 - 52
        $params["limit"] = $numResults;
57 - 53
 
99 - 54
        $pairs = array();
55
        foreach ($params as $key => $value) {
56
            array_push($pairs, rawurlencode($key)."=".rawurlencode($value));
57
        }
58
        $canonical_query_string = join("&", $pairs);
59
        $url .= "?" . $canonical_query_string;
60
 
61
        $result = getUrl($url);
62
 
63
        saveSearchCache("itunes", $query, $mediaType . $entityType, $result);
81 - 64
    }
99 - 65
 
65 - 66
    $result = json_decode($result);
57 - 67
 
65 - 68
    $arr = [];
57 - 69
 
65 - 70
    //echo "$url<pre>";print_r($result);echo "</pre>";
71
    // Check to see if the request found any results
72
    if (isset($result->resultCount)) {
57 - 73
        if ($result->resultCount <= 0) {
74
            return [];
75
        }
76
 
65 - 77
        // If the response was loaded, parse it and store in array
78
        foreach ($result->results as $item) {
79
            $merchantName = "iTunes";
80
            $barcode = "";
81
            $barcodeType = "";
82
            $price = 0.00;
57 - 83
 
65 - 84
            $pic = str_replace('http://', 'https://', (string)$item->artworkUrl100);
85
            if (empty($pic)) {
86
                continue;
87
            }
57 - 88
 
66 - 89
            $mediaType = "Digital";
57 - 90
 
91
            if (!empty($item->wrapperType)) {
65 - 92
                $title = (string)$item->artistName . " - " . (string)$item->collectionName;
93
                $url = str_replace('http://', 'https://', (string)$item->collectionViewUrl);
94
                if (isset($item->collectionPrice)) {
95
                    $price = number_format(floatval($item->collectionPrice) , 2, '.', '');
96
                }
57 - 97
                if ($item->wrapperType == "collection") {
66 - 98
                    $mediaType = "Digital";
65 - 99
                }
100
                else if ($item->wrapperType == "audiobook") {
66 - 101
                    $mediaType = "Book";
65 - 102
                }
103
            }
104
            else if (!empty($item->kind) && $item->kind == "ebook") {
66 - 105
                $mediaType = "Book";
65 - 106
                $title = (string)$item->artistName . " - " . (string)$item->trackName;
107
                $url = str_replace('http://', 'https://', (string)$item->trackViewUrl);
108
                if (isset($item->price)) {
109
                    $price = number_format(floatval($item->price) , 2, '.', '');
110
                }
111
            }
57 - 112
 
65 - 113
            if (empty($url)) {
114
                continue;
115
            }
57 - 116
 
65 - 117
            if ($price <= "0.00") {
118
                continue;
119
            }
57 - 120
 
66 - 121
            $condition = 'New';
122
            $detailCondition = 'Brand New';
65 - 123
            $country = 'US';
124
            $bestOffer = false;
57 - 125
 
65 - 126
            $currency = $item->currency;
127
            $timeLeft = 0;
128
            $listingType = 'Fixed';
129
            $location = 'US';
130
            $zip = '';
131
            $feedbackScore = - 1;
132
            $feedbackPercent = - 1;
133
            $sellerName = "";
134
            $handlingTime = - 1;
135
            $shippingCost = 0.00;
81 - 136
            $shippingEstimated = false;
65 - 137
            $shippingCurrency = 'USD';
57 - 138
            $freeShippingCap = 0;
139
 
65 - 140
            $arr[] = array(
141
                "Merchant" => $merchantName,
81 - 142
                "Condition" => $condition,
143
                "Title" => $title,
144
                "Barcode" => $barcode,
145
                "BarcodeType" => $barcodeType,
146
                "Image" => $pic,
147
                "URL" => $url,
148
                "MediaType" => $mediaType,
149
                "DetailCondition" => $detailCondition,
150
                "Country" => $country,
151
                "BestOffer" => $bestOffer,
152
                "TimeLeft" => $timeLeft,
153
                "Price" => $price,
154
                "Currency" => $currency,
155
                "ListingType" => $listingType,
156
                "Location" => $location,
157
                "Zip" => $zip,
158
                "FeedbackScore" => $feedbackScore,
159
                "FeedbackPercent" => $feedbackPercent,
160
                "SellerName" => $sellerName,
161
                "HandlingTime" => $handlingTime,
162
                "ShippingCost" => $shippingCost,
163
                "ShippingEstimated" => $shippingEstimated,
164
                "ShippingCurrency" => $shippingCurrency,
165
                "FreeShippingCap" => $freeShippingCap,
143 - 166
                "Show" => true,
167
                "Details" => ""
65 - 168
            );
169
        }
170
    }
171
    // If the response does not indicate 'Success,' log the error(s)
172
    else {
96 - 173
        my_error_log($url);
80 - 174
	if (!empty($result->errorMessage) && !empty($result->queryParameters)) {
96 - 175
            my_error_log($result->errorMessage);
176
            my_error_log($result->queryParameters);
80 - 177
        } else {
96 - 178
            my_error_log(print_r($result, 1));
80 - 179
        }
65 - 180
    }
57 - 181
 
65 - 182
    return $arr;
57 - 183
}