-
Notifications
You must be signed in to change notification settings - Fork 4
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
1 parent
0830a90
commit a289ccd
Showing
3 changed files
with
94 additions
and
9 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,13 +1,73 @@ | ||
package com.alura; | ||
|
||
/** | ||
* Hello world! | ||
* | ||
*/ | ||
public class App | ||
{ | ||
public static void main( String[] args ) | ||
{ | ||
System.out.println( "Hello World!" ); | ||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse.BodyHandlers; | ||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import org.apache.commons.configuration.ConfigurationException; | ||
import org.apache.commons.configuration.PropertiesConfiguration; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
@SuppressWarnings("squid:S106") | ||
public class App { | ||
|
||
public static String getMovies(String api) throws IOException, InterruptedException { | ||
return HttpClient.newHttpClient() | ||
.send(HttpRequest.newBuilder(URI.create(api)).GET().build(), | ||
BodyHandlers.ofString()) | ||
.body(); | ||
} | ||
|
||
public static String customText(String custom, String text) { | ||
return custom + text + "\033[0m"; | ||
} | ||
|
||
public static String countStar(Double rating) { | ||
StringBuilder star = new StringBuilder(); | ||
for (int i = 0; i < Math.round(rating); i++) { | ||
star.append("\u2605 "); | ||
} | ||
return star.toString(); | ||
} | ||
|
||
public static void printMovies(List<Movie> movies) { | ||
for (Movie movie : movies) { | ||
System.out.println("Titulo: " + customText("\033[1;37m", movie.getTitle())); | ||
System.out.println("Image: " + customText("\033[1;37m", movie.getImage())); | ||
Optional.ofNullable(movie.getImDbRating()).ifPresentOrElse( | ||
rating -> { | ||
System.out.println(customText("\033[0;105m", "Classificao: " + rating)); | ||
System.out.println(customText("\033[1;33m", countStar(rating))); | ||
}, | ||
() -> System.out.println(customText("\033[0;105m", "Sem Classificao"))); | ||
} | ||
} | ||
|
||
public static void main(String[] args) { | ||
try { | ||
PropertiesConfiguration property = new PropertiesConfiguration(); | ||
property.load("app.properties"); | ||
|
||
String url = property.getString("most.popular.tv") + System.getProperty("key"); | ||
|
||
var mapper = new ObjectMapper(); | ||
List<Movie> movies = mapper.treeToValue( | ||
mapper.readTree(getMovies(url)).get("items"), | ||
mapper.getTypeFactory().constructCollectionType( | ||
List.class, | ||
Movie.class)); | ||
|
||
printMovies(movies); | ||
|
||
} catch (IOException | ConfigurationException e) { | ||
e.printStackTrace(); | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} | ||
} | ||
} |
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,21 @@ | ||
package com.alura; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
|
||
import lombok.Getter; | ||
|
||
@Getter | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class Movie { | ||
|
||
private String id; | ||
private Long rank; | ||
private String title; | ||
private String fullTitle; | ||
private int year; | ||
private String image; | ||
private String crew; | ||
private Double imDbRating; | ||
private Long imDbRatingCount; | ||
|
||
} |
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,4 @@ | ||
top.250.movies=https://imdb-api.com/en/API/Top250Movies/ | ||
most.popular.movies=https://imdb-api.com/en/API/MostPopularMovies/ | ||
top.250.tv=https://imdb-api.com/en/API/Top250TVs/ | ||
most.popular.tv=https://imdb-api.com/en/API/MostPopularTVs/ |