Subversion Repositories cheapmusic

Rev

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