Skip to content
This repository has been archived by the owner on Dec 30, 2020. It is now read-only.

Commit

Permalink
More tests, Request refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
seregazhuk committed Aug 16, 2015
1 parent a578571 commit 2fc9f05
Show file tree
Hide file tree
Showing 9 changed files with 174 additions and 56 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ index.php
.idea
composer.lock
composer.phar
log
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ php composer.phar require "szhuk/pinterestapi:*"

## Quick Start

```php
```php
use Pinterest\ApiRequest;
use Pinterest\PinterestBot;

$api = new ApiRequest();
$api->useragent = "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36";
// pass useragent string to request object
$api = new ApiRequest("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36");
$bot = new PinterestBot('mypinterestlogin', 'mypinterestpassword', $api);
$bot->login();
```
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"ext-curl": "*"
},
"require-dev": {
"phpunit/phpunit": "*"
"phpunit/phpunit": "*",
"mikey179/vfsStream": "*"
},
"authors": [
{
Expand Down
15 changes: 11 additions & 4 deletions src/ApiRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,21 @@ class ApiRequest implements ApiInterface
protected $csrfToken = "";
protected $loggedIn;

public $useragent = 'Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0';
protected $useragent;

const COOKIE_NAME = 'pinterest_cookie';

public function __construct()
/**
* @param string $useragent
* @param null|string $cookiePath
*/
public function __construct(
$useragent = 'Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0',
$cookiePath = null
)
{
$this->useragent = $useragent;
$this->cookiePath = $cookiePath;
$this->cookieJarInit();
}

Expand Down Expand Up @@ -161,7 +170,6 @@ protected function cookieJarInit()
{

if (isset($this->cookiePath)) {

// If the given cookie path exists, then let's assume
// we're already logged in
$this->cookieJar = $this->cookiePath;
Expand All @@ -175,7 +183,6 @@ protected function cookieJarInit()
} else {
$this->cookieJar = tempnam(sys_get_temp_dir(), self::COOKIE_NAME);
}

}

/**
Expand Down
40 changes: 11 additions & 29 deletions src/PinterestBot.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@

/**
* Class PinterestBot
*
*@package Pinterest
* @package Pinterest
* @property string $username
* @property string $password
* @property ApiInterface $api
Expand Down Expand Up @@ -367,10 +366,6 @@ public function getUserData($username, $url, $sourceUrl, $bookmarks = [])
{
$this->checkLoggedIn();

if ( ! $username) {
$username = $this->username;
}

$dataJson = [
"options" => [
"username" => $username,
Expand All @@ -394,9 +389,7 @@ public function getUserData($username, $url, $sourceUrl, $bookmarks = [])
);


if ($this->checkErrorInResponse($res)) {
return [];
}
$this->checkErrorInResponse($res);

if ($res === null) {
return [];
Expand All @@ -423,18 +416,16 @@ public function getUserData($username, $url, $sourceUrl, $bookmarks = [])
* it.
*
* @param array $response
* @return bool
*/
protected function checkErrorInResponse($response)
public function checkErrorInResponse($response)
{
$this->lastApiErrorCode = null;
$this->lastApiErrorMsg = null;

if (isset($response['api_error_code'])) {
$this->lastApiErrorCode = $response['api_error_code'];
$this->lastApiErrorMsg = $response['message'];

return true;
}

return false;
}

/**
Expand Down Expand Up @@ -488,9 +479,7 @@ public function pin($imageUrl, $boardId, $description = "", $imagePreview = "")
$postString = UrlHelper::buildRequestString($post);
$res = $this->api->exec(self::URL_CREATE_PIN, self::URL_BASE, $postString);

if ($this->checkErrorInResponse($res)) {
return false;
}
$this->checkErrorInResponse($res);

if (isset($res['resource_response']['data']['id'])) {
return $res['resource_response']['data']['id'];
Expand Down Expand Up @@ -529,10 +518,7 @@ public function repin($repinId, $boardId, $description)

$postString = UrlHelper::buildRequestString($post);
$res = $this->api->exec(self::URL_REPIN, self::URL_BASE, $postString);

if ($this->checkErrorInResponse($res)) {
return false;
}
$this->checkErrorInResponse($res);

if (isset($res['resource_response']['data']['id'])) {
return $res['resource_response']['data']['id'];
Expand All @@ -558,7 +544,6 @@ public function deletePin($pinId)
"context" => new \stdClass(),
];

// And prepare the post data array
$post = [
"source_url" => "/pin/{$pinId}/",
"data" => json_encode($dataJson, JSON_FORCE_OBJECT),
Expand All @@ -567,11 +552,7 @@ public function deletePin($pinId)
$postString = UrlHelper::buildRequestString($post);
$res = $this->api->exec(self::URL_DELETE_PIN, self::URL_BASE, $postString);

if ($this->checkErrorInResponse($res)) {
return false;
}


$this->checkErrorInResponse($res);
if ($res) {
return true;
}
Expand All @@ -598,6 +579,7 @@ protected function getPaginatedData($function, $params, $batchesLimit = 0)
break;
}


$items = [];
$res = call_user_func_array([$this, $function], $params);

Expand Down Expand Up @@ -770,7 +752,7 @@ public function search($query, $scope, $bookmarks = [])
* @param int $batchesLimit
* @return \Generator
*/
public function searchPinners($query, $batchesLimit)
public function searchPinners($query, $batchesLimit = 0)
{
return $this->getPaginatedData('search',
['query' => $query, 'scope' => self::SEARCH_PEOPLE_SCOPES],
Expand Down
17 changes: 16 additions & 1 deletion tests/ApiRequestTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use Pinterest\ApiRequest;
use org\bovigo\vfs\vfsStream;

class ApiRequestTest extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -74,7 +75,7 @@ public function testCommonCurlOptions()
$requestOptions = $this->getProperty('options');
$this->assertArrayHasKey(CURLOPT_POST, $requestOptions);

$this->apiRequest->setCurlOptions($referer, $postString, $headers, true);
$this->apiRequest->setCurlOptions($referer, $postString, $headers, true, false);
$requestOptions = $this->getProperty('options');

$this->assertArraySubset([9 => 'X-CSRFToken: '], $requestOptions[CURLOPT_HTTPHEADER]);
Expand All @@ -87,4 +88,18 @@ public function testLoggedIn()
$this->assertEquals(true, $this->apiRequest->isLoggedIn());
}

public function testCookieJarInit()
{
vfsStream::setup();
$cookiePath = vfsStream::url('root/path_to_cookies.txt');
touch($cookiePath);

$api = new ApiRequest('My UserAgent String', $cookiePath);
$this->assertNotNull($cookiePath, $api->getCookieJar());
}

public function testExec()
{

}
}
Loading

0 comments on commit 2fc9f05

Please sign in to comment.