|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace seregazhuk\PinterestBot\Helpers\Providers; |
|
4
|
|
|
|
|
5
|
|
|
use seregazhuk\PinterestBot\Exceptions\AuthException; |
|
6
|
|
|
|
|
7
|
|
|
class PinnerHelper extends RequestHelper |
|
8
|
|
|
{ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Creates Pinterest API request to get user info according to |
|
12
|
|
|
* username, API url and bookmarks for pagination |
|
13
|
|
|
* |
|
14
|
|
|
* @param string $username |
|
15
|
|
|
* @param string $sourceUrl |
|
16
|
|
|
* @param array $bookmarks |
|
17
|
|
|
* @return array |
|
18
|
|
|
*/ |
|
19
|
|
|
public static function createUserDataRequest($username, $sourceUrl, $bookmarks) |
|
20
|
|
|
{ |
|
21
|
|
|
$dataJson = self::createPinnerRequestData($username); |
|
22
|
|
|
|
|
23
|
|
|
return self::createRequestData($dataJson, $sourceUrl, $bookmarks); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Parses Pinterest API response with pinner name |
|
28
|
|
|
* |
|
29
|
|
|
* @param $res |
|
30
|
|
|
* @return null |
|
31
|
|
|
*/ |
|
32
|
|
|
public static function parseAccountNameResponse($res) |
|
33
|
|
|
{ |
|
34
|
|
|
if (isset($res['resource_data_cache'][1]['resource']['options']['username'])) { |
|
35
|
|
|
return $res['resource_data_cache'][1]['resource']['options']['username']; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return null; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Creates Pinterest API request to login |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $username |
|
45
|
|
|
* @param string $password |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function createLoginRequest($username, $password) |
|
49
|
|
|
{ |
|
50
|
|
|
$dataJson = [ |
|
51
|
|
|
"options" => [ |
|
52
|
|
|
"username_or_email" => $username, |
|
53
|
|
|
"password" => $password |
|
54
|
|
|
], |
|
55
|
|
|
"context" => new \stdClass(), |
|
56
|
|
|
]; |
|
57
|
|
|
return self::createRequestData($dataJson, "/login/"); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Parses Pintrest Api response after login |
|
62
|
|
|
* |
|
63
|
|
|
* @param array $res |
|
64
|
|
|
* @return bool |
|
65
|
|
|
* @throws AuthException |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function parseLoginResponse($res) |
|
68
|
|
|
{ |
|
69
|
|
|
if (self::checkMethodCallResult($res)) { |
|
70
|
|
|
throw new AuthException($res['resource_response']['error']['message']); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Creates common Pinner request data by username |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $username |
|
80
|
|
|
* @return array |
|
81
|
|
|
*/ |
|
82
|
|
|
protected static function createPinnerRequestData($username) |
|
83
|
|
|
{ |
|
84
|
|
|
return [ |
|
85
|
|
|
"options" => [ |
|
86
|
|
|
"username" => $username, |
|
87
|
|
|
], |
|
88
|
|
|
"context" => new \stdClass(), |
|
89
|
|
|
]; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|