forked from hoersuppe/hoerapi.php
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHoerAPI.php
159 lines (138 loc) · 4.29 KB
/
HoerAPI.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
<?php
namespace HoerAPI;
/**
* Create localized Datetime object
*
* @param string $date date string from Hoersuppe API
* @param string $zone timezone description
* @return null|Datetime
*/
function parseDate($date, $zone = null)
{
static $Local, $GMT;
switch (strtolower($zone)) {
case null:
case 'local':
case 'default':
case 'berlin':
case 'europe/berlin':
if (!isset($Local)) {
$Local = new \DateTimeZone('Europe/Berlin');
}
$zone = $Local;
break;
case 'gmt':
if (!isset($GMT)) {
$GMT = new \DateTimeZone('GMT');
}
$zone = $GMT;
break;
default:
return null;
}
return new \Datetime($date, $zone);
}
class HoerAPI
{
const URL = "http://hoersuppe.de/api/";
protected static function get(array $params = array())
{
$query = http_build_query($params);
$data = @file_get_contents(self::URL . "?" . $query);
if ($data === false) return false;
$data = json_decode($data, true);
if ($data === null) return false;
return $data;
}
/**
* Check if the API is available
*
* @return bool
*/
public static function status()
{
$data = self::get();
return !!$data;
}
/**
* Get information on a given podcast
*
* @param string $slug slug of the podcast
* @return bool|Podcast
*/
public static function getPodcastData($slug)
{
$data = self::get(array("action" => "getPodcastData", "podcast" => $slug));
if ($data === false || $data['status'] === 0) return false;
return new Podcast($data['data']);
}
/**
* Get a list of all podcasts
*
* @return bool|PodcastList
*/
public static function getPodcasts()
{
$data = self::get(array("action" => "getPodcasts"));
if ($data === false || $data['status'] === 0) return false;
return new PodcastList($data['data']);
}
/**
* Get a list of live dates for a given podcast
*
* @param string $podcast slug of the podcast
* @param bool|int $count maximum number of live dates or false if all
* @return bool|PodcastLive
*/
public static function getPodcastLive($podcast, $count = false)
{
if ($count !== false) $data = self::get(array("action" => "getPodcastLive", "podcast" => $podcast, "count" => $count));
else $data = self::get(array("action" => "getPodcastLive", "podcast" => $podcast));
if ($data === false || $data['status'] === 0) return false;
return new PodcastLive($data['data']);
}
/**
* Get a live date by ID
*
* @param $id
* @return bool|PodcastLiveItem
*/
public static function getLiveById($id) {
$data = self::get(array("action" => "getLiveById", "ID" => $id));
if ($data === false || $data['status'] === 0 || count($data['data']) <= 1) return false;
return new PodcastLiveItem($data['data'][0]);
}
/**
* Get episodes of a given podcast
*
* @param string $podcast slug of the podcast
* @param int $count number of episodes, default: 10
* @return bool|PodcastEpisodes
*/
public static function getPodcastEpisodes($podcast, $count=10) {
$data = self::get(array("action" => "getPodcastEpisodes", "podcast" => $podcast, "count" => $count));
if ($data === false || $data['status'] === 0) return false;
return new PodcastEpisodes($data['data']);
}
/**
* Get a list of deleted live dates
*
* @return bool|PodcastDeleted
*/
public static function getDeleted() {
$data = self::get(array("action" => "getDeleted"));
if ($data === false || $data['status'] === 0) return false;
return new PodcastDeleted($data['data']);
}
/**
* Get a list of the latest episode
*
* @param int $count number of episodes
* @return bool|PodcastEpisodes
*/
public static function getLastEpisodes($count=10) {
$data = self::get(array("action" => "getLastEpisodes", "count" => $count));
if ($data === false || $data['status'] === 0) return false;
return new PodcastEpisodes($data['data']);
}
}