-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #172 from bounswe/imdb-api
Imdb api
- Loading branch information
Showing
8 changed files
with
228 additions
and
0 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
20 changes: 20 additions & 0 deletions
20
practice-app/backend/disasterresponse/src/main/java/Response/ImdbResponse/Image.java
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,20 @@ | ||
package Response.ImdbResponse; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class Image { | ||
private int height; | ||
private String id; | ||
private String url; | ||
private int width; | ||
|
||
} | ||
|
||
|
33 changes: 33 additions & 0 deletions
33
practice-app/backend/disasterresponse/src/main/java/Response/ImdbResponse/ImdbResponse.java
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,33 @@ | ||
package Response.ImdbResponse; | ||
|
||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class ImdbResponse { | ||
private String type; | ||
private String query; | ||
@JsonProperty("results") | ||
private List<Movie> movies; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
28 changes: 28 additions & 0 deletions
28
practice-app/backend/disasterresponse/src/main/java/Response/ImdbResponse/Movie.java
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,28 @@ | ||
package Response.ImdbResponse; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonRootName; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
@JsonRootName(value = "results") | ||
public class Movie { | ||
private String id; | ||
private Image image; | ||
private int runningTimeInMinutes; | ||
private String nextEpisode; | ||
private int numberOfEpisodes; | ||
private int seriesEndYear; | ||
private int seriesStartYear; | ||
private String title; | ||
private String titleType; | ||
private int year; | ||
private List<Principal> principals; | ||
} |
33 changes: 33 additions & 0 deletions
33
practice-app/backend/disasterresponse/src/main/java/Response/ImdbResponse/Principal.java
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,33 @@ | ||
package Response.ImdbResponse; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
class Principal { | ||
|
||
private String id; | ||
|
||
private String legacyNameText; | ||
|
||
private String name; | ||
|
||
private String category; | ||
|
||
private List<String> characters; | ||
|
||
private int endYear; | ||
|
||
private int episodeCount; | ||
|
||
private List<Role> roles; | ||
|
||
private int startYear; | ||
} |
16 changes: 16 additions & 0 deletions
16
practice-app/backend/disasterresponse/src/main/java/Response/ImdbResponse/Role.java
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,16 @@ | ||
package Response.ImdbResponse; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class Role { | ||
private String character; | ||
private String characterId; | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
...end/disasterresponse/src/main/java/com/a1/disasterresponse/controller/FilmController.java
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,35 @@ | ||
package com.a1.disasterresponse.controller; | ||
|
||
import Response.ImdbResponse.Movie; | ||
import com.a1.disasterresponse.service.FilmService; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
@Controller | ||
@RequestMapping("/film") | ||
public class FilmController { | ||
|
||
private final FilmService filmService; | ||
|
||
public FilmController(FilmService filmService) { | ||
this.filmService = filmService; | ||
} | ||
|
||
@GetMapping("/id") | ||
public ResponseEntity<List<Movie>> getFilmId(@RequestParam String query, @RequestParam int limit) { | ||
try { | ||
List<Movie> movies = filmService.getFilmId(query, limit); | ||
return new ResponseEntity<>(movies, HttpStatus.OK); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...p/backend/disasterresponse/src/main/java/com/a1/disasterresponse/service/FilmService.java
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,53 @@ | ||
package com.a1.disasterresponse.service; | ||
|
||
import Response.ImdbResponse.ImdbResponse; | ||
import Response.ImdbResponse.Movie; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import okhttp3.OkHttpClient; | ||
import okhttp3.Request; | ||
import okhttp3.Response; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.io.IOException; | ||
import java.net.URLEncoder; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
|
||
|
||
@Service | ||
public class FilmService { | ||
String BASE_URL = "https://imdb8.p.rapidapi.com/title/v2/find?title="; | ||
|
||
String API_KEY = "8720851bd4msh5b9408dcf528f49p16c2cfjsna4066bc59d74"; | ||
|
||
private static final OkHttpClient client = new OkHttpClient(); | ||
|
||
private static final ObjectMapper mapper = new ObjectMapper(); | ||
|
||
|
||
public List<Movie> getFilmId(String query, int limit) throws IOException { | ||
|
||
String encodedQueryParam = URLEncoder.encode(query, StandardCharsets.UTF_8).replaceAll("\\+", "%20"); | ||
|
||
Request request = new Request.Builder() | ||
.url(BASE_URL + encodedQueryParam + "&limit="+ Integer.toString(limit)+"&sortArg=moviemeter%2Casc") | ||
.get() | ||
.addHeader("X-RapidAPI-Key", API_KEY) | ||
.addHeader("X-RapidAPI-Host", "imdb8.p.rapidapi.com") | ||
.build(); | ||
try (Response response = client.newCall(request).execute()) { | ||
if (!response.isSuccessful()) { | ||
throw new IOException("Unexpected code " + response); | ||
} | ||
String body = response.body().string(); | ||
|
||
ImdbResponse imdbResponse = mapper.readValue(body, ImdbResponse.class); | ||
return imdbResponse.getMovies(); | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
} | ||
|