diff --git a/README.md b/README.md index fff3b88..9d15aa6 100644 --- a/README.md +++ b/README.md @@ -32,8 +32,9 @@ Pretty simple: Limitations ----------- -Only login (with list of new media) and fetching of images is implemented. -This is obviously a huge failing which I am to correct when I have more time. +Only login (with list of new media) and fetching of image/video snaps is +implemented. This is obviously a huge failing which I am to correct when I +have more time. Motivation and development process ---------------------------------- @@ -85,8 +86,7 @@ The TODO list is almost endless at this point: - DOCS!!! - Syncing (to mark snaps as seen) -- Video fetching -- Image/video posting +- Image/video uploading - Friend list maintenance - Port to Javascript (probably via Node + NPM since their API doesn't seem to support JSONP) diff --git a/examples/fetch_all_new_photos.php b/examples/fetch_all_new_photos.php index 912c624..b0ad780 100644 --- a/examples/fetch_all_new_photos.php +++ b/examples/fetch_all_new_photos.php @@ -19,6 +19,7 @@ $opts = array(); $opts['username'] = $argv[1]; $opts['password'] = $argv[2]; + // $opts['debug'] = 1; uncomment if having trouble $s = new Snaphax($opts); $result = $s->login(); @@ -28,8 +29,13 @@ if ($snap['st'] == SnapHax::STATUS_NEW) { echo "fetching $snap[id]\n"; $blob_data = $s->fetch($snap['id']); - if ($blob_data) - file_put_contents($snap['id'].'.jpg', $blob_data); + if ($blob_data) { + if ($snap['m'] == SnapHax::MEDIA_IMAGE) + $ext = '.jpg'; + else + $ext = '.mp4'; + file_put_contents($snap['id'].$ext, $blob_data); + } } } } diff --git a/snaphax.php b/snaphax.php index 864edbf..4619383 100644 --- a/snaphax.php +++ b/snaphax.php @@ -45,10 +45,11 @@ } class Snaphax { - // High level class to perform actions on Snapchat const STATUS_NEW = 1; + const MEDIA_IMAGE = 0; + const MEDIA_VIDEO = 1; function Snaphax($options) { global $SNAPHAX_DEFAULT_OPTIONS; @@ -98,6 +99,16 @@ echo "SNAPHAX DEBUG: $text\n"; } + private function isValidBlobHeader($header) { + if (($header[0] == chr(00) && // mp4 + $header[0] == chr(00)) || + ($header[0] == chr(0xFF) && // jpg + $header[1] == chr(0xD8))) + return true; + else + return false; + } + public function fetchBlob($snap_id, $username, $auth_token) { $un = urlencode($username); $ts = time(); @@ -110,11 +121,12 @@ $this->debug('blob result: ' . $result); $result_decoded = mcrypt_decrypt('rijndael-128', $this->options['blob_enc_key'], $result, 'ecb'); $this->debug('decoded: ' . $result_decoded); - if ($result_decoded[0] == chr(0xFF) && - $result_decoded[1] == chr(0xD8)) { + if ($this->isValidBlobHeader(substr($result_decoded, 0, 256))) { return $result_decoded; - } else + } else { + $this->debug('invalid image/video data header'); return false; + } } public function postCall($endpoint, $post_data, $param1, $param2, $json=1) {