Skip to content

Commit 0cfd5d5

Browse files
committed
Personagens e Comics/API
1 parent bc7a3a0 commit 0cfd5d5

10 files changed

+608
-58
lines changed

README.md

+75-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,75 @@
1-
# crawler-marvel
1+
# crawler-marvel
2+
3+
4+
Funciona com servidor próprio do laravel(php artisan serve).
5+
6+
7+
8+
## Começando
9+
10+
Clone o repositório do projeto:
11+
12+
opção 1:
13+
Caso você use HTTPS:
14+
15+
git clone https://github.com/dompossebon/crawler-marvel.git
16+
17+
opção 2:
18+
Caso você use SSH:
19+
20+
git clone git@github.com:dompossebon/crawler-marvel.git
21+
22+
---------------------------------------------------------
23+
24+
Após a clonagem, entre no diretório da aplicação:
25+
26+
cd crawler-marvel
27+
28+
em seguida execute o comandos abaixo:
29+
30+
composer install
31+
Caso aconteça erro de permissão, lembre-se de executar: (sudo chown -R $USER .) dentro do diretorio /bussolasocial
32+
e então repita o comando (composer install)
33+
34+
Na raiz do projeto localize e Duplique o arquivo .env.example e em seguida renomeie-o para .env usando o comando:
35+
36+
cp .env.example .env
37+
38+
39+
---------------------------------------------------------
40+
41+
42+
Então rode o comando:
43+
44+
- php artisan key:generate
45+
46+
47+
e em seguida
48+
49+
- php artisan serve
50+
51+
Agora basta
52+
53+
visite http: // http://127.0.0.1:8000/ para ver o aplicativo em ação.
54+
55+
56+
---------------------------------------------------------
57+
58+
Para Rodar Testes rode este comando
59+
60+
61+
## vendor/bin/phpunit
62+
63+
64+
---------------------------------------------------------
65+
66+
67+
## Construído com
68+
Laravel - O framework PHP para artesãos da Web
69+
70+
71+
## by Possebon
72+
## Contato dompossebon@gmail.com
73+
74+
:+1: ## By Possebon
75+

app/Http/Controllers/CrawlerMarvelController.php

+97-2
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,97 @@
55
use GuzzleHttp\Client;
66
use Illuminate\Support\Str;
77

8+
89
class CrawlerMarvelController extends Controller
910
{
1011
//
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()
1299
{
13100
$client = new Client();
14101
$response = $client->request('GET', 'https://www.marvel.com/comics?&options%5Boffset%5D=0&totalcount=12');
@@ -26,6 +113,7 @@ public function comics()
26113
$limpaTituloGeral1 = Str::after($keywords[$k], '<h2 class="module-header">');
27114
$limpaTituloGeral11 = Str::before($limpaTituloGeral1, '<');
28115
$tituloGeral[$k]['titulogeral'] = trim($limpaTituloGeral11);
116+
$tituloGeral[$k]['id'] = [];
29117
$tituloGeral[$k]['url'] = [];
30118
$tituloGeral[$k]['title'] = [];
31119
$tituloGeral[$k]['imagem'] = [];
@@ -39,6 +127,11 @@ public function comics()
39127
array_push($tituloGeral[$idTituloGeral]['url'], trim('https://' . $limpaTituloComic2));
40128
$totalColetado += 1;
41129

130+
131+
$limpaTituloId1 = Str::after($limpaTituloComic2, '/issue/');
132+
$limpaTituloId2 = Str::before($limpaTituloId1, '/');
133+
array_push($tituloGeral[$idTituloGeral]['id'], trim($limpaTituloId2));
134+
42135
if (Str::contains($keywords[$k], '<p class="meta-creators">')) {
43136
$limpaCreatorsComic1 = Str::after($keywords[$k], '<p class="meta-creators">');
44137
$limpaCreatorsComic2 = Str::before($limpaCreatorsComic1, '</p>');
@@ -66,6 +159,8 @@ public function comics()
66159
}
67160
}
68161
}
69-
return view('comics', compact('tituloGeral', 'totalColetado'));
162+
return view('comicsCrawler', compact('tituloGeral', 'totalColetado'));
70163
}
164+
165+
71166
}

0 commit comments

Comments
 (0)