-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.php
134 lines (109 loc) · 3.64 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
require 'autoload.php';
use PodcastCrawler\PodcastCrawler;
use PodcastCrawler\Provider\Itunes;
$podcast_domain = ['youtube', 'apple', 'spotify'];
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$url_input = $_REQUEST['url'];
$podcastLinks = [];
if (strpos($url_input, $podcast_domain[0]) !== false) { //Get by script tag === youtube URL
$podcastLinks[] = getYouTubeVideoDownloadLink($url_input);
} else if (strpos($url_input, $podcast_domain[1]) !== false) { //Get by script tag === Apple URL
$podcastLinks = getAppleDownloadUrls($url_input);
} else if (strpos($url_input, $podcast_domain[2]) !== false) { //Get by script tag === Apple URL
$podcastLinks = getSpotifyDownloadUrls($url_input);
header('Content-Type: application/json');
echo json_encode($podcastLinks);
return;
} else {
$html = file_get_contents($url_input);
//Instantiate the DOMDocument class.
$htmlDom = new DOMDocument;
// //Parse the HTML of the page using DOMDocument::loadHTML
@$htmlDom->loadHTML($html);
//Get by audio tag
$audios = $htmlDom->getElementsByTagName('audio');
foreach ($audios as $audio) {
$podcastLinks[] = $audio->nodeValue;
}
}
$podcastLinks = array_values(array_unique($podcastLinks));
header('Content-Type: application/json');
echo json_encode($podcastLinks);
}
// Spotify
function getSpotifyDownloadUrls($url_input)
{
$podcastLinks = [];
$code_input = $_REQUEST['code'];
if (!$code_input) {
return $podcastLinks;
}
$types = ['show'];
$path = parse_url($url_input)['path'];
$id = explode("/", $path);
$type = $id[1];
$id = end($id);
$session = new SpotifyWebAPI\Session(
$_ENV['SPOTIFY_CLIENT_ID'],
$_ENV['SPOTIFY_CLIENT_SECRET'],
$_ENV['SPOTIFY_CALL_BACK_URL'],
);
// Request a access token using the code from Spotify
$session->requestAccessToken($code_input);
$accessToken = $session->getAccessToken();
$api = new SpotifyWebAPI\SpotifyWebAPI();
$api->setAccessToken($accessToken);
switch ($type) {
case $types[0]:
$datas = $api->getShow($id);
if(isset($datas) && isset($datas->episodes) && isset($datas->episodes->items) && count($datas->episodes->items) > 0){
foreach ($datas->episodes->items as $value) {
$podcastLinks[] = $value->audio_preview_url;
}
}
break;
default:
break;
}
return $podcastLinks;
}
// Apple
function getAppleDownloadUrls($url_input)
{
$podcastLinks = [];
$path = parse_url($url_input)['path'];
$id = explode("/", $path);
$id = end($id);
$PodcastCrawler = new PodcastCrawler(new Itunes);
$get_by_id = $PodcastCrawler->limit(1)->get($id);
$find_podcasts = $PodcastCrawler->find($get_by_id['podcasts'][0]['rss']);
if (isset($find_podcasts) && $find_podcasts['episodes_total'] > 0) {
foreach ($find_podcasts['episodes'] as $value) {
$podcastLinks[] = $value['mp3'];
}
}
return $podcastLinks;
}
// Youtube
function extractYoutubeVideoId($video_url)
{
//parse the url
$parsed_url = parse_url($video_url);
if (isset($parsed_url["query"])) {
$query_string = $parsed_url["query"];
//parse the string separated by '&' to array
parse_str($query_string, $query_arr);
if (isset($query_arr["v"])) {
return $query_arr["v"];
}
}
}
function getYouTubeVideoDownloadLink($url)
{
parse_str(file_get_contents("http://youtube.com/get_video_info?video_id=" . extractYoutubeVideoId($url)), $data); //decode the data
$videoData = json_decode($data['player_response'], true);
$streamingData = $videoData['streamingData'];
$streamingDataFormats = $streamingData['formats'];
return $streamingDataFormats[1]['url'];
}