Skip to content

Commit fa06eb2

Browse files
committed
maxime_commit
1 parent 9d5b28b commit fa06eb2

File tree

2 files changed

+79
-31
lines changed

2 files changed

+79
-31
lines changed

app/src/main/java/com/example/golfier/android_youtube_api/DataApi.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class DataApi implements EasyPermissions.PermissionCallbacks {
3535
DataApi(Activity activity) {
3636
// Initialize credentials and service object.
3737
this.activity = activity;
38-
mProgress = new ProgressDialog(activity);
38+
mProgress = new ProgressDialog(this.activity);
3939
mCredential = GoogleAccountCredential.usingOAuth2(this.activity, Arrays.asList(SCOPES))
4040
.setBackOff(new ExponentialBackOff());
4141
}

app/src/main/java/com/example/golfier/android_youtube_api/MakeRequestTask.java

Lines changed: 78 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
* An asynchronous task that handles the YouTube Data API call.
2828
* Placing the API calls in their own task ensures the UI stays responsive.
2929
*/
30-
public class MakeRequestTask extends AsyncTask<Void, Void, List<String>> {
30+
public class MakeRequestTask extends AsyncTask<Void, Void, YoutubeUser> {
3131

3232
private com.google.api.services.youtube.YouTube mService = null;
3333
private Exception mLastError = null;
@@ -53,22 +53,17 @@ public class MakeRequestTask extends AsyncTask<Void, Void, List<String>> {
5353
* @param params no parameters needed for this task.
5454
*/
5555
@Override
56-
protected List<String> doInBackground(Void... params) {
57-
try {
58-
return getDataFromApi();
59-
} catch (Exception e) {
60-
mLastError = e;
61-
cancel(true);
62-
return null;
63-
}
56+
protected YoutubeUser doInBackground(Void... params) {
57+
getDataFromApi();
58+
return null;
6459
}
6560

6661
/**
6762
* Fetch information about the "GoogleDevelopers" YouTube channel.
6863
* @return List of Strings containing information about the channel.
6964
* @throws IOException
7065
*/
71-
private List<String> getDataFromApi2() throws IOException {
66+
private List<String> getPrimaryData() throws IOException {
7267
// Get a list of up to 10 files.
7368
List<String> channelInfo = new ArrayList<String>();
7469
ChannelListResponse result = mService.channels().list("snippet,contentDetails,statistics")
@@ -86,13 +81,7 @@ private List<String> getDataFromApi2() throws IOException {
8681
return channelInfo;
8782
}
8883

89-
// Call the API's channels.list method to retrieve the
90-
// resource that represents the authenticated user's channel.
91-
// In the API response, only include channel information needed for
92-
// this use case. The channel's contentDetails part contains
93-
// playlist IDs relevant to the channel, including the ID for the
94-
// list that contains videos uploaded to the channel.
95-
private List<String> getDataFromApi() throws IOException {
84+
private void getDataFromApi() {
9685
try{
9786
ChannelListResponse channelResult = mService.channels().list("snippet,contentDetails,statistics")
9887
.setMine(true)
@@ -122,8 +111,7 @@ private List<String> getDataFromApi() throws IOException {
122111
// Only retrieve data used in this application, thereby making
123112
// the application more efficient. See:
124113
// https://developers.google.com/youtube/v3/getting-started#partial
125-
playlistItemRequest.setFields(
126-
"items(contentDetails/videoId,snippet/title,snippet/publishedAt),nextPageToken,pageInfo");
114+
playlistItemRequest.setFields("items(contentDetails/videoId,snippet/title,snippet/publishedAt),nextPageToken,pageInfo");
127115

128116
String nextToken = "";
129117

@@ -140,20 +128,76 @@ private List<String> getDataFromApi() throws IOException {
140128
} while (nextToken != null);
141129

142130
// Prints information about the results.
143-
prettyPrint(playlistItemList.size(), playlistItemList.iterator());
131+
processPrint(playlistItemList.size(), playlistItemList.iterator());
144132
}
145133

146134
} catch (GoogleJsonResponseException e) {
147135
e.printStackTrace();
148-
System.err.println("There was a service error: " + e.getDetails().getCode() + " : "
149-
+ e.getDetails().getMessage());
136+
System.err.println("There was a service error: " + e.getDetails().getCode() + " : " + e.getDetails().getMessage());
150137

151138
} catch (Throwable t) {
152139
t.printStackTrace();
153140
}
154-
return null;
155141
}
156142

143+
private void getDataFromApiWithName(String name) {
144+
try{
145+
ChannelListResponse channelResult = mService.channels().list("snippet,contentDetails,statistics")
146+
.setForUsername(name)
147+
.setFields("items/contentDetails,nextPageToken,pageInfo")
148+
.execute();
149+
150+
System.out.println(channelResult);
151+
152+
List<Channel> channelsList = channelResult.getItems();
153+
154+
if (channelsList != null) {
155+
System.out.println(channelsList.get(0));
156+
// The user's default channel is the first item in the list.
157+
// Extract the playlist ID for the channel's videos from the
158+
// API response.
159+
String uploadPlaylistId =
160+
channelsList.get(0).getContentDetails().getRelatedPlaylists().getUploads();
161+
162+
// Define a list to store items in the list of uploaded videos.
163+
List<PlaylistItem> playlistItemList = new ArrayList<PlaylistItem>();
164+
165+
// Retrieve the playlist of the channel's uploaded videos.
166+
YouTube.PlaylistItems.List playlistItemRequest =
167+
mService.playlistItems().list("id,contentDetails,snippet");
168+
playlistItemRequest.setPlaylistId(uploadPlaylistId);
169+
170+
// Only retrieve data used in this application, thereby making
171+
// the application more efficient. See:
172+
// https://developers.google.com/youtube/v3/getting-started#partial
173+
playlistItemRequest.setFields("items(contentDetails/videoId,snippet/title,snippet/publishedAt),nextPageToken,pageInfo");
174+
175+
String nextToken = "";
176+
177+
// Call the API one or more times to retrieve all items in the
178+
// list. As long as the API response returns a nextPageToken,
179+
// there are still more items to retrieve.
180+
do {
181+
playlistItemRequest.setPageToken(nextToken);
182+
PlaylistItemListResponse playlistItemResult = playlistItemRequest.execute();
183+
184+
playlistItemList.addAll(playlistItemResult.getItems());
185+
186+
nextToken = playlistItemResult.getNextPageToken();
187+
} while (nextToken != null);
188+
189+
// Prints information about the results.
190+
processPrint(playlistItemList.size(), playlistItemList.iterator());
191+
}
192+
193+
} catch (GoogleJsonResponseException e) {
194+
e.printStackTrace();
195+
System.err.println("There was a service error: " + e.getDetails().getCode() + " : " + e.getDetails().getMessage());
196+
197+
} catch (Throwable t) {
198+
t.printStackTrace();
199+
}
200+
}
157201

158202
@Override
159203
protected void onPreExecute() {
@@ -162,13 +206,12 @@ protected void onPreExecute() {
162206
}
163207

164208
@Override
165-
protected void onPostExecute(List<String> output) {
209+
protected void onPostExecute(YoutubeUser output) {
166210
mProgress.hide();
167-
if (output == null || output.size() == 0) {
211+
if (output == null) {
168212
ri.addInfo("No results returned.");
169213
} else {
170-
output.add(0, "Data retrieved using the YouTube Data API:");
171-
ri.addInfo(TextUtils.join("\n", output));
214+
ri.addInfo("Data retrieved using the YouTube Data API:");
172215
}
173216
}
174217

@@ -197,23 +240,28 @@ protected void onCancelled() {
197240
*
198241
* @param iterator of Playlist Items from uploaded Playlist
199242
*/
200-
private void prettyPrint(int size, Iterator<PlaylistItem> playlistEntries) {
243+
private void processPrint(int size, Iterator<PlaylistItem> playlistEntries) {
244+
245+
List<YoutubeVideo> myVideo = new ArrayList<>();
246+
201247
System.out.println("=============================================================");
202248
System.out.println("\t\tTotal Videos Uploaded: " + size);
203249
System.out.println("=============================================================\n");
204250

205-
List<YoutubeVideo> myVideo = new ArrayList<>();
206-
207251
while (playlistEntries.hasNext()) {
252+
208253
PlaylistItem playlistItem = playlistEntries.next();
254+
209255
System.out.println(" video name = " + playlistItem.getSnippet().getTitle());
210256
System.out.println(" video id = " + playlistItem.getContentDetails().getVideoId());
211257
System.out.println(" upload date = " + playlistItem.getSnippet().getPublishedAt());
212258
System.out.println("\n-------------------------------------------------------------\n");
259+
213260
YoutubeVideo vid = new YoutubeVideo(playlistItem.getSnippet().getTitle(),
214261
playlistItem.getContentDetails().getVideoId(),
215262
playlistItem.getSnippet().getPublishedAt().toString());
216263
myVideo.add(vid);
264+
217265
}
218266
youtubeUser.addVideoContent(myVideo);
219267
}

0 commit comments

Comments
 (0)