Created
May 10, 2016 13:17
-
-
Save Ben834/019c5c6544959565c65d466fd95c8ba6 to your computer and use it in GitHub Desktop.
Java functions for retrieving the genre information
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
private static final String MUSIC_CHANNEL_ID = "UC-9-kyTW8ZkZNDHQJ6FgpwQ"; | |
/** | |
* Save the genre channels information in a json file. A filtered version is also saved | |
* alongside the default one. The genre playlist have to been generated for being able | |
* to produce the filtered version. | |
* | |
* @param youtube : A {@link YouTube} for making the youtube requests | |
*/ | |
static void saveGenreChannelsInfo(YouTube youtube) throws IOException { | |
//We retrieve the genre channel IDs | |
final List<String> genreChannelIds = getGenreChannelIds(youtube); | |
//We get the snippet for each channels IDs and save the json response in a text file | |
YouTube.Channels.List channelsInfo = youtube.channels().list("snippet"); | |
channelsInfo.setId(StringUtils.join(genreChannelIds, ',')); | |
ChannelListResponse channelListResponse = channelsInfo.execute(); | |
//We save the default result in a json file | |
String genreResponseJson = gson.toJson(channelListResponse); | |
FileWriter writer = new FileWriter(OUTPUT_DIR + GENRE_DIR + "/" + GENRE_INFORMATION_FILE_NAME); | |
writer.write(genreResponseJson); | |
//Filtering | |
List<Channel> channelList = channelListResponse.getItems().stream() | |
.map(c -> changeChannelThumbnail(c, getFirstVideoIdForGenre(c.getId()))) | |
.collect(Collectors.toList()); | |
//We save the filtered result | |
writer = new FileWriter(OUTPUT_DIR + GENRE_DIR + "/" + GENRE_INFORMATION_FILTER_FILE_NAME); | |
writer.write(gson.toJson(channelList)); | |
writer.close(); | |
} | |
/** | |
* Retrieves all the genre channel Ids and save the list of genres in a json file | |
* | |
* @param youtube : A {@link YouTube} for making the youtube request | |
* @return the list of genre ids | |
* @throws IOException | |
*/ | |
private static List<String> getGenreChannelIds(YouTube youtube) throws IOException { | |
//1 - Get the "Music genre" channel section ID | |
YouTube.ChannelSections.List channelsList = youtube.channelSections().list("snippet"); | |
channelsList.setChannelId(MUSIC_CHANNEL_ID); | |
ChannelSectionListResponse channelSectionListResponse = channelsList.execute(); | |
List<ChannelSection> channelSectionList = channelSectionListResponse.getItems(); | |
String musicGenreChannelSectionId = null; | |
for (ChannelSection section : channelSectionList) { | |
if (section.getSnippet().getType().equalsIgnoreCase("multipleChannels") | |
&& section.getSnippet().getTitle().equalsIgnoreCase("Music Genres")) { | |
musicGenreChannelSectionId = section.getId(); | |
break; | |
} | |
} | |
//2 - Get the genre channels IDs (Pop Music - Topic, Hip Hop Music - Topic etc) | |
YouTube.ChannelSections.List channels = youtube.channelSections().list("contentDetails"); | |
channels.setId(musicGenreChannelSectionId); | |
channelSectionListResponse = channels.execute(); | |
ChannelSection musicGenreSection = channelSectionListResponse.getItems().get(0); | |
return musicGenreSection.getContentDetails().getChannels(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment