From 510cdd017592f3a2779640966954f556237a7edc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marvin=20O=C3=9Fwald?= Date: Tue, 19 Jul 2016 16:46:29 +0200 Subject: [PATCH] new: basic support for pinterest --- Readme.md | 11 +++ composer.json | 3 +- composer.lock | 51 +++++++++++++- src/Drivers/Facebook/Driver.php | 2 +- src/Drivers/Pinterest/Driver.php | 66 ++++++++++++++++++ src/Drivers/Pinterest/Post.php | 114 +++++++++++++++++++++++++++++++ src/Drivers/Twitter/Driver.php | 2 +- src/Drivers/Twitter/Post.php | 11 +-- src/Socialmedia.php | 3 +- tests/PinterestTest.php | 43 ++++++++++++ 10 files changed, 290 insertions(+), 16 deletions(-) create mode 100644 src/Drivers/Pinterest/Driver.php create mode 100644 src/Drivers/Pinterest/Post.php create mode 100644 tests/PinterestTest.php diff --git a/Readme.md b/Readme.md index eafe4ff..ef0c5ad 100644 --- a/Readme.md +++ b/Readme.md @@ -57,6 +57,17 @@ Obtain permanent Access Token: https://dev.twitter.com/oauth/overview/applicatio [Api Documentation](https://developers.pinterest.com/docs/api/pins/) +How to get an access token: +- [Create Developer Account here](https://developers.pinterest.com) +- Create App and get app_id & app_secret from app details page +- [Issue new token](https://developers.pinterest.com/tools/access_token/) + +#### Settings +##### ENV Variables +- PINTEREST_APP_ID +- PINTEREST_APP_SECRET +- PINTEREST_ACCESS_TOKEN + ### Instagram [Hacky api endpoint](http://stackoverflow.com/a/26186389) diff --git a/composer.json b/composer.json index b6e2c48..7cf7845 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "minimum-stability": "dev", "require": { "facebook/php-sdk-v4": "^5.2", - "abraham/twitteroauth": "^0.6.4" + "abraham/twitteroauth": "^0.6.4", + "dirkgroenen/Pinterest-API-PHP": "^0.2.11" }, "require-dev": { "orchestra/testbench": "~3.2", diff --git a/composer.lock b/composer.lock index 714ee06..4d72675 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,8 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "be04f4f08f8bc127730d4d718937caf6", - "content-hash": "030e17f2488796eb496af24f21f5964f", + "hash": "f1b70d7e0617fe0b94b2c651992cf824", + "content-hash": "cfb9f7f5134db0ce34926e62a43f7c68", "packages": [ { "name": "abraham/twitteroauth", @@ -61,6 +61,53 @@ ], "time": "2016-04-20 23:23:26" }, + { + "name": "dirkgroenen/pinterest-api-php", + "version": "0.2.11", + "source": { + "type": "git", + "url": "https://github.com/dirkgroenen/Pinterest-API-PHP.git", + "reference": "afd5e531059c09774e958e142b859d201aa00c79" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/dirkgroenen/Pinterest-API-PHP/zipball/afd5e531059c09774e958e142b859d201aa00c79", + "reference": "afd5e531059c09774e958e142b859d201aa00c79", + "shasum": "" + }, + "require": { + "php": ">=5.4" + }, + "require-dev": { + "phpunit/phpunit": "4.7.*", + "vlucas/phpdotenv": "^2.2" + }, + "type": "library", + "autoload": { + "psr-4": { + "DirkGroenen\\Pinterest\\": "src/Pinterest" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "Apache-2.0" + ], + "authors": [ + { + "name": "Dirk Groenen", + "email": "dirk@bitlabs.nl", + "homepage": "http://dirkgroenen.nl", + "role": "Creator/Developer" + } + ], + "description": "PHP Wrapper for the official Pinterest API", + "keywords": [ + "api", + "pinterest", + "wrapper" + ], + "time": "2016-05-10 15:36:53" + }, { "name": "facebook/php-sdk-v4", "version": "dev-master", diff --git a/src/Drivers/Facebook/Driver.php b/src/Drivers/Facebook/Driver.php index e9e13f1..2214193 100644 --- a/src/Drivers/Facebook/Driver.php +++ b/src/Drivers/Facebook/Driver.php @@ -6,7 +6,7 @@ /** * Class FacebookDriver - * @package Marvinosswald\Socialmedia\Drivers + * @package Marvinosswald\Socialmedia\Drivers\Facebook */ class Driver implements DriverInterface { diff --git a/src/Drivers/Pinterest/Driver.php b/src/Drivers/Pinterest/Driver.php new file mode 100644 index 0000000..2dd0d7f --- /dev/null +++ b/src/Drivers/Pinterest/Driver.php @@ -0,0 +1,66 @@ +appId = $appId ?: env('PINTEREST_APP_ID'); + $this->appSecret = $appSecret ?: env('PINTEREST_APP_SECRET'); + $this->accessToken = $accessToken ?: env('PINTEREST_ACCESS_TOKEN'); + $this->defaultBoard = $defaultBoard ?: env('PINTEREST_DEFAULT_BOARD'); + + $this->pinterest = new Pinterest($this->appId,$this->appSecret); + $this->pinterest->auth->setOAuthToken($this->accessToken); + } + + /** + * @param $params + * @return string + */ + public function post($params) + { + $post = new Post($this->pinterest,$params); + return $post->exec(); + } + + public function delete($id) + { + $post = Post::withId($this->pinterest,$id); + return $post->delete(); + } +} \ No newline at end of file diff --git a/src/Drivers/Pinterest/Post.php b/src/Drivers/Pinterest/Post.php new file mode 100644 index 0000000..81616d6 --- /dev/null +++ b/src/Drivers/Pinterest/Post.php @@ -0,0 +1,114 @@ +/ + * + * @var string + */ + public $board = ''; + /** + * @var Pinterest + */ + protected $pinterest; + + /** + * Post constructor. + * @param $pinterest + * @param array $params + */ + public function __construct($pinterest,array $params=[]) + { + $this->pinterest = $pinterest; + if (!empty($params)){ + foreach ($params as $key => $value){ + if (gettype($value) == gettype($this->{$key})){ + $this->{$key} = $value; + } + } + } + } + + /** + * @param $driver + * @param $id + * @param array $params + * @return Post + */ + public static function withId($driver,$id,array $params=[]) + { + $i = new self($driver,$params); + $i->id = $id; + return $i; + } + /** + * @return string + */ + public function exec() + { + try{ + return $this->pinterest->pins->create($this->toArray()); + }catch (Exception $e){ + return $e->getMessage(); + } + + } + + /** + * @return array|object|string + */ + public function delete() + { + if($this->id){ + try{ + $post = $this->pinterest->pins->delete($this->id); + return $post; + }catch (Exception $e){ + return $e->getMessage(); + } + }else{ + return "Post id not set"; + } + } + public function toArray() + { + return array_filter([ + 'note' => $this->message, + 'link' => $this->link, + base64_decode($this->picture,true) ? 'image_base64': 'image_url' => $this->picture, + 'board' => $this->board ?: $this->pinterest->defaultBoard + ]); + } +} \ No newline at end of file diff --git a/src/Drivers/Twitter/Driver.php b/src/Drivers/Twitter/Driver.php index 6c9a603..d3a5bd4 100644 --- a/src/Drivers/Twitter/Driver.php +++ b/src/Drivers/Twitter/Driver.php @@ -6,7 +6,7 @@ /** * Class FacebookDriver - * @package Marvinosswald\Socialmedia\Drivers + * @package Marvinosswald\Socialmedia\Drivers\Twitter */ class Driver implements DriverInterface { diff --git a/src/Drivers/Twitter/Post.php b/src/Drivers/Twitter/Post.php index 38e89f1..df07029 100644 --- a/src/Drivers/Twitter/Post.php +++ b/src/Drivers/Twitter/Post.php @@ -151,16 +151,7 @@ public function delete() public function toArray() { return array_filter([ - 'status' => $this->message, - 'link' => $this->link, - 'picture' => $this->picture, - 'name' => $this->name, - 'caption' => $this->caption, - 'description' => $this->description, - 'place' => $this->place, - 'tags' => $this->tags, - 'privacy' => array_filter($this->privacy), - 'targeting' => array_filter($this->targeting) + 'status' => $this->message ]); } } \ No newline at end of file diff --git a/src/Socialmedia.php b/src/Socialmedia.php index 39f4ddf..9c3d120 100644 --- a/src/Socialmedia.php +++ b/src/Socialmedia.php @@ -15,7 +15,8 @@ class Socialmedia */ protected $drivers = [ 'facebook' => 'marvinosswald\Socialmedia\Drivers\Facebook\Driver', - 'twitter' => 'marvinosswald\Socialmedia\Drivers\Twitter\Driver' + 'twitter' => 'marvinosswald\Socialmedia\Drivers\Twitter\Driver', + 'pinterest' => 'marvinosswald\Socialmedia\Drivers\Pinterest\Driver' ]; /** diff --git a/tests/PinterestTest.php b/tests/PinterestTest.php new file mode 100644 index 0000000..5026d57 --- /dev/null +++ b/tests/PinterestTest.php @@ -0,0 +1,43 @@ + 'marvinosswald\Socialmedia\Facades\Socialmedia' + ]; + } + + /** + * Define environment setup. + * + * @param \Illuminate\Foundation\Application $app + * @return void + */ + protected function getEnvironmentSetUp($app) + { + if (file_exists (__DIR__."/../.env")){ + $dotenv = new Dotenv\Dotenv(__DIR__."/../"); + $dotenv->load(); + } + } + + public function testPinterestPost() + { + $params = [ + 'message' => 'Test Pin', + 'link' => 'http://google.com', + 'picture' => 'http://placehold.it/300/400', + 'board' => 'marivnosswald/reiseziele' + ]; + $post = Socialmedia::post($params,['pinterest']); + Socialmedia::delete($post['pinterest']->id,['pinterest']); + } +} \ No newline at end of file