27
27
* An asynchronous task that handles the YouTube Data API call.
28
28
* Placing the API calls in their own task ensures the UI stays responsive.
29
29
*/
30
- public class MakeRequestTask extends AsyncTask <Void , Void , List < String > > {
30
+ public class MakeRequestTask extends AsyncTask <Void , Void , YoutubeUser > {
31
31
32
32
private com .google .api .services .youtube .YouTube mService = null ;
33
33
private Exception mLastError = null ;
@@ -53,22 +53,17 @@ public class MakeRequestTask extends AsyncTask<Void, Void, List<String>> {
53
53
* @param params no parameters needed for this task.
54
54
*/
55
55
@ 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 ;
64
59
}
65
60
66
61
/**
67
62
* Fetch information about the "GoogleDevelopers" YouTube channel.
68
63
* @return List of Strings containing information about the channel.
69
64
* @throws IOException
70
65
*/
71
- private List <String > getDataFromApi2 () throws IOException {
66
+ private List <String > getPrimaryData () throws IOException {
72
67
// Get a list of up to 10 files.
73
68
List <String > channelInfo = new ArrayList <String >();
74
69
ChannelListResponse result = mService .channels ().list ("snippet,contentDetails,statistics" )
@@ -86,13 +81,7 @@ private List<String> getDataFromApi2() throws IOException {
86
81
return channelInfo ;
87
82
}
88
83
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 () {
96
85
try {
97
86
ChannelListResponse channelResult = mService .channels ().list ("snippet,contentDetails,statistics" )
98
87
.setMine (true )
@@ -122,8 +111,7 @@ private List<String> getDataFromApi() throws IOException {
122
111
// Only retrieve data used in this application, thereby making
123
112
// the application more efficient. See:
124
113
// 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" );
127
115
128
116
String nextToken = "" ;
129
117
@@ -140,20 +128,76 @@ private List<String> getDataFromApi() throws IOException {
140
128
} while (nextToken != null );
141
129
142
130
// Prints information about the results.
143
- prettyPrint (playlistItemList .size (), playlistItemList .iterator ());
131
+ processPrint (playlistItemList .size (), playlistItemList .iterator ());
144
132
}
145
133
146
134
} catch (GoogleJsonResponseException e ) {
147
135
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 ());
150
137
151
138
} catch (Throwable t ) {
152
139
t .printStackTrace ();
153
140
}
154
- return null ;
155
141
}
156
142
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
+ }
157
201
158
202
@ Override
159
203
protected void onPreExecute () {
@@ -162,13 +206,12 @@ protected void onPreExecute() {
162
206
}
163
207
164
208
@ Override
165
- protected void onPostExecute (List < String > output ) {
209
+ protected void onPostExecute (YoutubeUser output ) {
166
210
mProgress .hide ();
167
- if (output == null || output . size () == 0 ) {
211
+ if (output == null ) {
168
212
ri .addInfo ("No results returned." );
169
213
} 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:" );
172
215
}
173
216
}
174
217
@@ -197,23 +240,28 @@ protected void onCancelled() {
197
240
*
198
241
* @param iterator of Playlist Items from uploaded Playlist
199
242
*/
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
+
201
247
System .out .println ("=============================================================" );
202
248
System .out .println ("\t \t Total Videos Uploaded: " + size );
203
249
System .out .println ("=============================================================\n " );
204
250
205
- List <YoutubeVideo > myVideo = new ArrayList <>();
206
-
207
251
while (playlistEntries .hasNext ()) {
252
+
208
253
PlaylistItem playlistItem = playlistEntries .next ();
254
+
209
255
System .out .println (" video name = " + playlistItem .getSnippet ().getTitle ());
210
256
System .out .println (" video id = " + playlistItem .getContentDetails ().getVideoId ());
211
257
System .out .println (" upload date = " + playlistItem .getSnippet ().getPublishedAt ());
212
258
System .out .println ("\n -------------------------------------------------------------\n " );
259
+
213
260
YoutubeVideo vid = new YoutubeVideo (playlistItem .getSnippet ().getTitle (),
214
261
playlistItem .getContentDetails ().getVideoId (),
215
262
playlistItem .getSnippet ().getPublishedAt ().toString ());
216
263
myVideo .add (vid );
264
+
217
265
}
218
266
youtubeUser .addVideoContent (myVideo );
219
267
}
0 commit comments