forked from LouiseDenmar/iptv-sniper
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.php
More file actions
116 lines (93 loc) · 5.31 KB
/
index.php
File metadata and controls
116 lines (93 loc) · 5.31 KB
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
<?php
if (!isset($_GET["debug"])) {
header("Content-Type: audio/x-mpegurl");
header("Content-Disposition: attachment; filename=iptv-org.m3u");
}
//configuration parameters
$countries = isset($_GET["country"]) ? str_getcsv($_GET['country']) : ["us","uk","ca","au","nz"];
$quality = isset($_GET["quality"]) ? str_getcsv($_GET["quality"]) : ["0","240","480","720","1080","2160","4320"];
$nsfw = isset($_GET["nsfw"]) ? $_GET["nsfw"] : 0;
$debug = isset($_GET["debug"]) ? $_GET["debug"] : 0;
$import = isset($_GET["import"]) ? $_GET["import"] : null;
//get a list of all available online streams
$streams_api = file_get_contents('https://iptv-org.github.io/api/streams.json');
$channels = json_decode($streams_api);
$online_channels = array();
//for each available online stream, if the stream is in the list of specified countries, store it in a list
foreach ($channels as $channel) {
if ($channel->status == "online" && in_array(substr($channel->channel, -2), $countries) && property_exists($channel, "height") && in_array($channel->height, $quality))
$online_channels[$channel->channel] = $channel;
}
//get a list of all channel information
$channels_api = file_get_contents('https://iptv-org.github.io/api/channels.json');
$channels = json_decode($channels_api);
//match the channel information to the stored list of available online streams and merge it
foreach ($channels as $channel) {
if ($channel->is_nsfw == $nsfw && array_key_exists($channel->id, $online_channels)) {
$online_channels[$channel->id] = (object) array_merge((array) $channel, (array) $online_channels[$channel->id]);
$online_channels[$channel->id]->stream_url = $online_channels[$channel->id]->url;
unset($online_channels[$channel->id]->url);
}
}
//get a list of all tv guides
$guides_api = file_get_contents('https://iptv-org.github.io/api/guides.json');
$guides = json_decode($guides_api);
//match the tv guide to the stored list of available online streams and merge it
foreach ($guides as $guide) {
if (array_key_exists($guide->channel, $online_channels)) {
$online_channels[$guide->channel] = (object) array_merge((array) $online_channels[$guide->channel], (array) $guide);
$online_channels[$guide->channel]->guide_url = $guide->url;
unset($online_channels[$guide->channel]->url);
}
}
$tvg_urls = array();
//get all unique tv guides from the stored list of available online streams and store it in another list
foreach ($online_channels as $channel) {
if(property_exists($channel, "guide_url") && !in_array($channel->guide_url, $tvg_urls))
$tvg_urls[] = $channel->guide_url;
}
if ($import !== null) {
//get external m3u file
$m3u = file_get_contents($import);
//general regex patterns for a specific channel
$channel_pattern = '/#EXTINF:(.+?)[,]\s?(.+?)[\r\n]+?((?:https?|rtmp):\/\/(?:\S*?\.\S*?)(?:[\s)\[\]{};"\'<]|\.\s|$))/';
$channel_attributes = '/([a-zA-Z0-9\-\_]+?)="([^"]*)"/';
//replace some strings on the external m3u file for easier processing
$m3u = str_replace('tvg-id', 'id', $m3u);
$m3u = str_replace('tvg-name', 'name', $m3u);
$m3u = str_replace('tvg-logo', 'logo', $m3u);
$m3u = str_replace('group-title', 'group', $m3u);
//begin matching the regex on the entire external m3u file
preg_match_all($channel_pattern, $m3u, $channels);
$imported_channels = array();
//for each match, process them individually as a channel
foreach ($channels[0] as $channel) {
//rematch the channel pattern on each match to pluck its attributes
preg_match($channel_pattern, $channel, $match_list);
//get the stream url
$stream_url = preg_replace("/[\n\r]/","",$match_list[3]);
$stream_url = preg_replace('/\s+/', '', $stream_url);
//initialize final list of imported channel info with the stream url already included
$channel_info = array('stream_url' => $stream_url);
//pluck channel attributes
preg_match_all($channel_attributes, $channel, $channels, PREG_SET_ORDER);
//for each attribute match, add them to the final list of imported channel info
foreach ($channels as $match) {
if ($match[1] == "group")
$channel_info["categories"] = array($match[2]);
$channel_info[$match[1]] = $match[2];
unset($channel_info["group"]);
}
$imported_channels[$channels[0][2]] = (object) $channel_info;
}
//merge the imported list of channel info to the list of available online streams
foreach ($imported_channels as $channel)
$online_channels[$channel->id] = (array_key_exists($channel->id, $online_channels)) ? (object) array_merge((array) $online_channels[$channel->id], (array) $channel) : $channel;
}
if ($debug == true)
die("<pre>" . print_r($online_channels, true) . "</pre>");
?>#EXTM3U url-tvg="<?php echo implode(",", $tvg_urls); ?>"
<?php foreach ($online_channels as $channel): ?>
#EXTINF:-1 tvg-id="<?php echo $channel->id; ?>" tvg-name="<?php echo $channel->name; ?>" tvg-logo="<?php echo $channel->logo; ?>" group-title="<?php echo (property_exists($channel, "categories") && !empty($channel->categories)) ? ucfirst($channel->categories[0]) : "Uncategorized"; ?>",<?php echo $channel->name . "\n"; ?>
<?php echo $channel->stream_url . "\n"; ?>
<?php endforeach ?>