1
1
<?php
2
-
3
2
namespace App \Http \Controllers ;
4
3
5
4
use GuzzleHttp \Client ;
6
5
use Illuminate \Support \Str ;
7
6
8
-
9
7
class CrawlerMarvelController extends Controller
10
8
{
11
9
//
12
10
private $ client ;
13
11
14
12
public function __construct ()
15
13
{
16
-
17
14
$ baseUrl = 'http://gateway.marvel.com/v1/public/ ' ;
18
-
19
15
$ public_key = '1747ef87891d75e5ff61d9f6d2fadcd7 ' ;
20
-
21
16
$ private_key = '3e49f306668367a1ee7ac0231b0009c3a69a2a19 ' ;
22
-
23
17
$ ts = time ();
24
18
$ hash = md5 ($ ts . $ private_key . $ public_key );
25
-
26
19
$ this ->client = new Client ([
27
20
'base_uri ' => $ baseUrl ,
28
21
'query ' => [
@@ -31,171 +24,31 @@ public function __construct()
31
24
'hash ' => $ hash
32
25
]
33
26
]);
34
-
35
27
}
36
28
37
29
public function multiOption ($ option )
38
30
{
39
31
$ response = $ this ->client ->get ($ option );
40
-
41
32
$ response = json_decode ($ response ->getBody (), true );
42
33
$ multiDatas = $ response ['data ' ]['results ' ];
43
- return view ('generalApi ' , ['multiDatas ' => $ multiDatas ]);
44
- }
45
-
46
-
47
-
48
-
49
- public function comicsApi ()
50
- {
51
- $ response = $ this ->client ->get ('comics ' );
52
-
53
- $ response = json_decode ($ response ->getBody (), true );
54
-
55
- $ comics = $ response ['data ' ]['results ' ];
56
-
57
- return view ('comicsApi ' , ['comics ' => $ comics ]);
58
-
59
- }
60
-
61
- function comicApi ($ id )
62
- {
63
- $ page_data = [];
64
-
65
- $ response = $ this ->client ->get ('comics/ ' . $ id );
66
- $ response = json_decode ($ response ->getBody (), true );
67
- $ page_data ['copyright ' ] = $ response ['copyright ' ];
68
- $ page_data ['attributionText ' ] = $ response ['attributionText ' ];
69
- $ comic = $ response ['data ' ]['results ' ][0 ];
70
- $ page_data ['comic ' ] = $ comic ;
71
-
72
- if (!empty ($ comic ['series ' ])) {
73
- $ series_response = $ this ->client ->get ($ comic ['series ' ]['resourceURI ' ]);
74
- $ series_response = json_decode ($ series_response ->getBody (), true );
75
- $ page_data ['series ' ] = $ series_response ['data ' ]['results ' ][0 ];
76
- }
77
- return view ('comic ' , $ page_data );
78
- }
79
-
80
- public function charactersApi ()
81
- {
82
- $ response = $ this ->client ->get ('characters ' );
83
-
84
- $ response = json_decode ($ response ->getBody (), true );
85
-
86
- $ characters = $ response ['data ' ]['results ' ];
87
-
88
- return view ('charactersApi ' , ['characters ' => $ characters ]);
89
-
90
- }
91
-
92
- function characterApi ($ id )
93
- {
94
- $ page_data = [];
95
-
96
- $ response = $ this ->client ->get ('characters/ ' . $ id );
97
- $ response = json_decode ($ response ->getBody (), true );
98
- $ page_data ['copyright ' ] = $ response ['copyright ' ];
99
- $ page_data ['attributionText ' ] = $ response ['attributionText ' ];
100
- $ characters = $ response ['data ' ]['results ' ][0 ];
101
- $ page_data ['characters ' ] = $ characters ;
102
- if (!empty ($ characters ['series ' ])) {
103
- $ series_response = $ this ->client ->get ($ characters ['series ' ]['collectionURI ' ]);
104
- $ series_response = json_decode ($ series_response ->getBody (), true );
105
- $ page_data ['series ' ] = $ series_response ['data ' ]['results ' ][0 ];
106
- }
107
-
108
- return view ('character ' , $ page_data );
34
+ return view ('generalApi ' , ['multiDatas ' => $ multiDatas , 'option ' => $ option ]);
109
35
}
110
36
111
-
112
-
113
-
114
- public function eventsApi ()
115
- {
116
- $ response = $ this ->client ->get ('events ' );
117
-
118
- $ response = json_decode ($ response ->getBody (), true );
119
- $ events = $ response ['data ' ]['results ' ];
120
- return view ('eventsApi ' , ['events ' => $ events ]);
121
- }
122
-
123
- function eventApi ($ id )
37
+ function detailOption ($ id , $ option )
124
38
{
125
39
$ page_data = [];
126
-
127
- $ response = $ this ->client ->get ('events/ ' . $ id );
40
+ $ response = $ this ->client ->get ($ option . '/ ' . $ id );
128
41
$ response = json_decode ($ response ->getBody (), true );
129
42
$ page_data ['copyright ' ] = $ response ['copyright ' ];
130
43
$ page_data ['attributionText ' ] = $ response ['attributionText ' ];
131
- $ events = $ response ['data ' ]['results ' ][0 ];
132
- $ page_data ['events ' ] = $ events ;
133
-
134
- if (!empty ($ events ['series ' ])) {
135
- $ series_response = $ this ->client ->get ($ events ['series ' ]['collectionURI ' ]);
44
+ $ multiDatas = $ response ['data ' ]['results ' ][0 ];
45
+ $ page_data ['comic ' ] = $ multiDatas ;
46
+ if (isset ($ multiDatas ['series ' ])) {
47
+ $ series_response = $ this ->client ->get ($ multiDatas ['series ' ]['resourceURI ' ] ?? $ multiDatas ['series ' ]['collectionURI ' ]);
136
48
$ series_response = json_decode ($ series_response ->getBody (), true );
137
49
$ page_data ['series ' ] = $ series_response ['data ' ]['results ' ][0 ];
138
50
}
139
-
140
- return view ('event ' , $ page_data );
141
- }
142
-
143
- public function seriesApi ()
144
- {
145
- $ response = $ this ->client ->get ('series ' );
146
-
147
- $ response = json_decode ($ response ->getBody (), true );
148
- $ series = $ response ['data ' ]['results ' ];
149
- return view ('seriesApi ' , ['series ' => $ series ]);
150
- }
151
-
152
- function serieApi ($ id )
153
- {
154
- $ page_data = [];
155
-
156
- $ response = $ this ->client ->get ('series/ ' . $ id );
157
- $ response = json_decode ($ response ->getBody (), true );
158
- $ page_data ['copyright ' ] = $ response ['copyright ' ];
159
- $ page_data ['attributionText ' ] = $ response ['attributionText ' ];
160
- $ series = $ response ['data ' ]['results ' ][0 ];
161
- $ page_data ['series ' ] = $ series ;
162
-
163
- if (!empty ($ series ['series ' ])) {
164
- $ series_response = $ this ->client ->get ($ series ['series ' ]['collectionURI ' ]);
165
- $ series_response = json_decode ($ series_response ->getBody (), true );
166
- $ page_data ['series ' ] = $ series_response ['data ' ]['results ' ][0 ];
167
- }
168
-
169
- return view ('serie ' , $ page_data );
170
- }
171
-
172
- public function storiesApi ()
173
- {
174
- $ response = $ this ->client ->get ('stories ' );
175
-
176
- $ response = json_decode ($ response ->getBody (), true );
177
- $ stories = $ response ['data ' ]['results ' ];
178
- return view ('storiesApi ' , ['stories ' => $ stories ]);
179
- }
180
-
181
- function storieApi ($ id )
182
- {
183
- $ page_data = [];
184
-
185
- $ response = $ this ->client ->get ('stories/ ' . $ id );
186
- $ response = json_decode ($ response ->getBody (), true );
187
- $ page_data ['copyright ' ] = $ response ['copyright ' ];
188
- $ page_data ['attributionText ' ] = $ response ['attributionText ' ];
189
- $ series = $ response ['data ' ]['results ' ][0 ];
190
- $ page_data ['series ' ] = $ series ;
191
-
192
- if (!empty ($ series ['series ' ])) {
193
- $ series_response = $ this ->client ->get ($ series ['series ' ]['collectionURI ' ]);
194
- $ series_response = json_decode ($ series_response ->getBody (), true );
195
- $ page_data ['series ' ] = $ series_response ['data ' ]['results ' ][0 ];
196
- }
197
-
198
- return view ('serie ' , $ page_data );
51
+ return view ('detailApi ' , $ page_data );
199
52
}
200
53
201
54
public function comicsCrawler ()
@@ -210,7 +63,6 @@ public function comicsCrawler()
210
63
$ totalColetado = 0 ;
211
64
$ tituloGeral = array ();
212
65
foreach ($ keywords as $ k => $ v ) {
213
-
214
66
if (Str::contains ($ keywords [$ k ], '<h2 class="module-header"> ' )) {
215
67
$ idTituloGeral = $ k ;
216
68
$ limpaTituloGeral1 = Str::after ($ keywords [$ k ], '<h2 class="module-header"> ' );
@@ -223,7 +75,6 @@ public function comicsCrawler()
223
75
$ tituloGeral [$ k ]['criadores ' ] = [];
224
76
225
77
foreach ($ keywords as $ k => $ v ) {
226
-
227
78
if (Str::contains ($ keywords [$ k ], '<div class="row-item-image"> ' )) {
228
79
$ limpaTituloComic1 = Str::after ($ keywords [$ k ], '<a href=" // ' );
229
80
$ limpaTituloComic2 = Str::before ($ limpaTituloComic1 , '" class=" ' );
@@ -264,6 +115,4 @@ public function comicsCrawler()
264
115
}
265
116
return view ('comicsCrawler ' , compact ('tituloGeral ' , 'totalColetado ' ));
266
117
}
267
-
268
-
269
118
}
0 commit comments