Skip to content

Commit

Permalink
LLM POC (#31)
Browse files Browse the repository at this point in the history
* Update gradle-wrapper version

* Update dependencies versions

* Update the method name that creates the YouTube client

* Add "content details" part constant

* Add method to TokenService to get access token from the `authorizedClientService`

* Add a service-layer method to retrieve data for videos

* Add controller to load video data by ids and update service layer

* Add JPA and Docker Compose dependencies

* Add docker compose & set necessary properties inside application.yml

* Update uri for VideoController to load data by video ids

* Create a basic flow to add watch later data in DB

* Remove commented out line

* Update how env variables are set in compose file

* Remove commented out code in application.yml

* Update `getVideoData` method to iteratively get video data

YouTube API does not allow getting more than 50 videos at once, so we need to iteratively request data if the provided list with ids has more than 50 items.

* Update method name

* Remove hibernate.ddl-auto property in config

* Bump spring version to 3.3.3

* Implement token caching

* Move mapper into `dto` package

* Move YouTube-related services into a separate package

* Replace an explicit constructor call with @requiredargsconstructor annotation

* Add @transactional to `importCsv` method

* PR fixes
  • Loading branch information
leingenm authored Sep 1, 2024
1 parent be7f5f7 commit 8cfca26
Show file tree
Hide file tree
Showing 26 changed files with 303 additions and 52 deletions.
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:
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 {
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
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

0 comments on commit 8cfca26

Please sign in to comment.