mirror of
https://github.com/YGGverse/gemini-dl.git
synced 2026-03-31 09:05:31 +00:00
implement basic redirection support (status codes 30, 31) change default --match rules
This commit is contained in:
parent
090c930c7b
commit
3395d4fa17
3 changed files with 141 additions and 37 deletions
|
|
@ -16,6 +16,7 @@ use \Yggverse\Net\Address;
|
||||||
class Cli
|
class Cli
|
||||||
{
|
{
|
||||||
// Init totals
|
// Init totals
|
||||||
|
public int $redirects = 0;
|
||||||
public int $save = 0;
|
public int $save = 0;
|
||||||
public int $size = 0;
|
public int $size = 0;
|
||||||
public float $time = 0;
|
public float $time = 0;
|
||||||
|
|
@ -122,41 +123,139 @@ class Cli
|
||||||
// Calculate response time
|
// Calculate response time
|
||||||
$this->time += $time = microtime(true) - $time; // @TODO to API
|
$this->time += $time = microtime(true) - $time; // @TODO to API
|
||||||
|
|
||||||
// Check response code success
|
// Route
|
||||||
if (20 === $response->getCode())
|
switch ($response->getCode())
|
||||||
{
|
{
|
||||||
print(
|
case 20: // success
|
||||||
Message::magenta(
|
|
||||||
sprintf(
|
|
||||||
_("\tcode: %d"),
|
|
||||||
$response->getCode()
|
|
||||||
)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
else
|
// Reset redirection counter
|
||||||
{
|
$this->redirects = 0;
|
||||||
print(
|
|
||||||
Message::red(
|
print(
|
||||||
sprintf(
|
Message::magenta(
|
||||||
_("\tcode: %d"),
|
sprintf(
|
||||||
intval(
|
_("\tcode: %d (success)"),
|
||||||
$response->getCode()
|
$response->getCode()
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
// Crawl next address...
|
|
||||||
if ($this->option->crawl)
|
|
||||||
{
|
|
||||||
$this->start(
|
|
||||||
$offset + 1
|
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
return; // stop next operations in queue
|
break;
|
||||||
|
|
||||||
|
case 30: // redirection
|
||||||
|
case 31:
|
||||||
|
|
||||||
|
// Increase redirection counter
|
||||||
|
$this->redirects++;
|
||||||
|
|
||||||
|
// Print event debug
|
||||||
|
print(
|
||||||
|
Message::yellow(
|
||||||
|
sprintf(
|
||||||
|
_("\tcode: %d (redirection #%d)"),
|
||||||
|
$response->getCode(),
|
||||||
|
$this->redirects
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
print(
|
||||||
|
Message::yellow(
|
||||||
|
sprintf(
|
||||||
|
_("\tmeta: %s"),
|
||||||
|
$response->getMeta()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
print(
|
||||||
|
Message::magenta(
|
||||||
|
sprintf(
|
||||||
|
_("\ttime: %f -d %f"),
|
||||||
|
$time,
|
||||||
|
$this->option->delay + $time
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Crawl next address...
|
||||||
|
if ($this->option->crawl)
|
||||||
|
{
|
||||||
|
if ($this->redirects <= 5) // @TODO optional (currently protocol spec)
|
||||||
|
{
|
||||||
|
// Validate redirection target location
|
||||||
|
if (filter_var($response->getMeta(), FILTER_VALIDATE_URL)) // @TODO resolve relative locations
|
||||||
|
{
|
||||||
|
// Apply redirection target to the current destination
|
||||||
|
$this->source[$offset] = $response->getMeta();
|
||||||
|
|
||||||
|
// Rescan current destination using updated location
|
||||||
|
$this->start(
|
||||||
|
$offset
|
||||||
|
);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
print(
|
||||||
|
Message::red(
|
||||||
|
sprintf(
|
||||||
|
_("\tskip invalid redirection URL: `%s`"),
|
||||||
|
$response->getMeta()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Continue next location
|
||||||
|
$this->start(
|
||||||
|
$offset + 1
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
print(
|
||||||
|
Message::red(
|
||||||
|
sprintf(
|
||||||
|
_("\tredirection count reached, continue next address in queue"),
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Continue next location
|
||||||
|
$this->start(
|
||||||
|
$offset + 1
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return; // panic @TODO
|
||||||
|
|
||||||
|
break;
|
||||||
|
default: // failure
|
||||||
|
|
||||||
|
print(
|
||||||
|
Message::red(
|
||||||
|
sprintf(
|
||||||
|
_("\tcode: %d (unsupported)"),
|
||||||
|
intval(
|
||||||
|
$response->getCode()
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
// Reset redirection counter
|
||||||
|
$this->redirects = 0;
|
||||||
|
|
||||||
|
// Crawl next address...
|
||||||
|
if ($this->option->crawl)
|
||||||
|
{
|
||||||
|
$this->start(
|
||||||
|
$offset + 1
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
return; // panic @TODO
|
||||||
}
|
}
|
||||||
|
|
||||||
// Calculate document size
|
// Calculate document size
|
||||||
|
|
@ -218,8 +317,8 @@ class Cli
|
||||||
$source
|
$source
|
||||||
);
|
);
|
||||||
|
|
||||||
// Check link match common source rules, @TODO --external links
|
// Check link match common source rules
|
||||||
if (!$this->_source($address->get()) || $address->getHost() != $source->getHost())
|
if (!$this->_source($address->get()))
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,18 @@ class Message
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static function yellow(
|
||||||
|
string $message,
|
||||||
|
bool $bold = false
|
||||||
|
): string
|
||||||
|
{
|
||||||
|
return self::plain(
|
||||||
|
$message,
|
||||||
|
$bold ? Color::LIGHT_YELLOW
|
||||||
|
: Color::YELLOW
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public static function green(
|
public static function green(
|
||||||
string $message
|
string $message
|
||||||
): string
|
): string
|
||||||
|
|
|
||||||
|
|
@ -110,13 +110,6 @@ class Option
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!preg_match($this->match, $this->source))
|
|
||||||
{
|
|
||||||
throw new \Exception(
|
|
||||||
_('--source does not --match condition!')
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate target
|
// Validate target
|
||||||
if (empty($this->target))
|
if (empty($this->target))
|
||||||
{
|
{
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue