Subversion Repositories cheapmusic

Rev

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