diff --git a/README.asciidoc b/README.asciidoc deleted file mode 100644 index 9995793..0000000 --- a/README.asciidoc +++ /dev/null @@ -1,136 +0,0 @@ -Fotolia API Kits -================ -Olivier Sirven -:website: http://www.fotolia.com/ - -Introduction ------------- -Fotolia’s industry-leading API allows developers and businesses to -quickly integrate a vast database of images in their workflows and -business models. - -Whether you’re developing an application with millions of photos, or -integrating a stock photography library into your business to increase -customer satisfaction, Fotolia’s powerful APIs are key. But technical -excellence is only part of the formula – our business development team -will work with you to determine the best way to maximize results. - -Fotolia’s API line-up ---------------------- - -Partner API -~~~~~~~~~~~ -Our Partner API allows you to embed royalty-free image search into -your website. - -- Your customers may search the entire Fotolia library to find images - without leaving your website. -- After finding the perfect image, your customer clicks through to us to - buy. -- Since our Affiliate Program is also built into this API, we - automatically pay you for all revenues generated. - -http://www.fotolia.com/Services/API/Partner[Learn more] - -Business API -~~~~~~~~~~~~ -Our Business API is for on-demand businesses producing custom -merchandise and products, including web site templates and printed -materials. - -- Your customers will gain access to high-resolution images directly - within your application or web-based tool. -- Wholesale pricing is available. -- You can also use this API to embed Fotolia into your intranet or - application to make search and download faster. - -http://www.fotolia.com/Services/API/Business[Learn more] - -Reseller API -~~~~~~~~~~~~ -Our Reseller API allows you to sell Fotolia’s images under your brand name to your customers. - -- Access Fotolia’s entire microstock image library, with full search - and download capability. -- A complete white-label solution, available in select markets around - the world. - -http://www.fotolia.com/Services/API/Reseller[Learn more] - -Content of this package ------------------------ - -This package contains a full implementation of the API for PHP, python -and java. Each provides an example file to show how to use it. - -PHP -~~~ - -Requirements: - -- http://www.php.net[PHP >= 5.2] -- http://www.php.net/curl[PHP cURL extension] - -.Example usage - -[source,php,numbered] -------------------------------------------- -include::php/example.php[] -------------------------------------------- - - -Python -~~~~~~ - -Requirements: - -- http://python.org/[Python >= 2.7] -- http://pycurl.sourceforge.net/[pycurl extension] - - -.Example usage - -[source,python,numbered] -------------------------------------------- -include::python/example.py[] -------------------------------------------- - - -Java -~~~~ - -Requirements: - -- http://hc.apache.org[apache common HTTP client] -- http://code.google.com/p/json-simple/[json simple] -- http://www.java.com[java JDK >= 6.26] - -A simple Makefile is provided to help using it. It supports following -commands: - -build:: - Build the entire package - -jar:: - Create a JAR file for easy inclusion - -clean:: - Clean the build files - -example:: - Build the example program - -.Example usage - -[source,java,numbered] ------------------------- -include::java/example.java[] ------------------------- - -Informations ------------- -Email: api@fotolia.com - -http://groups.google.com/group/FotoliaAPI?pli=1[Google Groups] - -http://us.fotolia.com/Services/API/Rest/Documentation[Online Documentation] diff --git a/README.html b/README.html index c0d4ad4..d1da6c1 100644 --- a/README.html +++ b/README.html @@ -713,11 +713,11 @@

PHP

00024: 00025: // loggin in and retrieving user data 00026: $api->loginUser('your_login', 'your_password'); -00027: print_r(api.getUserData()); +00027: print_r($api->getUserData()); 00028: 00029: // purchasing and downloading a file -00030: $dl_data = $api.getMedia(35957426, 'XS'); -00031: $api.downloadMedia($dl_data['url'], '/tmp/' + $dl_data['name']); +00030: $dl_data = $api->getMedia(35957426, 'XS'); +00031: $api->downloadMedia($dl_data['url'], '/tmp/' + $dl_data['name']);

