-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblogger_upload.php
259 lines (219 loc) · 8.19 KB
/
blogger_upload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php
/**
* To install required packages, run these commands in your terminal:
*
* composer require guzzlehttp/guzzle
* composer require aws/aws-sdk-php
* composer require google/apiclient
*/
session_start(); // For OAuth session handling
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client as HttpClient;
use Aws\S3\S3Client;
use Aws\Exception\AwsException;
use Google_Client;
use Google_Service_Blogger;
use Google_Service_Blogger_Post;
/* ------------------------------------------------------
A) SETTINGS
------------------------------------------------------ */
/**
* Pexels API key for fetching images
*/
define('PEXELS_API_KEY', 'YOUR_PEXELS_API_KEY');
/**
* AWS S3 credentials
*/
define('AWS_ACCESS_KEY', 'YOUR_AWS_ACCESS_KEY');
define('AWS_SECRET_KEY', 'YOUR_AWS_SECRET_KEY');
define('AWS_REGION', 'eu-central-1'); // Change to your region (e.g., us-east-1)
define('AWS_BUCKET_NAME', 'your-s3-bucket-name');
/**
* Blogger API credentials:
* The JSON file you downloaded from Google Cloud Console
*/
define('GOOGLE_CLIENT_SECRET_PATH', __DIR__ . '/client_secret.json');
/**
* The file that will store your token after Google OAuth
*/
define('GOOGLE_TOKEN_PATH', __DIR__ . '/token.json');
/**
* The numeric ID of your Blogger blog (e.g. 1234567890123456789)
*/
define('BLOG_ID', '1234567890123456789');
/**
* Example blog post title and content (in Turkish here, but can be any language).
* Make sure the content is at least 500 words if needed.
*/
$blogTitle = "Television Hakkında Her Şey";
$blogContent = <<<EOT
Televizyon (television), icadıyla birlikte kitle iletişim araçları arasında en önemli konuma yerleşen bir aygıttır.
Bu cihazların kökeni 19. yüzyıla kadar uzanmaktadır. İlk zamanlarda ...
(Paragraflarınız burada devam eder, en az 500 kelime olduğunu varsayıyoruz.)
Daha fazla bilgi için...
EOT;
/**
* The keyword for searching images on Pexels (or any other stock service)
*/
$keyword = "television";
/* ------------------------------------------------------
B) FETCH 2 IMAGE LINKS FROM PEXELS API
------------------------------------------------------ */
function getPexelsImages($keyword, $count = 2)
{
// Pexels API documentation: https://www.pexels.com/api/
$client = new HttpClient([
'base_uri' => 'https://api.pexels.com/v1/',
'headers' => [
'Authorization' => PEXELS_API_KEY
]
]);
$response = $client->get('search', [
'query' => [
'query' => $keyword,
'per_page' => $count
]
]);
$data = json_decode($response->getBody()->getContents(), true);
$imageUrls = [];
// Extract URLs from the returned JSON structure
if (!empty($data['photos'])) {
foreach ($data['photos'] as $photo) {
// We can choose 'large', 'original', or other sizes provided by Pexels
$imageUrls[] = $photo['src']['large'];
}
}
return $imageUrls;
}
/* ------------------------------------------------------
C) UPLOAD THE IMAGES TO S3 AND RETURN THE URL
------------------------------------------------------ */
function uploadImageToS3($imageUrl)
{
// Download the image temporarily into memory
$imageData = file_get_contents($imageUrl);
if (!$imageData) {
throw new Exception("Failed to download image: " . $imageUrl);
}
// Attempt to detect the file extension
$extension = pathinfo($imageUrl, PATHINFO_EXTENSION);
if (empty($extension)) {
$extension = 'jpg'; // default to jpg if unknown
}
// Create a unique file name in S3
$fileName = 'images/' . uniqid('img_') . '.' . $extension;
// Initialize the AWS S3 client
$s3 = new S3Client([
'version' => 'latest',
'region' => AWS_REGION,
'credentials' => [
'key' => AWS_ACCESS_KEY,
'secret' => AWS_SECRET_KEY,
]
]);
try {
$result = $s3->putObject([
'Bucket' => AWS_BUCKET_NAME,
'Key' => $fileName,
'Body' => $imageData,
'ACL' => 'public-read', // Make it publicly accessible
'ContentType' => 'image/jpeg'
]);
// Return the public URL to the uploaded object
return $result['ObjectURL'];
} catch (AwsException $e) {
throw new Exception("Error uploading to S3: " . $e->getMessage());
}
}
/* ------------------------------------------------------
D) SET UP BLOGGER API CONNECTION
------------------------------------------------------ */
function getBloggerService()
{
$client = new Google_Client();
$client->setScopes(['https://www.googleapis.com/auth/blogger']);
$client->setAuthConfig(GOOGLE_CLIENT_SECRET_PATH);
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
// If we already have a token saved, load it
if (file_exists(GOOGLE_TOKEN_PATH)) {
$accessToken = json_decode(file_get_contents(GOOGLE_TOKEN_PATH), true);
$client->setAccessToken($accessToken);
}
// If the token is expired, refresh or get a new one
if ($client->isAccessTokenExpired()) {
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
// If we don't have a refresh token, initiate the OAuth flow
if (!isset($_GET['code'])) {
$authUrl = $client->createAuthUrl();
echo "Please open the following URL in your browser to authorize:\n";
echo "<a href='$authUrl' target='_blank'>$authUrl</a>";
exit;
} else {
$client->authenticate($_GET['code']);
}
}
file_put_contents(GOOGLE_TOKEN_PATH, json_encode($client->getAccessToken()));
}
return new Google_Service_Blogger($client);
}
/* ------------------------------------------------------
E) INSERT IMAGES INTO SPECIFIC PARAGRAPHS
------------------------------------------------------ */
function insertImagesIntoParagraphs($content, $imageUrl1, $imageUrl2)
{
// Split text by double newlines to identify paragraphs
$paragraphs = preg_split("/(\r?\n){2,}|\n{2,}/", $content);
$finalHtml = "";
foreach ($paragraphs as $index => $para) {
$para = trim($para);
if (empty($para)) {
continue;
}
// Wrap each paragraph in <p> tags
$finalHtml .= "<p>" . nl2br($para) . "</p>\n";
// Insert the first image after the first paragraph
if ($index == 0 && $imageUrl1) {
$finalHtml .= '<img src="' . $imageUrl1 . '" alt="Image1" />' . "\n";
}
// Insert the second image after the third paragraph
if ($index == 2 && $imageUrl2) {
$finalHtml .= '<img src="' . $imageUrl2 . '" alt="Image2" />' . "\n";
}
}
return $finalHtml;
}
/* ------------------------------------------------------
F) PUBLISH TO BLOGGER
------------------------------------------------------ */
function postToBlogger($title, $htmlContent)
{
// Get an authenticated Blogger service instance
$service = getBloggerService();
$post = new Google_Service_Blogger_Post();
$post->setTitle($title);
$post->setContent($htmlContent);
// Publish the post immediately (isDraft=false)
$result = $service->posts->insert(BLOG_ID, $post, ['isDraft' => false]);
echo "A new post has been published on Blogger.\n";
echo "Post ID: " . $result->id . "\n";
echo "URL: " . $result->url . "\n";
}
/* ------------------------------------------------------
G) BRING IT ALL TOGETHER
------------------------------------------------------ */
// 1) Fetch two image URLs related to the chosen keyword
$imageUrls = getPexelsImages($keyword, 2);
if (count($imageUrls) < 2) {
die("Not enough images found or a Pexels API issue occurred.\n");
}
// 2) Upload these images to S3 and retrieve their public URLs
$s3Url1 = uploadImageToS3($imageUrls[0]);
$s3Url2 = uploadImageToS3($imageUrls[1]);
// 3) Insert the uploaded image URLs into the blog content
$finalHtmlContent = insertImagesIntoParagraphs($blogContent, $s3Url1, $s3Url2);
// 4) Publish to Blogger
postToBlogger($blogTitle, $finalHtmlContent);
echo "Process completed successfully!\n";