|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Borfast\Socializr\Connectors; |
|
4
|
|
|
|
|
5
|
|
|
use Borfast\Socializr\Blog; |
|
6
|
|
|
use Borfast\Socializr\Exceptions\TumblrPostingException; |
|
7
|
|
|
use Borfast\Socializr\Post; |
|
8
|
|
|
use Borfast\Socializr\Response; |
|
9
|
|
|
|
|
10
|
|
|
class TumblrBlog extends Tumblr |
|
11
|
|
|
{ |
|
12
|
|
|
public static $provider = 'Tumblr'; |
|
13
|
|
|
|
|
14
|
|
|
protected $base_hostname; |
|
15
|
|
|
|
|
16
|
|
|
public function request($path, $method = 'GET', $params = [], $headers = []) |
|
17
|
|
|
{ |
|
18
|
|
|
$result = parent::request($path, $method, $params, $headers); |
|
19
|
|
|
|
|
20
|
|
|
$json_result = json_decode($result); |
|
21
|
|
|
|
|
22
|
|
|
$status = $json_result->meta->status; |
|
23
|
|
|
|
|
24
|
|
|
if ($status < 200 || $status > 299) { |
|
25
|
|
|
$msg = $json_result->meta->msg; |
|
26
|
|
|
|
|
27
|
|
|
if ($status == 400) { |
|
28
|
|
|
$media_error_messages = [ |
|
29
|
|
|
"Error uploading photo.", |
|
30
|
|
|
"Nice image, but we don't support that format. Try resaving it as a gif, jpg, or png.", |
|
31
|
|
|
]; |
|
32
|
|
|
|
|
33
|
|
|
foreach ($media_error_messages as $media_error_message) { |
|
34
|
|
|
if (strpos($json_result->response->errors[0], $media_error_message) !== false) { |
|
35
|
|
|
$msg .= ': ' . $media_error_message; |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
throw new TumblrPostingException($msg, $status); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $result; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
public function post(Post $post) |
|
48
|
|
|
{ |
|
49
|
|
|
$path = 'blog/'.$this->options['base_hostname'].'/post'; |
|
50
|
|
|
$method = 'POST'; |
|
51
|
|
|
|
|
52
|
|
|
$params = []; |
|
53
|
|
|
if (!empty($post->tags)) { |
|
54
|
|
|
$params['tags'] = $post->tags; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
if (empty($post->media)) { |
|
59
|
|
|
$body = '<p>' . $post->body . '</p>'; |
|
60
|
|
|
$body .= '<strong>' . $post->url . '</strong>'; |
|
61
|
|
|
|
|
62
|
|
|
$params['type'] = 'text'; |
|
63
|
|
|
$params['title'] = $post->title; |
|
64
|
|
|
$params['body'] = $body; |
|
65
|
|
|
} else { |
|
66
|
|
|
$caption = '<h2>' . $post->title . '</h2>'; |
|
67
|
|
|
$caption .= '<p>' . $post->body . '</p>'; |
|
68
|
|
|
$caption .= '<strong>' . $post->url . '</strong>'; |
|
69
|
|
|
|
|
70
|
|
|
$params['type'] = 'photo'; |
|
71
|
|
|
$params['caption'] = $caption; |
|
72
|
|
|
$params['source'] = $post->media[0]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$result = $this->request($path, $method, $params); |
|
76
|
|
|
|
|
77
|
|
|
$response = new Response; |
|
78
|
|
|
$response->setRawResponse(json_encode($result)); |
|
79
|
|
|
$result_json = json_decode($result); |
|
80
|
|
|
$response->setProvider('Tumblr'); |
|
81
|
|
|
$response->setPostId($result_json->response->id); |
|
82
|
|
|
|
|
83
|
|
|
return $response; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function getBlog() |
|
87
|
|
|
{ |
|
88
|
|
|
$api_key = $this->config['consumer_key']; |
|
89
|
|
|
$path = 'blog/'.$this->options['base_hostname'].'/info?api_key='.$api_key; |
|
90
|
|
|
$result = $this->request($path); |
|
91
|
|
|
$json_result = json_decode($result, true); |
|
92
|
|
|
|
|
93
|
|
|
$mapping = [ |
|
94
|
|
|
'id' => 'name', |
|
95
|
|
|
'link' => 'url', |
|
96
|
|
|
'title' => 'title', |
|
97
|
|
|
'name' => 'name', |
|
98
|
|
|
'description' => 'description', |
|
99
|
|
|
'ask' => 'ask', |
|
100
|
|
|
'ask_anon' => 'ask_anon', |
|
101
|
|
|
'followers' => 'followers' |
|
102
|
|
|
]; |
|
103
|
|
|
|
|
104
|
|
|
$blog = Blog::create($mapping, $json_result['response']['blog']); |
|
105
|
|
|
|
|
106
|
|
|
return $blog; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
public function getPermissions() |
|
111
|
|
|
{ |
|
112
|
|
|
return null; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
public function getStats() |
|
116
|
|
|
{ |
|
117
|
|
|
$profile = $this->getBlog(); |
|
118
|
|
|
|
|
119
|
|
|
return $profile->followers; |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|