Skip to content

Commit

Permalink
new: basic support for pinterest
Browse files Browse the repository at this point in the history
  • Loading branch information
Marvin Oßwald committed Jul 19, 2016
1 parent 990cf4b commit 510cdd0
Show file tree
Hide file tree
Showing 10 changed files with 290 additions and 16 deletions.
11 changes: 11 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
51 changes: 49 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/Drivers/Facebook/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/**
* Class FacebookDriver
* @package Marvinosswald\Socialmedia\Drivers
* @package Marvinosswald\Socialmedia\Drivers\Facebook
*/
class Driver implements DriverInterface
{
Expand Down
66 changes: 66 additions & 0 deletions src/Drivers/Pinterest/Driver.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php
namespace marvinosswald\Socialmedia\Drivers\Pinterest;

use Abraham\TwitterOAuth\TwitterOAuth;
use DirkGroenen\Pinterest\Pinterest;
use marvinosswald\Socialmedia\Contracts\DriverInterface;

/**
* Class PinterestDriver
* @package Marvinosswald\Socialmedia\Drivers\Pinterest
*/
class Driver implements DriverInterface
{
/**
* @var string
*/
public $appId = '';
/**
* @var string
*/
public $appSecret = '';
/**
* @var string
*/
public $accessToken = '';
/**
* @var
*/
public $pinterest;
/**
* @var string
*/
public $defaultBoard = '';

/**
* Driver constructor.
* @param bool $appId
* @param bool $appSecret
*/
public function __construct($appId = false, $appSecret = false, $accessToken = false, $defaultBoard = '')
{
$this->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();
}
}
114 changes: 114 additions & 0 deletions src/Drivers/Pinterest/Post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
namespace marvinosswald\Socialmedia\Drivers\Pinterest;

use Abraham\TwitterOAuth\TwitterOAuth;
use DirkGroenen\Pinterest\Pinterest;
use marvinosswald\Socialmedia\Contracts\PostInterface;

/**
* Class Post
* @package Marvinosswald\Socialmedia\Drivers\Pinterest
*/
class Post implements PostInterface{
/**
* @var string|int
*/
public $id;
/**
* The main body of the post, otherwise called the status message. Either link, place, or message must be supplied.
*
* @var string
*/
public $message = '';

/**
* The URL of a link to attach to the post. Either link, place, or message must be supplied. Additional fields associated with link are shown below.
*
* @var string
*/
public $link = '';
/**
* Determines the preview image associated with the link.
*
* @var string
*/
public $picture = '';
/**
* Board reference like <username>/<boardname>
*
* @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
]);
}
}
2 changes: 1 addition & 1 deletion src/Drivers/Twitter/Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/**
* Class FacebookDriver
* @package Marvinosswald\Socialmedia\Drivers
* @package Marvinosswald\Socialmedia\Drivers\Twitter
*/
class Driver implements DriverInterface
{
Expand Down
11 changes: 1 addition & 10 deletions src/Drivers/Twitter/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
]);
}
}
3 changes: 2 additions & 1 deletion src/Socialmedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
];

/**
Expand Down
43 changes: 43 additions & 0 deletions tests/PinterestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

use \marvinosswald\Socialmedia\Facades\Socialmedia;

class PinterestTest extends Orchestra\Testbench\TestCase {
protected function getPackageProviders($app)
{
return ['marvinosswald\Socialmedia\Providers\SocialmediaServiceProvider'];
}

protected function getPackageAliases($app)
{
return [
'Socialmedia' => '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']);
}
}

0 comments on commit 510cdd0

Please sign in to comment.