-
Notifications
You must be signed in to change notification settings - Fork 0
/
podcast_tracker.php
160 lines (137 loc) · 5.77 KB
/
podcast_tracker.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
class podcast_tracker
{
private $podcast_id = '';
public $name = '';
public function __construct($id){
//iTunes Podcast ID.
$this->podcast_id = $id;
$this->name = $this->getPodcastName();
}
public function getVotes(){
$curl = curl_init();
//Runs request against podcast URL. Mimics iTunes User agent so that it will return HTML to parse.
//Uses the US X-Apple-Store-Front so only will pull the total number of reviews in the US.
//This can be modfied to pull from other contries.
//Code modified from
//https://gist.github.com/sgmurphy/1878352
curl_setopt_array($curl, array(
CURLOPT_URL => 'http://itunes.apple.com/WebObjects/MZStore.woa/wa/customerReviews?displayable-kind=4&id='.$this->podcast_id,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'User-Agent: iTunes/10.3.1 (Macintosh; Intel Mac OS X 10.6.8) AppleWebKit/533.21.1',
'X-Apple-Store-Front: 143441-1,12',
'X-Apple-Tz: -18000',
'Accept-Language: en-us, en;q=0.50'
),
));
$ratings = array();
$response = curl_exec($curl);
$dom = new DOMDocument();
@$dom->loadHTML($response);
//Parses HTML to find the raiting count of 5 star reviews.
$xpath = new DOMXPath($dom);
$nodes = $xpath->query("//span[@class='rating-count']");
if ($nodes->item(0)) {
array_push($ratings,(int)preg_replace('/([\d]+)/', '$1', $nodes->item(0)->nodeValue));
} else {
$ratings[] = 0;
}
curl_close($curl);
return $ratings;
}
private function getPodcastName(){
//Gets the name of the podcast based on the rss feed.
$lookupURL = "https://itunes.apple.com/lookup?id=".$this->podcast_id."&entity=podcast";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://itunes.apple.com/lookup?id=1105760780&entity=podcast',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$response = $this->object_to_array(json_decode(curl_exec($curl)));
curl_close($curl);
return $response['results'][0]['collectionName'];
}
public function getRandomReview(){
$curl = curl_init();
//Get RSS of Podcast Comments
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://itunes.apple.com/us/rss/customerreviews/id='.$this->podcast_id.'/json',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$rss = $this->object_to_array(json_decode(curl_exec($curl)));
curl_close($curl);
//If Podcast doesn't have any comments or error display title
if(count($rss['feed']) < 1){
return array("comment"=>'You have no comments',"name"=>'Anonymous', "title"=>'Sorry');
}
//get total number of reviews to make sure we don't pick a random number that doesn't exist
$reviews = count($rss['feed']['entry']);
//Choose a random review to parse data from the RRS feed with
$rand = mt_rand(0,$reviews);
//Get name, comment, title, and rating
$name = $rss["feed"]["entry"][$rand]["author"]["name"]["label"];
$comment = $rss["feed"]["entry"][$rand]["content"]["label"];
$title = $rss["feed"]["entry"][$rand]["title"]["label"];
//you could use rating to filter an re-run if the number does not = 5
$rating = $rss["feed"]["entry"][$rand]["im:rating"]["label"];
return array("comment"=>$comment,"name"=>$name, "title"=>$title);
}
public function getArtwork(){
//Gets largest podcast artwork and returns the URL
$lookupURL = "https://itunes.apple.com/lookup?id=".$this->podcast_id."&entity=podcast";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://itunes.apple.com/lookup?id=1105760780&entity=podcast',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'GET',
));
$response = $this->object_to_array(json_decode(curl_exec($curl)));
curl_close($curl);
//Get the largest size image that iTunes will allow here
$artworkURL = $response['results'][0]['artworkUrl600'];
//return artwork url;
return $artworkURL;
}
private function html_to_obj($html) {
$dom = new DOMDocument();
$dom->loadHTML($html);
return $dom;
}
private function object_to_array($data){
if (is_array($data) || is_object($data))
{
$result = array();
foreach ($data as $key => $value)
{
$result[$key] = $this->object_to_array($value);
}
return $result;
}
return $data;
}
}
?>