forked from bendodson/itunes-artwork-finder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
113 lines (97 loc) · 3.02 KB
/
api.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
header("Access-Control-Allow-Origin: *");
$search = $_GET['query'];
$results = array();
if ($search) {
$entity = ($_GET['entity']) ? $_GET['entity'] : 'tvSeason';
$country = ($_GET['country']) ? $_GET['country'] : 'us';
$width = 600;
$height = 600;
$warning = '';
$shortFilm = false;
if ($entity == 'shortFilm') {
$shortFilm = true;
$entity = 'movie';
}
$url = 'http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/wa/wsSearch?term='.urlencode($search).'&country='.$country.'&entity='.$entity;
if ($shortFilm) {
$url .= '&attribute=shortFilmTerm';
$entity = 'shortFilm';
}
if ($_GET['entity'] == 'id' || $_GET['entity'] == 'idAlbum') {
$url = 'https://itunes.apple.com/lookup?id='.urlencode($search).'&country='.$_GET['country'];
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec($ch);
if ($errno = curl_errno($ch)) {
echo json_encode(array('error' => "We were unable to connect to iTunes. Please try again shortly."));
exit;
}
curl_close($ch);
$obj = json_decode($json);
foreach ($obj->results as $result) {
$data = array();
$data['url'] = str_replace('100x100', '600x600', $result->artworkUrl100);
$hires = str_replace('100x100bb', '100000x100000-999', $result->artworkUrl100);
$parts = parse_url($hires);
$hires = 'http://is5.mzstatic.com'.$parts['path'];
$data['hires'] = $hires;
$data['title'] = ($entity == 'movie') ? $result->trackName : $result->collectionName;
switch ($entity) {
case 'musicVideo':
$data['title'] = $result->trackName.' (by '.$result->artistName.')';
$data['url'] = $hires;
$width = 640;
$height = 464;
break;
case 'tvSeason':
$data['title'] = $result->collectionName;
break;
case 'movie':
case 'id':
case 'shortFilm':
$data['title'] = $result->trackName;
$width = 400;
$warning = '(may not work)';
break;
case 'ebook':
$data['title'] = $result->trackName.' (by '.$result->artistName.')';
$width = 400;
$warning = '(probably won\'t work)';
break;
case 'album':
case 'idAlbum':
$data['title'] = $result->collectionName.' (by '.$result->artistName.')';
//$warning = '(probably won\'t work)';
break;
case 'audiobook':
$data['title'] = $result->collectionName.' (by '.$result->artistName.')';
$warning = '(probably won\'t work)';
break;
case 'podcast':
$data['title'] = $result->collectionName.' (by '.$result->artistName.')';
break;
case 'software':
$data['url'] = str_replace('512x512bb', '1024x1024bb', $result->artworkUrl512);
$data['appstore'] = $result->trackViewUrl;
$data['title'] = $result->trackName;
$width = 512;
$height = 512;
break;
default:
break;
}
if ($data['title']) {
$data['width'] = $width;
$data['height'] = $height;
$data['warning'] = $warning;
$results[] = $data;
}
}
}
echo json_encode($results);
?>