-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
224 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
.idea | ||
out | ||
target | ||
entrada | ||
saida | ||
alura-stickers.iml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
public class Apod { | ||
private final String title; | ||
private final String url; | ||
|
||
public Apod(String title, String url) { | ||
this.title = title; | ||
this.url = url; | ||
} | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public String getUrl() { | ||
return url; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
|
||
public class ClienteHttp { | ||
|
||
public String buscaDados(String url) { | ||
|
||
try { | ||
URI endereco = URI.create(url); | ||
HttpClient httpClient = HttpClient.newHttpClient(); | ||
HttpRequest request = HttpRequest.newBuilder(endereco).GET().build(); | ||
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); | ||
return response.body(); | ||
} catch (IOException | InterruptedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
public class Conteudo { | ||
private final String titulo; | ||
private final String urlImagem; | ||
|
||
public Conteudo(String titulo, String urlImagem) { | ||
this.titulo = titulo; | ||
this.urlImagem = urlImagem; | ||
} | ||
|
||
public String getTitulo() { | ||
return titulo; | ||
} | ||
|
||
public String getUrlImagem() { | ||
return urlImagem; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import java.util.List; | ||
|
||
public interface ExtratorConteudo { | ||
|
||
List<Conteudo> extrairConteudos(String json); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import com.google.gson.Gson; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ExtratorConteudoIMDB implements ExtratorConteudo { | ||
|
||
public List<Conteudo> extrairConteudos(String json) { | ||
|
||
// extrair os dados que interessam (titulo, poster, classificação) | ||
Gson gson = new Gson(); | ||
ListaDeFilmes filmeList = gson.fromJson(json, ListaDeFilmes.class); | ||
|
||
List<Conteudo> conteudos = new ArrayList<>(); | ||
|
||
// popular a lista | ||
for (Filme filme : filmeList.items) { | ||
String titulo = filme.getTitle() | ||
.replace(":", "-"); | ||
String urlImagem = filme.getImage() | ||
.replaceAll("(@+)(.*).jpg$", "$1.jpg"); | ||
var conteudo = new Conteudo(titulo, urlImagem); | ||
|
||
conteudos.add(conteudo); | ||
} | ||
|
||
return conteudos; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import com.google.gson.Gson; | ||
import com.google.gson.reflect.TypeToken; | ||
|
||
import java.lang.reflect.Type; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class ExtratorConteudoNasa implements ExtratorConteudo { | ||
|
||
public List<Conteudo> extrairConteudos(String json) { | ||
|
||
// extrair os dados que interessam (titulo, poster, classificação) | ||
Gson gson = new Gson(); | ||
Type apodListType = new TypeToken<List<Apod>>(){}.getType(); | ||
List<Apod> apodList = gson.fromJson(json, apodListType); | ||
|
||
List<Conteudo> conteudos = new ArrayList<>(); | ||
|
||
// popular a lista | ||
for (Apod apod : apodList) { | ||
String titulo = apod.getTitle() | ||
.replace(":", "-"); | ||
String urlImagem = apod.getUrl(); | ||
var conteudo = new Conteudo(titulo, urlImagem); | ||
|
||
conteudos.add(conteudo); | ||
} | ||
|
||
return conteudos; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,23 @@ | ||
public class Filme { | ||
private String title; | ||
private String imDbRating; | ||
private String image; | ||
private final String title; | ||
private final String image; | ||
private final String imDbRating; | ||
|
||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public void setTitle(String title) { | ||
public Filme(String title, String image, String imDbRating) { | ||
this.title = title; | ||
this.image = image; | ||
this.imDbRating = imDbRating; | ||
} | ||
|
||
public String getImDbRating() { | ||
return imDbRating; | ||
} | ||
|
||
public void setImDbRating(String imDbRating) { | ||
this.imDbRating = imDbRating; | ||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
public String getImage() { | ||
return image; | ||
} | ||
|
||
public void setImage(String image) { | ||
this.image = image; | ||
public String getImDbRating() { | ||
return imDbRating; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,5 @@ | ||
import java.util.List; | ||
|
||
public class ListaDeFilmes { | ||
private List<Filme> items; | ||
|
||
public List<Filme> getItems() { | ||
return items; | ||
} | ||
|
||
public void setItems(List<Filme> items) { | ||
this.items = items; | ||
} | ||
List<Filme> items; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,37 @@ | ||
import com.diogonunes.jcolor.Attribute; | ||
import com.google.gson.Gson; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.net.http.HttpResponse.BodyHandlers; | ||
|
||
import static com.diogonunes.jcolor.Ansi.colorize; | ||
import static com.diogonunes.jcolor.Attribute.*; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
import java.util.List; | ||
|
||
|
||
public class Main { | ||
public static void main(String[] args) throws IOException, InterruptedException { | ||
|
||
// fazer a requisição HTTP com o metodo GET dos top 250 filmes de acordo com o site imdb. | ||
|
||
String url = "https://mocki.io/v1/9a7c1ca9-29b4-4eb3-8306-1adb9d159060"; | ||
URI endereco = URI.create(url); | ||
HttpClient httpClient = HttpClient.newHttpClient(); | ||
HttpRequest request = HttpRequest.newBuilder(endereco).GET().build(); | ||
HttpResponse<String> response = httpClient.send(request, BodyHandlers.ofString()); | ||
String body = response.body(); | ||
ExtratorConteudo extrator = new ExtratorConteudoIMDB(); | ||
|
||
// String url = "https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY&start_date=2022-07-20&end_date=2022-07-22"; | ||
// ExtratorConteudo extrator = new ExtratorConteudoNasa(); | ||
|
||
var http = new ClienteHttp(); | ||
String jsonText = http.buscaDados(url); | ||
|
||
// extrair os dados que interessam (titulo, poster, classificação) | ||
// exibir e manipular os dados | ||
List<Conteudo> conteudos = extrator.extrairConteudos(jsonText); | ||
|
||
Gson gsonParser = new Gson(); | ||
ListaDeFilmes filmes = gsonParser.fromJson(body, ListaDeFilmes.class); | ||
var generator = new StickerGenerator(); | ||
|
||
// manipular e exibir os dados | ||
for (int k = 0; k < 3; k++) { | ||
|
||
Attribute[] myFormat1 = new Attribute[]{BLACK_TEXT(), CYAN_BACK(), BOLD()}; | ||
Attribute[] myFormat2 = new Attribute[]{BRIGHT_CYAN_TEXT(), BOLD()}; | ||
Conteudo conteudo = conteudos.get(k); | ||
|
||
for (Filme filme : filmes.getItems()) { | ||
System.out.println(colorize("Nome: " + filme.getTitle(), myFormat1)); | ||
System.out.println(colorize("Capa[url]: ", myFormat2) + filme.getImage()); | ||
System.out.println(colorize("Nota: " + filme.getImDbRating(),myFormat2)); | ||
System.out.println("\n"); | ||
try (InputStream inputStream = new URL(conteudo.getUrlImagem()).openStream()) { | ||
System.out.println(conteudo.getTitulo()); | ||
generator.criar(inputStream, "saida/" + conteudo.getTitulo() + ".png"); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import javax.imageio.ImageIO; | ||
import java.awt.*; | ||
import java.awt.font.TextLayout; | ||
import java.awt.image.BufferedImage; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URL; | ||
|
||
public class StickerGenerator { | ||
|
||
public void criar(InputStream inputStream, String nomeArquivo) throws IOException { | ||
|
||
// leitura da imagem | ||
// InputStream inputStream = new FileInputStream("entrada/filme-maior.jpg"); | ||
// InputStream inputStream = new URL("https://m.media-amazon.com/images/M/MV5BMDFkYTc0MGEtZmNhMC00ZDIzLWFmNTEtODM1ZmRlYWMwMWFmXkEyXkFqcGdeQXVyMTMxODk2OTU@.jpg").openStream(); | ||
BufferedImage imagemOriginal = ImageIO.read(inputStream); | ||
|
||
// criar nova imagem em memoria com transparencia e com tamanho novo | ||
int largura = imagemOriginal.getWidth(); | ||
int altura = imagemOriginal.getHeight(); | ||
int novaAltura = altura + 200; | ||
BufferedImage novaImagem = new BufferedImage(largura, novaAltura, BufferedImage.TRANSLUCENT); | ||
|
||
// copiar a imagem original pra novo imagem (em memoria) | ||
Graphics2D graphics = (Graphics2D) novaImagem.getGraphics(); | ||
graphics.drawImage(imagemOriginal, 0, 0, null); | ||
|
||
// configurar a fonte | ||
Font fonte = new Font("impact", Font.BOLD, 64); | ||
graphics.setColor(Color.yellow); | ||
graphics.setFont(fonte); | ||
|
||
// escrever uma frase na nova imagem | ||
String mensagem = "TOPZERA"; | ||
int centralizarMensagem = (int) (largura / 3.3); | ||
graphics.drawString(mensagem, centralizarMensagem, novaAltura - 100); | ||
|
||
// fazer o contorno(outline) na mensagem | ||
TextLayout tl = new TextLayout(mensagem, fonte, graphics.getFontRenderContext()); | ||
Shape shape = tl.getOutline(null); | ||
|
||
graphics.translate(centralizarMensagem, novaAltura - 100); | ||
|
||
graphics.setColor(Color.BLACK); | ||
graphics.setStroke(new BasicStroke(2f)); | ||
graphics.draw(shape); | ||
|
||
// escrever a imagem final em disco | ||
File imagemFinal = new File(nomeArquivo); | ||
if (!imagemFinal.getParentFile().exists()) imagemFinal.getParentFile().mkdirs(); | ||
ImageIO.write(novaImagem, "png", imagemFinal); | ||
|
||
} | ||
|
||
// public static void main(String[] args) throws IOException { | ||
// StickerGenerator stickerGenerator = new StickerGenerator(); | ||
// InputStream inputStream = new URL("https://m.media-amazon.com/images/M/MV5BMDFkYTc0MGEtZmNhMC00ZDIzLWFmNTEtODM1ZmRlYWMwMWFmXkEyXkFqcGdeQXVyMTMxODk2OTU@.jpg").openStream(); | ||
// stickerGenerator.criar(inputStream, "saida/filme5.png"); | ||
// } | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.