Categories: Development

A Wget output formatter and parser made with PHP

A Wget output parser that turns a Wget file download into a viewable JSON output.

This provides a different observation and makes documenting network speeds possible.

The doWget() function attempts to download a file with Wget whilst saving the output to a text file:

function doWget(string $file_url, string $save_as, string $output_name = 'wget.txt'): void
{//3 tries at 4 seconds per attempt. Dont follow 301 redirects
    $wget = shell_exec("wget --tries=3 --max-redirect=0 --connect-timeout=4 -o $output_name -O $save_as $file_url");
}

The options passed through to Wget ensure it doesn’t hang or follow 301 redirects.

To get a JSON output of the Wget download progress use formatWgetOutput(). This returns information such as start and finish time, seconds taken, file URL, file saved as, file size, average speed and steps.

function formatWgetOutput(string $file): array
{
    $result = array();
    $handle = fopen($file, 'rb');
    if ($handle) {
        $line_number = 0;
        while (($line = fgets($handle)) !== false) {
            if ($line_number === 0) {
                if (!str_starts_with($line, '--')) {
                    return array('success' => false, 'message' => "Not a Wget output file");
                }
                $line_arr = explode(' ', $line);
                $file_name = trim(str_replace('\n', '', $line_arr[3]));
                $start_dt = str_replace('--', '', $line_arr[0] . ' ' . $line_arr[1]);
            } elseif (str_contains($line, 'HTTP request sent, awaiting response...')) {
                $line_arr = explode(' ', $line);
                if ($line_arr[5] !== '200') {
                    return array('success' => false, 'message' => "HTTP status code: {$line_arr[5]}");
                }
            } elseif ($line_number <= 10 && !str_contains($line, '.....')) {
                if (str_contains($line, 'Length:')) {
                    $size_line = preg_match('#\((.*?)\)#', $line, $file_size);
                } elseif (str_contains($line, 'Saving to:')) {
                    $saving_as = trim(str_replace(["Saving to:", "'"], "", trim($line)));
                } elseif (str_contains($line, 'Connecting to ') && str_contains($line, 'connected.')) {
                    $ss_start = (strpos($line, '|') + strlen('|'));
                    $ip = substr($line, $ss_start, (strpos($line, '|', $ss_start) - $ss_start));
                }
            } elseif (str_contains($line, '.....')) {
                $line_output = preg_replace('!\s+!', ' ', $line);
                $line_arr = explode(' ', $line_output);
                if (isset($line_arr[7], $line_arr[8])) {
                    $speed = $line_arr[8];
                    if (str_contains($speed, 'M')) {
                        $speed_type = 'MB/s';
                    } else if (str_contains($speed, 'G')) {
                        $speed_type = 'GB/s';
                    } else if (str_contains($speed, 'K')) {
                        $speed_type = 'KB/s';
                    } else {
                        $speed_type = 'B/s';
                    }
                    $speed_value = (float)$speed;
                    $percent = (float)$line_arr[7];
                    $result['steps'][] = array('percent' => $percent, 'speed_value' => $speed_value, 'speed_type' => $speed_type);
                }
                if ($line_arr[1] === '0K') {
                    $start_dl_line = $line_number;
                } elseif (str_contains($line_output, "100%")) {
                    $total_hops = ($line_number - $start_dl_line);
                    $result['steps'][] = array('percent' => 100, 'speed_value' => null, 'speed_type' => null);
                }
            } elseif (str_contains($line, 'saved')) {
                $line_output = preg_replace('!\s+!', ' ', $line);
                $line_arr = explode(' ', $line_output);
                $finish_dt = $line_arr[0] . ' ' . $line_arr[1];
                $avg_speed_line = preg_match('#\((.*?)\)#', $line_output, $avg_speed);
            }
            $line_number++;
        }
        $result = array_merge($result, array(
            'success' => true, 'start_datetime' => $start_dt, 'finish_datetime' => $finish_dt, 'seconds' => (strtotime($finish_dt) - strtotime($start_dt)),
            'ip' => $ip, 'download_steps' => $total_hops, 'avg_speed' => $avg_speed[1], 'file_size' => $file_size[1], 'file_url' => $file_name, 'saved_as' => $saving_as));
        fclose($handle);
    } else {//Error opening the file.
        $result = array('success' => false, 'message' => "Error opening file: $file");
    }
    return $result;
}

 

Share
Tags: LinuxPHPWget

Recent Posts

Kennington reservoir drained drone images

A drained and empty Kennington reservoir images from a drone in early July 2024. The…

2 years ago

Merrimu Reservoir drone images

Merrimu Reservoir from drone. Click images to view larger.

2 years ago

Early September spring shots

Some early September spring shots from a bushwalk, It is still too early for wildflowers.

3 years ago

Cloning an NVMe to SK Hynix Platinum P41 NVMe

I recently needed to clone over a smaller NVMe to a 2TB SK Hynix Platinum…

3 years ago

Using Laravel’s deleteDirectory to delete folders and their contents

If you ever needed a simple solution in Laravel to delete an entire directory and…

3 years ago

How to allow only 1 user to register in a Laravel application

How to enforce only one user to register or be registered in a Laravel web…

3 years ago