Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

131
LINES

< > BotCompany Repo | #1003373 // List my YouTube uploads (not working, needs Google APIs)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Uses 72K of libraries. Compilation Failed (169L/3K).

1  
!7
2  
3  
lib 1009385 // google auth client
4  
lib 1009386 // google api client json (not useful)
5  
6  
import com.google.api.client.auth.oauth2.Credential;
7  
import com.google.api.client.googleapis.json.GoogleJsonResponseException;
8  
import com.google.api.services.samples.youtube.cmdline.Auth;
9  
import com.google.api.services.youtube.YouTube;
10  
import com.google.api.services.youtube.model.Channel;
11  
import com.google.api.services.youtube.model.ChannelListResponse;
12  
import com.google.api.services.youtube.model.PlaylistItem;
13  
import com.google.api.services.youtube.model.PlaylistItemListResponse;
14  
import com.google.common.collect.Lists;
15  
16  
/**
17  
 * Print a list of videos uploaded to the authenticated user's YouTube channel.
18  
 *
19  
 * @author Jeremy Walker
20  
 */
21  
22  
    /**
23  
     * Define a global instance of a Youtube object, which will be used
24  
     * to make YouTube Data API requests.
25  
     */
26  
    private static YouTube youtube;
27  
28  
    /**
29  
     * Authorize the user, call the youtube.channels.list method to retrieve
30  
     * the playlist ID for the list of videos uploaded to the user's channel,
31  
     * and then call the youtube.playlistItems.list method to retrieve the
32  
     * list of videos in that playlist.
33  
     *
34  
     * @param args command line args (not used).
35  
     */
36  
    public static void main(String[] args) {
37  
38  
        // This OAuth 2.0 access scope allows for read-only access to the
39  
        // authenticated user's account, but not other types of account access.
40  
        List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/youtube.readonly");
41  
42  
        try {
43  
            // Authorize the request.
44  
            Credential credential = Auth.authorize(scopes, "myuploads");
45  
46  
            // This object is used to make YouTube Data API requests.
47  
            youtube = new YouTube.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential).setApplicationName(
48  
                    "youtube-cmdline-myuploads-sample").build();
49  
50  
            // Call the API's channels.list method to retrieve the
51  
            // resource that represents the authenticated user's channel.
52  
            // In the API response, only include channel information needed for
53  
            // this use case. The channel's contentDetails part contains
54  
            // playlist IDs relevant to the channel, including the ID for the
55  
            // list that contains videos uploaded to the channel.
56  
            YouTube.Channels.List channelRequest = youtube.channels().list("contentDetails");
57  
            channelRequest.setMine(true);
58  
            channelRequest.setFields("items/contentDetails,nextPageToken,pageInfo");
59  
            ChannelListResponse channelResult = channelRequest.execute();
60  
61  
            List<Channel> channelsList = channelResult.getItems();
62  
63  
            if (channelsList != null) {
64  
                // The user's default channel is the first item in the list.
65  
                // Extract the playlist ID for the channel's videos from the
66  
                // API response.
67  
                String uploadPlaylistId =
68  
                        channelsList.get(0).getContentDetails().getRelatedPlaylists().getUploads();
69  
70  
                // Define a list to store items in the list of uploaded videos.
71  
                List<PlaylistItem> playlistItemList = new ArrayList<PlaylistItem>();
72  
73  
                // Retrieve the playlist of the channel's uploaded videos.
74  
                YouTube.PlaylistItems.List playlistItemRequest =
75  
                        youtube.playlistItems().list("id,contentDetails,snippet");
76  
                playlistItemRequest.setPlaylistId(uploadPlaylistId);
77  
78  
                // Only retrieve data used in this application, thereby making
79  
                // the application more efficient. See:
80  
                // https://developers.google.com/youtube/v3/getting-started#partial
81  
                playlistItemRequest.setFields(
82  
                        "items(contentDetails/videoId,snippet/title,snippet/publishedAt),nextPageToken,pageInfo");
83  
84  
                String nextToken = "";
85  
86  
                // Call the API one or more times to retrieve all items in the
87  
                // list. As long as the API response returns a nextPageToken,
88  
                // there are still more items to retrieve.
89  
                do {
90  
                    playlistItemRequest.setPageToken(nextToken);
91  
                    PlaylistItemListResponse playlistItemResult = playlistItemRequest.execute();
92  
93  
                    playlistItemList.addAll(playlistItemResult.getItems());
94  
95  
                    nextToken = playlistItemResult.getNextPageToken();
96  
                } while (nextToken != null);
97  
98  
                // Prints information about the results.
99  
                prettyPrint(playlistItemList.size(), playlistItemList.iterator());
100  
            }
101  
102  
        } catch (GoogleJsonResponseException e) {
103  
            e.printStackTrace();
104  
            System.err.println("There was a service error: " + e.getDetails().getCode() + " : "
105  
                    + e.getDetails().getMessage());
106  
107  
        } catch (Throwable t) {
108  
            t.printStackTrace();
109  
        }
110  
    }
111  
112  
    /*
113  
     * Print information about all of the items in the playlist.
114  
     *
115  
     * @param size size of list
116  
     *
117  
     * @param iterator of Playlist Items from uploaded Playlist
118  
     */
119  
    private static void prettyPrint(int size, Iterator<PlaylistItem> playlistEntries) {
120  
        System.out.println("=============================================================");
121  
        System.out.println("\t\tTotal Videos Uploaded: " + size);
122  
        System.out.println("=============================================================\n");
123  
124  
        while (playlistEntries.hasNext()) {
125  
            PlaylistItem playlistItem = playlistEntries.next();
126  
            System.out.println(" video name  = " + playlistItem.getSnippet().getTitle());
127  
            System.out.println(" video id    = " + playlistItem.getContentDetails().getVideoId());
128  
            System.out.println(" upload date = " + playlistItem.getSnippet().getPublishedAt());
129  
            System.out.println("\n-------------------------------------------------------------\n");
130  
        }
131  
    }

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1003373
Snippet name: List my YouTube uploads (not working, needs Google APIs)
Eternal ID of this version: #1003373/4
Text MD5: b9d4acac778a6b93d5a1cce7c489875d
Transpilation MD5: 7ae68da2a1a565f06c341cca5066d29b
Author: stefan
Category: javax / networking
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-07-28 17:42:00
Source code size: 6212 bytes / 131 lines
Pitched / IR pitched: No / No
Views / Downloads: 551 / 609
Version history: 3 change(s)
Referenced in: [show references]