diff --git a/src/TwitterBot/.gitignore b/src/TwitterBot/.gitignore new file mode 100644 index 0000000000000000000000000000000000000000..5ac0380977fcfebe039711dbdff2c4c564137c13 --- /dev/null +++ b/src/TwitterBot/.gitignore @@ -0,0 +1,2 @@ +rmnApiKey.php +twitterCredentials.php diff --git a/src/TwitterBot/Helpers.php b/src/TwitterBot/Helpers.php new file mode 100644 index 0000000000000000000000000000000000000000..07f85f88fb7dfb06ea98a95a35f4ffcf765adcf3 --- /dev/null +++ b/src/TwitterBot/Helpers.php @@ -0,0 +1,21 @@ +<?php + +function getCURLOutput($url, $APIkey){ + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 2); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); + curl_setopt($ch, CURLOPT_HEADER, 0); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); + curl_setopt($ch, CURLOPT_AUTOREFERER, true); + curl_setopt($ch, CURLOPT_VERBOSE, 1); + curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17'); + curl_setopt($ch, CURLOPT_HTTPHEADER, array( + 'ApiKey: ' . $APIkey + )); + $output = curl_exec($ch); + curl_close($ch); + return json_decode($output); +} + +?> diff --git a/src/TwitterBot/ImagesDArtBot.php b/src/TwitterBot/ImagesDArtBot.php new file mode 100644 index 0000000000000000000000000000000000000000..dc5b889e2600b76259811323009309ef7356050e --- /dev/null +++ b/src/TwitterBot/ImagesDArtBot.php @@ -0,0 +1,114 @@ +<?php + +include('rmnApiKey.php'); +include('twitterCredentials.php'); +include("Helpers.php"); +include("museumToTwitter.php"); +require_once('TwitterAPIExchange.php'); +header('Content-Type: text/html; charset=utf-8'); + +// RMN-GP API key +$RmnAPIKey = $rmnApiKey; + +/** Set access tokens here - see: https://apps.twitter.com/ **/ +$APIsettings = array( + '1075384445732118528-kmIktkDSNa4NW0pgzOSm0OqAiBkmC3' => $oauthToken, + 'XxHeqBsTrgrvRfUiWfYNgj9HeSx1Xw7lzf0zEwag8vNU2' => $oauthTokenSecret, + 'EYQcXy3UQ2J00nfB2X2DUUv6b' => $consumerKey, + 'J3pWi6ju579jiPGs91YL8LMYL3ZjrNHhNWCgQeKdNkvdkwop6b' => $consumerSecret +); +$twitter = new TwitterAPIExchange($APIsettings); + +// Get Twitter config +$twitterConfigURL = 'https://api.twitter.com/1.1/help/configuration.json'; +$requestMethod = 'GET'; +$twitterConfig = $twitter->setGetfield('') + ->buildOauth($twitterConfigURL, $requestMethod) + ->performRequest(); +$twitterConfig = json_decode($twitterConfig); + +// Twitter Media Upload URL +$mediaUploadURL = 'https://upload.twitter.com/1.1/media/upload.json'; + +// Get number of total hits available for works with image and title +$requestURL = 'http://api.art.rmngp.fr:80/v1/works?exists=images%2Ctitle.fr&per_page=0'; +$result = getCURLOutput($requestURL, $RmnAPIKey); + +if(isset($result->hits)){ + $totalHits = $result->hits->total; + + // Get a random work with image and title + $randomPage = rand(1, $totalHits); + $requestURL = 'http://api.art.rmngp.fr:80/v1/works?exists=images%2Ctitle.fr&per_page=1&page=' . $randomPage; + $result = getCURLOutput($requestURL, $RmnAPIKey); + + if(isset($result->hits) && count($result->hits->hits) > 0){ + $work = $result->hits->hits[0]->_source; + + // Upload image of the work + foreach ($work->images as $image) { + if($image->default){ + $permalink = 'http://art.rmngp.fr/fr/library/artworks/' . $work->slug; + $imageContent = file_get_contents($image->urls->original); + $imageData = base64_encode($imageContent); + + $postfields = array('media_data' => $imageData); + $requestMethod = "POST"; + $response = $twitter->resetFields() + ->buildOauth($mediaUploadURL, $requestMethod) + ->setPostfields($postfields) + ->performRequest(); + $response = json_decode($response); + if(isset($response->media_id_string)) + $mediaId = $response->media_id_string; + } + } + if(!isset($mediaId)) + return false; + + // Build tweet + // Set max string length based on Twitter config + $maxLength = 140 - ($twitterConfig->short_url_length + 1); + // Work title + $tweet = $work->title->fr; + // Add first author if available and not too long, else add first authorship detail if available and not too long + if(isset($work->authors) && count($work->authors) > 0 && isset($work->authors[0]->name) && isset($work->authors[0]->name->fr) + && strlen($tweet . ", " . $work->authors[0]->name->fr) < $maxLength + 1){ + $tweet .= ", " . $work->authors[0]->name->fr; + }else if(isset($work->authorship_details) && count($work->authorship_details) > 0 && isset($work->authorship_details[0]->name) && isset($work->authorship_details[0]->name->fr) + && strlen($tweet . ", " . $work->authorship_details[0]->name->fr) < $maxLength + 1){ + $tweet .= ", " . $work->authorship_details[0]->name->fr; + } + // Add location if available and not too long + if(isset($work->location) && isset($work->location->suggest_fr) && isset($work->location->suggest_fr->output)){ + $location = $work->location->suggest_fr->output; + $museumMention = ""; + foreach ($museumToTwitter as $loc => $mention) { + if(stripos($location, $loc) !== FALSE) + $museumMention .= " @" . $mention; + } + if (strlen($museumMention) > 0 && strlen($tweet . $museumMention) < $maxLength + 1) + $tweet .= $museumMention; + else if(strlen($tweet . ", " . $location) < $maxLength + 1) + $tweet .= ", " . $location; + } + // Finalize tweet + if($tweet > $maxLength) + $tweet = substr($tweet, 0, $maxLength - 3) . "..."; + $tweet .= " " . $permalink; + + // Post tweet with media + $postfields = array( + 'status' => $tweet, + 'media_ids' => $mediaId); + $url = "https://api.twitter.com/1.1/statuses/update.json"; + $requestMethod = "POST"; + echo $twitter->resetFields() + ->buildOauth($url, $requestMethod) + ->setPostfields($postfields) + ->performRequest(); + + } +} + + ?> diff --git a/src/TwitterBot/LICENSE b/src/TwitterBot/LICENSE new file mode 100644 index 0000000000000000000000000000000000000000..2ebc8b2e103490b89d63730cb624f565c9c10b5e --- /dev/null +++ b/src/TwitterBot/LICENSE @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2015 Bilgé Kimyonok + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + diff --git a/src/TwitterBot/README.md b/src/TwitterBot/README.md new file mode 100644 index 0000000000000000000000000000000000000000..9415f25a64993316d0a8b40fe7b44c56f4fb37b3 --- /dev/null +++ b/src/TwitterBot/README.md @@ -0,0 +1,14 @@ +# Images d'Art Bot + +A Twitter bot that tweets images of art from the Images d'Art database + +The Twitter bot is [@ImagesDArtBot](https://twitter.com/ImagesDArtBot) + +## Use +It uses the [Images d'Art API](http://api.art.rmngp.fr/?locale=en). +Please remember to carefully read the [API Terms and Conditions of Use](http://api.art.rmngp.fr/cgu?locale=en). +Especially, remember to always specify the source and the photographic credit information when using images and data from the API. +In this case, the source for all images and metadata is RMN-Grand Palais as specified in the bot's biography, the photographer credit for every image is included in the tweet. + +## License +The source code of this bot is available under the terms of the [MIT license](http://www.opensource.org/licenses/mit-license.php). diff --git a/src/TwitterBot/TwitterAPIExchange.php b/src/TwitterBot/TwitterAPIExchange.php new file mode 100644 index 0000000000000000000000000000000000000000..a1e5c6fc0ddc626eb74cb520d79a209e3e3557fc --- /dev/null +++ b/src/TwitterBot/TwitterAPIExchange.php @@ -0,0 +1,386 @@ +<?php + +/** + * Twitter-API-PHP : Simple PHP wrapper for the v1.1 API + * + * PHP version 5.3.10 + * + * @category Awesomeness + * @package Twitter-API-PHP + * @author James Mallison <me@j7mbo.co.uk> + * @license MIT License + * @version 1.0.4 + * @link http://github.com/j7mbo/twitter-api-php + */ +class TwitterAPIExchange +{ + /** + * @var string + */ + private $oauth_access_token; + + /** + * @var string + */ + private $oauth_access_token_secret; + + /** + * @var string + */ + private $consumer_key; + + /** + * @var string + */ + private $consumer_secret; + + /** + * @var array + */ + private $postfields; + + /** + * @var string + */ + private $getfield; + + /** + * @var mixed + */ + protected $oauth; + + /** + * @var string + */ + public $url; + + /** + * @var string + */ + public $requestMethod; + + /** + * Create the API access object. Requires an array of settings:: + * oauth access token, oauth access token secret, consumer key, consumer secret + * These are all available by creating your own application on dev.twitter.com + * Requires the cURL library + * + * @throws \Exception When cURL isn't installed or incorrect settings parameters are provided + * + * @param array $settings + */ + public function __construct(array $settings) + { + if (!in_array('curl', get_loaded_extensions())) + { + throw new Exception('You need to install cURL, see: http://curl.haxx.se/docs/install.html'); + } + + if (!isset($settings['oauth_access_token']) + || !isset($settings['oauth_access_token_secret']) + || !isset($settings['consumer_key']) + || !isset($settings['consumer_secret'])) + { + throw new Exception('Make sure you are passing in the correct parameters'); + } + + $this->oauth_access_token = $settings['oauth_access_token']; + $this->oauth_access_token_secret = $settings['oauth_access_token_secret']; + $this->consumer_key = $settings['consumer_key']; + $this->consumer_secret = $settings['consumer_secret']; + } + + /** + * Set postfields array, example: array('screen_name' => 'J7mbo') + * + * @param array $array Array of parameters to send to API + * + * @throws \Exception When you are trying to set both get and post fields + * + * @return TwitterAPIExchange Instance of self for method chaining + */ + public function setPostfields(array $array) + { + if (!is_null($this->getGetfield())) + { + throw new Exception('You can only choose get OR post fields.'); + } + + if (isset($array['status']) && substr($array['status'], 0, 1) === '@') + { + $array['status'] = sprintf("\0%s", $array['status']); + } + + $this->postfields = $array; + + // rebuild oAuth + if (isset($this->oauth['oauth_signature'])) { + $this->buildOauth($this->url, $this->requestMethod); + } + + return $this; + } + + /** + * Set getfield string, example: '?screen_name=J7mbo' + * + * @param string $string Get key and value pairs as string + * + * @throws \Exception + * + * @return \TwitterAPIExchange Instance of self for method chaining + */ + public function setGetfield($string) + { + if (!is_null($this->getPostfields())) + { + throw new Exception('You can only choose get OR post fields.'); + } + + $getfields = preg_replace('/^\?/', '', explode('&', $string)); + $params = array(); + + foreach ($getfields as $field) + { + if ($field !== '') + { + list($key, $value) = explode('=', $field); + $params[$key] = $value; + } + } + + $this->getfield = '?' . http_build_query($params); + + return $this; + } + + /** + * Get getfield string (simple getter) + * + * @return string $this->getfields + */ + public function getGetfield() + { + return $this->getfield; + } + + /** + * Get postfields array (simple getter) + * + * @return array $this->postfields + */ + public function getPostfields() + { + return $this->postfields; + } + + /** + * Resets the fields to allow a new query + * with different method + */ + public function resetFields() { + $this->postfields = null; + $this->getfield = null; + return $this; + } + + /** + * Build the Oauth object using params set in construct and additionals + * passed to this method. For v1.1, see: https://dev.twitter.com/docs/api/1.1 + * + * @param string $url The API url to use. Example: https://api.twitter.com/1.1/search/tweets.json + * @param string $requestMethod Either POST or GET + * + * @throws \Exception + * + * @return \TwitterAPIExchange Instance of self for method chaining + */ + public function buildOauth($url, $requestMethod) + { + if (!in_array(strtolower($requestMethod), array('post', 'get'))) + { + throw new Exception('Request method must be either POST or GET'); + } + + $consumer_key = $this->consumer_key; + $consumer_secret = $this->consumer_secret; + $oauth_access_token = $this->oauth_access_token; + $oauth_access_token_secret = $this->oauth_access_token_secret; + + $oauth = array( + 'oauth_consumer_key' => $consumer_key, + 'oauth_nonce' => time(), + 'oauth_signature_method' => 'HMAC-SHA1', + 'oauth_token' => $oauth_access_token, + 'oauth_timestamp' => time(), + 'oauth_version' => '1.0' + ); + + $getfield = $this->getGetfield(); + + if (!is_null($getfield)) + { + $getfields = str_replace('?', '', explode('&', $getfield)); + + foreach ($getfields as $g) + { + $split = explode('=', $g); + + /** In case a null is passed through **/ + if (isset($split[1])) + { + $oauth[$split[0]] = urldecode($split[1]); + } + } + } + + $postfields = $this->getPostfields(); + + if (!is_null($postfields)) { + foreach ($postfields as $key => $value) { + $oauth[$key] = $value; + } + } + + $base_info = $this->buildBaseString($url, $requestMethod, $oauth); + $composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret); + $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true)); + $oauth['oauth_signature'] = $oauth_signature; + + $this->url = $url; + $this->requestMethod = $requestMethod; + $this->oauth = $oauth; + + return $this; + } + + /** + * Perform the actual data retrieval from the API + * + * @param boolean $return If true, returns data. This is left in for backward compatibility reasons + * @param array $curlOptions Additional Curl options for this request + * + * @throws \Exception + * + * @return string json If $return param is true, returns json data. + */ + public function performRequest($return = true, $curlOptions = array()) + { + if (!is_bool($return)) + { + throw new Exception('performRequest parameter must be true or false'); + } + + $header = array($this->buildAuthorizationHeader($this->oauth), 'Expect:'); + + $getfield = $this->getGetfield(); + $postfields = $this->getPostfields(); + + $options = array( + CURLOPT_HTTPHEADER => $header, + CURLOPT_HEADER => false, + CURLOPT_URL => $this->url, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_TIMEOUT => 10, + ) + $curlOptions; + + if (!is_null($postfields)) + { + $options[CURLOPT_POSTFIELDS] = http_build_query($postfields); + } + else + { + if ($getfield !== '') + { + $options[CURLOPT_URL] .= $getfield; + } + } + + $feed = curl_init(); + curl_setopt_array($feed, $options); + $json = curl_exec($feed); + + if (($error = curl_error($feed)) !== '') + { + curl_close($feed); + + throw new \Exception($error); + } + + curl_close($feed); + + return $json; + } + + /** + * Private method to generate the base string used by cURL + * + * @param string $baseURI + * @param string $method + * @param array $params + * + * @return string Built base string + */ + private function buildBaseString($baseURI, $method, $params) + { + $return = array(); + ksort($params); + + foreach($params as $key => $value) + { + $return[] = rawurlencode($key) . '=' . rawurlencode($value); + } + + return $method . "&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $return)); + } + + /** + * Private method to generate authorization header used by cURL + * + * @param array $oauth Array of oauth data generated by buildOauth() + * + * @return string $return Header used by cURL for request + */ + private function buildAuthorizationHeader(array $oauth) + { + $return = 'Authorization: OAuth '; + $values = array(); + + foreach($oauth as $key => $value) + { + if (in_array($key, array('oauth_consumer_key', 'oauth_nonce', 'oauth_signature', + 'oauth_signature_method', 'oauth_timestamp', 'oauth_token', 'oauth_version'))) { + $values[] = "$key=\"" . rawurlencode($value) . "\""; + } + } + + $return .= implode(', ', $values); + return $return; + } + + /** + * Helper method to perform our request + * + * @param string $url + * @param string $method + * @param string $data + * @param array $curlOptions + * + * @throws \Exception + * + * @return string The json response from the server + */ + public function request($url, $method = 'get', $data = null, $curlOptions = array()) + { + if (strtolower($method) === 'get') + { + $this->setGetfield($data); + } + else + { + $this->setPostfields($data); + } + + return $this->buildOauth($url, $method)->performRequest(true, $curlOptions); + } +} diff --git a/src/TwitterBot/museumToTwitter.php b/src/TwitterBot/museumToTwitter.php new file mode 100644 index 0000000000000000000000000000000000000000..7a89aa1ae35ec98a113c1c1b4b72e80ef2b7d39d --- /dev/null +++ b/src/TwitterBot/museumToTwitter.php @@ -0,0 +1,5 @@ +<?php + +$museumToTwitter = array("Louvre" => "MuseeLouvre", "Orsay" => "MuseeOrsay", "Pompidou" => "CentrePompidou", "BNF" => "laBnF", "INHA" => "INHA_Fr", "Cluny" => "museecluny", "ENSBA" => "beauxartsparis", "Guimet" => "MuseeGuimet", "Musée de l’Armée" => "MuseeArmee", "Henner" => "MuseeHenner", "Musée Condé" => "chantillydomain", "Judaïsme" => "infosdumahj", "MNHN" => "Le_Museum", "Fontainebleau, château" => "CFontainebleau", "Château de Compiègne" => "palaisimperial", "Compiègne, château" => "palaisimperial", "Branly" => "quaibranly", "Sèvres" => "sevresceramique", "châteaux de Malmaison" => "museemalmaison", "Archéologie Nationale" => "Archeonationale", "Carnavalet" => "museecarnavalet", "Paris, musée Picasso" => "MuseePicasso", "Abbeville" => "AbbevilleMusee", "Fesch" => "PalaisFesch", "Angers, musée des Beaux-Arts" => "Museesdangers", "Baron-Gérard" => "BayeuxMuseum", "MUDO" => "MUDOOise ", "Unterlinden" => "MUnterlinden", "Vix" => "TresordeVix", "Magnin" => "MagninMusee", "Eyzies" => "Les_Eyzies", "Palais des Beaux-Arts" => "PBALille", "Méditerranée" => "Mucem", "Nantes, Musée des Beaux-Arts" => "MuseeArtsNantes", "Rennes, Musée des Beaux-Arts" => "mbar_officiel", "Maurice Denis" => "MuseeMDenis", "de Versailles et de Trianon" => "CVersailles", "national de la Renaissance" => "chateau_ecouen", "Gustave Moreau" => "MuseeMoreau"); + +?> \ No newline at end of file