Permalink
Browse files

flickr download

  • Loading branch information...
0 parents commit 229835e6fb90d63c294e0fc488e50521b125cfa7 @saturngod committed Sep 25, 2016
Showing with 389 additions and 0 deletions.
  1. +31 −0 README.md
  2. +128 −0 download.php
  3. +65 −0 flickr.php
  4. +15 −0 index.php
  5. +77 −0 oauthtoken.php
  6. +73 −0 rest.php
@@ -0,0 +1,31 @@
+# Flickr Downloader
+
+Put flickr download file to your localhost.
+
+Get KEY and SECRECT at [https://www.flickr.com/services/apps/create/](https://www.flickr.com/services/apps/create/).
+
+Open **index.php** and change `$callbackURL = "http://localhost/flickr_download/index.php";` and update key,secrect.
+
+If your folder name is `flickr` , it will be like `$callbackURL = "http://localhost/flickr/index.php";`.
+
+Open in browser `http://localhost/flickr_download/index.php`.
+
+It will authorize oauth and after finish you will receive token and secrect.
+
+
+Example:
+
+```
+O_TOKEN: 86157674079732196-70bdda496a9f908f
+O_SECRECT: b2a74ac72785db78
+```
+
+Update `O_TOKEN` and `O_SECRECT` in `download.php`.
+
+Afer that , run
+
+```
+php download.php
+```
+
+It will create download folder and download all photo in there.
@@ -0,0 +1,128 @@
+<?php
+
+
+require_once("rest.php");
+
+$photo_id = array();
+$user_id = "";
+define("O_TOKEN","ENTER_O_TOKEN");
+define("O_SECRECT","ENTER_O_SECRECT");
+define("DOWNLOAD_FOLDER",dirname(__FILE__) . "/download/");
+
+
+
+if (O_TOKEN == "ENTER_O_TOKEN" || O_SECRECT == "ENTER_O_SECRECT") {
+ echo "UPDATE O_TOKEN AND O_SECRECT\n";
+ exit;
+}
+
+echo "Testing Token...\n";
+
+
+$login = rest("flickr.test.login","",O_TOKEN,O_SECRECT);
+
+
+
+
+if(isset($login["user"])) {
+ $user = $login["user"];
+ $user_id = $user["id"];
+
+
+
+ if (!file_exists(DOWNLOAD_FOLDER)) {
+ mkdir(DOWNLOAD_FOLDER);
+ }
+ photos_id();
+
+
+}
+
+
+function download_photo($id) {
+
+
+ $arg = "photo_id=".$id;
+
+
+ $photoInfo = rest("flickr.photos.getInfo",$arg,O_TOKEN,O_SECRECT);
+
+ if (isset($photoInfo["photo"])) {
+ $photo = $photoInfo["photo"];
+ $pid = $photo["id"];
+ $server = $photo["server"];
+ $originalsecret = $photo["originalsecret"];
+ $ext = $photo["originalformat"];
+
+ $obj = [];
+ $obj["name"] = $pid."_".$originalsecret."_o_d.".$ext;
+ $obj["url"] = "https://farm8.staticflickr.com/".$server."/".$obj["name"];
+ return $obj;
+ }
+ else {
+ return "";
+ }
+}
+
+function download_file($url,$name) {
+ set_time_limit(0);
+ //This is the file where we save the information
+
+ $fp = fopen (DOWNLOAD_FOLDER.$name, 'w+');
+ //Here is the file we are downloading, replace spaces with %20
+ $ch = curl_init(str_replace(" ","%20",$url));
+ curl_setopt($ch, CURLOPT_TIMEOUT, 50);
+ // write curl response to file
+ curl_setopt($ch, CURLOPT_FILE, $fp);
+ curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
+ // get curl response
+ curl_exec($ch);
+ curl_close($ch);
+ fclose($fp);
+}
+
+function photos_id($page=1) {
+ global $user_id;
+ global $photo_id;
+ echo "PAGE IS : ".$page;
+ echo "\n";
+
+ sleep(1);
+
+ $arg = "page=$page&content_type=6&per_page=500&user_id=".$user_id;
+
+
+ $photosStream = rest("flickr.people.getPhotos",$arg,O_TOKEN,O_SECRECT);
+
+ if(isset($photosStream["photos"])) {
+ $photos = $photosStream["photos"];
+ $page = intval($photos["page"]);
+ $totalpages = intval($photos["pages"]);
+
+ $photoList = $photos["photo"];
+
+ foreach ($photoList as $ph) {
+
+ $photoObj = download_photo($ph["id"]);
+
+ if($photoObj != "") {
+ //array_push($photo_id,$url);
+ echo "Downloading... \n".$photoObj["url"]."\n";
+ download_file($photoObj["url"],$photoObj["name"]);
+ echo "\n";
+ }
+
+ sleep(1);
+ }
+
+ if ($page != $totalpages) {
+ $page = $page + 1;
+ echo "Go to Next page : $page";
+ echo "\n";
+
+ sleep(2);
+
+ photos_id($page);
+ }
+ }
+}
@@ -0,0 +1,65 @@
+<?php
+
+$mt = microtime();
+$rand = mt_rand();
+$oauth_nonce = md5($mt . $rand);
+$request_token_url = "https://www.flickr.com/services/oauth/request_token";
+
+$nonce = $oauth_nonce;
+$timestamp = gmdate('U'); //It must be UTC time
+
+$cc_key = "d62ac97b3cad4fed3e069c9229c408b1";
+$cc_secret = "e18e6aad977f1d58";
+$sig_method = "HMAC-SHA1";
+$oversion = "1.0";
+
+
+$basestring = "oauth_callback=".urlencode($callbackURL)."&oauth_consumer_key=".$cc_key."&oauth_nonce=".$nonce."&oauth_signature_method=".$sig_method."&oauth_timestamp=".$timestamp."&oauth_version=".$oversion."&perms=write";
+$basestring = "GET&".urlencode($request_token_url)."&".urlencode($basestring);
+$hashkey = $cc_secret."&";
+
+
+$oauth_signature = base64_encode(hash_hmac('sha1', $basestring, $hashkey, true));
+
+$fields = array(
+'oauth_nonce'=>$nonce,
+'oauth_timestamp'=>$timestamp,
+'oauth_consumer_key'=>$cc_key,
+'oauth_signature_method'=>$sig_method,
+'oauth_version'=>$oversion,
+'oauth_signature'=>$oauth_signature,
+'oauth_callback' => $callbackURL
+);
+
+$fields_string = "";
+foreach($fields as $key=>$value) $fields_string .= "$key=".urlencode($value)."&";
+$fields_string = rtrim($fields_string,'&');
+
+$url = $request_token_url."?".$fields_string."&perms=write";
+
+$ch = curl_init();
+$timeout = 60;
+curl_setopt ($ch, CURLOPT_URL, $url);
+curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
+curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
+curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
+curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
+$file_contents = curl_exec($ch);
+
+if (FALSE === $file_contents){
+ echo curl_error($ch);
+ exit;
+}
+
+curl_close($ch);
+parse_str($file_contents,$rsp_arr);
+
+if(isset($rsp_arr["oauth_token"])) {
+
+ if (isset($rsp_arr["oauth_token_secret"])) {
+ $_SESSION['oauth_token_secret'] = $rsp_arr['oauth_token_secret'];
+ }
+ echo "<script>window.location.replace(\"https://www.flickr.com/services/oauth/authorize?perms=read&oauth_token=".$rsp_arr["oauth_token"]."\")</script>";
+
+
+}
@@ -0,0 +1,15 @@
+<?php
+session_start();
+
+$callbackURL = "http://localhost/flickr_download/index.php";
+
+//get key and secrect at https://www.flickr.com/services/apps/create/
+$cc_key = "API KEY";
+$cc_secret = "API SECRECT";
+
+if(isset($_GET['oauth_token'])) {
+ require_once('oauthtoken.php');
+}
+else {
+ require_once('flickr.php');
+}
@@ -0,0 +1,77 @@
+<?php
+
+
+if(isset($_GET['oauth_token'])) {
+ $oauth_token = $_GET['oauth_token'];
+ $oauth_verifier = $_GET['oauth_verifier'];
+
+$mt = microtime();
+$rand = mt_rand();
+$oauth_nonce = md5($mt . $rand);
+$nonce = $oauth_nonce;
+$timestamp = gmdate('U'); //It must be UTC time
+$request_token_url = "https://www.flickr.com/services/oauth/access_token";
+
+$oauth_token_secret = $_SESSION['oauth_token_secret'];
+
+
+ $sig_method = "HMAC-SHA1";
+ $oversion = "1.0";
+
+ $basestring = "oauth_consumer_key=$cc_key&oauth_nonce=$oauth_nonce&oauth_signature_method=HMAC-SHA1&oauth_timestamp=$timestamp&oauth_token=$oauth_token&oauth_verifier=$oauth_verifier&oauth_version=$oversion";
+ $basestring = "GET&".urlencode($request_token_url)."&".urlencode($basestring);
+ $hashkey = $cc_secret."&".$oauth_token_secret;
+
+
+ $oauth_signature = base64_encode(hash_hmac('sha1', $basestring, $hashkey, true));
+
+
+
+ $fields = array(
+ 'oauth_nonce'=>$nonce,
+ 'oauth_timestamp'=>$timestamp,
+ 'oauth_verifier'=>$oauth_verifier,
+ 'oauth_consumer_key'=>$cc_key,
+ 'oauth_signature_method'=>$sig_method,
+ 'oauth_version'=>$oversion,
+ 'oauth_token' =>$oauth_token,
+ 'oauth_signature'=>$oauth_signature
+ );
+
+ $fields_string = "";
+ foreach($fields as $key=>$value) $fields_string .= "$key=".urlencode($value)."&";
+ $fields_string = rtrim($fields_string,'&');
+
+ $url = $request_token_url."?".$fields_string;
+ $ch = curl_init();
+ $timeout = 5;
+
+
+ curl_setopt ($ch, CURLOPT_URL, $url);
+ curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
+ curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
+ curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
+ curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
+ $file_contents = curl_exec($ch);
+ curl_close($ch);
+
+
+
+
+ parse_str($file_contents,$rsp_arr);
+
+ if(isset($rsp_arr["oauth_token"])) {
+ $_SESSION['oauth_token'] = $rsp_arr["oauth_token"];
+ $_SESSION['oauth_token_secret'] = $rsp_arr["oauth_token_secret"];
+
+
+ echo "O_TOKEN: ".$_SESSION['oauth_token']."<br/>";
+ echo "O_SECRECT: ".$_SESSION['oauth_token_secret']."<br/>";
+ }
+ else {
+ echo $file_contents;
+ }
+
+
+
+}
Oops, something went wrong.

0 comments on commit 229835e

Please sign in to comment.