質問編集履歴

7

少し変えました

2018/09/19 10:10

投稿

makioo
makioo

スコア28

test CHANGED
File without changes
test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
 
4
4
 
5
- ここを参考にしながら視聴回数をとってこようと思ったのですが、視聴回数がとってこれません
5
+ ここを参考にしながらYoutubeで検索をかけてその結果の動画の視聴回数をとってこようと思ったのですが、視聴回数がとってこれません
6
6
 
7
7
 
8
8
 

6

タグの追加

2018/09/19 10:10

投稿

makioo
makioo

スコア28

test CHANGED
File without changes
test CHANGED
File without changes

5

誤字

2018/09/19 10:07

投稿

makioo
makioo

スコア28

test CHANGED
File without changes
test CHANGED
@@ -308,7 +308,7 @@
308
308
 
309
309
 
310
310
 
311
- ###追記2( )
311
+ ###追記2( )
312
312
 
313
313
 
314
314
 

4

考え方

2018/09/19 09:54

投稿

makioo
makioo

スコア28

test CHANGED
File without changes
test CHANGED
@@ -305,3 +305,11 @@
305
305
  }
306
306
 
307
307
  ```
308
+
309
+
310
+
311
+ ###追記2( )
312
+
313
+
314
+
315
+ 上のserachメソッドより得られたvideoIdをもとにList<Viedo> videoのような変数に新しく追加していくことなどできれば行けそうですが、どうでしょうか

3

追記

2018/09/19 08:03

投稿

makioo
makioo

スコア28

test CHANGED
File without changes
test CHANGED
@@ -34,4 +34,274 @@
34
34
 
35
35
  ```
36
36
 
37
- この部分をコメントにしてすべての情報を持ってきてみるとそもそもstatisticsは入っていませんでした。
37
+ この部分をコメントにしてすべての情報を持ってきてみるとそもそもstatisticsは入っていませんでした。]
38
+
39
+
40
+
41
+
42
+
43
+ ```
44
+
45
+ import com.google.api.client.googleapis.json.GoogleJsonResponseException;
46
+
47
+ import com.google.api.client.http.HttpRequest;
48
+
49
+ import com.google.api.client.http.HttpRequestInitializer;
50
+
51
+ import com.google.api.client.http.HttpTransport;
52
+
53
+ import com.google.api.client.http.javanet.NetHttpTransport;
54
+
55
+ import com.google.api.client.json.JsonFactory;
56
+
57
+ import com.google.api.client.json.jackson2.JacksonFactory;
58
+
59
+ import com.google.api.services.youtube.YouTube;
60
+
61
+ import com.google.api.services.youtube.model.ResourceId;
62
+
63
+ import com.google.api.services.youtube.model.SearchListResponse;
64
+
65
+ import com.google.api.services.youtube.model.SearchResult;
66
+
67
+ import com.google.api.services.youtube.model.Thumbnail;
68
+
69
+ import com.google.api.services.youtube.model.VideoStatistics;
70
+
71
+ import com.google.api.services.youtube.model.*;
72
+
73
+
74
+
75
+
76
+
77
+ import java.io.BufferedReader;
78
+
79
+ import java.io.IOException;
80
+
81
+ import java.io.InputStream;
82
+
83
+ import java.io.InputStreamReader;
84
+
85
+ import java.util.Iterator;
86
+
87
+ import java.util.List;
88
+
89
+ import java.util.Properties;
90
+
91
+
92
+
93
+ public class Search {
94
+
95
+
96
+
97
+ private static String PROPERTIES_FILENAME = "youtube.properties";
98
+
99
+
100
+
101
+ private static final HttpTransport HTTP_TRANSPORT = new NetHttpTransport();
102
+
103
+
104
+
105
+ private static final JsonFactory JSON_FACTORY = new JacksonFactory();
106
+
107
+
108
+
109
+ /*max 50らしい*/
110
+
111
+ private static final long NUMBER_OF_VIDEOS_RETURNED = 1;
112
+
113
+
114
+
115
+ private static YouTube youtube;
116
+
117
+
118
+
119
+
120
+
121
+ public static void main(String[] args) {
122
+
123
+
124
+
125
+ Properties properties = new Properties();
126
+
127
+ try {
128
+
129
+ InputStream in = Search.class.getResourceAsStream("/" + PROPERTIES_FILENAME);
130
+
131
+ properties.load(in);
132
+
133
+
134
+
135
+ } catch (IOException e) {
136
+
137
+ System.err.println("There was an error reading " + PROPERTIES_FILENAME + ": " + e.getCause()
138
+
139
+ + " : " + e.getMessage());
140
+
141
+ System.exit(1);
142
+
143
+ }
144
+
145
+
146
+
147
+ try {
148
+
149
+ youtube = new YouTube.Builder(HTTP_TRANSPORT, JSON_FACTORY, new HttpRequestInitializer() {
150
+
151
+ public void initialize(HttpRequest request) throws IOException {}
152
+
153
+ }).setApplicationName("youtube-cmdline-search-sample").build();
154
+
155
+
156
+
157
+ String queryTerm = getInputQuery();
158
+
159
+
160
+
161
+ YouTube.Search.List search = youtube.search().list("id,snippet");
162
+
163
+
164
+
165
+ String apiKey = properties.getProperty("youtube.apikey");
166
+
167
+ search.setKey(apiKey);
168
+
169
+ search.setQ(queryTerm);
170
+
171
+
172
+
173
+ search.setType("videos");
174
+
175
+
176
+
177
+ // search.setFields("items(id/kind,id/videoId,snippet/title,snippet/thumbnails/default/url)");
178
+
179
+ //search.setFields("items(id,snippet,statistics)");
180
+
181
+ search.setMaxResults(NUMBER_OF_VIDEOS_RETURNED);
182
+
183
+ SearchListResponse searchResponse = search.execute();
184
+
185
+
186
+
187
+ List<SearchResult> searchResultList = searchResponse.getItems();
188
+
189
+
190
+
191
+ if (searchResultList != null) {
192
+
193
+ prettyPrint(searchResultList.iterator(), queryTerm);
194
+
195
+ }
196
+
197
+ } catch (GoogleJsonResponseException e) {
198
+
199
+ System.err.println("There was a service error: " + e.getDetails().getCode() + " : "
200
+
201
+ + e.getDetails().getMessage());
202
+
203
+ } catch (IOException e) {
204
+
205
+ System.err.println("There was an IO error: " + e.getCause() + " : " + e.getMessage());
206
+
207
+ } catch (Throwable t) {
208
+
209
+ t.printStackTrace();
210
+
211
+ }
212
+
213
+ }
214
+
215
+
216
+
217
+ private static String getInputQuery() throws IOException {
218
+
219
+
220
+
221
+ String inputQuery = "";
222
+
223
+
224
+
225
+ System.out.print("Please enter a search term: ");
226
+
227
+ BufferedReader bReader = new BufferedReader(new InputStreamReader(System.in));
228
+
229
+ inputQuery = bReader.readLine();
230
+
231
+
232
+
233
+ if (inputQuery.length() < 1) {
234
+
235
+ inputQuery = "YouTube Developers Live";
236
+
237
+ }
238
+
239
+ return inputQuery;
240
+
241
+ }
242
+
243
+
244
+
245
+ private static void prettyPrint(Iterator<SearchResult> iteratorSearchResults, String query) {
246
+
247
+
248
+
249
+ System.out.println("\n=============================================================");
250
+
251
+ System.out.println(
252
+
253
+ " First " + NUMBER_OF_VIDEOS_RETURNED + " videos for search on \"" + query + "\".");
254
+
255
+ System.out.println("=============================================================\n");
256
+
257
+
258
+
259
+ if (!iteratorSearchResults.hasNext()) {
260
+
261
+ System.out.println(" There aren't any results for your query.");
262
+
263
+ }
264
+
265
+
266
+
267
+ while (iteratorSearchResults.hasNext()) {
268
+
269
+
270
+
271
+ SearchResult singleVideo = iteratorSearchResults.next();
272
+
273
+ System.out.println(singleVideo);
274
+
275
+ ResourceId rId = singleVideo.getId();
276
+
277
+
278
+
279
+
280
+
281
+ // Double checks the kind is video.
282
+
283
+ if (rId.getKind().equals("youtube#video")) {
284
+
285
+ Thumbnail thumbnail = (Thumbnail) singleVideo.getSnippet().getThumbnails().get("default");
286
+
287
+
288
+
289
+ System.out.println(" Video Id" + rId.getVideoId());
290
+
291
+ System.out.println(" Title: " + singleVideo.getSnippet().getTitle());
292
+
293
+ System.out.println(" Thumbnail: " + thumbnail.getUrl());
294
+
295
+ // System.out.println("view all Count" + singleVideo.getStatistics().getViewCount());
296
+
297
+ System.out.println("\n-------------------------------------------------------------\n");
298
+
299
+ }
300
+
301
+ }
302
+
303
+ }
304
+
305
+ }
306
+
307
+ ```

