-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
10 changed files
with
157 additions
and
88 deletions.
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
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,86 @@ | ||
package kr.apo2073.ytliv; | ||
|
||
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; | ||
import com.google.api.client.json.jackson2.JacksonFactory; | ||
import com.google.api.services.youtube.YouTube; | ||
import com.google.api.services.youtube.model.Channel; | ||
|
||
import java.io.IOException; | ||
import java.security.GeneralSecurityException; | ||
import java.util.List; | ||
|
||
public class YouTubeInfo { | ||
private final String channelName; | ||
private final String channelSubscriptionCount; | ||
private final Channel channel; | ||
private static String API_KEY; | ||
|
||
public static YouTubeInfo from(String videoId, String api) { | ||
try { | ||
YouTube youtube = new YouTube.Builder( | ||
GoogleNetHttpTransport.newTrustedTransport(), | ||
JacksonFactory.getDefaultInstance(), | ||
null | ||
).setApplicationName("YouTubeLiv").build(); | ||
API_KEY=api; | ||
|
||
return new YouTubeInfo(youtube, videoId); | ||
} catch (GeneralSecurityException | IOException e) { | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
} | ||
|
||
private YouTubeInfo(YouTube youtube, String videoId) { | ||
Channel tempChannel = null; | ||
String tempName = null; | ||
String tempCount = null; | ||
|
||
try { | ||
tempChannel = getChannel(youtube, videoId); | ||
if (tempChannel != null) { | ||
tempName = tempChannel.getSnippet().getTitle(); | ||
tempCount = tempChannel.getStatistics().getSubscriberCount().toString(); | ||
} | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
this.channel = tempChannel; | ||
this.channelName = tempName; | ||
this.channelSubscriptionCount = tempCount; | ||
} | ||
|
||
private Channel getChannel(YouTube youtube, String videoId) throws IOException { | ||
YouTube.Videos.List videoRequest = youtube.videos() | ||
.list(List.of("snippet")) | ||
.setKey(API_KEY) | ||
.setId(List.of(videoId)); | ||
|
||
var videoResponse = videoRequest.execute(); | ||
if (videoResponse.getItems() == null || videoResponse.getItems().isEmpty()) return null; | ||
String channelId = videoResponse.getItems().get(0).getSnippet().getChannelId(); | ||
|
||
YouTube.Channels.List channelRequest = youtube.channels() | ||
.list(List.of("snippet", "statistics")) | ||
.setKey(API_KEY) | ||
.setId(List.of(channelId)); | ||
|
||
var channelResponse = channelRequest.execute(); | ||
if (channelResponse.getItems() == null || channelResponse.getItems().isEmpty()) return null; | ||
|
||
return channelResponse.getItems().get(0); | ||
} | ||
|
||
public String getChannelName() { | ||
return channelName; | ||
} | ||
|
||
public String getSubscriptionCount() { | ||
return channelSubscriptionCount; | ||
} | ||
|
||
public Channel getChannel() { | ||
return channel; | ||
} | ||
} |
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,5 @@ | ||
package kr.apo2073.ytliv.data; | ||
|
||
import com.google.api.services.youtube.model.LiveChatMessage; | ||
|
||
public record Author(String name, LiveChatMessage message) {} |
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,21 +1,29 @@ | ||
package kr.apo2073.ytliv.data; | ||
|
||
import com.google.api.services.youtube.model.LiveChatMessage; | ||
import com.google.api.services.youtube.model.LiveChatMessageAuthorDetails; | ||
|
||
public class Chatting { | ||
private final String author; | ||
private final String message; | ||
private final LiveChatMessage liveChatMessage; | ||
private final String videoId; | ||
|
||
public Chatting(String message, LiveChatMessage liveChatMessage) { | ||
public Chatting(String author, String message, String videoId, LiveChatMessage liveChatMessage) { | ||
this.author = author; | ||
this.message = message; | ||
this.liveChatMessage = liveChatMessage; | ||
this.videoId=videoId; | ||
} | ||
|
||
public String getMessage() { | ||
return message; | ||
} | ||
public LiveChatMessageAuthorDetails author() { | ||
return liveChatMessage.getAuthorDetails(); | ||
|
||
public String getVideoId() { | ||
return videoId; | ||
} | ||
|
||
public Author author() { | ||
return new Author(author, liveChatMessage); | ||
} | ||
} |
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
Oops, something went wrong.