-
Notifications
You must be signed in to change notification settings - Fork 1
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
c08394e
commit 70ecc53
Showing
19 changed files
with
138 additions
and
13 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified
BIN
+221 Bytes
(120%)
server/build/classes/java/main/com/example/hackathon/dataset/domain/DataField.class
Binary file not shown.
Binary file added
BIN
+469 Bytes
...uild/classes/java/main/com/example/hackathon/dataset/repository/DataFieldRepository.class
Binary file not shown.
Binary file added
BIN
+4.37 KB
server/build/classes/java/main/com/example/hackathon/tmdb/application/TmdbService.class
Binary file not shown.
Binary file added
BIN
+1019 Bytes
server/build/classes/java/main/com/example/hackathon/tmdb/controller/TmdbController.class
Binary file not shown.
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
Binary file added
BIN
+1.24 KB
server/build/tmp/compileJava/compileTransaction/stash-dir/TmdbController.class.uniqueId0
Binary file not shown.
Binary file added
BIN
+2.71 KB
server/build/tmp/compileJava/compileTransaction/stash-dir/TmdbService.class.uniqueId1
Binary file not shown.
Binary file modified
BIN
+240 Bytes
(100%)
server/build/tmp/compileJava/previous-compilation-data.bin
Binary file not shown.
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
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
95 changes: 95 additions & 0 deletions
95
server/src/main/java/com/example/hackathon/tmdb/application/TmdbService.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,95 @@ | ||
package com.example.hackathon.tmdb.application; | ||
|
||
import com.example.hackathon.dataset.domain.DataField; | ||
import com.example.hackathon.dataset.repository.DataFieldRepository; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
import org.springframework.web.util.UriComponentsBuilder; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
@Service | ||
public class TmdbService { | ||
|
||
@Value("${tmdb.api.key}") | ||
private String apiKey; | ||
|
||
private final RestTemplate restTemplate = new RestTemplate(); | ||
|
||
@Autowired | ||
private DataFieldRepository dataFieldRepository; | ||
|
||
public void saveImage(){ | ||
List<DataField> dataFields = dataFieldRepository.findAll(); | ||
for(DataField dataField : dataFields){ | ||
if(dataField.getMediaType().equals("movie")){ | ||
dataField.setImage(getMovieImage(dataField.getTitleName())); | ||
dataFieldRepository.save(dataField); | ||
} | ||
else{ | ||
dataField.setImage(getTvImage(dataField.getTitleName())); | ||
dataFieldRepository.save(dataField); | ||
} | ||
} | ||
} | ||
|
||
public String getTvImage(String query) { | ||
String searchUrl = UriComponentsBuilder.fromHttpUrl("https://api.themoviedb.org/3/search/tv") | ||
.queryParam("api_key", apiKey) | ||
.queryParam("query", query) | ||
.queryParam("language", "ko-KR") | ||
.toUriString(); | ||
|
||
Map<String, Object> searchResponse = restTemplate.getForObject(searchUrl, Map.class); | ||
|
||
if (searchResponse != null && !((List<?>) searchResponse.get("results")).isEmpty()) { | ||
Map<String, Object> firstResult = ((List<Map<String, Object>>) searchResponse.get("results")).get(0); | ||
String id = String.valueOf(firstResult.get("id")); | ||
|
||
String detailsUrl = UriComponentsBuilder.fromHttpUrl("https://api.themoviedb.org/3/tv/" + id) | ||
.queryParam("api_key", apiKey) | ||
.queryParam("language", "ko-KR") | ||
.toUriString(); | ||
|
||
Map<String, Object> detailsResponse = restTemplate.getForObject(detailsUrl, Map.class); | ||
|
||
if (detailsResponse != null && detailsResponse.get("poster_path") != null) { | ||
String posterPath = String.valueOf(detailsResponse.get("poster_path")); | ||
return "https://image.tmdb.org/t/p/w500" + posterPath; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
// 영화 이미지 가져오기 | ||
public String getMovieImage(String query) { | ||
String searchUrl = UriComponentsBuilder.fromHttpUrl("https://api.themoviedb.org/3/search/movie") | ||
.queryParam("api_key", apiKey) | ||
.queryParam("query", query) | ||
.queryParam("language", "ko-KR") | ||
.toUriString(); | ||
|
||
Map<String, Object> searchResponse = restTemplate.getForObject(searchUrl, Map.class); | ||
|
||
if (searchResponse != null && !((List<?>) searchResponse.get("results")).isEmpty()) { | ||
Map<String, Object> firstResult = ((List<Map<String, Object>>) searchResponse.get("results")).get(0); | ||
String id = String.valueOf(firstResult.get("id")); | ||
|
||
String detailsUrl = UriComponentsBuilder.fromHttpUrl("https://api.themoviedb.org/3/movie/" + id) | ||
.queryParam("api_key", apiKey) | ||
.queryParam("language", "ko-KR") | ||
.toUriString(); | ||
|
||
Map<String, Object> detailsResponse = restTemplate.getForObject(detailsUrl, Map.class); | ||
|
||
if (detailsResponse != null && detailsResponse.get("poster_path") != null) { | ||
String posterPath = String.valueOf(detailsResponse.get("poster_path")); | ||
return "https://image.tmdb.org/t/p/w500" + posterPath; | ||
} | ||
} | ||
return null; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
server/src/main/java/com/example/hackathon/tmdb/controller/TmdbController.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,23 @@ | ||
package com.example.hackathon.tmdb.controller; | ||
|
||
import com.example.hackathon.tmdb.application.TmdbService; | ||
import lombok.AllArgsConstructor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@AllArgsConstructor | ||
@RequestMapping("/api") | ||
public class TmdbController { | ||
|
||
private final TmdbService tmdbService; | ||
|
||
// @GetMapping("/movie-image") | ||
// public String getMovieImage() { | ||
// tmdbService.saveImage(); | ||
// return "success save image"; | ||
// } | ||
} |
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