Index: scripts/gen_dartisans_playlist.dart |
diff --git a/scripts/gen_dartisans_playlist.dart b/scripts/gen_dartisans_playlist.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..a4fdb40be560e28eda8d300f23784e128570e20e |
--- /dev/null |
+++ b/scripts/gen_dartisans_playlist.dart |
@@ -0,0 +1,98 @@ |
+import 'dart:json'; |
+import 'dart:io'; |
+import 'dart:uri'; |
+ |
+final String YT_PLAYLIST_URL = 'http://gdata.youtube.com/feeds/api/playlists/'; |
+final String PLAYLIST_ID = 'PLOU2XLYxmsIIS2zgjdmBEwTrA6m5YgHBs'; |
+ |
+final String OUTPUT_FILENAME = 'src/site/dartisans/episodes.yaml'; |
+ |
+Future<String> loadDartisansPlaylist() { |
+ var completer = new Completer(); |
+ var client = new HttpClient(); |
+ var conn = client.getUrl(new Uri('$YT_PLAYLIST_URL$PLAYLIST_ID?alt=json')); |
+ conn.onResponse = (HttpClientResponse resp) { |
+ var buffer = new StringBuffer(); |
+ var input = resp.inputStream; |
+ input.onData = () => buffer.add(new String.fromCharCodes(input.read())); |
+ input.onClosed = () { |
+ print('Done loading JSON from API'); |
+ client.shutdown(); // XXX do this or the app remains running |
+ completer.complete(buffer.toString()); |
+ }; |
+ input.onError = (e) { |
+ completer.completeException("ERROR reading from input: $e"); |
+ }; |
+ }; |
+ conn.onError = (e) { |
+ completer.completeException("ERROR CONNECTING TO YT: $e"); |
+ }; |
+ return completer.future; |
+} |
+ |
+Map parsePlaylistJson(String json) => JSON.parse(json); |
+ |
+writePlaylistYaml(Directory directory, Map playlist) { |
+ var episodeOffset = 2; |
+ var file = new File('${directory.path}/../$OUTPUT_FILENAME'); |
+ var output = file.openOutputStream(); |
+ output.writeString(""" |
+url-prefix: http://commondatastorage.googleapis.com/dartlang-podcast/ |
+episodes: |
+"""); |
+ var entries = playlist['feed']['entry']; |
+ for (var i = 0; i < entries.length; i++) { |
+ var epNum = entries.length - i + episodeOffset; |
+ writeEntry(output, entries[i], epNum); |
+ } |
+ output.close(); |
+} |
+ |
+writeEntry(OutputStream out, Map entry, int epNum) { |
+ String playerUrl = entry[r'media$group'][r'media$player'][0]['url']; |
+ String youtubeId = new RegExp("v=(.*)&").firstMatch(playerUrl)[1]; |
+ String title = entry['title'][r'$t']; |
+ String subtitle; |
+ Match match = new RegExp(r": (.*)$").firstMatch(title); |
+ if (match == null) { |
+ print("Title '$title' does not have a :"); |
+ return; |
+ } else { |
+ subtitle = match[1]; |
+ } |
+ String thumbnail = entry[r'media$group'][r'media$thumbnail'][0]['url']; |
+ String recorded; |
+ if (entry[r'yt$recorded'] == null) { |
+ print("No explicit recorded date for $title. Please set."); |
+ return; |
+ } else { |
+ recorded = entry[r'yt$recorded'][r'$t']; |
+ } |
+ String desc = entry['content'][r'$t'] |
+ .replaceAll("\n", ' ') |
+ .replaceAll('"', "'"); |
+ |
+ out.writeString(""" |
+- title: "${title}" |
+ subtitle: "${subtitle}" |
+# file: unknown.mp3 |
+ pubdate: ${recorded} |
+ description: "${desc}" |
+# length: 21815187 |
+ num: $epNum |
+ youtubeid: $youtubeId |
+ thumbnail: $thumbnail |
+"""); |
+} |
+ |
+main() { |
+ var script = new File(new Options().script); |
+ var directory = script.directorySync(); |
+ |
+ loadDartisansPlaylist() |
+ .then((String json) { |
+ Map data = parsePlaylistJson(json); |
+ writePlaylistYaml(directory, data); |
+ print("Complete!"); |
+ }); |
+} |