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

Commit

Permalink
Patch limit, add allowRawResponceDebug() method
Browse files Browse the repository at this point in the history
Bump version
  • Loading branch information
1271 committed Jul 20, 2018
1 parent b5c8103 commit 02e71c6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 33 deletions.
46 changes: 39 additions & 7 deletions src/Vaud/AlAudio.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ class AlAudio extends AlAudioBase
*/
public function __construct(int $uid, array $cookies, ?string $userAgent = null)
{
$this->uid = $uid;
$this->cookies = $cookies;
$this->uid = $uid;
$this->cookies = $cookies;
$this->userAgent = $userAgent ?? \sprintf('%s %s %s %s',
'Mozilla/5.0 (Windows NT 10.0; Win64; x64)',
'AppleWebKit/537.36 (KHTML, like Gecko)',
Expand All @@ -41,10 +41,16 @@ public function setLimitOffset(int $limit = 0, int $offset = 0): void
*/
public function main(): array
{
$this->fillPlaylist();
$this->parsePlaylist();
if (empty($this->playlist))
{
$this->fillPlaylist();
}
if (empty($this->decodedPlaylist))
{
$this->parsePlaylist();
}

if($this->limit > 0)
if ($this->limit > 0)
{
return array_slice($this->decodedPlaylist, 0, $this->limit);
}
Expand Down Expand Up @@ -85,6 +91,15 @@ public function setDebugCallback(callable $callback): void
$this->debugCallback = $callback;
}

/**
* Allow or disallow send raw curl response to debug callback
* @param $allow bool
*/
public function allowRawResponceDebug($allow = true): void
{
$this->allowRawResponceDebug = $allow;
}

/**
* @param int $offset
*/
Expand All @@ -111,13 +126,29 @@ protected function fillPlaylist(int $offset = 0): void

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

if(empty($response->hasMore))
if (empty($response->hasMore))
{
break;
}

$offset = $response->nextOffset;
$offset = $response->nextOffset ?? 0;

$currentLength = $this->offset + $this->limit;
print_r($currentLength);
if ($currentLength > 0 && $currentLength <= $response->nextOffset)
{
break;
}
}
}

public function getRawPlaylist()
{
if (empty($this->playlist))
{
$this->fillPlaylist();
}
return $this->playlist;
}

/**
Expand All @@ -126,6 +157,7 @@ protected function fillPlaylist(int $offset = 0): void
protected function parsePlaylist(): void
{
$_ = [];
\is_callable($this->debugCallback) && \call_user_func($this->debugCallback, 'Parse raw playlist', ['playlist' => $this->playlist]);
foreach ($this->playlist as $item)
{
if (empty($item[2]))
Expand Down
44 changes: 37 additions & 7 deletions src/Vaud/AlAudioBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ abstract class AlAudioBase
protected $offset = 0;
protected $unParsedTracks = [];
protected $debugCallback;
protected $allowRawResponceDebug = false;

/**
* @param int $offset
Expand All @@ -35,7 +36,7 @@ protected function loadData($offset = 0): array
'offset' => $offset,
'owner_id' => $this->uid,
'playlist_id' => $this->playlistId,
'type' => 'playlist'
'type' => 'playlist',
];
}

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

curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$before_curl_time = time();
if ($this->allowRawResponceDebug)
{
\is_callable($this->debugCallback) && \call_user_func($this->debugCallback, [
'before_curl' => $before_curl_time,
]);
}

$result = \curl_exec($ch);
$error = \curl_error($ch);
$errno = \curl_errno($ch);
\curl_close($ch);

if ($this->allowRawResponceDebug)
{
\is_callable($this->debugCallback) && \call_user_func($this->debugCallback, [
'raw_responce' => $result,
'error' => $error,
'errorno' => $errno,
'curl_delta' => time() - $before_curl_time,
]);
}

return $result;
}

Expand All @@ -134,6 +158,10 @@ protected function parseResponse($response, $default = [])
try
{
\preg_match('~<!json>(.+?)<!>~', $response, $matches);
if (!isset($matches[1]))
{
return $default;
}
$result = \json_decode($matches[1]);
if (\json_last_error())
{
Expand All @@ -144,9 +172,9 @@ protected function parseResponse($response, $default = [])
{
if (\is_callable($this->debugCallback))
{
\call_user_func($this->debugCallback,\json_last_error_msg());
\call_user_func($this->debugCallback,'Matches: ' . \count($matches));
\call_user_func($this->debugCallback,$response);
\call_user_func($this->debugCallback, \json_last_error_msg());
\call_user_func($this->debugCallback, 'Matches: ' . \count($matches));
\call_user_func($this->debugCallback, $response);
}

$result = $default;
Expand Down Expand Up @@ -204,7 +232,7 @@ protected function parseMoreAudio(array $items): void
*/
private function fillUnparsedHiddenTracks(array $items, array $response): void
{
if(\count($response) < \count($items))
if (\count($response) < \count($items))
{
$map = [];
foreach ($response as $item)
Expand All @@ -214,7 +242,7 @@ private function fillUnparsedHiddenTracks(array $items, array $response): void

foreach ($items as $item)
{
if(!in_array($item[0], $map))
if (!in_array($item[0], $map))
{
$this->unParsedTracks[] = $item;
}
Expand Down Expand Up @@ -250,6 +278,7 @@ private function tracksIds(array $items): array
{
$_[] = sprintf('%d_%d', $item[1], $item[0]);
}

return $_;
}

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

if (!\count($data) && $count)
{
\is_callable($this->debugCallback) && \call_user_func($this->debugCallback,'Time ban. Sleep...');
\is_callable($this->debugCallback) && \call_user_func($this->debugCallback, 'Time ban. Sleep...');

sleep($this->sleepTime);

return $this->tryLoadElements($_);
}

Expand Down
12 changes: 0 additions & 12 deletions src/Vaud/Vaud.php

This file was deleted.

7 changes: 0 additions & 7 deletions tests/Test/AlAudioTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use YuruYuri\Vaud\AlAudio;
use YuruYuri\Vaud\Decoder;
use YuruYuri\Vaud\Vaud;


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

public function testInstanceVoid()
{
$vaud = new Vaud(1);
$this->assertInstanceOf(Decoder::class, $vaud);
}

public function testCallback()
{
$data = [];
Expand Down

1 comment on commit 02e71c6

@1271
Copy link
Member Author

@1271 1271 commented on 02e71c6 Jul 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#3

Please sign in to comment.