Created
January 26, 2011 08:44
-
-
Save wezm/796432 to your computer and use it in GitHub Desktop.
Example of using genx to produce an Atom feed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var genx = require('genx') | |
| , http = require('http') | |
| , jsdom = require('jsdom').jsdom | |
| , qs = require('querystring') | |
| , uuid = require('node-uuid'); | |
| function ISODateString(d){ | |
| function pad(n){return n<10 ? '0'+n : n} | |
| return d.getUTCFullYear()+'-' | |
| + pad(d.getUTCMonth()+1)+'-' | |
| + pad(d.getUTCDate())+'T' | |
| + pad(d.getUTCHours())+':' | |
| + pad(d.getUTCMinutes())+':' | |
| + pad(d.getUTCSeconds())+'Z'} | |
| function generateXml(body) { | |
| var w = new genx.Writer() | |
| , window = jsdom(body, null, { | |
| features: { | |
| FetchExternalResources: false, | |
| MutationEvents: false | |
| }}).createWindow() | |
| , $ = require('jquery').create(window); | |
| w.on('data', function(data) { | |
| process.stdout.write(data); | |
| }); | |
| // Declare the elements and attributes up-front | |
| var ns = w.declareNamespace('http://www.w3.org/2005/Atom', '') | |
| , feed = w.declareElement(ns, 'feed') | |
| , title = w.declareElement(ns, 'title') | |
| , link = w.declareElement(ns, 'link') | |
| , updated = w.declareElement(ns, 'updated') | |
| , author = w.declareElement(ns, 'author') | |
| , name = w.declareElement(ns, 'name') | |
| , id = w.declareElement(ns, 'id') | |
| , entry = w.declareElement(ns, 'entry') | |
| , summary = w.declareElement(ns, 'summary') | |
| // Attributes | |
| , href = w.declareAttribute('href'); | |
| var updatedDate = ISODateString(new Date()); | |
| // This is not a processing instruction and as such can be generated by Genx | |
| process.stdout.write("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"); | |
| // Write out the start of the document | |
| w.startDocument() | |
| .startElement(feed) | |
| .startElement(title).addText("Genx Example Feed").endElement() | |
| .startElement(link).addAttribute(href, "https://github.com/wezm/node-genx").endElement() | |
| .startElement(updated).addText(updatedDate).endElement() | |
| .startElement(author) | |
| .startElement(name).addText("Wesley Moore").endElement() | |
| .endElement() | |
| .startElement(id).addText("urn:uuid:" + uuid()).endElement(); | |
| // Remove the green URLs from the result, don't want it in the description | |
| $('#ires ol li .s .f').remove(); | |
| // Loop over each result | |
| $("#ires ol li").each(function(i, li) { | |
| var result = { | |
| title: $('h3.r a', li).text(), | |
| href: $('h3.r a', li).attr('href'), | |
| description: $('#ires ol li .s').text() | |
| }; | |
| // start each document on a new line | |
| w.addText("\n") | |
| .startElement(entry) | |
| .startElement(title).addText(result.title).endElement() | |
| .startElement(link).addAttribute(href, result.href).endElement() | |
| .startElement(id).addText("urn:uuid:" + uuid()).endElement() | |
| .startElement(updated).addText(updatedDate).endElement() | |
| .startElement(summary).addText(result.description).endElement() | |
| .endElement() | |
| }); | |
| w.addText("\n") | |
| .endElement() | |
| .endDocument(); | |
| } | |
| // Make the request and when finished generate the XML | |
| // http://www.google.com/search?q=node.js+xml | |
| var google = http.createClient(80, 'www.google.com'); | |
| var query = qs.stringify({ | |
| q: "node.js xml", | |
| ie: 'UTF-8', | |
| oe: 'UTF-8' | |
| }); | |
| var request = google.request( | |
| 'GET', | |
| '/search?' + query, | |
| {'host': 'www.google.com'} | |
| ); | |
| request.end(); | |
| request.on('response', function (response) { | |
| if (response.statusCode != 200) { | |
| console.log("Error: Unexpected response code " + response.statusCode); | |
| return; // Would be nice to abort the request here | |
| } | |
| var body = ''; | |
| response.setEncoding('utf8'); | |
| response.on('data', function (chunk) { | |
| body += chunk; | |
| }); | |
| response.on('end', function() { | |
| generateXml(body); | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment