-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpublish.php
More file actions
600 lines (466 loc) · 22.9 KB
/
publish.php
File metadata and controls
600 lines (466 loc) · 22.9 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
<?php
/**
* Publisher ::::: YouTube Shorts Uploader
* Automatic | YT | Shorts | Uploader
* Multi Channel Supported
*
* :::::-
*
* @author BotolMehedi
* @email hello@mehedi.fun
* @version 1.0.1
*/
require_once 'vendor/autoload.php';
use Google\Client;
use Google\Service\YouTube;
class AutomaticYTShortsUploader {
private $config;
private $logFile;
public function __construct() {
$this->config = require 'config.php';
$this->logFile = $this->config['global']['logs_directory'] . 'yt_shorts_uploader.log';
$this->createDirectories();
}
private function createDirectories() {
$directories = [
$this->config['global']['logs_directory'],
$this->config['global']['tokens_directory'],
'titles',
'uploaded'
];
foreach ($directories as $dir) {
if (!file_exists($dir)) {
mkdir($dir, 0755, true);
}
}
// Create channel specific folders
foreach ($this->config['channels'] as $channelId => $channel) {
if (!file_exists($channel['video_directory'])) {
mkdir($channel['video_directory'], 0755, true);
}
// Unnecessary
$uploadedDir = $channel['video_directory'] . 'uploaded/';
if (!file_exists($uploadedDir)) {
mkdir($uploadedDir, 0755, true);
}
}
}
private function log($message, $channelId = null) {
$timestamp = date('Y-m-d H:i:s');
$channelPrefix = $channelId ? "[$channelId] " : "";
$logMessage = "[$timestamp] $channelPrefix$message" . PHP_EOL;
file_put_contents($this->logFile, $logMessage, FILE_APPEND | LOCK_EX);
// Channel Logs
if ($channelId) {
$channelLogFile = $this->config['global']['logs_directory'] . $channelId . '.log';
file_put_contents($channelLogFile, $logMessage, FILE_APPEND | LOCK_EX);
}
echo $logMessage;
}
private function setupGoogleClient($channelId) {
$channel = $this->config['channels'][$channelId];
$client = new Client();
$client->setClientId($this->config['google']['client_id']);
$client->setClientSecret($this->config['google']['client_secret']);
$client->setRedirectUri($this->config['google']['redirect_uri']);
$client->addScope(YouTube::YOUTUBE_UPLOAD);
$client->addScope(YouTube::YOUTUBE_READONLY);
$client->setAccessType('offline');
$client->setApprovalPrompt('force');
$this->loadAndValidateToken($client, $channel, $channelId);
return $client;
}
private function loadAndValidateToken($client, $channel, $channelId) {
if (!file_exists($channel['token_file'])) {
throw new Exception("Access token file not found for channel '$channelId'. Please authorize this channel first.");
}
$tokenData = json_decode(file_get_contents($channel['token_file']), true);
if (!$tokenData) {
throw new Exception("Invalid token file for channel '$channelId'. Please re-authenticate.");
}
$client->setAccessToken($tokenData);
// Token Check
if ($this->isTokenExpiredOrExpiring($client)) {
$this->log('Access token expired or expiring soon. Attempting to refresh...', $channelId);
$this->refreshAccessToken($client, $channel, $channelId);
} else {
$this->log('Access token is valid.', $channelId);
}
}
private function isTokenExpiredOrExpiring($client) {
if ($client->isAccessTokenExpired()) {
return true;
}
$token = $client->getAccessToken();
if (isset($token['created']) && isset($token['expires_in'])) {
$expiresAt = $token['created'] + $token['expires_in'];
$fiveMinutesFromNow = time() + 300;
if ($expiresAt <= $fiveMinutesFromNow) {
return true;
}
}
return false;
}
private function refreshAccessToken($client, $channel, $channelId) {
$refreshToken = $client->getRefreshToken();
if (!$refreshToken) {
$tokenData = json_decode(file_get_contents($channel['token_file']), true);
if (isset($tokenData['refresh_token'])) {
$refreshToken = $tokenData['refresh_token'];
$client->setAccessToken($tokenData);
}
}
if ($refreshToken) {
try {
$this->log("Refreshing access token using refresh token...", $channelId);
$newToken = $client->fetchAccessTokenWithRefreshToken($refreshToken);
if (isset($newToken['error'])) {
throw new Exception('Token refresh failed: ' . $newToken['error_description']);
}
if (!isset($newToken['refresh_token']) && $refreshToken) {
$newToken['refresh_token'] = $refreshToken;
}
// Save new token
file_put_contents($channel['token_file'], json_encode($newToken));
$this->log('Access token refreshed successfully. New expiry: ' . date('Y-m-d H:i:s', time() + $newToken['expires_in']), $channelId);
$client->setAccessToken($newToken);
} catch (Exception $e) {
$this->log('Failed to refresh access token: ' . $e->getMessage(), $channelId);
throw new Exception("Unable to refresh access token for channel '$channelId'. Please re-authenticate. Error: " . $e->getMessage());
}
} else {
throw new Exception("No refresh token available for channel '$channelId'. Please re-authenticate.");
}
}
private function shouldUploadNow($channel, $channelId) {
$currentTime = new DateTime('now', new DateTimeZone($this->config['global']['timezone']));
$currentHour = (int)$currentTime->format('H');
$currentMinute = (int)$currentTime->format('i');
foreach ($channel['upload_schedule'] as $timeRange) {
if ($currentHour == $timeRange['hour']) {
if ($currentMinute >= $timeRange['minute_start'] && $currentMinute <= $timeRange['minute_end']) {
return true;
}
}
}
return false;
}
private function checkLastUpload($channelId, $minHours) {
$lastUploadFile = $this->config['global']['logs_directory'] . "last_upload_$channelId.txt";
if (!file_exists($lastUploadFile)) {
return true;
}
$lastUpload = file_get_contents($lastUploadFile);
$lastUploadTime = new DateTime($lastUpload, new DateTimeZone($this->config['global']['timezone']));
$currentTime = new DateTime('now', new DateTimeZone($this->config['global']['timezone']));
$diff = $currentTime->diff($lastUploadTime);
$hoursDiff = ($diff->days * 24) + $diff->h;
return $hoursDiff >= $minHours;
}
private function updateLastUpload($channelId) {
$currentTime = new DateTime('now', new DateTimeZone($this->config['global']['timezone']));
$lastUploadFile = $this->config['global']['logs_directory'] . "last_upload_$channelId.txt";
file_put_contents($lastUploadFile, $currentTime->format('Y-m-d H:i:s'));
}
private function getVideoTitle($channel, $videoPath) {
if (!file_exists($channel['titles_file'])) {
throw new Exception('Titles file not found: ' . $channel['titles_file']);
}
$titlesData = json_decode(file_get_contents($channel['titles_file']), true);
if (!$titlesData || !isset($titlesData['type']) || !isset($titlesData['titles'])) {
throw new Exception('Invalid titles file structure: ' . $channel['titles_file']);
}
$type = $titlesData['type'];
$videoFileName = basename($videoPath);
if ($type === 'random') {
if (empty($titlesData['titles'])) {
throw new Exception('No titles available in random titles list');
}
return $titlesData['titles'][array_rand($titlesData['titles'])];
} elseif ($type === 'fixed') {
foreach ($titlesData['titles'] as $item) {
if (isset($item['name']) && $item['name'] === $videoFileName) {
return $item['title'];
}
}
throw new Exception("No matching fixed title found for video: $videoFileName");
} else {
throw new Exception('Unknown titles type: ' . $type);
}
}
private function removeUploadedVideo($videoPath, $channel) {
if (file_exists($videoPath)) {
if (unlink($videoPath)) {
$this->log("Deleted uploaded video: " . basename($videoPath));
return true;
} else {
$this->log("Failed to delete uploaded video: " . basename($videoPath));
}
}
return false;
}
private function getNextVideo($channel) {
$videoDir = $channel['video_directory'];
if (!is_dir($videoDir)) {
throw new Exception("Video directory not found: $videoDir");
}
$videos = glob($videoDir . '*.{mp4,mov,avi,mkv,flv,wmv,MP4,MOV,AVI}', GLOB_BRACE);
$videos = array_filter($videos, function($video) {
return !is_dir($video);
});
if (empty($videos)) {
throw new Exception('No videos found in the directory: ' . $videoDir);
}
// Select Random file
$randomIndex = array_rand($videos);
return $videos[$randomIndex];
}
public function uploadVideoToChannel($channelId) {
if (!isset($this->config['channels'][$channelId])) {
throw new Exception("Channel '$channelId' not found in configuration.");
}
$channel = $this->config['channels'][$channelId];
if (!$channel['enabled']) {
$this->log("Channel is disabled. Skipping.", $channelId);
return false;
}
// Check if it's time to upload
if (!$this->shouldUploadNow($channel, $channelId)) {
$this->log("Not in upload time period. Skipping.", $channelId);
return false;
}
// If you want you can use it
/*
if (!$this->checkLastUpload($channelId, $channel['min_hours_between_uploads'])) {
$this->log("Not enough time passed since last upload. Skipping.", $channelId);
return false;
}
*/
try {
$client = $this->setupGoogleClient($channelId);
$youtube = new YouTube($client);
$videoPath = $this->getNextVideo($channel);
$this->log("Selected video: " . basename($videoPath), $channelId);
$title = $this->getVideoTitle($channel, $videoPath);
$this->log("Selected title: $title", $channelId);
// VDO Description & Tags
$description = $channel['upload_settings']['description'];
$tags = $channel['upload_settings']['tags'];
// Upload
$result = $this->uploadVideo($youtube, $videoPath, $title, $description, $tags, $channel, $channelId);
if ($result) {
$this->log("Upload successful! Video ID: " . $result->getId(), $channelId);
$this->log("Video URL: https://youtube.com/watch?v=" . $result->getId(), $channelId);
// Delete uploaded video
$this->removeUploadedVideo($videoPath, $channel);
// Update last upload time
$this->updateLastUpload($channelId);
return $result;
}
} catch (Exception $e) {
$this->log("Error: " . $e->getMessage(), $channelId);
throw $e;
}
return false;
}
private function uploadVideo($youtube, $videoPath, $title, $description, $tags, $channel, $channelId) {
try {
$video = new Google\Service\YouTube\Video();
$videoSnippet = new Google\Service\YouTube\VideoSnippet();
// Set title
$videoSnippet->setTitle($title);
// Set description (must be a string, not an array)
if (is_array($description)) {
$description = implode("\n", $description);
}
$videoSnippet->setDescription($description);
// Set category
$videoSnippet->setCategoryId($channel['upload_settings']['category_id']);
// Set tags (must be an array of strings)
if (is_string($tags)) {
$tags = explode(',', $tags);
$tags = array_map('trim', $tags);
}
// Ensure tags is an array
if (!is_array($tags)) {
$tags = [];
}
$videoSnippet->setTags($tags);
// Set languages
if (isset($channel['upload_settings']['default_language'])) {
$videoSnippet->setDefaultLanguage($channel['upload_settings']['default_language']);
$videoSnippet->setDefaultAudioLanguage($channel['upload_settings']['default_language']);
}
$video->setSnippet($videoSnippet);
// Set video status
$videoStatus = new Google\Service\YouTube\VideoStatus();
$videoStatus->setPrivacyStatus($channel['upload_settings']['privacy_status']);
$videoStatus->setMadeForKids($channel['upload_settings']['made_for_kids']);
$video->setStatus($videoStatus);
$this->log("Starting upload for: $title", $channelId);
$this->log("File size: " . $this->formatBytes(filesize($videoPath)), $channelId);
// Upload retry
$maxRetries = 3;
$retryCount = 0;
while ($retryCount < $maxRetries) {
try {
$insertRequest = $youtube->videos->insert(
'status,snippet',
$video,
[
'data' => file_get_contents($videoPath),
'mimeType' => 'video/*',
'uploadType' => 'multipart'
]
);
$this->log("Video uploaded successfully!", $channelId);
return $insertRequest;
} catch (Exception $e) {
$retryCount++;
// Log the full error for debugging
$this->log("Upload error (attempt $retryCount): " . $e->getMessage(), $channelId);
if (strpos($e->getMessage(), 'Invalid Credentials') !== false ||
strpos($e->getMessage(), 'unauthorized') !== false) {
$this->log("Authentication error during upload attempt $retryCount. Refreshing token...", $channelId);
// refresh token
$client = $this->setupGoogleClient($channelId);
$youtube = new YouTube($client);
if ($retryCount < $maxRetries) {
$this->log("Retrying upload after token refresh...", $channelId);
sleep(2);
continue;
}
}
if ($retryCount >= $maxRetries) {
throw new Exception("Upload failed after $maxRetries attempts. Last error: " . $e->getMessage());
}
throw $e;
}
}
} catch (Exception $e) {
$this->log("Error uploading video: " . $e->getMessage(), $channelId);
throw $e;
}
}
private function formatBytes($bytes, $precision = 2) {
$units = array('B', 'KB', 'MB', 'GB', 'TB');
for ($i = 0; $bytes > 1024; $i++) {
$bytes /= 1024;
}
return round($bytes, $precision) . ' ' . $units[$i];
}
public function runForChannel($channelId) {
$this->log("=== Upload Starting | $channelId ===", $channelId);
try {
$result = $this->uploadVideoToChannel($channelId);
if ($result) {
$this->log("✅ Video Uploaded successfully", $channelId);
} else {
$this->log("ℹ️ Video uploading skipped (timing/conditions not met)", $channelId);
}
} catch (Exception $e) {
$this->log("❌ Video Upload failed: " . $e->getMessage(), $channelId);
if (strpos($e->getMessage(), 'refresh') !== false ||
strpos($e->getMessage(), 'authenticate') !== false) {
$this->log("IMPORTANT: Authentication issue detected for $channelId. Re-authorization may be needed.", $channelId);
}
}
$this->log("=== Uploading Finished | $channelId ===", $channelId);
}
public function runAllChannels() {
$this->log("=== Uploading Process Running ===");
$this->log("Timezone: " . $this->config['global']['timezone']);
$this->log("Current time: " . date('Y-m-d H:i:s'));
$totalChannels = 0;
$successfulUploads = 0;
$skippedChannels = 0;
$errorChannels = 0;
foreach ($this->config['channels'] as $channelId => $channel) {
$totalChannels++;
if (!$channel['enabled']) {
$this->log("Channel '$channelId' is disabled. Skipping.", $channelId);
$skippedChannels++;
continue;
}
try {
$result = $this->uploadVideoToChannel($channelId);
if ($result) {
$successfulUploads++;
$this->log("✅ Upload successful for $channelId");
} else {
$skippedChannels++;
$this->log("ℹ️ Upload skipped for $channelId");
}
// Wait between channels to avoid API rate limits
sleep(2);
} catch (Exception $e) {
$errorChannels++;
$this->log("❌ Error for channel $channelId: " . $e->getMessage());
}
}
// Summary
$this->log("=== Upload Summary ===");
$this->log("Total channels: $totalChannels");
$this->log("Successful uploads: $successfulUploads");
$this->log("Skipped channels: $skippedChannels");
$this->log("Error channels: $errorChannels");
$this->log("=== Uploading Process Finished ===");
}
public function getChannelStatus($channelId = null) {
$status = [];
$channels = $channelId ? [$channelId => $this->config['channels'][$channelId]] : $this->config['channels'];
foreach ($channels as $id => $channel) {
$tokenExists = file_exists($channel['token_file']);
$videosCount = count(glob($channel['video_directory'] . '*.{mp4,mov,avi,mkv,flv,wmv,MP4,MOV,AVI}', GLOB_BRACE));
$titlesCount = 0;
if (file_exists($channel['titles_file'])) {
$titles = json_decode(file_get_contents($channel['titles_file']), true);
$titlesCount = isset($titles['titles']) && is_array($titles['titles'])
? count($titles['titles'])
: 0;
}
$lastUploadFile = $this->config['global']['logs_directory'] . "last_upload_$id.txt";
$lastUpload = file_exists($lastUploadFile) ? file_get_contents($lastUploadFile) : 'Never';
$status[$id] = [
'name' => $channel['name'],
'enabled' => $channel['enabled'],
'authorized' => $tokenExists,
'videos_available' => $videosCount,
'titles_available' => $titlesCount,
'last_upload' => $lastUpload,
'next_upload_windows' => $this->getNextUploadWindows($channel),
];
}
return $status;
}
private function getNextUploadWindows($channel) {
$windows = [];
$currentTime = new DateTime('now', new DateTimeZone($this->config['global']['timezone']));
$today = $currentTime->format('Y-m-d');
foreach ($channel['upload_schedule'] as $timeWindow) {
$windowStart = new DateTime("$today {$timeWindow['hour']}:{$timeWindow['minute_start']}:00",
new DateTimeZone($this->config['global']['timezone']));
$windowEnd = new DateTime("$today {$timeWindow['hour']}:{$timeWindow['minute_end']}:59",
new DateTimeZone($this->config['global']['timezone']));
// Time Periods
if ($windowEnd < $currentTime) {
$windowStart->add(new DateInterval('P1D'));
$windowEnd->add(new DateInterval('P1D'));
}
$windows[] = $windowStart->format('H:i') . '-' . $windowEnd->format('H:i');
}
return $windows;
}
}
$channelId = $argv[1] ?? null;
try {
$uploader = new AutomaticYTShortsUploader();
if ($channelId && $channelId !== 'all') {
$uploader->runForChannel($channelId);
} else {
$uploader->runAllChannels();
}
} catch (Exception $e) {
error_log("Critical Error: " . $e->getMessage());
echo "Critical Error: " . $e->getMessage() . "\n";
}