OLD | NEW |
(Empty) | |
| 1 import 'dart:json'; |
| 2 import 'dart:io'; |
| 3 import 'dart:uri'; |
| 4 |
| 5 final String YT_PLAYLIST_URL = 'http://gdata.youtube.com/feeds/api/playlists/'; |
| 6 final String PLAYLIST_ID = 'PLOU2XLYxmsIIS2zgjdmBEwTrA6m5YgHBs'; |
| 7 |
| 8 final String OUTPUT_FILENAME = 'src/site/dartisans/episodes.yaml'; |
| 9 |
| 10 Future<String> loadDartisansPlaylist() { |
| 11 var completer = new Completer(); |
| 12 var client = new HttpClient(); |
| 13 var conn = client.getUrl(new Uri('$YT_PLAYLIST_URL$PLAYLIST_ID?alt=json')); |
| 14 conn.onResponse = (HttpClientResponse resp) { |
| 15 var buffer = new StringBuffer(); |
| 16 var input = resp.inputStream; |
| 17 input.onData = () => buffer.add(new String.fromCharCodes(input.read())); |
| 18 input.onClosed = () { |
| 19 print('Done loading JSON from API'); |
| 20 client.shutdown(); // XXX do this or the app remains running |
| 21 completer.complete(buffer.toString()); |
| 22 }; |
| 23 input.onError = (e) { |
| 24 completer.completeException("ERROR reading from input: $e"); |
| 25 }; |
| 26 }; |
| 27 conn.onError = (e) { |
| 28 completer.completeException("ERROR CONNECTING TO YT: $e"); |
| 29 }; |
| 30 return completer.future; |
| 31 } |
| 32 |
| 33 Map parsePlaylistJson(String json) => JSON.parse(json); |
| 34 |
| 35 writePlaylistYaml(Directory directory, Map playlist) { |
| 36 var episodeOffset = 2; |
| 37 var file = new File('${directory.path}/../$OUTPUT_FILENAME'); |
| 38 var output = file.openOutputStream(); |
| 39 output.writeString(""" |
| 40 # DO NOT EDIT THIS FILE - IT IS AUTOGENERATED |
| 41 # See scripts/gen_dartisans_playlist.dart |
| 42 url-prefix: http://commondatastorage.googleapis.com/dartlang-podcast/ |
| 43 episodes: |
| 44 """); |
| 45 var entries = playlist['feed']['entry']; |
| 46 for (var i = 0; i < entries.length; i++) { |
| 47 var epNum = entries.length - i + episodeOffset; |
| 48 writeEntry(output, entries[i], epNum); |
| 49 } |
| 50 output.close(); |
| 51 } |
| 52 |
| 53 writeEntry(OutputStream out, Map entry, int epNum) { |
| 54 String playerUrl = entry[r'media$group'][r'media$player'][0]['url']; |
| 55 String youtubeId = new RegExp("v=(.*)&").firstMatch(playerUrl)[1]; |
| 56 String title = entry['title'][r'$t']; |
| 57 String subtitle; |
| 58 Match match = new RegExp(r": (.*)$").firstMatch(title); |
| 59 if (match == null) { |
| 60 print("Title '$title' does not have a :"); |
| 61 return; |
| 62 } else { |
| 63 subtitle = match[1]; |
| 64 } |
| 65 String thumbnail = entry[r'media$group'][r'media$thumbnail'][0]['url']; |
| 66 String recorded; |
| 67 if (entry[r'yt$recorded'] == null) { |
| 68 print("No explicit recorded date for $title. Please set."); |
| 69 return; |
| 70 } else { |
| 71 recorded = entry[r'yt$recorded'][r'$t']; |
| 72 } |
| 73 String desc = entry['content'][r'$t'] |
| 74 .replaceAll("\n", ' ') |
| 75 .replaceAll('"', "'"); |
| 76 |
| 77 out.writeString(""" |
| 78 - title: "${title}" |
| 79 subtitle: "${subtitle}" |
| 80 # file: unknown.mp3 |
| 81 pubdate: ${recorded} |
| 82 description: "${desc}" |
| 83 # length: 21815187 |
| 84 num: $epNum |
| 85 youtubeid: $youtubeId |
| 86 thumbnail: $thumbnail |
| 87 """); |
| 88 } |
| 89 |
| 90 main() { |
| 91 var script = new File(new Options().script); |
| 92 var directory = script.directorySync(); |
| 93 |
| 94 loadDartisansPlaylist() |
| 95 .then((String json) { |
| 96 Map data = parsePlaylistJson(json); |
| 97 writePlaylistYaml(directory, data); |
| 98 print("Complete!"); |
| 99 }); |
| 100 } |
OLD | NEW |