5
5
use GuzzleHttp \Client ;
6
6
use Illuminate \Support \Str ;
7
7
8
+
8
9
class CrawlerMarvelController extends Controller
9
10
{
10
11
//
11
- public function comics ()
12
+ private $ client ;
13
+
14
+ public function __construct ()
15
+ {
16
+
17
+ $ baseUrl = 'http://gateway.marvel.com/v1/public/ ' ;
18
+
19
+ $ public_key = '1747ef87891d75e5ff61d9f6d2fadcd7 ' ;
20
+
21
+ $ private_key = '3e49f306668367a1ee7ac0231b0009c3a69a2a19 ' ;
22
+
23
+ $ ts = time ();
24
+ $ hash = md5 ($ ts . $ private_key . $ public_key );
25
+
26
+ $ this ->client = new Client ([
27
+ 'base_uri ' => $ baseUrl ,
28
+ 'query ' => [
29
+ 'apikey ' => $ public_key ,
30
+ 'ts ' => $ ts ,
31
+ 'hash ' => $ hash
32
+ ]
33
+ ]);
34
+
35
+ }
36
+
37
+ public function comicsApi ()
38
+ {
39
+ $ response = $ this ->client ->get ('comics ' );
40
+
41
+ $ response = json_decode ($ response ->getBody (), true );
42
+
43
+ $ comics = $ response ['data ' ]['results ' ];
44
+
45
+ return view ('comicsApi ' , ['comics ' => $ comics ]);
46
+
47
+ }
48
+
49
+ function comicApi ($ id )
50
+ {
51
+ $ page_data = [];
52
+
53
+ $ response = $ this ->client ->get ('comics/ ' . $ id );
54
+ $ response = json_decode ($ response ->getBody (), true );
55
+ $ page_data ['copyright ' ] = $ response ['copyright ' ];
56
+ $ page_data ['attributionText ' ] = $ response ['attributionText ' ];
57
+ $ comic = $ response ['data ' ]['results ' ][0 ];
58
+ $ page_data ['comic ' ] = $ comic ;
59
+
60
+ if (!empty ($ comic ['series ' ])) {
61
+ $ series_response = $ this ->client ->get ($ comic ['series ' ]['resourceURI ' ]);
62
+ $ series_response = json_decode ($ series_response ->getBody (), true );
63
+ $ page_data ['series ' ] = $ series_response ['data ' ]['results ' ][0 ];
64
+ }
65
+ return view ('comic ' , $ page_data );
66
+ }
67
+
68
+ public function charactersApi ()
69
+ {
70
+ $ response = $ this ->client ->get ('characters ' );
71
+
72
+ $ response = json_decode ($ response ->getBody (), true );
73
+
74
+ $ characters = $ response ['data ' ]['results ' ];
75
+
76
+ return view ('charactersApi ' , ['characters ' => $ characters ]);
77
+
78
+ }
79
+
80
+ function characterApi ($ id )
81
+ {
82
+ $ page_data = [];
83
+
84
+ $ response = $ this ->client ->get ('characters/ ' . $ id );
85
+ $ response = json_decode ($ response ->getBody (), true );
86
+ $ page_data ['copyright ' ] = $ response ['copyright ' ];
87
+ $ page_data ['attributionText ' ] = $ response ['attributionText ' ];
88
+ $ characters = $ response ['data ' ]['results ' ][0 ];
89
+ $ page_data ['characters ' ] = $ characters ;
90
+ if (!empty ($ characters ['series ' ])) {
91
+ $ series_response = $ this ->client ->get ($ characters ['series ' ]['collectionURI ' ]);
92
+ $ series_response = json_decode ($ series_response ->getBody (), true );
93
+ $ page_data ['series ' ] = $ series_response ['data ' ]['results ' ][0 ];
94
+ }
95
+
96
+ return view ('character ' , $ page_data );
97
+ }
98
+ public function comicsCrawler ()
12
99
{
13
100
$ client = new Client ();
14
101
$ response = $ client ->request ('GET ' , 'https://www.marvel.com/comics?&options%5Boffset%5D=0&totalcount=12 ' );
@@ -26,6 +113,7 @@ public function comics()
26
113
$ limpaTituloGeral1 = Str::after ($ keywords [$ k ], '<h2 class="module-header"> ' );
27
114
$ limpaTituloGeral11 = Str::before ($ limpaTituloGeral1 , '< ' );
28
115
$ tituloGeral [$ k ]['titulogeral ' ] = trim ($ limpaTituloGeral11 );
116
+ $ tituloGeral [$ k ]['id ' ] = [];
29
117
$ tituloGeral [$ k ]['url ' ] = [];
30
118
$ tituloGeral [$ k ]['title ' ] = [];
31
119
$ tituloGeral [$ k ]['imagem ' ] = [];
@@ -39,6 +127,11 @@ public function comics()
39
127
array_push ($ tituloGeral [$ idTituloGeral ]['url ' ], trim ('https:// ' . $ limpaTituloComic2 ));
40
128
$ totalColetado += 1 ;
41
129
130
+
131
+ $ limpaTituloId1 = Str::after ($ limpaTituloComic2 , '/issue/ ' );
132
+ $ limpaTituloId2 = Str::before ($ limpaTituloId1 , '/ ' );
133
+ array_push ($ tituloGeral [$ idTituloGeral ]['id ' ], trim ($ limpaTituloId2 ));
134
+
42
135
if (Str::contains ($ keywords [$ k ], '<p class="meta-creators"> ' )) {
43
136
$ limpaCreatorsComic1 = Str::after ($ keywords [$ k ], '<p class="meta-creators"> ' );
44
137
$ limpaCreatorsComic2 = Str::before ($ limpaCreatorsComic1 , '</p> ' );
@@ -66,6 +159,8 @@ public function comics()
66
159
}
67
160
}
68
161
}
69
- return view ('comics ' , compact ('tituloGeral ' , 'totalColetado ' ));
162
+ return view ('comicsCrawler ' , compact ('tituloGeral ' , 'totalColetado ' ));
70
163
}
164
+
165
+
71
166
}
0 commit comments