|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
|
4
|
|
|
|
|
5
|
|
|
use Alaouy\Youtube\Facades\Youtube as Yt; |
|
6
|
|
|
use Illuminate\Console\Command; |
|
7
|
|
|
use Illuminate\Support\Facades\Log; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Task to register Youtube API Videos |
|
11
|
|
|
* Class Youtube. |
|
12
|
|
|
*/ |
|
13
|
|
|
class Youtube extends Command |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* The name and signature of the console command. |
|
17
|
|
|
* |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
protected $signature = 'youtube:import {keyword}'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* The console command description. |
|
24
|
|
|
* |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $description = 'Youtube import videos on Mongo'; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Create a new command instance. |
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct() |
|
33
|
|
|
{ |
|
34
|
|
|
parent::__construct(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Execute the console command. |
|
39
|
|
|
* |
|
40
|
|
|
* @return mixed |
|
41
|
|
|
*/ |
|
42
|
|
|
public function handle() |
|
43
|
|
|
{ |
|
44
|
|
|
$keyword = $this->argument('keyword'); |
|
45
|
|
|
|
|
46
|
|
|
$channel = Yt::getChannelByName('allocine'); |
|
47
|
|
|
|
|
48
|
|
|
if (!empty($channel)) { |
|
49
|
|
|
$manager = new \MongoDB\Driver\Manager('mongodb://localhost:27017'); |
|
50
|
|
|
$collection = new \MongoDB\Collection($manager, 'laravel.stats'); |
|
|
|
|
|
|
51
|
|
|
$collection->deleteMany([]); |
|
52
|
|
|
|
|
53
|
|
|
$collection = new \MongoDB\Collection($manager, 'laravel.stats'); |
|
|
|
|
|
|
54
|
|
|
$stat = [ |
|
55
|
|
|
'origin' => 'Youtube', |
|
56
|
|
|
'type' => 'search', |
|
57
|
|
|
'data' => $channel, |
|
58
|
|
|
'created' => new \MongoDB\BSON\UTCDatetime(time()), |
|
59
|
|
|
]; |
|
60
|
|
|
$collection->insertOne($stat); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$params = [ |
|
64
|
|
|
'q' => $keyword, |
|
65
|
|
|
'type' => 'video', |
|
66
|
|
|
'part' => 'id, snippet', |
|
67
|
|
|
'maxResults' => 30, |
|
68
|
|
|
]; |
|
69
|
|
|
|
|
70
|
|
|
$videos = Yt::searchAdvanced($params, true)['results']; |
|
71
|
|
|
|
|
72
|
|
|
if (!empty($videos)) { |
|
73
|
|
|
$collection = new \MongoDB\Collection($manager, 'laravel.videos'); |
|
|
|
|
|
|
74
|
|
|
$collection->deleteMany([]); |
|
75
|
|
|
|
|
76
|
|
|
foreach ($videos as $video) { |
|
77
|
|
|
$collection = new \MongoDB\Collection($manager, 'laravel.videos'); |
|
|
|
|
|
|
78
|
|
|
$stat = [ |
|
79
|
|
|
'data' => $video, |
|
80
|
|
|
'created' => new \MongoDB\BSON\UTCDatetime(time()), |
|
81
|
|
|
]; |
|
82
|
|
|
$collection->insertOne($stat); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
Log::info("Import de l'API Youtube video done! "); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
This check looks for function calls that miss required arguments.