Skip to content

Commit

Permalink
feat: 드라마, 영화 포스터 저장 완료
Browse files Browse the repository at this point in the history
  • Loading branch information
JONG-KYEONG committed Aug 13, 2024
1 parent c08394e commit 70ecc53
Show file tree
Hide file tree
Showing 19 changed files with 138 additions and 13 deletions.
Binary file modified server/.gradle/8.8/executionHistory/executionHistory.bin
Binary file not shown.
Binary file modified server/.gradle/8.8/executionHistory/executionHistory.lock
Binary file not shown.
Binary file modified server/.gradle/8.8/fileHashes/fileHashes.bin
Binary file not shown.
Binary file modified server/.gradle/8.8/fileHashes/fileHashes.lock
Binary file not shown.
Binary file modified server/.gradle/8.8/fileHashes/resourceHashesCache.bin
Binary file not shown.
Binary file modified server/.gradle/buildOutputCleanup/buildOutputCleanup.lock
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
9 changes: 6 additions & 3 deletions server/build/resources/main/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@
#app.auth.tokenExpirationMsec = 864000000
#app.cors.allowedOrigins=http://localhost:3000,http://localhost:8080,https://port-0-hackathon-be-lyqylohp8957ca6e.sel5.cloudtype.app/
#app.oauth2.authorizedRedirectUris=http://localhost:3000/oauth2/redirect,myandroidapp://oauth2/redirect,myiosapp://oauth2/redirect
#
#tmdb.api.key=${TMDB_API_KEY}


#OAuth2
#???
spring.security.oauth2.client.registration.google.client-id=240772131497-d17ho20q4gb42n5s42ncohdh5k9379rc.apps.googleusercontent.com
spring.security.oauth2.client.registration.google.client-secret=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
spring.security.oauth2.client.registration.google.redirect-uri=http://glogglogglog-env.eba-fuksumx7.ap-northeast-2.elasticbeanstalk.com/oauth2/callback/google
Expand Down Expand Up @@ -86,4 +87,6 @@ spring.datasource.password=0322!
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.ddl-auto=update

tmdb.api.key=808f8aebcaf5dd72860ce88499d315e3
Binary file not shown.
Binary file not shown.
Binary file modified server/build/tmp/compileJava/previous-compilation-data.bin
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public class DataController {

private final DataService dataService;

@PostMapping("/upload")
public String uploadCSV(@RequestParam("file") MultipartFile file) {
try {
dataService.saveCsv(file);
return "File uploaded and data saved successfully!";
} catch (Exception e) {
return "Failed to upload and save data: " + e.getMessage();
}
}
// @PostMapping("/upload")
// public String uploadCSV(@RequestParam("file") MultipartFile file) {
// try {
// dataService.saveCsv(file);
// return "File uploaded and data saved successfully!";
// } catch (Exception e) {
// return "Failed to upload and save data: " + e.getMessage();
// }
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public class DataField {
private Long id;
@Column(name = "titleName")
private String titleName;
@Column(name = "mediaType")
private String mediaType;
@Column(name = "image")
private String image;
}
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;
}
}
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";
// }
}
4 changes: 3 additions & 1 deletion server/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,6 @@ cloud.aws.stack.auto=false
app.auth.tokenSecret=04ca023b39512e46d0c2cf4b48d5aac61d34302994c87ed4eff225dcf3b0a218739f3897051a057f9b846a69ea2927a587044164b7bae5e1306219d50b588cb1
app.auth.tokenExpirationMsec = 864000000
app.cors.allowedOrigins=http://localhost:3000,http://localhost:8080,https://port-0-hackathon-be-lyqylohp8957ca6e.sel5.cloudtype.app/
app.oauth2.authorizedRedirectUris=http://localhost:3000/oauth2/redirect,myandroidapp://oauth2/redirect,myiosapp://oauth2/redirect
app.oauth2.authorizedRedirectUris=http://localhost:3000/oauth2/redirect,myandroidapp://oauth2/redirect,myiosapp://oauth2/redirect

tmdb.api.key=${TMDB_API_KEY}

0 comments on commit 70ecc53

Please sign in to comment.