2

追記

2018/09/19 05:57

投稿

makioo
makioo

スコア28

test CHANGED
File without changes
test CHANGED
@@ -14,7 +14,7 @@
14
14
 
15
15
 
16
16
 
17
- の部分からよりそもそもstatisticsを持ってきていないから当たり前なのですがzstatisticsをいれようとするとInvalid fieldと言われました。
17
+ の部分からよりそもそもstatisticsを持ってきていないから当たり前なのですがstatisticsをいれようとするとInvalid fieldと言われました。
18
18
 
19
19
  ご教授お願いします。
20
20
 
@@ -24,4 +24,14 @@
24
24
 
25
25
  ###追記
26
26
 
27
- [fie-ldについて](https://developers.google.com/youtube/v3/getting-started?hl=ja#part)このあたりを見るととってこれそうな感じもするのですが...
27
+ [fieldについて](https://developers.google.com/youtube/v3/getting-started?hl=ja#part)このあたりを見るととってこれそうな感じもするのですが...
28
+
29
+
30
+
31
+ ```
32
+
33
+ search.setFields("items(id/kind,id/videoId,snippet/title,snippet/thumbnails/default/url)");
34
+
35
+ ```
36
+
37
+ この部分をコメントにしてすべての情報を持ってきてみるとそもそもstatisticsは入っていませんでした。

1

追記

2018/09/19 03:42

投稿

makioo
makioo

スコア28

test CHANGED
File without changes
test CHANGED
@@ -17,3 +17,11 @@
17
17
  の部分からよりそもそもstatisticsを持ってきていないから当たり前なのですがzstatisticsをいれようとするとInvalid fieldと言われました。
18
18
 
19
19
  ご教授お願いします。
20
+
21
+
22
+
23
+
24
+
25
+ ###追記
26
+
27
+ [fie-ldについて](https://developers.google.com/youtube/v3/getting-started?hl=ja#part)このあたりを見るととってこれそうな感じもするのですが...