-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
211 lines (171 loc) · 6.26 KB
/
script.js
File metadata and controls
211 lines (171 loc) · 6.26 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
const RSS_FEED_URL = "https://feeds.soundon.fm/podcasts/429de7c0-0c71-4fc9-a2a3-fcc3a651988e.xml";
const SPOTIFY_RSS_URL = "https://anchor.fm/s/10a17491c/podcast/rss";
const CORS_PROXY = "https://proxy.sitcon.org/?url=";
const formatDate = dateString => {
try {
const date = new Date(dateString);
if (isNaN(date.getTime())) {
return "";
}
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
return `${year}/${month}/${day}`;
} catch (error) {
console.error("日期格式化錯誤:", error);
return "";
}
};
const formatDuration = duration => {
if (!duration) return "";
try {
if (duration.includes(":")) {
const parts = duration.split(":").map(p => parseInt(p));
let totalMinutes = 0;
if (parts.length === 3) {
totalMinutes = parts[0] * 60 + parts[1];
} else if (parts.length === 2) {
totalMinutes = parts[0];
}
return totalMinutes > 0 ? `${totalMinutes}M` : "";
}
const seconds = parseInt(duration);
if (!isNaN(seconds) && seconds > 0) {
const minutes = Math.floor(seconds / 60);
return `${minutes}M`;
}
return "";
} catch (error) {
console.error("時間格式化錯誤:", error);
return "";
}
};
const getTextContent = (element, selector, defaultValue = "") => {
try {
const el = element.querySelector(selector);
return el?.textContent?.trim() || defaultValue;
} catch (error) {
return defaultValue;
}
};
const PODCAST_PLATFORMS = {
spotify: "https://sitcon.org/podcast-sp",
apple: "https://sitcon.org/podcast-ap",
youtube: "https://sitcon.org/podcast-yt"
};
const loadSpotifyLinks = async () => {
try {
const response = await fetch(`${CORS_PROXY}${SPOTIFY_RSS_URL}`);
if (!response.ok) {
console.warn("無法載入 Spotify RSS");
return {};
}
const text = await response.text();
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(text, "text/xml");
const items = xmlDoc.querySelectorAll("item");
const spotifyMap = {};
items.forEach(item => {
const title = getTextContent(item, "title");
let link = getTextContent(item, "link");
if (!link.includes("spotify.com")) {
const guid = getTextContent(item, "guid");
if (guid && guid.includes("spotify.com")) {
link = guid;
}
const description = getTextContent(item, "description");
const spotifyMatch = description.match(/https:\/\/[^\s"'<>]*spotify\.com[^\s"'<>]*/);
if (spotifyMatch) {
link = spotifyMatch[0];
}
}
if (title && link) {
const cleanTitle = title.trim().toLowerCase();
spotifyMap[cleanTitle] = link;
console.log(`找到 Spotify 連結: ${title} -> ${link}`);
}
});
console.log(`成功載入 ${Object.keys(spotifyMap).length} 個 Spotify 連結`);
return spotifyMap;
} catch (error) {
console.error("載入 Spotify RSS 失敗:", error);
return {};
}
};
const createPodcastElement = (item, spotifyLinks) => {
const title = getTextContent(item, "title");
const pubDate = getTextContent(item, "pubDate");
const link = getTextContent(item, "link", "#");
let duration = getTextContent(item, "itunes\\:duration") || getTextContent(item, "duration") || item.querySelector("duration")?.textContent?.trim() || "";
const formattedDate = formatDate(pubDate);
const formattedDuration = formatDuration(duration);
let dateTimeText = formattedDate;
if (formattedDuration) {
dateTimeText += `・${formattedDuration}`;
}
// 嘗試從 Spotify RSS 找到對應的連結
const cleanTitle = title.trim().toLowerCase();
const spotifyLink = spotifyLinks[cleanTitle] || PODCAST_PLATFORMS.spotify;
const podcastDiv = document.createElement("div");
podcastDiv.className = "podcast";
podcastDiv.innerHTML = `
<p id='date'>${dateTimeText}</p>
<p>${title}</p>
<div class="podcastLink">
<a href="${spotifyLink}" class="" target="_blank" rel="noopener noreferrer" title="Spotify">
<img src="img/icons/spotify.svg" alt="Spotify">
</a>
<a href="${PODCAST_PLATFORMS.apple}" class="" target="_blank" rel="noopener noreferrer" title="Apple Podcast">
<img src="img/icons/podcast.svg" alt="Apple Podcast">
</a>
<a href="${PODCAST_PLATFORMS.youtube}" class="" target="_blank" rel="noopener noreferrer" title="YouTube">
<img src="img/icons/youtube.svg" alt="YouTube">
</a>
<a href="${link}" class="" target="_blank" rel="noopener noreferrer" title="SoundOn">
<img src="img/icons/soundon.svg" alt="SoundOn">
</a>
</div>
`;
return podcastDiv;
};
const podcastListDiv = document.getElementById("podcastList");
try {
podcastListDiv.innerHTML = '<div class="loading-spinner">載入中...</div>';
const [spotifyLinks, response] = await Promise.all([loadSpotifyLinks(), fetch(`${CORS_PROXY}${RSS_FEED_URL}`)]);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const text = await response.text();
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(text, "text/xml");
const parseError = xmlDoc.querySelector("parsererror");
if (parseError) {
throw new Error("XML 解析失敗");
}
const items = xmlDoc.querySelectorAll("item");
podcastListDiv.innerHTML = "";
if (items.length === 0) {
podcastListDiv.innerHTML = '<p style="text-align: center; padding: 20px;">目前沒有 podcast 內容</p>';
}
items.forEach(item => {
try {
const podcastElement = createPodcastElement(item, spotifyLinks);
podcastListDiv.appendChild(podcastElement);
} catch (error) {
console.error("建立 podcast 元素時發生錯誤:", error);
}
});
console.log(`成功載入 ${items.length} 個 podcast episodes`);
} catch (error) {
console.error("載入 podcast 失敗:", error);
podcastListDiv.innerHTML = `
<div class="error-message">
<p>載入失敗,請稍後再試</p>
<p style="font-size: 0.9em; margin-top: 10px;">錯誤訊息: ${error.message}</p>
<button id="retryBtn" style="margin-top: 10px; padding: 8px 16px; cursor: pointer;">
重新載入
</button>
</div>
`;
document.getElementById("retryBtn")?.addEventListener("click", () => loadPodcasts());
}