Created
January 3, 2016 09:20
-
-
Save math3vz/f9aa6355075e9659d9ce to your computer and use it in GitHub Desktop.
Script in PHP to download an entire UOL TV Search.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* To run just access it on your server, or run in command line (recommended). | |
**/ | |
error_reporting(E_ERROR); | |
ini_set('memory_limit', '-1'); | |
$folder = '/path/to/destination/folder/'; | |
$videosQty = 10; | |
// url of naruto episodes, its only a example. | |
$searchUrl = 'http://mais.b.uol.com.br/sys/search?num=' . $videosQty . '&start=1&lm=1&order=1&types=&edFilter=&safeSearch=on&viewHotContent=0%0D%0A&userNickname=Super+Animes+1000&codProfile=3oi6bemc1vvg&q=Naruto%20Shippuuden'; | |
$doc = new DOMDocument(); | |
$doc->loadHTMLFile($searchUrl); | |
$finder = new DomXPath($doc); | |
$nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' video ')]"); | |
foreach ($nodes as $node) { | |
if ($node->hasAttribute('href')) { | |
$href = $node->getAttribute('href'); | |
if (strpos($href, 'view')) { | |
$name = trim($node->nodeValue); | |
$path = $folder . '/' . $name . '.mp4'; | |
if ( ! is_dir($folder)) { | |
mkdir($folder, 0777, true); | |
} | |
if ( ! file_exists($name)) { | |
$output = fopen($path,"wb"); | |
preg_match_all("/\/([0-9]*)\?/", $href, $id); | |
$id = $id[1][0]; | |
$url = "http://video25.mais.uol.com.br/{$id}.mp4?ver=0&r=http://mais.uol.com.br"; | |
$curl = curl_init(); | |
curl_setopt($curl, CURLOPT_URL, $url); | |
curl_setopt($curl, CURLOPT_FILE, $output); | |
curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'); | |
curl_setopt($curl, CURLOPT_TIMEOUT, 10000000000); | |
curl_setopt($curl, CURLOPT_NOPROGRESS, false); | |
curl_setopt($curl, CURLOPT_BUFFERSIZE, 10000000); | |
curl_setopt($curl, CURLOPT_PROGRESSFUNCTION, function ($curl, $download_size, $downloaded_size, $upload_size, $uploaded_size) use ($name) | |
{ | |
static $previusPercent; | |
$percent = number_format($downloaded_size * 100 / $download_size); | |
if ($percent == 0) { | |
$previusPercent = 0; | |
} | |
if ($percent > $previusPercent) { | |
$previusPercent = $percent; | |
echo $name . ' | ' . $percent . '/100' . "\n"; | |
} | |
}); | |
$stdout = curl_exec($curl); | |
fwrite($output, $stdout); | |
fclose($output); | |
echo curl_error($curl); | |
curl_close($curl); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment