Subversion Repositories cheapmusic

Rev

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