-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LLM POC #31
Merged
LLM POC #31
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
a6a58e8
Update gradle-wrapper version
leingenm 6425ac8
Update dependencies versions
leingenm 75ba726
Update the method name that creates the YouTube client
leingenm 5564119
Add "content details" part constant
leingenm 57a3b34
Add method to TokenService to get access token from the `authorizedCl…
leingenm 733c8b6
Add a service-layer method to retrieve data for videos
leingenm b44bdd5
Add controller to load video data by ids and update service layer
leingenm b74a874
Add JPA and Docker Compose dependencies
leingenm 0b0e449
Add docker compose & set necessary properties inside application.yml
leingenm 1eb490d
Update uri for VideoController to load data by video ids
leingenm 73f5b69
Create a basic flow to add watch later data in DB
leingenm 4f3234a
Remove commented out line
leingenm 67306a0
Update how env variables are set in compose file
leingenm 125adef
Remove commented out code in application.yml
leingenm 138ef8b
Update `getVideoData` method to iteratively get video data
leingenm f34116c
Update method name
leingenm e5af6dc
Remove hibernate.ddl-auto property in config
leingenm 7e53a31
Bump spring version to 3.3.3
leingenm 76c3088
Implement token caching
leingenm 02d2438
Move mapper into `dto` package
leingenm 5fd9e33
Move YouTube-related services into a separate package
leingenm a1e9004
Replace an explicit constructor call with @RequiredArgsConstructor an…
leingenm 3bc8989
Add @Transactional to `importCsv` method
leingenm 24398a1
PR fixes
leingenm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,14 @@ | ||
services: | ||
postgres-db: | ||
image: 'postgres:latest' | ||
environment: | ||
- POSTGRES_DB=ypm-db | ||
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD} | ||
- POSTGRES_USER=${POSTGRES_USERNAME} | ||
ports: | ||
- '5432:5432' | ||
volumes: | ||
- ypm-db:/var/lib/postgresql/data | ||
|
||
volumes: | ||
ypm-db: |
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
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
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
32 changes: 32 additions & 0 deletions
32
src/main/java/com/ypm/controller/LibraryImportController.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,32 @@ | ||
package com.ypm.controller; | ||
|
||
import com.ypm.persistence.entity.VideoImport; | ||
import com.ypm.service.youtube.ImportService; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
import org.springframework.web.multipart.MultipartFile; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
@RestController | ||
@RequestMapping("/import") | ||
@RequiredArgsConstructor | ||
public class LibraryImportController { | ||
|
||
private final ImportService importService; | ||
|
||
@PostMapping("/watch-later") | ||
public ResponseEntity<String> importWatchLaterLibrary(@RequestParam("file") MultipartFile file) throws IOException { | ||
if (file.isEmpty()) { | ||
return ResponseEntity.badRequest().build(); | ||
} | ||
|
||
List<VideoImport> savedVideos; | ||
savedVideos = importService.importCsv(file); | ||
|
||
var responseBody = String.format("Saved %s videos", savedVideos.size()); | ||
return ResponseEntity.ok().body(responseBody); | ||
} | ||
} |
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
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,11 @@ | ||
package com.ypm.dto; | ||
|
||
import java.util.List; | ||
|
||
public record VideoDto( | ||
String id, | ||
String title, | ||
String description, | ||
List<String> tags, | ||
String channelName) { | ||
} |
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,25 @@ | ||
package com.ypm.dto.mapper; | ||
|
||
import com.google.api.services.youtube.model.Video; | ||
import com.ypm.dto.VideoDto; | ||
|
||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class VideoMapper { | ||
|
||
public static List<VideoDto> mapToVideoDto(List<Video> videos) { | ||
return videos.stream() | ||
.map(video -> { | ||
var snippet = video.getSnippet(); | ||
return new VideoDto( | ||
video.getId(), | ||
snippet.getTitle(), | ||
snippet.getDescription(), | ||
snippet.getTags(), | ||
snippet.getChannelTitle() | ||
); | ||
}) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
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,26 @@ | ||
package com.ypm.persistence.entity; | ||
|
||
import jakarta.persistence.Column; | ||
import jakarta.persistence.Entity; | ||
import jakarta.persistence.Id; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.OffsetDateTime; | ||
|
||
@Entity | ||
@Data | ||
@NoArgsConstructor | ||
public class VideoImport { | ||
|
||
@Id | ||
private String videoId; | ||
|
||
@Column(name = "creation_timestamp") | ||
private OffsetDateTime dateAdded; | ||
|
||
public VideoImport(String videoId, String videoTimeStamp) { | ||
this.videoId = videoId; | ||
this.dateAdded = OffsetDateTime.parse(videoTimeStamp); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/ypm/persistence/repository/VideoRepository.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,9 @@ | ||
package com.ypm.persistence.repository; | ||
|
||
import com.ypm.persistence.entity.VideoImport; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
daverbk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public interface VideoRepository extends JpaRepository<VideoImport, Long> { | ||
} |
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,12 +1,45 @@ | ||
package com.ypm.service; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient; | ||
import org.springframework.security.oauth2.client.OAuth2AuthorizedClientService; | ||
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.time.Instant; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class TokenService { | ||
|
||
private final OAuth2AuthorizedClientService authorizedClientService; | ||
|
||
private String cachedToken; | ||
private Instant expiresAt; | ||
|
||
public String getToken(OAuth2AuthorizedClient authClient) { | ||
return authClient.getAccessToken().getTokenValue(); | ||
} | ||
|
||
public String getToken() { | ||
if (isTokenExpired()) refreshToken(); | ||
|
||
return cachedToken; | ||
} | ||
|
||
private boolean isTokenExpired() { | ||
return cachedToken == null || Instant.now().isAfter(expiresAt); | ||
} | ||
|
||
private void refreshToken() { | ||
var oauthToken = (OAuth2AuthenticationToken) SecurityContextHolder.getContext().getAuthentication(); | ||
var clientRegistrationId = oauthToken.getAuthorizedClientRegistrationId(); | ||
var principalName = oauthToken.getPrincipal().getName(); | ||
var client = authorizedClientService.loadAuthorizedClient(clientRegistrationId, principalName); | ||
|
||
var accessToken = client.getAccessToken(); | ||
cachedToken = accessToken.getTokenValue(); | ||
expiresAt = accessToken.getExpiresAt(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just out of curiosity - why do we need a volume here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So the data is persistent even if we stop and start a container with PostgreSQL. But I see a point in not making it persistent