Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/php -q
- <?php
- /*
- * PHP CLI script to download tv shows from from "Project - Free TV"
- * http://www.free-tv-video-online.me
- *
- * Notes
- *
- * this is a quick and dirty script to download TV shows from the command line
- * it is not polished and only supports a few video hosts. Future versions may be better
- * structured, supporting video site drivers as loadable objects, but this meets my needs for
- * now.
- *
- * It is easily used by other shell scripts to do things like scheduled batch downloads
- * of TV series with less risk or bandwidth requirement than bittorrent.
- *
- * Downloaded video files are usually in .flv or .mp4 format and can be played with VLC,
- * mplayer, etc.
- *
- * Keep it neat: tabs are 4 spaces
- *
- * Installation
- *
- * This script is written for common Debian-derived linux systems, but may me made to work
- * on any OS with a little tinkering. To install one will first need PHP CLI:
- *
- * sudo apt-get install php5-cli php5-curl youtube-dl
- *
- * Then copy this script somewhere in global PATH and make executable:
- *
- * sudo cp ./pftv /usr/bin/pftv
- * sudo chmod +x /usr/bin/pftv
- *
- * Extending
- *
- * It should be a simple matter to add other video hosting services such as youtube to the
- * set which this script can use, expecially if one can assume other scripts such as
- * youtube-dl.
- *
- * It should also be a fairly simple matter to extend this to download movies linked from the
- * Project - Free TV site. Have not done so since the quality of move links is so unreliable.
- *
- * @version: 0.0 alpha
- * @author: Anonymous
- * @date: 2013-02-01
- */
- /*
- * Globals (ugly ugly globals)
- */
- $a_show = ''; // Show name, eg 'the_wire', 'futurama' [string]
- $a_season = ''; // Show season, integer as string [string]
- $a_episode = ''; // Episode number in this season , eg "e1", "e12" [string[
- $a_link = ''; // Link number, eg "l0", "l3" [string]
- /*
- * Interpret command line args
- */
- if (false == array_key_exists(1, $argv)) { printHelp(); die(); }
- if (true == array_key_exists(1, $argv)) {
- if (('--list' == $argv[1]) || ('-l' == $argv[1])) { listShows(); die(); }
- if (('--help' == $argv[1]) || ('-h' == $argv[1])) { printHelp(); die(); }
- if (('--version' == $argv[1]) || ('-v' == $argv[1])) {
- echo "Project - Free TV CLI\n";
- echo "Version: 0.0 alpha\n";
- die();
- }
- }
- if (true == array_key_exists(1, $argv)) { $a_show = $argv[1]; }
- if (true == array_key_exists(2, $argv)) { $a_season = str_replace('s', '', $argv[2]); }
- if (true == array_key_exists(3, $argv)) { $a_episode = $argv[3]; }
- if (true == array_key_exists(4, $argv)) { $a_link = $argv[4]; }
- if (5 == count($argv)) { downloadEpisode($a_show, $a_season, $a_episode, $a_link); die(); }
- if (4 == count($argv)) { printEpisode($a_show, $a_season, $a_episode); die(); }
- if (3 == count($argv)) { printSeason($a_show, $a_season); die(); }
- printHelp();
- die();
- /**
- * Built in documentation
- */
- function printHelp() {
- echo ''
- . "Project - Free TV CLI Script\n"
- . "Usage: pftv [show] [season] [episode] [link]\n"
- . "Usage: pftv --list\n\n"
- . "Examples:\n\n"
- . " pftv --list\n"
- . " List shows available on the site.\n\n"
- . " pftv breaking_bad s2\n"
- . " List episodes and links for season two of 'Breaking Bad'\n\n"
- . " pftv futurama s4 e3\n"
- . " List links for Futurama s04e03 'Anthology of Interest II'\n\n"
- . " pftv futurama s4 e3 l0\n"
- . " Download Futurama s04e03 'Anthology of Interest II' from default link.\n\n"
- . " pftv --version\n"
- . " Print version of this script.\n\n"
- . " pftv --help\n"
- . " If you're reading this you've figured it out.\n\n"
- . "\n";
- }
- /**
- * Print list of shows currently available on http://www.free-tv-video-online.me/
- */
- function listShows() {
- $listingUrl = 'http://www.free-tv-video-online.me/internet/';
- $raw = implode(file($listingUrl));
- $lines = explode("\n", $raw);
- foreach($lines as $line) {
- if (false != strpos($line, 'class="mnlcategorylist"')) {
- $title = strip_tags($line);
- $showId = str_delim($line, 'href="', '/"');
- //echo "$showId ==> $title\n";
- echo "show: $showId\n";
- }
- }
- }
- /**
- * Print a season listing
- * @param $show Identifier of a tv show, eg 'the_wire', 'american_dad' [string]
- * @param $season Eg '1', '2', '3' [string]
- */
- function printSeason($show, $season) {
- echo "Finding episodes for $show season $season\n";
- $list = listSeason($show, $season);
- foreach($list as $eId => $episode) {
- echo $eId . ": " . $episode['title'] . "\n";
- foreach($episode['links'] as $lId => $link) {
- $link = str_replace('http://www.free-tv-video-online.me/player/', '../', $link);
- echo " " . $lId . ": " . $link . " (" . $list[$eId]['ratings'][$lId] . ")\n";
- }
- echo "\n";
- }
- }
- /**
- * Print list of links for an episode
- * @param $show Identifier of a tv show, eg 'the_wire', 'american_dad' [string]
- * @param $season Eg '1', '2', '3' [string]
- * @param $episode Eg '1', '2', '3' [string]
- */
- function printEpisode($show, $season, $episode) {
- echo "Finding links for $show season $season episode $episode \n";
- $list = listSeason($show, $season);
- $found = false;
- foreach($list as $eId => $epData) {
- if ($eId == $episode) {
- $found = true;
- echo $eId . ": " . $epData['title'] . "\n";
- foreach($epData['links'] as $lId => $link) {
- $link = str_replace('http://www.free-tv-video-online.me/player/', '../', $link);
- echo $lId . ": " . $link . " (" . $list[$eId]['ratings'][$lId] . ")\n";
- }
- }
- }
- if (false == $found) {
- echo "Unknown episode.\n";
- }
- }
- /**
- * Read a season listing
- *
- * This returns a nested array of episodes:
- *
- * 'e1' =>
- * [listing] => 'Some show season X'
- * [title] => 'Title of episode 1'
- * [links] => ['l1' => 'http://e1link1/', 'l2' => 'http://e1link2/', ...etc...]
- * [ratings] => ['l1' => '', 'l2' => '', ...etc...]
- * 'e2' =>
- * [listing] => 'Some show season X'
- * [title] => 'Title of episode 2'
- * [links] => ['l1' => 'http://e2link1/', 'l2' => 'http://e2link2/', ...etc...]
- * [ratings] => ['l1' => '', 'l2' => '', ...etc...]
- *
- * @param $show Show identifier as given by --list option
- * @param $season '1', '2', etc
- */
- function listSeason($show, $season) {
- $retVal = array();
- $seasonUrl = "http://www.free-tv-video-online.me/internet/{$show}/season_{$season}.html";
- $seasonPage = implode(file($seasonUrl));
- $lines = explode("\n", $seasonPage);
- $listing = '';
- $episodeTitle = '';
- $eidx = 0;
- $lidx = 0;
- foreach($lines as $line) {
- // get show / season title
- if (false !== strpos($line, 'mnlbreadcrumbs')) {
- $listing = trim(strip_tags($line));
- //echo "Season Title: $listing\n";
- }
- // get episode title
- if (false !== strpos($line, '<td class="episode">')) {
- $eidx++;
- $lidx = -1;
- $episodeTitle = trim(strip_tags($line));
- //echo "[e" . $eidx . "] Episode: $episodeTitle\n";
- $retVal['e' . $eidx] = array(
- 'listing' => $listing,
- 'title' => $episodeTitle,
- 'links' => array(),
- 'ratings' => array()
- );
- }
- // get episode links
- if (
- (false !== strpos($line, 'http://www.free-tv-video-online.me/player/vreer')) ||
- (false !== strpos($line, 'http://www.free-tv-video-online.me/player/nowvideo')) ||
- (false !== strpos($line, 'http://www.free-tv-video-online.me/player/novamov')) ||
- (false !== strpos($line, 'http://www.free-tv-video-online.me/player/divxstage')) ||
- (false !== strpos($line, 'http://www.free-tv-video-online.me/player/gorillavid'))
- /* add your own download drivers here :-) */
- ) {
- $lidx++;
- $playerUrl = str_delim($line, 'href="', '"');
- //echo "[e" . $eidx . "][l" . $lidx . "] $playerUrl\n";
- $retVal['e' . $eidx]['links']['l' . $lidx] = $playerUrl;
- }
- // get episode ratings
- if (false !== strpos($line, '% Said Work')) {
- $line = trim(strip_tags($line));
- $line = str_replace(' ', '', $line);
- $retVal['e' . $eidx]['ratings']['l' . $lidx] = $line;
- }
- }
- return $retVal;
- }
- /**
- * Download an episode if specified
- *
- * @param $show Show identifier as given by --list
- * @param $season '1', '2', etc
- * @param $episode 'e1', 'e2', etc
- * @param $link 'l1', 'l2', etc
- */
- function downloadEpisode($show, $season, $episode, $link) {
- echo "Downloading $show season $season episode $episode link $link\n";
- $list = listSeason($show, $season);
- $t = '';
- $found = false;
- foreach($list as $eId => $epData) {
- if ($eId == $episode) {
- $t = $epData['title'];
- echo "Episode: " . $epData['title'] . "\n";
- foreach($epData['links'] as $lId => $url) {
- if ($lId == $link) {
- $found = true;
- echo "Emedded on: $url\n";
- /* Download scripts are dispatched from here *****************************/
- if (false !== strpos($url, 'vreer.php')) { downloadVreer($url, $t); }
- if (false !== strpos($url, 'nowvideo.php')) { downloadEuroplayer($url, $t); }
- if (false !== strpos($url, 'novamov.php')) { downloadEuroplayer($url, $t); }
- if (false !== strpos($url, 'divxstage.php')) { downloadEuroplayer($url, $t); }
- if (false !== strpos($url, 'gorillavid.php')) { downloadGorilla($url, $t); }
- // add your own download drivers here :-)
- }
- }
- }
- }
- if (false == $found) { echo "Unknown episode or link.\n"; }
- }
- /**
- * Helper function to extract a substring
- * @param $line Just a string
- * @param $begins Opening delimeter
- * @param $ends Closing delimeter
- * @return Substring
- */
- function str_delim($line, $begins, $ends) {
- $start = strpos($line, $begins);
- if (false == $start) { return ''; }
- $start = $start + strlen($begins);
- $end = strpos($line, $ends, $start + 1);
- if (false == $end) { return ''; }
- return substr($line, $start, $end - $start);
- }
- /**
- * Helper function to clean titles of non-alphanumeric chars
- * @param $title String to make a filename from
- * @return Filename component
- */
- function makeFileName($title) {
- return preg_replace("/[^A-Za-z0-9 ]/", '', $title);
- }
- /**
- * Helper function to download a file with cURL
- * @param $url To be retrieved via GET request
- */
- function curlGet($url) {
- return implode(file($url));
- //TODO: debugme
- $ch = curl_init($url);
- curl_setopt($ch, 'CURLOPT_RETURNTRANSFER', true);
- $result = curl_exec($ch);
- echo "Downloaded " . strlen($result) . " bytes\n";
- return $result;
- }
- /**
- * helper function to download from vreer.com
- * @param $link Embedded iFrame URL
- * @param $title Name of this episode
- */
- function downloadVreer($link, $title) {
- $containerPage = curlGet($link);
- $lines = explode("\n", $containerPage);
- $vreerUrl = '';
- foreach($lines as $line) {
- if (false !== strpos($line, 'IFRAME SRC="http://vreer.com/')) {
- $vreerUrl = str_delim($line, 'SRC="', '"');
- echo "Extracting iframe: " . $vreerUrl . "\n";
- }
- }
- if ('' == $vreerUrl) {
- echo "[error] Vreer.com iframe not found in $link \n";
- return '';
- }
- $containerPage = curlGet($vreerUrl);
- $lines = explode("\n", $containerPage);
- foreach($lines as $line) {
- if (false !== strpos($line, 'file: "')) {
- $dnLink = str_delim($line, 'file: "', '"');
- echo "Download link: " . $dnLink . "\n";
- echo "Downloading, please wait... \n";
- $shellCmd = 'wget'
- . ' --output-document="' . makeFileName($title) . '.flv"'
- . ' "' . $dnLink . '"';
- shell_exec($shellCmd);
- echo "Done.\n";
- }
- }
- }
- /**
- * helper function to download from nowvideo.eu, divxstage.eu, etc
- *
- * Example of auth script called by embedded flash player
- *
- * http://www.nowvideo.eu/api/player.api.php?
- * file=i7joob3q1aqo9&
- * pass=undefined&
- * cid2=undefined&
- * key=196%2E215%2E173%2E187%2Df4e8269b0947e8f55c272631b52f881d&
- * cid3=undefined&
- * user=undefined&
- * cid=undefined
- *
- * @param $link Embedded iFrame URL
- * @param $title Name of this episode
- */
- function downloadEuroplayer($link, $title) {
- $containerPage = implode(file($link));
- $lines = explode("\n", $containerPage);
- $ifUrl = '';
- $domain = '';
- foreach($lines as $line) {
- if (false !== strpos($line, 'http://embed.nowvideo.eu/')) {
- $ifUrl = str_delim($line, 'src="', '"');
- $domain = 'www.nowvideo.eu';
- echo "Extracting iframe: " . $ifUrl . "\n";
- }
- if (false !== strpos($line, 'http://embed.divxstage.eu/')) {
- $ifUrl = str_delim($line, 'src="', '"');
- $domain = 'www.divxstage.eu';
- echo "Extracting iframe: " . $ifUrl . "\n";
- }
- if (false !== strpos($line, 'http://www.novamov.com/embed.php')) {
- $ifUrl = str_delim($line, "src='", "'");
- $domain = 'www.novamov.com';
- echo "Extracting iframe: " . $ifUrl . "\n";
- }
- }
- if ('' == $ifUrl) {
- echo "[error] Vreer.com iframe not found in $link \n";
- return '';
- }
- $containerPage = implode(file($ifUrl));
- $lines = explode("\n", $containerPage);
- $fileId = '';
- $fileKey = '';
- foreach($lines as $line) {
- if (false !== strpos($line, 'flashvars')) {
- echo $line . "\n";
- }
- if (false !== strpos($line, 'flashvars.file=')) {
- $fileId = str_delim($line, 'file="', '"');
- echo "Found fileId: $fileId\n";
- }
- if (false !== strpos($line, 'flashvars.filekey=')) {
- $fileKey = str_delim($line, 'filekey="', '"');
- echo "Found fileKey: $fileKey\n";
- }
- }
- if ('' == $fileId) { echo "No fileId\n"; return; }
- if ('' == $fileKey) { echo "No fileKey\n"; return; }
- $fileKey = urlencode($fileKey);
- $fileKey = str_replace('.', '%2E', $fileKey);
- $fileKey = str_replace('-', '%2D', $fileKey);
- $validateUrl = "http://" . $domain . "/api/player.api.php?file=" . $fileId
- . "&pass=undefined"
- . "&cid2=undefined"
- . "&key=" . $fileKey
- . "&cid3=undefined"
- . "&user=undefined"
- . "&cid=undefined";
- echo "Validation URL:" . $validateUrl . "\n";
- $authData = implode(file($validateUrl));
- $parts = explode('&', $authData);
- $dnLink = '';
- foreach($parts as $part) {
- $kv = explode('=', $part, 2);
- echo "AuthData: " . $part . "\n(" . $kv[0] . " => " . $kv[1] . ")\n";
- if ('url' == $kv[0]) { $dnLink = $kv[1]; }
- }
- echo "Download link: " . $dnLink . "\n";
- echo "Downloading, please wait... \n";
- $shellCmd = 'wget'
- . ' --output-document="' . makeFileName($title) . '.flv"'
- . ' "' . $dnLink . '"';
- echo $shellCmd . "\n";
- shell_exec($shellCmd);
- echo "Done.\n";
- }
- /**
- * helper function to download from Gorillavid.in
- * @param $link Embedded iFrame URL
- * @param $title Name of this episode
- */
- function downloadGorilla($link, $title) {
- $containerPage = curlGet($link);
- $lines = explode("\n", $containerPage);
- $ifUrl = '';
- foreach($lines as $line) {
- if (false !== strpos($line, 'IFRAME SRC="http://gorillavid.in/')) {
- $ifUrl = str_delim($line, 'SRC="', '"');
- echo "Extracting iframe: " . $ifUrl . "\n";
- }
- }
- if ('' == $ifUrl) {
- echo "[error] gorillavid.in iframe not found in $link \n";
- return '';
- }
- $containerPage = curlGet($ifUrl);
- $lines = explode("\n", $containerPage);
- foreach($lines as $line) {
- if (false !== strpos($line, 'file:"')) {
- $dnLink = str_delim($line, 'file:"', '"');
- echo "Download link: " . $dnLink . "\n";
- echo "Downloading, please wait... \n";
- $shellCmd = 'wget'
- . ' --output-document="' . makeFileName($title) . '.flv"'
- . ' "' . $dnLink . '"';
- echo $shellCmd . "\n";
- shell_exec($shellCmd);
- echo "Done.\n";
- }
- }
- }
- /**
- * helper function to download from smotri.com (INCOMPLETE)
- *
- * TODO: finish this
- *
- * @param $link Embedded iFrame URL
- * @param $title Name of this episode
- */
- function downloadSmotri($link, $title) {
- $containerPage = curlGet($link);
- $lines = explode("\n", $containerPage);
- $flashUrl = '';
- foreach($lines as $line) {
- if (false !== strpos($line, '<a href="http://smotri.com/video/view/')) {
- $flashUrl = str_delim($line, 'href="', '"');
- echo "Extracting iframe: " . $flashUrl . "\n";
- }
- }
- if ('' == $flashUrl) {
- echo "[error] smotri flash not found in $link \n";
- return '';
- }
- // POST TO: http://smotri.com/video/view/url/bot/
- //
- // context=
- // &p%5Fid%5B2%5D=2
- // &getvideoinfo=1
- // &p%5Fid%5B1%5D=1
- // &p%5Fid%5B0%5D=7
- // &devid=LoadupFlashPlayer
- // &u%5Funiq=adv%5Fuid1131590999219%2E5173 <--- player ID?
- // &ticket=u16451225cfc <--- file ID
- // &frame%5Furl=1
- //
- // THEN
- //
- // ticket=u16451083805
- // &p%5Fid%5B2%5D=4
- // &p%5Fid%5B6%5D=7
- // &p%5Fid%5B1%5D=3
- // &p%5Fid%5B5%5D=6
- // &p%5Fid%5B7%5D=9
- // &begun=1
- // &p%5Fid%5B41%5D=5
- // &context=
- // &p%5Fid%5B4%5D=5
- // &p%5Fid%5B71%5D=9
- // &video%5Furl=1
- // &p%5Fid%5B42%5D=5
- // &p%5Fid%5B74%5D=9
- // &p%5Fid%5B44%5D=5
- // &p%5Fid%5B75%5D=9
- // &p%5Fid%5B43%5D=5
- // &p%5Fid%5B73%5D=9
- // &p%5Fid%5B72%5D=9
- // &p%5Fid%5B45%5D=5
- // &devid=LoadupFlashPlayer
- // &u%5Funiq=adv%5Fuid1131590999219%2E5173
- //
- // Referrer: http://pics.smotri.com/scrubber_custom8.swf?
- // file=u16451083805
- // &bufferTime=3
- // &autoStart=false
- // &str_lang=rus
- // &xmlsource=http%3A%2F%2Fpics%2Esmotri%2Ecom%2Fcskins%2Fblue%2Fskin%5Fcolor%2Exml
- // &xmldatasource=http%3A%2F%2Fpics%2Esmotri%2Ecom%2Fcskins%2Fblue%2Fskin%5Fng%2Exml/[[DYNAMIC]]/1
- $containerPage = curlGet($flashUrl);
- $lines = explode("\n", $containerPage);
- foreach($lines as $line) {
- if (false !== strpos($line, 'file:"')) {
- $dnLink = str_delim($line, 'file:"', '"');
- echo "Download link: " . $dnLink . "\n";
- echo "Downloading, please wait... \n";
- $shellCmd = 'wget'
- . ' --output-document="' . makeFileName($title) . '.flv"'
- . ' "' . $dnLink . '"';
- echo $shellCmd . "\n";
- shell_exec($shellCmd);
- echo "Done.\n";
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement