|
| 1 | +/* |
| 2 | + * Copyright (c) 2015 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
| 5 | + * in compliance with the License. You may obtain a copy of the License at |
| 6 | + * |
| 7 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + * |
| 9 | + * Unless required by applicable law or agreed to in writing, software distributed under the License |
| 10 | + * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
| 11 | + * or implied. See the License for the specific language governing permissions and limitations under |
| 12 | + * the License. |
| 13 | + */ |
| 14 | + |
| 15 | +package com.google.api.services.samples.youtube.cmdline.reporting; |
| 16 | + |
| 17 | +import com.google.api.client.auth.oauth2.Credential; |
| 18 | +import com.google.api.client.googleapis.json.GoogleJsonResponseException; |
| 19 | +import com.google.api.services.samples.youtube.cmdline.Auth; |
| 20 | +import com.google.api.services.youtubereporting.YouTubeReporting; |
| 21 | +import com.google.api.services.youtubereporting.YouTubeReporting.Media.Download; |
| 22 | +import com.google.api.services.youtubereporting.model.Job; |
| 23 | +import com.google.api.services.youtubereporting.model.ListJobsResponse; |
| 24 | +import com.google.api.services.youtubereporting.model.ListReportsResponse; |
| 25 | +import com.google.api.services.youtubereporting.model.Report; |
| 26 | + |
| 27 | +import com.google.common.collect.Lists; |
| 28 | + |
| 29 | +import java.io.BufferedReader; |
| 30 | +import java.io.ByteArrayOutputStream; |
| 31 | +import java.io.File; |
| 32 | +import java.io.FileOutputStream; |
| 33 | +import java.io.IOException; |
| 34 | +import java.io.InputStreamReader; |
| 35 | +import java.util.List; |
| 36 | + |
| 37 | +import javax.print.attribute.standard.Media; |
| 38 | + |
| 39 | +/** |
| 40 | + * This sample retrieves reports created by a specific job by: |
| 41 | + * |
| 42 | + * 1. Listing the jobs using the "jobs.list" method. |
| 43 | + * 2. Retrieving reports using the "reports.list" method. |
| 44 | + * |
| 45 | + * @author Ibrahim Ulukaya |
| 46 | + */ |
| 47 | +public class RetrieveReports { |
| 48 | + |
| 49 | + /** |
| 50 | + * Define a global instance of a YouTube Reporting object, which will be used to make |
| 51 | + * YouTube Reporting API requests. |
| 52 | + */ |
| 53 | + private static YouTubeReporting youtubeReporting; |
| 54 | + |
| 55 | + |
| 56 | + /** |
| 57 | + * Retrieve reports. |
| 58 | + * |
| 59 | + * @param args command line args (not used). |
| 60 | + */ |
| 61 | + public static void main(String[] args) { |
| 62 | + |
| 63 | + /* |
| 64 | + * This OAuth 2.0 access scope allows for read access to the YouTube Analytics monetary reports for |
| 65 | + * authenticated user's account. Any request that retrieves earnings or ad performance metrics must |
| 66 | + * use this scope. |
| 67 | + */ |
| 68 | + List<String> scopes = Lists.newArrayList("https://www.googleapis.com/auth/yt-analytics-monetary.readonly"); |
| 69 | + |
| 70 | + try { |
| 71 | + // Authorize the request. |
| 72 | + Credential credential = Auth.authorize(scopes, "retrievereports"); |
| 73 | + |
| 74 | + // This object is used to make YouTube Reporting API requests. |
| 75 | + youtubeReporting = new YouTubeReporting.Builder(Auth.HTTP_TRANSPORT, Auth.JSON_FACTORY, credential) |
| 76 | + .setApplicationName("youtube-cmdline-retrievereports-sample").build(); |
| 77 | + |
| 78 | + if (listReportingJobs()) { |
| 79 | + if(retrieveReports(getJobIdFromUser())) { |
| 80 | + downloadReport(getReportUrlFromUser()); |
| 81 | + } |
| 82 | + } |
| 83 | + } catch (GoogleJsonResponseException e) { |
| 84 | + System.err.println("GoogleJsonResponseException code: " + e.getDetails().getCode() |
| 85 | + + " : " + e.getDetails().getMessage()); |
| 86 | + e.printStackTrace(); |
| 87 | + |
| 88 | + } catch (IOException e) { |
| 89 | + System.err.println("IOException: " + e.getMessage()); |
| 90 | + e.printStackTrace(); |
| 91 | + } catch (Throwable t) { |
| 92 | + System.err.println("Throwable: " + t.getMessage()); |
| 93 | + t.printStackTrace(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Lists reporting jobs. (jobs.listJobs) |
| 99 | + * @return true if at least one reporting job exists |
| 100 | + * @throws IOException |
| 101 | + */ |
| 102 | + private static boolean listReportingJobs() throws IOException { |
| 103 | + // Call the YouTube Reporting API's jobs.list method to retrieve reporting jobs. |
| 104 | + ListJobsResponse jobsListResponse = youtubeReporting.jobs().list().execute(); |
| 105 | + List<Job> jobsList = jobsListResponse.getJobs(); |
| 106 | + |
| 107 | + if (jobsList == null || jobsList.isEmpty()) { |
| 108 | + System.out.println("No jobs found."); |
| 109 | + return false; |
| 110 | + } else { |
| 111 | + // Print information from the API response. |
| 112 | + System.out.println("\n================== Reporting Jobs ==================\n"); |
| 113 | + for (Job job : jobsList) { |
| 114 | + System.out.println(" - Id: " + job.getId()); |
| 115 | + System.out.println(" - Name: " + job.getName()); |
| 116 | + System.out.println(" - Report Type Id: " + job.getReportTypeId()); |
| 117 | + System.out.println("\n-------------------------------------------------------------\n"); |
| 118 | + } |
| 119 | + } |
| 120 | + return true; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Lists reports created by a specific job. (reports.listJobsReports) |
| 125 | + * |
| 126 | + * @param jobId The ID of the job. |
| 127 | + * @throws IOException |
| 128 | + */ |
| 129 | + private static boolean retrieveReports(String jobId) |
| 130 | + throws IOException { |
| 131 | + // Call the YouTube Reporting API's reports.list method |
| 132 | + // to retrieve reports created by a job. |
| 133 | + ListReportsResponse reportsListResponse = youtubeReporting.jobs().reports().list(jobId).execute(); |
| 134 | + List<Report> reportslist = reportsListResponse.getReports(); |
| 135 | + |
| 136 | + if (reportslist == null || reportslist.isEmpty()) { |
| 137 | + System.out.println("No reports found."); |
| 138 | + return false; |
| 139 | + } else { |
| 140 | + // Print information from the API response. |
| 141 | + System.out.println("\n============= Reports for the job " + jobId + " =============\n"); |
| 142 | + for (Report report : reportslist) { |
| 143 | + System.out.println(" - Id: " + report.getId()); |
| 144 | + System.out.println(" - From: " + report.getStartTime()); |
| 145 | + System.out.println(" - To: " + report.getEndTime()); |
| 146 | + System.out.println(" - Download Url: " + report.getDownloadUrl()); |
| 147 | + System.out.println("\n-------------------------------------------------------------\n"); |
| 148 | + } |
| 149 | + } |
| 150 | + return true; |
| 151 | + } |
| 152 | + |
| 153 | + /** |
| 154 | + * Download the report specified by the URL. (media.download) |
| 155 | + * |
| 156 | + * @param reportUrl The URL of the report to be downloaded. |
| 157 | + * @throws IOException |
| 158 | + */ |
| 159 | + private static boolean downloadReport(String reportUrl) |
| 160 | + throws IOException { |
| 161 | + // Call the YouTube Reporting API's media.download method to download a report. |
| 162 | + Download request = youtubeReporting.media().download(""); |
| 163 | + FileOutputStream fop = new FileOutputStream(new File("report")); |
| 164 | + request.getMediaHttpDownloader().download(new GenericUrl(reportUrl), fop); |
| 165 | + return true; |
| 166 | + } |
| 167 | + |
| 168 | + /* |
| 169 | + * Prompt the user to enter a job id for report retrieval. Then return the id. |
| 170 | + */ |
| 171 | + private static String getJobIdFromUser() throws IOException { |
| 172 | + |
| 173 | + String id = ""; |
| 174 | + |
| 175 | + System.out.print("Please enter the job id for the report retrieval: "); |
| 176 | + BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in)); |
| 177 | + id = bReader.readLine(); |
| 178 | + |
| 179 | + System.out.println("You chose " + id + " as the job Id for the report retrieval."); |
| 180 | + return id; |
| 181 | + } |
| 182 | + |
| 183 | + /* |
| 184 | + * Prompt the user to enter a URL for report download. Then return the URL. |
| 185 | + */ |
| 186 | + private static String getReportUrlFromUser() throws IOException { |
| 187 | + |
| 188 | + String url = ""; |
| 189 | + |
| 190 | + System.out.print("Please enter the report URL to download: "); |
| 191 | + BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in)); |
| 192 | + url = bReader.readLine(); |
| 193 | + |
| 194 | + System.out.println("You chose " + url + " as the URL to download."); |
| 195 | + return url; |
| 196 | + }} |
| 197 | + |
0 commit comments