-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFeedYouTubeChannel.java
More file actions
152 lines (141 loc) · 4.86 KB
/
FeedYouTubeChannel.java
File metadata and controls
152 lines (141 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package youtube;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSParser;
/**
* フィード
*/
public class FeedYouTubeChannel {
/** フィードの URL */
private String urlString;
/** DOMツリー */
private Document document;
/**
* コンストラクタ
* @param urlString フィードのURL
* @param encoding フィードの文字コード
*/
public static ArrayList<String> getTitle(String channelId){
FeedYouTubeChannel feed = new FeedYouTubeChannel("https://www.youtube.com/feeds/videos.xml?channel_id="+ channelId);
ArrayList<String> entryList = feed.getEntryList();
// System.out.println("↓↓↓取得した動画タイトル一覧↓↓↓");
// System.out.println();
// for(String entry: entryList) {
// System.out.print("・");
// System.out.println(entry);
//System.out.println();
// }
return entryList;
}
public FeedYouTubeChannel(String urlString, String encoding) {
// TLS v1.2 の有効化 (Java 8 以降では指定不要)
System.setProperty("https.protocols", "TLSv1,TLSv1.1,TLSv1.2");
this.urlString = urlString;
try {
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
connection.connect();
InputStream inputStream = connection.getInputStream();
// DOMツリーの構築
document = this.buildDocument(inputStream, encoding);
}
catch (Exception e) {
e.printStackTrace();
}
}
/**
* コンストラクタ
* フィードの文字コードは UTF-8 を想定
* @param urlString フィードのURL
*/
public FeedYouTubeChannel(String urlString) {
this(urlString, "utf-8");
}
/**
* フィードの URLを返す
* @return URL
*/
public String getURLString() {
return urlString;
}
/**
* item 要素のリストを返す
* @return item要素の ArrayList
*/
public ArrayList<String> getEntryList() {
ArrayList<String> entryList = new ArrayList<String>();
try {
// XPath の表現を扱う XPath オブジェクトを生成
XPath xPath = XPathFactory.newInstance().newXPath();
// item要素のリストを得る (RSS 2.0の場合)
NodeList entryNodeList = (NodeList)xPath.evaluate("/feed/entry",
document, XPathConstants.NODESET);
// item要素のリストを得る (RSS 1.0の場合)
if(entryNodeList.getLength() == 0) {
entryNodeList = (NodeList)xPath.evaluate("/feed/entry",
document, XPathConstants.NODESET);
}
for(int i = 0; i < entryNodeList.getLength(); i++) { // 各item要素について
Node entryNode= entryNodeList.item(i);
// item要素(itemNode) の子ノードである title, link, description 要素の内容を得る
// それぞれ1要素しかないので NodeList で受ける必要がない
String title = xPath.evaluate("title", entryNode);
//String link = xPath.evaluate("link", itemNode);
//String description = xPath.evaluate("description", itemNode);
entryList.add(title);
}
}
catch (Exception e) {
e.printStackTrace();
}
return entryList;
}
/**
* DOM ツリーの構築
* @param inputStream XMLで記述されたテキストを InputStream にしたもの
* @param encoding テキストの文字コード
* @return 文書全体の DOM ツリー
*/
public Document buildDocument(InputStream inputStream, String encoding) {
Document document = null;
try {
// DOM実装(implementation)の用意 (Load and Save用)
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS implementation = (DOMImplementationLS)registry.getDOMImplementation("XML 1.0");
// 読み込み対象の用意
LSInput input = implementation.createLSInput();
input.setByteStream(inputStream);
input.setEncoding(encoding);
// 構文解析器(parser)の用意
LSParser parser = implementation.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
parser.getDomConfig().setParameter("namespaces", false);
// DOMの構築
document = parser.parse(input);
}
catch (Exception e) {
e.printStackTrace();
}
return document;
}
/** 動作確認用
public static void main(String[] args) {
FeedYouTubeChannel3 feed = new FeedYouTubeChannel3("https://www.youtube.com/feeds/videos.xml?channel_id=UCFOsYGDAw16cr57cCqdJdVQ");
ArrayList<String> entryList = feed.getEntryList();
for(String entry: entryList) {
System.out.println(entry);
System.out.println();
}
}
*/
}