-
Notifications
You must be signed in to change notification settings - Fork 975
/
Copy pathimportSiteFromAPI.php
370 lines (315 loc) · 14.9 KB
/
importSiteFromAPI.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
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
<?php
/**
* USAGE
* php importSiteFromAPI.php https://yoursiteURL.com/ APISecret [users_id] [categories_id]
* if does not pass users_id it will also import users
* if does not pass categories id it will also import categories
*
*/
$siteURL = trim(@$argv[1]);
$APISecret = trim(@$argv[2]);
$imported_users_id = intval(@$argv[3]);
$imported_categories_id = intval(@$argv[4]);
$total_to_import = intval(@$argv[5]);
$type = trim(@$argv[6]);
//streamer config
require_once '../videos/configuration.php';
if (!isCommandLineInterface()) {
return die('Command Line only');
}
ob_end_flush();
function download($url, $filename, $path, $forceDownload = false) {
$parts = explode("/{$filename}/", $url);
if (empty($parts[1])) {
if (preg_match("/\.mp3$/", $url)) {
$parts[1] = "{$filename}.mp3";
}
}
if (empty($parts[1])) {
_error_log("importVideo::download ERROR on download {$url}");
return false;
}
$parts2 = explode('?', $parts[1]);
$file = $parts2[0];
$destination = $path . $file;
if ($forceDownload || !file_exists($destination)) {
_error_log("importVideo::download [$destination]");
return wget($url, $destination, true);
} else {
_error_log("importVideo::download skipped [$destination]");
}
return false;
}
set_time_limit(360000);
ini_set('max_execution_time', 360000);
$global['rowCount'] = $global['limitForUnlimitedVideos'] = 999999;
while (empty($siteURL) || !isValidURL($siteURL)) {
$siteURL = readline('Enter a valid URL: ');
}
while (empty($APISecret)) {
$APISecret = readline('Enter a valid APISecret: ');
}
if (empty($imported_users_id)) {
$imported_users_id = (int) readline('Enter a users id to be the video owner or 0 to also import the users: ');
}
if (empty($imported_categories_id)) {
$imported_categories_id = (int) readline('Enter a category id to be linked to the video or 0 to also import the categories: ');
}
if (empty($total_to_import)) {
$total_to_import = (int) readline('How many videos do you want to import? type 0 to import all: ');
}
$rowCount = 50;
$current = 1;
$hasNewContent = true;
_error_log("importSite: start {$siteURL} imported_users_id=$imported_users_id imported_categories_id=$imported_categories_id total_to_import=$total_to_import");
//exit;
if ($type !== 'm3u8') {
if (empty($imported_categories_id) || $imported_categories_id < 0) {
// get categories
while ($hasNewContent) {
$APIURL = "{$siteURL}plugin/API/get.json.php?APIName=category&rowCount={$rowCount}¤t={$current}&APISecret={$APISecret}";
$content = url_get_contents($APIURL, "", 30);
$hasNewContent = false;
$current++;
if (!empty($content)) {
_error_log("importCategory: SUCCESS {$APIURL}");
$json = _json_decode($content);
if (!empty($json) && !empty($json->response) && !empty($json->response->totalRows) && !empty($json->response->rows)) {
_error_log("importCategory: JSON SUCCESS totalRows={$json->response->totalRows}");
$hasNewContent = true;
foreach ($json->response->rows as $key => $value) {
$cat = Category::getCategoryByName($value->clean_name);
if (!empty($cat)) {
_error_log("importCategory: category exists [{$cat['id']}]{$cat['clean_name']}");
continue;
}
$o = new Category(0);
$o->setName($value->name);
$o->setClean_name($value->clean_name);
$o->setDescription($value->description);
$o->setIconClass($value->iconClass);
$o->setPrivate($value->private);
$o->setAllow_download($value->allow_download);
$o->setOrder($value->order);
$o->setSuggested($value->suggested);
$o->setNextVideoOrder($value->nextVideoOrder);
$o->setUsers_id(1);
_error_log("importCategory: Saving ...");
$id = $o->save(true);
if ($id) {
_error_log("importCategory: saved {$id}");
} else {
_error_log("importCategory: ERROR NOT saved");
}
//exit;
}
} else {
_error_log("importCategory: JSON ERROR {$content} ");
}
} else {
_error_log("importCategory: ERROR {$APIURL} content is empty");
}
}
}
if (empty($imported_users_id)) {
$current = 1;
$hasNewContent = true;
// get users
while ($hasNewContent) {
$APIURL = "{$siteURL}plugin/API/get.json.php?APIName=users_list&rowCount={$rowCount}¤t={$current}&APISecret={$APISecret}";
$content = url_get_contents($APIURL, "", 30);
$hasNewContent = false;
$current++;
if (!empty($content)) {
_error_log("importUsers: SUCCESS {$APIURL}");
$json = _json_decode($content);
if (!empty($json) && !empty($json->response)) {
_error_log("importUsers: JSON SUCCESS");
$hasNewContent = true;
foreach ($json->response as $key => $value) {
$user = User::getUserFromEmail($value->email);
if (empty($user)) {
$user = User::getUserFromChannelName($value->channelName);
}
if (!empty($user)) {
_error_log("importUsers: exists [{$user['id']}]{$user['clean_name']}");
continue;
}
$o = new User(0);
$o->setUser($value->user);
$o->setPassword($value->user);
$o->setName($value->name);
$o->setEmail($value->email);
$o->setIsAdmin(0);
$o->setStatus($value->a);
$o->setCanStream($value->canStream);
$o->setCanUpload($value->canUpload);
$o->setCanCreateMeet($value->canCreateMeet);
$o->setCanViewChart($value->canViewChart);
$o->setChannelName($value->channelName);
$o->setEmailVerified($value->emailVerified);
$o->setAbout($value->about);
$o->setAnalyticsCode($value->analyticsCode);
$o->setExternalOptions($value->externalOptions);
$o->setFirst_name($value->first_name);
$o->setLast_name($value->last_name);
$o->setAddress($value->address);
$o->setZip_code($value->zip_code);
$o->setCountry($value->country);
$o->setRegion($value->region);
$o->setCity($value->city);
$o->setDonationLink($value->donationLink);
$o->setExtra_info($value->extra_info);
$o->setPhone($value->phone);
$o->setIs_company($value->is_company);
_error_log("importUsers: Saving ...");
$id = $o->save(false);
if ($id) {
_error_log("importUsers: saved {$id}");
wget($value->photo, "{$global['systemRootPath']}videos/userPhoto/photo{$id}.png", true);
//wget($value->background, "{$global['systemRootPath']}videos/userPhoto/photo{$id}.png", true);
} else {
_error_log("importUsers: ERROR NOT saved");
$video->setStatus(Video::$statusBrokenMissingFiles);
}
//exit;
}
} else {
_error_log("importUsers: JSON ERROR " . json_last_error_msg());
//exit;
_error_log("importUsers: JSON ERROR {$content} ");
}
} else {
_error_log("importUsers: ERROR {$APIURL} content is empty");
}
}
}
}
$current = 1;
$hasNewContent = true;
$total_imported = 0;
// import videos
while ($hasNewContent) {
$APIURL = "{$siteURL}plugin/API/get.json.php?APIName=video&rowCount={$rowCount}¤t={$current}&APISecret={$APISecret}&sort[created]=desc";
$content = url_get_contents($APIURL, "", 30);
$hasNewContent = false;
$current++;
if (!empty($content)) {
_error_log("importVideos: SUCCESS {$APIURL}");
$json = _json_decode($content);
if (!empty($json) && !empty($json->response) && !empty($json->response->totalRows) && !empty($json->response->rows)) {
_error_log("importVideo: JSON SUCCESS totalRows={$json->response->totalRows}");
$hasNewContent = true;
foreach ($json->response->rows as $key => $value) {
if ($type == 'm3u8') {
if (empty($value->videos->m3u8)) {
continue;
}
}
$videos_id = 0;
$row = Video::getVideoFromFileNameLight($value->filename);
if (!empty($row)) {
_error_log("importVideo: Video found");
$videos_id = $row['id'];
} else {
_error_log("importVideo: Video NOT found");
}
_error_log("importVideo: Video {$videos_id} {$value->title} {$value->fileName}");
if (empty($imported_users_id)) {
$users_id = 1;
$user = User::getUserFromEmail($value->email);
if (empty($user)) {
$user = User::getUserFromChannelName($value->channelName);
}
if (!empty($user)) {
$users_id = $user['id'];
}
} else {
$users_id = $imported_users_id;
}
if (empty($imported_categories_id) || $imported_categories_id < 0) {
$cat = Category::getCategoryByName($value->clean_category);
$categories_id = $cat['id'];
} else {
$categories_id = $imported_categories_id;
}
$video = new Video($value->title, $value->filename, $videos_id);
$video->setCreated("'$value->created'");
$video->setDuration($value->duration);
$video->setType($value->type);
$video->setVideoDownloadedLink($value->videoDownloadedLink);
$video->setDuration_in_seconds($value->duration_in_seconds);
$video->setDescription($value->description);
$video->setUsers_id($users_id);
$video->setCategories_id($categories_id);
$path = getVideosDir() . $value->filename . DIRECTORY_SEPARATOR;
$size = getDirSize($path);
if ($size < 10000000) {
if(empty($videos_id)){
$video->setStatus(Video::$statusTranfering);
_error_log("importVideo status: transfering ($size) " . humanFileSize($size));
}else{
if ($size > 1000000) {
$video->setStatus(Video::$statusActive);
}
_error_log("importVideo status: else ($size) " . humanFileSize($size));
}
}
if(empty($videos_id)){
$video->setStatus(Video::$statusTranfering);
}
_error_log("importVideo: Saving video");
$id = $video->save(false, true);
if ($id) {
_error_log("importVideo: Video saved {$id} categories_id=$categories_id ($value->clean_category) created=$value->created");
make_path($path);
// download images
download($value->images->poster, $value->filename, $path);
download($value->images->thumbsGif, $value->filename, $path);
foreach ($value->videos->mp4 as $key2 => $value2) {
_error_log("importVideo MP4: key = {$key} key2 = {$key2} APIURL = $APIURL");
download($value2, $value->filename, $path);
}
if (!empty($value->videos->mp3)) {
_error_log("importVideo MP3: {$value->videos->mp3} APIURL = $APIURL");
download($value->videos->mp3, $value->filename, $path);
}
$video->setStatus(Video::$statusActive);
if (!empty($value->videos->m3u8)) {
if ($size < 10000000) {
if(empty($videos_id)){
_error_log("importVideo m3u8: {$value->videos->m3u8->url} APIURL = $APIURL ($size) " . humanFileSize($size));
sendToEncoder($id, $value->videos->m3u8->url);
}
if(empty($videos_id)){
$video->setStatus(Video::$statusEncoding);
}
} else {
_error_log("importVideo m3u8 NOT SEND: ($size) " . humanFileSize($size));
}
}
if(empty($videos_id)){
$total_imported++;
}
if (!empty($total_to_import) && $total_to_import > 0 && $total_imported >= $total_to_import) {
_error_log("importVideo completed: total_imported=$total_imported >= total_to_import=$total_to_import ");
$hasNewContent = false;
break;
}else{
_error_log("importVideo continue: total_imported=$total_imported < total_to_import=$total_to_import ");
}
} else {
_error_log("importVideo: ERROR Video NOT saved");
$video->setStatus(Video::$statusBrokenMissingFiles);
}
$video->save(false, true);
//exit;
}
} else {
_error_log("importVideo: JSON ERROR {$content} ");
}
} else {
_error_log("importVideo: ERROR {$APIURL} content is empty");
}
}
die();