Skip to content

Instantly share code, notes, and snippets.

@brabijan
Last active December 15, 2015 01:09
Show Gist options
  • Save brabijan/5177701 to your computer and use it in GitHub Desktop.
Save brabijan/5177701 to your computer and use it in GitHub Desktop.
Czshare downloader
<?php
class CzshareDownloader extends Nette\Object {
/** @var string */
private $loginUrl = "https://czshare.com/index.php";
/** @var string */
private $grabberLink = "http://czshare.com/index.php?pg=grabber";
/** @var string */
private $username;
/** @var string */
private $password;
/**
* @param $username czshare username
* @param $password czshare password
*/
public function __construct($username, $password) {
$this->username = $username;
$this->password = $password;
}
/**
* @param $url
* @param $downloadDir
*/
public function downloadUrl($url, $downloadDir) {
$url = $this->parseFileCredentials($url, $this->login());
`cd $downloadDir && curl -LJO $url`;
}
/**
* @param $url
* @param $cookies
* @return string url of file to download
*/
private function parseFileCredentials($url, $cookies) {
$request = new Kdyby\Curl\Request($this->grabberLink);
$request->cookies = $cookies;
$response = $request->post(array("linky" => $url));
list($smth, $url) = explode('<div class="forum clearfix">', $response->response);
list($smth, $url) = explode('href="', $url);
list($url) = explode('"', $url);
return $url;
}
/**
* @return array cookies
*/
private function login() {
$request = new Kdyby\Curl\Request( $this->loginUrl );
$response = $request->post(array(
"login-name" => $this->username,
"login-password" => $this->password,
"trvale" => "on",
"Prihlasit" => "Přihlásit"
));
return $response->cookies;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment