Skip to content
This repository was archived by the owner on Apr 11, 2024. It is now read-only.

Commit 02e71c6

Browse files
committed
Patch limit, add allowRawResponceDebug() method
Bump version
1 parent b5c8103 commit 02e71c6

File tree

4 files changed

+76
-33
lines changed

4 files changed

+76
-33
lines changed

src/Vaud/AlAudio.php

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ class AlAudio extends AlAudioBase
1717
*/
1818
public function __construct(int $uid, array $cookies, ?string $userAgent = null)
1919
{
20-
$this->uid = $uid;
21-
$this->cookies = $cookies;
20+
$this->uid = $uid;
21+
$this->cookies = $cookies;
2222
$this->userAgent = $userAgent ?? \sprintf('%s %s %s %s',
2323
'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
2424
'AppleWebKit/537.36 (KHTML, like Gecko)',
@@ -41,10 +41,16 @@ public function setLimitOffset(int $limit = 0, int $offset = 0): void
4141
*/
4242
public function main(): array
4343
{
44-
$this->fillPlaylist();
45-
$this->parsePlaylist();
44+
if (empty($this->playlist))
45+
{
46+
$this->fillPlaylist();
47+
}
48+
if (empty($this->decodedPlaylist))
49+
{
50+
$this->parsePlaylist();
51+
}
4652

47-
if($this->limit > 0)
53+
if ($this->limit > 0)
4854
{
4955
return array_slice($this->decodedPlaylist, 0, $this->limit);
5056
}
@@ -85,6 +91,15 @@ public function setDebugCallback(callable $callback): void
8591
$this->debugCallback = $callback;
8692
}
8793

94+
/**
95+
* Allow or disallow send raw curl response to debug callback
96+
* @param $allow bool
97+
*/
98+
public function allowRawResponceDebug($allow = true): void
99+
{
100+
$this->allowRawResponceDebug = $allow;
101+
}
102+
88103
/**
89104
* @param int $offset
90105
*/
@@ -111,13 +126,29 @@ protected function fillPlaylist(int $offset = 0): void
111126

112127
$this->playlist = \array_merge($this->playlist, $response->list);
113128

114-
if(empty($response->hasMore))
129+
if (empty($response->hasMore))
115130
{
116131
break;
117132
}
118133

119-
$offset = $response->nextOffset;
134+
$offset = $response->nextOffset ?? 0;
135+
136+
$currentLength = $this->offset + $this->limit;
137+
print_r($currentLength);
138+
if ($currentLength > 0 && $currentLength <= $response->nextOffset)
139+
{
140+
break;
141+
}
142+
}
143+
}
144+
145+
public function getRawPlaylist()
146+
{
147+
if (empty($this->playlist))
148+
{
149+
$this->fillPlaylist();
120150
}
151+
return $this->playlist;
121152
}
122153