Python

diff --git a/README.md b/README.md new file mode 100644 index 0000000..b3eb64e --- /dev/null +++ b/README.md @@ -0,0 +1,211 @@ +Fotolia API Kits +================ +Olivier Sirven +[Fotolia.com](http://www.fotolia.com/ "Fotolia") + +Introduction +============ +Fotolia's industry-leading API allows developers and businesses to +quickly integrate a vast database of images in their workflows and +business models. + +Whether you're developing an application with millions of photos, or +integrating a stock photography library into your business to increase +customer satisfaction, Fotolia's powerful APIs are key. But technical +excellence is only part of the formula – our business development team +will work with you to determine the best way to maximize results. + +Fotolia's API line-up +===================== + +Partner API +----------- +Our Partner API allows you to embed royalty-free image search into +your website. + +* Your customers may search the entire Fotolia library to find images + without leaving your website. +* After finding the perfect image, your customer clicks through to us to + buy. +* Since our Affiliate Program is also built into this API, we + automatically pay you for all revenues generated. + +[Learn more](http://www.fotolia.com/Services/API/Partner "Fotolia Partner API") + +Business API +------------ +Our Business API is for on-demand businesses producing custom +merchandise and products, including web site templates and printed +materials. + +* Your customers will gain access to high-resolution images directly + within your application or web-based tool. +* Wholesale pricing is available. +* You can also use this API to embed Fotolia into your intranet or + application to make search and download faster. + +[Learn more](http://www.fotolia.com/Services/API/Business "Fotolia Business API") + +Reseller API +------------ +Our Reseller API allows you to sell Fotolia's images under your brand name to your customers. + +* Access Fotolia's entire microstock image library, with full search + and download capability. +* A complete white-label solution, available in select markets around + the world. + +[Learn more](http://www.fotolia.com/Services/API/Reseller "Fotolia Reseller API") + +Content of this package +======================= + +This package contains a full implementation of the API for PHP, python +and java. Each provides an example file to show how to use it. + +PHP +--- + +Requirements: + +* [PHP >= 5.2](http://www.php.net) +* [PHP cURL extension](http://www.php.net/curl) + +.Example usage + +
+#!/usr/bin/env php
+<?php
+
+require_once 'fotolia-api.php';
+
+$api = new Fotolia_Api('your_api_key');
+
+// searching for files
+$results = $api->getSearchResults(
+    array(
+        'words' => 'car',
+        'language_id' => Fotolia_Api::LANGUAGE_ID_EN_US,
+        'limit' => 1,
+    ));
+
+printf("Found %d results", $results['nb_results']);
+
+foreach ($results as $key => $value) {
+    // iterating only over numeric keys and silently skip other keys
+    if (is_numeric($key)) {
+        printf("matching media ID: %d", $value['id']);
+    }
+}
+
+// loggin in and retrieving user data
+$api->loginUser('your_login', 'your_password');
+print_r($api->getUserData());
+
+// purchasing and downloading a file
+$dl_data = $api->getMedia(35957426, 'XS');
+$api->downloadMedia($dl_data['url'], '/tmp/' . $dl_data['name']);
+
+ +Python +------ + +Requirements: + +* [Python >= 2.7](http://python.org/) +* [pycurl extension](http://pycurl.sourceforge.net/) + + +.Example usage + +
+#!/usr/bin/env python
+
+import fotolia_api
+
+api = fotolia_api.FotoliaApi('your_api_key')
+
+# searching for files
+results = api.get_search_results({'words': 'car', 'language_id': fotolia_api.LANGUAGE_ID_EN_US, 'limit': 1})
+print "Found %d results" % results['nb_results']
+
+for key, value in results.items():
+    try:
+        int(key)
+        print "matching media ID: %d" % value['id']
+    except ValueError:
+        # iterating only over numeric keys and silently skip other keys
+        pass
+
+# loggin in and retrieving user data
+api.login_user('your_login', 'your_password')
+print api.get_user_data()
+
+# purchasing and downloading a file
+dl_data = api.get_media(35957426, 'XS')
+api.download_media(dl_data['url'], '/tmp/' + str(dl_data['name']))
+
+ +Java +---- + +Requirements: + +* [apache common HTTP client](http://hc.apache.org) +* [json simple](http://code.google.com/p/json-simple/) +* [java JDK >= 6.26](http://www.java.com) + +A simple Makefile is provided to help using it. It supports following +commands: + +build:: + Build the entire package + +jar:: + Create a JAR file for easy inclusion + +clean:: + Clean the build files + +example:: + Build the example program + +.Example usage + +
+import org.webservice.fotolia.*;
+import org.json.simple.JSONObject;
+
+public class example
+{
+    public static void main(String[] args)
+    {
+        FotoliaApi client = new FotoliaApi("your_api_key");
+
+        // fetching a media data
+        System.out.println(client.getMediaData(18136053));
+
+        // searching files
+        FotoliaSearchQuery query = new FotoliaSearchQuery();
+        query.setWords("car").setLanguageId(FotoliaApi.LANGUAGE_ID_EN_US).setLimit(1);
+        System.out.println(client.getSearchResults(query));
+
+        // buying and downloading a file
+        try {
+            client.loginUser("your_login", "your_password");
+            JSONObject res = client.getMedia(35957426, "XS");
+            client.downloadMedia((String) res.get("url"), "/tmp/" + (String) res.get("name"));
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}
+
+ +Informations +------------ +Email: api@fotolia.com + +[Google Groups](http://groups.google.com/group/FotoliaAPI?pli=1 "Fotolia API Google Group") + +[Online Documentation](http://us.fotolia.com/Services/API/Rest/Documentation "Fotolia REST API Documentation") diff --git a/java/org/webservice/fotolia/FotoliaApi.java b/java/org/webservice/fotolia/FotoliaApi.java index cf3ea41..9a86807 100644 --- a/java/org/webservice/fotolia/FotoliaApi.java +++ b/java/org/webservice/fotolia/FotoliaApi.java @@ -31,7 +31,7 @@ public class FotoliaApi { - public static String REST_URI = "api.fotolia.com/Rest"; + public static String REST_URI = "https://api.fotolia.com/Rest"; public static String REST_VERSION = "1"; @@ -68,11 +68,6 @@ public class FotoliaApi */ private final String _api_key; - /** - * HTTPs mode flag - */ - private boolean _use_https; - /** * Current session ID */ @@ -98,12 +93,10 @@ public FotoliaApi(final String api_key) * Constructor * * @param api_key - * @param use_https */ - public FotoliaApi(final String api_key, final boolean use_https) + public FotoliaApi(final String api_key) { this._api_key = api_key; - this.setHttpsMode(use_https); } /** @@ -116,18 +109,6 @@ public String getApiKey() return this._api_key; } - /** - * Sets the HTTPs mode flag - * - * @param flag - * @return FotoliaApi - */ - public FotoliaApi setHttpsMode(final boolean flag) - { - this._use_https = flag; - return this; - } - /** * Fotolia API test mode * @@ -1431,13 +1412,7 @@ private String _getUri(final String method_name, final FotoliaApiArgs args) namespace += "/"; } - uri = "http"; - - if (this._use_https) { - uri += 's'; - } - - uri += "://" + FotoliaApi.REST_URI + "/" + FotoliaApi.REST_VERSION + "/" + namespace + method_name; + uri = FotoliaApi.REST_URI + "/" + FotoliaApi.REST_VERSION + "/" + namespace + method_name; if (args != null) { uri += "?" + args; diff --git a/php/example.php b/php/example.php index eaca174..0e96087 100644 --- a/php/example.php +++ b/php/example.php @@ -22,6 +22,10 @@ } } +// download comp file +$comp_dl_data = $api->getMediaComp(35957426); +$api->downloadMediaComp($comp_dl_data['url'], '/tmp/comp.jpg'); + // loggin in and retrieving user data $api->loginUser('your_login', 'your_password'); print_r($api->getUserData()); diff --git a/php/fotolia-api.php b/php/fotolia-api.php index b1176fb..1790676 100644 --- a/php/fotolia-api.php +++ b/php/fotolia-api.php @@ -22,7 +22,7 @@ class Fotolia_Api /** * Fotolia REST uri */ - const FOTOLIA_REST_URI = 'api.fotolia.com/Rest'; + const FOTOLIA_REST_URI = 'https://api.fotolia.com/Rest'; /** * Fotolia REST API version @@ -65,11 +65,6 @@ class Fotolia_Api */ private $_api_key; - /** - * HTTPs mode flag - */ - private $_use_https; - /** * Current session id * @@ -89,12 +84,11 @@ class Fotolia_Api * * @param string $api_key */ - public function __construct($api_key, $use_https = FALSE) + public function __construct($api_key) { $this->_api_key = $api_key; $this->_session_id = NULL; $this->_session_id_timestamp = NULL; - $this->_use_https = $use_https; } /** @@ -107,15 +101,6 @@ public function getApiKey() return $this->_api_key; } - /** - * Toggle HTTPS - */ - public function setHttpsMode($flag) - { - $this->_use_https = $flag; - return $this; - } - /** * This method makes possible to search media in fotolia image bank. * Full search capabilities are available through the API @@ -201,6 +186,22 @@ public function getGalleries($language_id = Fotolia_Api::LANGUAGE_ID_EN_US) )); } + /** + * This method returns public seasonal galleries for a defined language + * + * @param int $language_id + * @return array + */ + public function getSeasonalGalleries($language_id = Fotolia_Api::LANGUAGE_ID_EN_US, $thumbnail_size = 110, $theme_id = null) + { + return $this->_api('getSeasonalGalleries', + array( + 'language_id' => $language_id, + 'thumbnail_size' => $thumbnail_size, + 'theme_id' => $theme_id + )); + } + /** * This method returns Fotolia list of countries. * @@ -314,11 +315,38 @@ public function getMedia($id, $license_name, $subaccount_id = NULL) * @param string $output_file if null the downloaded file will be echoed on standard output */ public function downloadMedia($download_url, $output_file = NULL) + { + return $this->_download($download_url, $output_file); + } + + /** + * Download a media comp and write it to a file if necessary + * + * @param string $download_url URL as returned by getMediaComp() + * @param string $output_file if null the downloaded file will be echoed on standard output + */ + public function downloadMediaComp($download_url, $output_file = NULL) + { + return $this->_download($download_url, $output_file, false); + } + + /** + * Download a content and write it to a file if necessary + * + * @param string $download_url URL + * @param string $output_file if null the downloaded file will be echoed on standard output + * @param bool $http_auth set curl password if needed + */ + private function _download($download_url, $output_file = NULL, $http_auth_required = true) { $ch = $this->_getCurlHandler($download_url); if ($output_file === NULL) { - $output_file = 'php://stdout'; + if ($this->_isShellMode()) { + $output_file = 'php://stdout'; + } else { + $output_file = 'php://output'; + } } $output_fd = fopen($output_file, 'w'); @@ -327,7 +355,8 @@ public function downloadMedia($download_url, $output_file = NULL) } curl_setopt($ch, CURLOPT_FILE, $output_fd); - curl_setopt($ch, CURLOPT_USERPWD, $this->_getHttpAuth(TRUE, TRUE)); + curl_setopt($ch, CURLOPT_USERPWD, $this->_getHttpAuth(TRUE, $http_auth_required)); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE); $response = curl_exec($ch); @@ -391,21 +420,12 @@ public function getMediaComp($id) */ public function loginUser($login, $pass) { - $old_https_flag = $this->_use_https; - if (!$old_https_flag) { - $this->setHttpsMode(true); - } - $res = $this->_api('loginUser', array( 'login' => $login, 'pass' => $pass, )); - if (!$old_https_flag) { - $this->setHttpsMode(false); - } - $this->_session_id = $res['session_token']; $this->_session_id_timestamp = time(); } @@ -917,6 +937,7 @@ protected function _getNamespace($method) case 'getCategories2': case 'getTags': case 'getGalleries': + case 'getSeasonalGalleries': case 'getCountries': return 'search'; @@ -998,14 +1019,12 @@ protected function _getSessionId($auto_refresh_token = TRUE) */ private function _getFullURI($method, array $query = NULL) { - $scheme = $this->_use_https ? 'https' : 'http'; - $namespace = $this->_getNamespace($method); if (!empty($namespace)) { $namespace .= '/'; } - $uri = $scheme . '://' . Fotolia_Api::FOTOLIA_REST_URI . '/' + $uri = Fotolia_Api::FOTOLIA_REST_URI . '/' . Fotolia_Api::FOTOLIA_REST_VERSION . '/' . $namespace . $method; if ($query !== NULL) { @@ -1099,6 +1118,14 @@ private function _isPostMethod($method) return $is_post_method; } + + /* + * Define if the api is called in CLI mode + */ + private function _isShellMode() + { + return !empty($_SERVER['SHELL']); + } } /** diff --git a/python/fotolia_api/__init__.py b/python/fotolia_api/__init__.py index 0005926..f76d813 100644 --- a/python/fotolia_api/__init__.py +++ b/python/fotolia_api/__init__.py @@ -1,4 +1,4 @@ -REST_URI = "api.fotolia.com/Rest" +REST_URI = "https://api.fotolia.com/Rest" REST_VERSION = "1" LANGUAGE_ID_FR_FR = 1 diff --git a/python/fotolia_api/rest.py b/python/fotolia_api/rest.py index 8ac3119..818a54d 100644 --- a/python/fotolia_api/rest.py +++ b/python/fotolia_api/rest.py @@ -15,22 +15,18 @@ class Rest: # api key _api_key = None - # https toggle flag - _use_https = False - # session ID if any _session_id = None # session ID timestamp if any _session_id_timestamp = None - def __init__(self, api_key, use_https = False): + def __init__(self, api_key): """ Constructor """ self._api_key = api_key - self._use_https = use_https self._session_id = None self._session_id_timestamp = None @@ -41,13 +37,6 @@ def get_api_key(self): return self._api_key - def set_https_mode(self, flag): - """ - Toggle HTTPS - """ - - self._use_https = flag - def get_search_results(self, search_params, result_columns = []): """ This method makes possible to search media in fotolia image bank. @@ -113,6 +102,21 @@ def get_galleries(self, language_id = fotolia_api.LANGUAGE_ID_EN_US): return self._api('getGalleries', params) + def get_seasonal_galleries(self, language_id = fotolia_api.LANGUAGE_ID_EN_US, thumbnail_size = fotolia_api.THUMB_SIZE_SMALL, theme_id = None): + """ + This method returns public galleries for a defined language + """ + + params = { + 'language_id': language_id, + 'thumbnail_size': thumbnail_size + } + + if theme_id != None: + params['theme_id'] = theme_id + + return self._api('getSeasonalGalleries', params) + def get_countries(self, language_id = fotolia_api.LANGUAGE_ID_EN_US): """ This method returns Fotolia list of countries @@ -707,17 +711,13 @@ def _get_full_uri(self, method, query = None): Generate the full URI to use for API calls """ - scheme = 'http' - if self._use_https: - scheme += 's' - namespace = self._get_namespace(method) if namespace != None: namespace += '/' else: namespace = '' - uri = scheme + '://' + fotolia_api.REST_URI + '/' + fotolia_api.REST_VERSION + '/' + namespace + method + uri = fotolia_api.REST_URI + '/' + fotolia_api.REST_VERSION + '/' + namespace + method if query != None: uri += '?' + self._recursive_urlencode(query) @@ -735,6 +735,7 @@ def _get_namespace(self, method): method == 'getCategories2' or \ method == 'getTags' or \ method == 'getGalleries' or \ + method == 'getSeasonalGalleries' or \ method == 'getCountries': return 'search'