/**
* Downloads a snap.
*
* @param string $id
* The snap ID.
*
* @return mixed
* The snap data or FALSE on failure.
* Snap data can returned as an Array of more than one file.
* array(
* overlay~zip-CE6F660A-4A9F-4BD6-8183-245C9C75B8A0 => overlay_file_data,
* media~zip-CE6F660A-4A9F-4BD6-8183-245C9C75B8A0 => m4v_file_data
* )
*/
function getMedia($id, $from = null, $time = null, $subdir = null)
{
// Make sure we're logged in and have a valid access token.
if (!$this->auth_token || !$this->username) {
return FALSE;
}
if ($subdir == null) {
$subdir = $this->username;
}
$path = __DIR__ . DIRECTORY_SEPARATOR . "snaps" . DIRECTORY_SEPARATOR . $subdir . DIRECTORY_SEPARATOR . $from;
if (!file_exists($path)) {
mkdir($path, 0777, true);
}
$file = $path . DIRECTORY_SEPARATOR . date("Y-m-d H-i-s", (int) ($time / 1000));
$extensions = array(".jpg", ".png", ".mp4", "");
foreach ($extensions as $ext) {
if (file_exists($file . $ext)) {
return false;
}
}
$timestamp = parent::timestamp();
$result = parent::post('/bq/blob', array('id' => $id, 'timestamp' => $timestamp, 'username' => $this->username), array($this->auth_token, $timestamp), $multipart = false, $debug = $this->debug);
if (!parent::isMedia(substr($result, 0, 2))) {
//When a snapchat video is sent with "text" or overlay
//the overlay is a transparent PNG file Zipped together
//with the M4V file.
//First two bytes are "PK" x50x4B; thus the previous media check
//will fail and would've returned a FALSE on an available media.
if (parent::isCompressed(substr($result, 0, 2))) {
//Uncompress
$result = parent::unCompress($result);
//Return Media and Overlay
}
}
if ($from != null && $time != null) {
if (is_array($result)) {
foreach ($result as $key => $value) {
$this->writeToFile($file, $value);
}
} else {
$this->writeToFile($file, $result);
}
}
return $result;
}