123154
/**
@@ -126,6 +157,7 @@ protected function fillPlaylist(int $offset = 0): void
126157
protected function parsePlaylist(): void
127158
{
128159
$_ = [];
160+
\is_callable($this->debugCallback) && \call_user_func($this->debugCallback, 'Parse raw playlist', ['playlist' => $this->playlist]);
129161
foreach ($this->playlist as $item)
130162
{
131163
if (empty($item[2]))

src/Vaud/AlAudioBase.php

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ abstract class AlAudioBase
1919
protected $offset = 0;
2020
protected $unParsedTracks = [];
2121
protected $debugCallback;
22+
protected $allowRawResponceDebug = false;
2223

2324
/**
2425
* @param int $offset
@@ -35,7 +36,7 @@ protected function loadData($offset = 0): array
3536
'offset' => $offset,
3637
'owner_id' => $this->uid,
3738
'playlist_id' => $this->playlistId,
38-
'type' => 'playlist'
39+
'type' => 'playlist',
3940
];
4041
}
4142

@@ -117,9 +118,32 @@ protected function post(string $url, array $data = []): string
117118
\curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
118119
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
119120

121+
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
122+
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
123+
124+
$before_curl_time = time();
125+
if ($this->allowRawResponceDebug)
126+
{
127+
\is_callable($this->debugCallback) && \call_user_func($this->debugCallback, [
128+
'before_curl' => $before_curl_time,
129+
]);
130+
}
131+
120132
$result = \curl_exec($ch);
133+
$error = \curl_error($ch);
134+
$errno = \curl_errno($ch);
121135
\curl_close($ch);
122136

137+
if ($this->allowRawResponceDebug)
138+
{
139+
\is_callable($this->debugCallback) && \call_user_func($this->debugCallback, [
140+
'raw_responce' => $result,
141+
'error' => $error,
142+
'errorno' => $errno,
143+
'curl_delta' => time() - $before_curl_time,
144+
]);
145+
}
146+
123147
return $result;
124148
}
125149

@@ -134,6 +158,10 @@ protected function parseResponse($response, $default = [])
134158
try
135159
{
136160
\preg_match('~<!json>(.+?)<!>~', $response, $matches);
161+
if (!isset($matches[1]))
162+
{
163+
return $default;
164+
}
137165
$result = \json_decode($matches[1]);
138166
if (\json_last_error())
139167
{
@@ -144,9 +172,9 @@ protected function parseResponse($response, $default = [])
144172
{
145173
if (\is_callable($this->debugCallback))
146174
{
147-
\call_user_func($this->debugCallback,\json_last_error_msg());
148-
\call_user_func($this->debugCallback,'Matches: ' . \count($matches));
149-
\call_user_func($this->debugCallback,$response);
175+
\call_user_func($this->debugCallback, \json_last_error_msg());
176+
\call_user_func($this->debugCallback, 'Matches: ' . \count($matches));
177+
\call_user_func($this->debugCallback, $response);
150178
}
151179

152180
$result = $default;
@@ -204,7 +232,7 @@ protected function parseMoreAudio(array $items): void
204232
*/
205233
private function fillUnparsedHiddenTracks(array $items, array $response): void
206234
{
207-
if(\count($response) < \count($items))
235+
if (\count($response) < \count($items))
208236
{
209237
$map = [];
210238
foreach ($response as $item)
@@ -214,7 +242,7 @@ private function fillUnparsedHiddenTracks(array $items, array $response): void
214242

215243
foreach ($items as $item)
216244
{
217-
if(!in_array($item[0], $map))
245+
if (!in_array($item[0], $map))
218246
{
219247
$this->unParsedTracks[] = $item;
220248
}
@@ -250,6 +278,7 @@ private function tracksIds(array $items): array
250278
{
251279
$_[] = sprintf('%d_%d', $item[1], $item[0]);
252280
}
281+
253282
return $_;
254283
}
255284

@@ -275,9 +304,10 @@ protected function tryLoadElements(array $_, int $count = 0)
275304

276305
if (!\count($data) && $count)
277306
{
278-
\is_callable($this->debugCallback) && \call_user_func($this->debugCallback,'Time ban. Sleep...');
307+
\is_callable($this->debugCallback) && \call_user_func($this->debugCallback, 'Time ban. Sleep...');
279308

280309
sleep($this->sleepTime);
310+
281311
return $this->tryLoadElements($_);
282312
}
283313

src/Vaud/Vaud.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/Test/AlAudioTest.php

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use YuruYuri\Vaud\AlAudio;
66
use YuruYuri\Vaud\Decoder;
7-
use YuruYuri\Vaud\Vaud;
87

98

109
class MockAlAudio extends AlAudio
@@ -80,12 +79,6 @@ public function testLimitOffset()
8079
$this->assertSame($items[0]['id'], $itemsWithoutOffset[$offset]['id']);
8180
}
8281

83-
public function testInstanceVoid()
84-
{
85-
$vaud = new Vaud(1);
86-
$this->assertInstanceOf(Decoder::class, $vaud);
87-
}
88-
8982
public function testCallback()
9083
{
9184
$data = [];

0 commit comments

Comments
 (0)