Skip to content
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
merged 24 commits into from
Sep 1, 2024
Merged
Show file tree
Hide file tree
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 Aug 10, 2024
6425ac8
Update dependencies versions
leingenm Aug 10, 2024
75ba726
Update the method name that creates the YouTube client
leingenm Aug 10, 2024
5564119
Add "content details" part constant
leingenm Aug 10, 2024
57a3b34
Add method to TokenService to get access token from the `authorizedCl…
leingenm Aug 10, 2024
733c8b6
Add a service-layer method to retrieve data for videos
leingenm Aug 10, 2024
b44bdd5
Add controller to load video data by ids and update service layer
leingenm Aug 10, 2024
b74a874
Add JPA and Docker Compose dependencies
leingenm Aug 11, 2024
0b0e449
Add docker compose & set necessary properties inside application.yml
leingenm Aug 11, 2024
1eb490d
Update uri for VideoController to load data by video ids
leingenm Aug 11, 2024
73f5b69
Create a basic flow to add watch later data in DB
leingenm Aug 11, 2024
4f3234a
Remove commented out line
leingenm Aug 28, 2024
67306a0
Update how env variables are set in compose file
leingenm Aug 28, 2024
125adef
Remove commented out code in application.yml
leingenm Aug 28, 2024
138ef8b
Update `getVideoData` method to iteratively get video data
leingenm Aug 28, 2024
f34116c
Update method name
leingenm Aug 28, 2024
e5af6dc
Remove hibernate.ddl-auto property in config
leingenm Aug 29, 2024
7e53a31
Bump spring version to 3.3.3
leingenm Aug 29, 2024
76c3088
Implement token caching
leingenm Aug 29, 2024
02d2438
Move mapper into `dto` package
leingenm Aug 29, 2024
5fd9e33
Move YouTube-related services into a separate package
leingenm Aug 29, 2024
a1e9004
Replace an explicit constructor call with @RequiredArgsConstructor an…
leingenm Aug 29, 2024
3bc8989
Add @Transactional to `importCsv` method
leingenm Aug 29, 2024
24398a1
PR fixes
leingenm Aug 31, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
plugins {
java
id("org.springframework.boot") version "3.2.3"
id("io.spring.dependency-management") version "1.1.4"
id("org.springframework.boot") version "3.3.3"
id("io.spring.dependency-management") version "1.1.6"
}

group = "com.ypm"
version = "0.0.1-SNAPSHOT"

java {
sourceCompatibility = JavaVersion.VERSION_17
toolchain {
languageVersion = JavaLanguageVersion.of(17)
}
}

configurations {
Expand All @@ -27,16 +29,23 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-security")
implementation("org.springframework.boot:spring-boot-starter-oauth2-client")
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
implementation("org.springframework.boot:spring-boot-starter-actuator")

// Data Access
implementation("org.springframework.boot:spring-boot-starter-data-jpa")
runtimeOnly("org.postgresql:postgresql")

// Dev
developmentOnly("org.springframework.boot:spring-boot-devtools")
developmentOnly("org.springframework.boot:spring-boot-docker-compose")
annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")

// YouTube Client
implementation("com.google.apis:google-api-services-youtube:v3-rev20240310-2.0.0")
implementation("com.google.api-client:google-api-client:2.4.0")
implementation("com.google.api-client:google-api-client:2.6.0")
implementation("com.google.http-client:google-http-client:1.44.1")
implementation("com.google.oauth-client:google-oauth-client-jetty:1.35.0")
implementation("com.google.code.gson:gson:2.10.1")
implementation("com.google.oauth-client:google-oauth-client-jetty:1.36.0")
implementation("com.google.code.gson:gson:2.10")

// Lombok
compileOnly("org.projectlombok:lombok")
Expand Down
14 changes: 14 additions & 0 deletions compose.yaml
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:
Copy link
Collaborator

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?

Copy link
Owner Author

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

ypm-db:
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
17 changes: 1 addition & 16 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,21 +1,6 @@
#
# Copyright 2012-2024 the original author or authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
7 changes: 5 additions & 2 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0
#

##############################################################################
#
Expand Down Expand Up @@ -55,7 +57,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down Expand Up @@ -84,7 +86,8 @@ done
# shellcheck disable=SC2034
APP_BASE_NAME=${0##*/}
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
APP_HOME=$( cd -P "${APP_HOME:-./}" > /dev/null && printf '%s
' "$PWD" ) || exit

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD=maximum
Expand Down
22 changes: 12 additions & 10 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
@rem See the License for the specific language governing permissions and
@rem limitations under the License.
@rem
@rem SPDX-License-Identifier: Apache-2.0
@rem

@if "%DEBUG%"=="" @echo off
@rem ##########################################################################
Expand Down Expand Up @@ -43,11 +45,11 @@ set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand All @@ -57,11 +59,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
public class YouTubeClientConfiguration {

@Bean
public YouTube getYouTubeClient() throws GeneralSecurityException, IOException {
public YouTube youTubeClient() throws GeneralSecurityException, IOException {
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GsonFactory jsonFactory = GsonFactory.getDefaultInstance();

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/ypm/constant/Part.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.ypm.constant;

public enum Part {
SNIPPET, STATUS;
SNIPPET, STATUS, CONTENT_DETAILS;

@Override
public String toString() {
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/ypm/controller/LibraryImportController.java
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);
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/ypm/controller/PlayListController.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import com.google.api.services.youtube.model.PlaylistSnippet;
import com.ypm.dto.PlaylistDto;
import com.ypm.dto.request.MergePlayListsRequest;
import com.ypm.service.PlayListService;
import com.ypm.service.youtube.PlayListService;
import com.ypm.service.TokenService;
import com.ypm.service.VideoService;
import com.ypm.service.youtube.VideoService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/com/ypm/controller/VideoController.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.ypm.controller;

import com.ypm.dto.VideoDto;
import com.ypm.service.TokenService;
import com.ypm.service.VideoService;
import com.ypm.service.youtube.VideoService;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.client.OAuth2AuthorizedClient;
import org.springframework.security.oauth2.client.annotation.RegisteredOAuth2AuthorizedClient;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/videos")
Expand All @@ -21,6 +20,13 @@ public class VideoController {
private final VideoService videosService;
private final TokenService tokenService;

@PostMapping("/load")
public ResponseEntity<List<VideoDto>> getVideoData(@RequestBody List<String> videoIds) throws IOException {
daverbk marked this conversation as resolved.
Show resolved Hide resolved
var videoData = videosService.getVideoData(videoIds);

return ResponseEntity.ok(videoData);
}

@DeleteMapping("/{videoId}")
public ResponseEntity<Void> deleteVideos(
@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authClient,
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/com/ypm/dto/VideoDto.java
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) {
}
25 changes: 25 additions & 0 deletions src/main/java/com/ypm/dto/mapper/VideoMapper.java
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());
}
}
26 changes: 26 additions & 0 deletions src/main/java/com/ypm/persistence/entity/VideoImport.java
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);
}
}
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> {
}
33 changes: 33 additions & 0 deletions src/main/java/com/ypm/service/TokenService.java
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();
}
}
Loading