-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpimple.php
178 lines (152 loc) · 4.41 KB
/
pimple.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
class Curl
{
/**
* @var string
*/
protected $url;
/**
* Curl constructor.
* @param string $url
*/
public function __construct($url)
{
$this->url = $url;
}
/**
* @return bool|string
* @throws Exception
*/
public function exec()
{
if ($this->url) {
$curl = curl_init($this->url);
curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36");
curl_setopt($curl, CURLOPT_RETURNTRANSFER,1);
$html = curl_exec($curl);
curl_close ($curl);
return $html;
} else {
throw new Exception("No URL for Curl");
}
}
}
trait Parser
{
/**
* @var string
*/
protected $html;
/**
* Parser constructor.
* @param string $html
*/
public function __construct($html)
{
$this->html = $html;
}
}
class MainPageParser
{
use Parser;
/**
* @return array
*/
public function getLinksForBroadcastPages()
{
$partWithTodayMatches = $this->findPartWithTodayMatches();
if ($partWithTodayMatches) {
return $this->findLinksForBroadcastPages($partWithTodayMatches);
}
return [];
}
/**
* @return string
*/
protected function findPartWithTodayMatches()
{
try {
$today = new DateTime('now', new DateTimeZone('Europe/Moscow'));
$todayDayNumber = (int)$today->format('d');
$tomorrowDayNumber = $todayDayNumber + 1;
} catch (Exception $e) {
echo $e->getMessage();
exit();
}
$pattern = '#<div class="streams-day">' . $todayDayNumber . '(.*)#';
preg_match($pattern, $this->html, $matches, PREG_OFFSET_CAPTURE);
if (isset($matches[1])) {
$startPosition = $matches[1][1];
$pattern = '#<div class="streams-day">' . $tomorrowDayNumber . '(.*)#';
preg_match($pattern, $this->html, $matches, PREG_OFFSET_CAPTURE);
$endPosition = isset($matches[1]) ? $matches[1][1] : null;
return mb_strcut($this->html, $startPosition, $endPosition);
} else {
return '';
}
}
/**
* @param string $partWithTodayMatches
* @return array|mixed
*/
protected function findLinksForBroadcastPages($partWithTodayMatches)
{
$pattern = '#<a href="/broadcast/football/(.*)" rel="bookmark">(.*)</a>#';
$numberOfMatches = preg_match_all($pattern, $this->html, $matches);
if ($numberOfMatches > 0) {
return array_map(function ($link, $name) {
return [
'name' => $name,
'link' => $link,
];
}, $matches[1], $matches[2]);
}
return [];
}
}
class MatchPageParser
{
use Parser;
/**
* @return string
*/
public function getLinkForAceStreamBroadcast()
{
$pattern = '#href="(acestream://.*)">#';
preg_match($pattern, $this->html, $matches);
if (isset($matches[1])) {
return $matches[1];
}
return null;
}
}
class App
{
const PIMPLE_BASE_URL = 'https://www.pimpletv.ru/category/broadcast/football/';
const PIMPLE_MATCH_URL = 'https://www.pimpletv.ru/broadcast/football/';
public function init()
{
ini_set('max_execution_time', 0); // 0 hrs
ini_set('memory_limit', '500M'); // 500 Mb
$curl = new Curl(self::PIMPLE_BASE_URL);
$html = $curl->exec();
$matchPageParser = new MainPageParser($html);
$linksForBroadcastPages = $matchPageParser->getLinksForBroadcastPages();
$linksForBroadcasts = [];
foreach ($linksForBroadcastPages as $item) {
$curl = new Curl(self::PIMPLE_MATCH_URL . $item['link']);
$html = $curl->exec();
$matchPageParser = new MatchPageParser($html);
$linksForBroadcasts[] = [
'name' => $item['name'],
'acestream' => $matchPageParser->getLinkForAceStreamBroadcast(),
];
}
return $linksForBroadcasts;
}
}
header("Access-Control-Allow-Origin: *");
header('Content-Type: application/json; charset=utf-8');
$app = new App();
$data = $app->init();
echo json_encode($data, JSON_UNESCAPED_UNICODE);