PageRenderTime 42ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/WeebTV.php

https://github.com/tejastank/Scripts
PHP | 550 lines | 118 code | 5 blank | 427 comment | 17 complexity | 871e05222ff3dc1225aaf5f937234890 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. class CLI
  3. {
  4. protected static $ACCEPTED = array(
  5. 0 => array(
  6. 'help' => 'displays this help',
  7. 'list' => 'display formatted channels list and exit',
  8. 'print' => 'only print the base rtmpdump command, don\'t start anything',
  9. 'quiet' => 'disables unnecessary output'
  10. ),
  11. 1 => array(
  12. 'proxy' => 'use proxy to retrieve channel information',
  13. 'url' => 'use specified url without displaying channels list'
  14. )
  15. );
  16. var $params = array();
  17. function __construct()
  18. {
  19. global $argc, $argv;
  20. // Parse params
  21. if ($argc > 1)
  22. {
  23. $paramSwitch = false;
  24. for ($i = 1; $i < $argc; $i++)
  25. {
  26. $arg = $argv[$i];
  27. $isSwitch = preg_match('/^--/', $arg);
  28. if ($isSwitch)
  29. $arg = preg_replace('/^--/', '', $arg);
  30. if ($paramSwitch && $isSwitch)
  31. {
  32. echo "[param] expected after '$paramSwitch' switch (" . self::$ACCEPTED[1][$paramSwitch] . ")\n";
  33. exit(1);
  34. }
  35. else if (!$paramSwitch && !$isSwitch)
  36. {
  37. echo "'$arg' is an invalid switch, use --help to display valid switches\n";
  38. exit(1);
  39. }
  40. else if (!$paramSwitch && $isSwitch)
  41. {
  42. if (isset($this->params[$arg]))
  43. {
  44. echo "'$arg' switch cannot occur more than once\n";
  45. exit(1);
  46. }
  47. $this->params[$arg] = true;
  48. if (isset(self::$ACCEPTED[1][$arg]))
  49. $paramSwitch = $arg;
  50. else if (!isset(self::$ACCEPTED[0][$arg]))
  51. {
  52. echo "there's no '$arg' switch, use --help to display all switches\n";
  53. exit(1);
  54. }
  55. }
  56. else if ($paramSwitch && !$isSwitch)
  57. {
  58. $this->params[$paramSwitch] = $arg;
  59. $paramSwitch = false;
  60. }
  61. }
  62. }
  63. // Final check
  64. foreach ($this->params as $k => $v)
  65. if (isset(self::$ACCEPTED[1][$k]) && $v === true)
  66. {
  67. echo "[param] expected after '$k' switch (" . self::$ACCEPTED[1][$k] . ")\n";
  68. exit(1);
  69. }
  70. }
  71. function getParam($name)
  72. {
  73. if (isset($this->params[$name]))
  74. return $this->params[$name];
  75. else
  76. return "";
  77. }
  78. function displayHelp()
  79. {
  80. echo "You can use script with following switches: \n\n";
  81. foreach (self::$ACCEPTED[0] as $key => $value)
  82. printf(" --%-14s%s\n", $key, $value);
  83. foreach (self::$ACCEPTED[1] as $key => $value)
  84. printf(" --%-5s%-9s%s\n", $key, " [param]", $value);
  85. }
  86. }
  87. class cURL
  88. {
  89. var $headers, $user_agent, $compression, $cookie_file;
  90. var $cert_check, $proxy;
  91. static $ref = 0;
  92. function cURL($cookies = true, $cookie = 'Cookies.txt', $compression = 'gzip', $proxy = '')
  93. {
  94. $this->headers = $this->headers();
  95. $this->user_agent = 'Mozilla/5.0 (Windows NT 5.1; rv:17.0) Gecko/20100101 Firefox/17.0';
  96. $this->compression = $compression;
  97. $this->cookies = $cookies;
  98. if ($this->cookies == true)
  99. $this->cookie($cookie);
  100. $this->cert_check = true;
  101. $this->proxy = $proxy;
  102. self::$ref++;
  103. }
  104. function __destruct()
  105. {
  106. if ((self::$ref <= 1) and file_exists($this->cookie_file))
  107. unlink($this->cookie_file);
  108. self::$ref--;
  109. }
  110. function headers()
  111. {
  112. $headers[] = 'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8';
  113. $headers[] = 'Connection: Keep-Alive';
  114. return $headers;
  115. }
  116. function cookie($cookie_file)
  117. {
  118. if (file_exists($cookie_file))
  119. $this->cookie_file = $cookie_file;
  120. else
  121. {
  122. $file = fopen($cookie_file, 'w') or $this->error('The cookie file could not be opened. Make sure this directory has the correct permissions');
  123. $this->cookie_file = $cookie_file;
  124. fclose($file);
  125. }
  126. }
  127. function get($url)
  128. {
  129. $process = curl_init($url);
  130. curl_setopt($process, CURLOPT_HTTPHEADER, $this->headers);
  131. curl_setopt($process, CURLOPT_HEADER, 0);
  132. curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
  133. if ($this->cookies == true)
  134. {
  135. curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
  136. curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
  137. }
  138. curl_setopt($process, CURLOPT_ENCODING, $this->compression);
  139. curl_setopt($process, CURLOPT_TIMEOUT, 30);
  140. if ($this->proxy)
  141. $this->setProxy($process, $this->proxy);
  142. curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
  143. curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
  144. if (!$this->cert_check)
  145. curl_setopt($process, CURLOPT_SSL_VERIFYPEER, 0);
  146. $return = curl_exec($process);
  147. curl_close($process);
  148. return $return;
  149. }
  150. function post($url, $data)
  151. {
  152. $process = curl_init($url);
  153. $headers = $this->headers;
  154. $headers[] = 'Content-Type: application/x-www-form-urlencoded;charset=UTF-8';
  155. curl_setopt($process, CURLOPT_HTTPHEADER, $headers);
  156. curl_setopt($process, CURLOPT_HEADER, 1);
  157. curl_setopt($process, CURLOPT_USERAGENT, $this->user_agent);
  158. if ($this->cookies == true)
  159. {
  160. curl_setopt($process, CURLOPT_COOKIEFILE, $this->cookie_file);
  161. curl_setopt($process, CURLOPT_COOKIEJAR, $this->cookie_file);
  162. }
  163. curl_setopt($process, CURLOPT_ENCODING, $this->compression);
  164. curl_setopt($process, CURLOPT_TIMEOUT, 30);
  165. if ($this->proxy)
  166. $this->setProxy($process, $this->proxy);
  167. curl_setopt($process, CURLOPT_POSTFIELDS, $data);
  168. curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
  169. curl_setopt($process, CURLOPT_FOLLOWLOCATION, 1);
  170. curl_setopt($process, CURLOPT_POST, 1);
  171. if (!$this->cert_check)
  172. curl_setopt($process, CURLOPT_SSL_VERIFYPEER, 0);
  173. $return = curl_exec($process);
  174. curl_close($process);
  175. return $return;
  176. }
  177. function setProxy(&$process, $proxy)
  178. {
  179. $type = substr($proxy, 0, stripos($proxy, "://"));
  180. if ($type)
  181. {
  182. $type = strtolower($type);
  183. $proxy = substr($proxy, stripos($proxy, "://") + 3);
  184. }
  185. switch ($type)
  186. {
  187. case "socks4":
  188. $type = CURLPROXY_SOCKS4;
  189. break;
  190. case "socks5":
  191. $type = CURLPROXY_SOCKS5;
  192. break;
  193. default:
  194. $type = CURLPROXY_HTTP;
  195. }
  196. curl_setopt($process, CURLOPT_PROXY, $proxy);
  197. curl_setopt($process, CURLOPT_PROXYTYPE, $type);
  198. }
  199. function error($error)
  200. {
  201. echo "cURL Error : $error";
  202. die;
  203. }
  204. }
  205. function runAsyncBatch($command, $filename)
  206. {
  207. $BatchFile = fopen("WeebTV.bat", 'w');
  208. fwrite($BatchFile, "@Echo off\r\n");
  209. fwrite($BatchFile, "Title $filename\r\n");
  210. fwrite($BatchFile, "$command\r\n");
  211. fwrite($BatchFile, "Del \"WeebTV.bat\"\r\n");
  212. fclose($BatchFile);
  213. $WshShell = new COM("WScript.Shell");
  214. $oExec = $WshShell->Run("WeebTV.bat", 1, false);
  215. unset($WshShell, $oExec);
  216. }
  217. function SafeFileName($filename)
  218. {
  219. $len = strlen($filename);
  220. for ($i = 0; $i < $len; $i++)
  221. {
  222. $char = ord($filename[$i]);
  223. if (($char < 32) || ($char >= 127))
  224. $filename = substr_replace($filename, ' ', $i, 1);
  225. }
  226. $filename = preg_replace('/[\/\\\?\*\:\|\<\>]/i', ' ', $filename);
  227. $filename = preg_replace('/\s\s+/i', ' ', $filename);
  228. $filename = trim($filename);
  229. return $filename;
  230. }
  231. function ShowHeader($header)
  232. {
  233. global $cli;
  234. $len = strlen($header);
  235. $width = (int) ((80 - $len) / 2) + $len;
  236. $format = "\n%" . $width . "s\n\n";
  237. if (!$cli->getParam('quiet'))
  238. printf($format, $header);
  239. }
  240. function KeyName(array $a, $pos)
  241. {
  242. $temp = array_slice($a, $pos, 1, true);
  243. return key($temp);
  244. }
  245. function ci_uksort($a, $b)
  246. {
  247. $a = strtolower($a);
  248. $b = strtolower($b);
  249. return strnatcmp($a, $b);
  250. }
  251. function Display($items, $format, $columns)
  252. {
  253. global $cli;
  254. // Display formatted channels list for external script
  255. if ($cli->getParam('list'))
  256. {
  257. foreach ($items as $name => $url)
  258. {
  259. printf("%-25.25s = %s\n", preg_replace('/=/', '-', $name), $url);
  260. }
  261. exit(0);
  262. }
  263. $numcols = $columns;
  264. $numitems = count($items);
  265. $numrows = ceil($numitems / $numcols);
  266. for ($row = 1; $row <= $numrows; $row++)
  267. {
  268. $cell = 0;
  269. for ($col = 1; $col <= $numcols; $col++)
  270. {
  271. if ($col === 1)
  272. {
  273. $cell += $row;
  274. printf($format, $cell, KeyName($items, $cell - 1));
  275. }
  276. else
  277. {
  278. $cell += $numrows;
  279. if (isset($items[KeyName($items, $cell - 1)]))
  280. printf($format, $cell, KeyName($items, $cell - 1));
  281. }
  282. }
  283. echo "\n\n";
  284. }
  285. }
  286. function Close($message)
  287. {
  288. global $cli, $windows;
  289. if ($message)
  290. qecho($message . "\n");
  291. if ($windows)
  292. exec("chcp 1252");
  293. if (!count($cli->params))
  294. sleep(2);
  295. die();
  296. }
  297. function ReadSettings()
  298. {
  299. global $quality, $username, $password;
  300. if (file_exists("WeebTV.xml"))
  301. {
  302. $xml = simplexml_load_file("WeebTV.xml");
  303. $quality = $xml->quality;
  304. $username = $xml->username;
  305. $password = $xml->password;
  306. }
  307. else
  308. {
  309. $quality = "HI";
  310. $username = "";
  311. $password = "";
  312. }
  313. }
  314. function LogIn()
  315. {
  316. global $cc, $logged_in, $username, $password;
  317. if (($username != "") && ($password != ""))
  318. {
  319. $cc->post("http://weeb.tv/account/login", "username=$username&userpassword=$password");
  320. $logged_in = true;
  321. }
  322. else
  323. $logged_in = false;
  324. }
  325. function LogOut()
  326. {
  327. global $cc, $logged_in;
  328. if ($logged_in)
  329. {
  330. $cc->get("http://weeb.tv/account/logout");
  331. $logged_in = false;
  332. }
  333. }
  334. function ShowChannel($url, $filename)
  335. {
  336. global $cc, $format, $password, $PremiumUser, $quality, $username, $vlc, $windows, $cli;
  337. qecho("Retrieving html . . .\n");
  338. $cc->headers = $cc->headers();
  339. $html = $cc->get($url);
  340. preg_match('/flashvars.*?cid[^\d]+?(\d+)/is', $html, $cid);
  341. if (!isset($cid[1]))
  342. Close("No channel id found");
  343. // Retrieve rtmp stream info
  344. $cc->headers[] = "Referer: http://weeb.tv/static/player.swf";
  345. $response = $cc->post("http://weeb.tv/api/setPlayer", "cid=$cid[1]&watchTime=0&firstConnect=1&ip=NaN");
  346. $result = explode("\r\n\r\n", $response, 2);
  347. $flashVars = explode("&", trim($result[1]));
  348. foreach ($flashVars as $flashVar)
  349. {
  350. $temp = explode("=", $flashVar);
  351. $name = strtolower($temp[0]);
  352. $Params[$name] = $temp[1];
  353. }
  354. $rtmp = urldecode($Params["10"]);
  355. $playpath = urldecode($Params["11"]);
  356. $MultiBitrate = urldecode($Params["20"]);
  357. $PremiumUser = urldecode($Params["5"]);
  358. if ($MultiBitrate)
  359. $playpath .= $quality;
  360. $BlockType = urldecode($Params["13"]);
  361. if ($BlockType != 0)
  362. {
  363. switch ($BlockType)
  364. {
  365. case 1:
  366. $BlockTime = urldecode($Params["14"]);
  367. $ReconnectionTime = urldecode($Params["16"]);
  368. Close("You have crossed free viewing limit. you have been blocked for $BlockTime minutes. try again in $ReconnectionTime minutes.");
  369. break;
  370. case 11:
  371. Close("No free slots available");
  372. break;
  373. default:
  374. break;
  375. }
  376. }
  377. if (!isset($Params["73"]))
  378. {
  379. // Retrieve authentication token
  380. $response = $cc->post("http://weeb.tv/setplayer", "cid=$cid[2]&watchTime=0&firstConnect=0&ip=NaN");
  381. $result = explode("\r\n\r\n", $response, 2);
  382. $flashVars = explode("&", trim($result[1]));
  383. foreach ($flashVars as $flashVar)
  384. {
  385. $temp = explode("=", $flashVar);
  386. $name = strtolower($temp[0]);
  387. $Params[$name] = $temp[1];
  388. }
  389. }
  390. if (isset($Params["73"]))
  391. $token = $Params["73"];
  392. else
  393. Close("Server seems busy. please try after some time.");
  394. qprintf($format, "RTMP Url", $rtmp);
  395. qprintf($format, "Playpath", $playpath);
  396. qprintf($format, "Token", $token);
  397. qprintf($format, "Premium", $PremiumUser ? "Yes" : "No");
  398. if (($username != "") && ($password != ""))
  399. $token = "$token;$username;$password";
  400. $filename = SafeFileName($filename);
  401. if (file_exists($filename . ".flv"))
  402. unlink($filename . ".flv");
  403. $basecmd = 'rtmpdump -r "' . $rtmp . "/" . $playpath . '" -W "http://static2.weeb.tv/player.swf" --weeb "' . $token . "\" --live";
  404. $command = $basecmd . " | \"$vlc\" --meta-title \"$filename\" -";
  405. if ($cli->getParam('print'))
  406. {
  407. echo $basecmd;
  408. exit(0);
  409. }
  410. qprintf($format, "Command", $command);
  411. if ($rtmp && $token)
  412. if ($windows)
  413. runAsyncBatch($command, $filename);
  414. else
  415. exec($command);
  416. }
  417. function qecho($str)
  418. {
  419. global $cli;
  420. if (!$cli->getParam('quiet'))
  421. echo $str;
  422. }
  423. function qprintf($format, $param, $arg)
  424. {
  425. global $cli;
  426. if (!$cli->getParam('quiet'))
  427. printf($format, $param, $arg);
  428. }
  429. // Global code starts here
  430. $header = "KSV WeebTV Downloader";
  431. $format = "%-8s: %s\n";
  432. $ChannelFormat = "%2d) %-22.21s";
  433. strncasecmp(php_uname('s'), "Win", 3) == 0 ? $windows = true : $windows = false;
  434. if ($windows)
  435. {
  436. exec("chcp 65001");
  437. if (file_exists("C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe"))
  438. $vlc = "C:\\Program Files (x86)\\VideoLAN\\VLC\\vlc.exe";
  439. else
  440. $vlc = "C:\\Program Files\\VideoLAN\\VLC\\vlc.exe";
  441. }
  442. else
  443. $vlc = "vlc";
  444. $cli = new CLI();
  445. $cc = new cURL();
  446. ShowHeader($header);
  447. if ($cli->getParam('help'))
  448. {
  449. $cli->displayHelp();
  450. Close("");
  451. }
  452. if ($cli->getParam('proxy'))
  453. $cc->proxy = $cli->getParam('proxy');
  454. ReadSettings();
  455. LogIn();
  456. if ($cli->getParam('url'))
  457. {
  458. $url = $cli->getParam('url');
  459. // You can use only the channel name
  460. if (!preg_match('/^http/', $url))
  461. {
  462. $url = preg_replace('/\.\S+$/', '', $url); // also with extension (like .mpg etc...)
  463. $url = "http://weeb.tv/online/$url";
  464. }
  465. $filename = strrchr($url, '/');
  466. ShowChannel($url, $filename);
  467. }
  468. else
  469. {
  470. $html = $cc->get("http://weeb.tv/channels/live");
  471. preg_match('/<ul class="channels">(.*?)<\/ul>/is', $html, $html);
  472. $html = $html[1];
  473. preg_match_all('/<fieldset[^>]+>(.*?)<\/fieldset>/is', $html, $fieldSets);
  474. foreach ($fieldSets[1] as $fieldSet)
  475. {
  476. preg_match('/12px.*?<a href="([^"]+)"[^>]+>(.*?)<\/a>/i', $fieldSet, $channelVars);
  477. $ChannelList[$channelVars[2]] = $channelVars[1];
  478. }
  479. uksort($ChannelList, 'ci_uksort');
  480. $FirstRun = true;
  481. $KeepRunning = true;
  482. while ($KeepRunning)
  483. {
  484. if ($FirstRun)
  485. $FirstRun = false;
  486. else
  487. ShowHeader($header);
  488. Display($ChannelList, $ChannelFormat, 3);
  489. echo "Enter Channel Number : ";
  490. $channel = trim(fgets(STDIN));
  491. if (is_numeric($channel) && ($channel >= 1) && ($channel <= count($ChannelList)))
  492. {
  493. $url = $ChannelList[KeyName($ChannelList, $channel - 1)];
  494. $filename = KeyName($ChannelList, $channel - 1);
  495. ShowChannel($url, $filename);
  496. }
  497. else
  498. $KeepRunning = false;
  499. }
  500. }
  501. Close("Finished");
  502. ?>