Skip to content

Commit

Permalink
Merge pull request #172 from bounswe/imdb-api
Browse files Browse the repository at this point in the history
Imdb api
  • Loading branch information
alperenDagi authored May 10, 2023
2 parents 2df694d + ead8b09 commit 12d5d26
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 0 deletions.
10 changes: 10 additions & 0 deletions practice-app/backend/disasterresponse/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,17 @@
<artifactId>okhttp</artifactId>
<version>4.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand Down
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;

}


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;
}












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;
}
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;
}
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;

}
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);
}
}
}
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();
}
}




}

0 comments on commit 12d5d26

Please sign in to comment.