{"id":2643,"date":"2013-03-15T10:13:25","date_gmt":"2013-03-15T01:13:25","guid":{"rendered":"https:\/\/gunmagisgeek.com\/wordpress\/?p=2643"},"modified":"2017-02-03T01:19:29","modified_gmt":"2017-02-02T16:19:29","slug":"post-2643","status":"publish","type":"post","link":"https:\/\/gunmagisgeek.com\/blog\/node-js\/2643","title":{"rendered":"\u3010node.js+D3.js\u3011RSS\u30ea\u30fc\u30c0\u30fc\u3092\u4f5c\u308b"},"content":{"rendered":"<p>feedparser\u306e\u4ed5\u69d8\u304c\u5909\u308f\u3063\u305f\u305f\u3081\u73fe\u5728\u52d5\u304d\u307e\u305b\u3093\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2644\" alt=\"googlereader2\" src=\"http:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/03\/googlereader2.jpg\" width=\"615\" height=\"341\" srcset=\"https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/03\/googlereader2.jpg 615w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/03\/googlereader2-271x150.jpg 271w\" sizes=\"auto, (max-width: 615px) 100vw, 615px\" \/><\/p>\n<p><a href=\"http:\/\/itpro.nikkeibp.co.jp\/article\/NEWS\/20130314\/463318\/?top_nhl\">Google\u304c\u9177\u3044\u3053\u3068<\/a>\u3092\u3057\u305f\u306e\u3067\u81ea\u524d\u306eRSS\u30ea\u30fc\u30c0\u30fc\u3092\u4f5c\u6210\u3057\u3066\u307f\u307e\u3059\u3002<br \/>\n\u30d5\u30a3\u30fc\u30c9\u3092\u8aad\u307f\u8fbc\u3093\u3067\u30ea\u30b9\u30c8\u3068\u3057\u3066\u8868\u793a\u3059\u308b\u3060\u3051\u306e\u3001\u6700\u4f4e\u9650\u306e\u6a5f\u80fd\u3057\u304b\u3042\u308a\u307e\u305b\u3093\u3002<\/p>\n<h2>node.js\u3067RSS\u53d6\u5f97\u30b5\u30fc\u30d0\u30fc\u3092\u4f5c\u6210<\/h2>\n<p>\u30d5\u30a3\u30fc\u30c9\u306e\u8aad\u307f\u8fbc\u307f\u306b\u306f\u300cfeedparser\u300d\u30e9\u30a4\u30d6\u30e9\u30ea\u3092\u4f7f\u7528\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true \" >$ npm install feedparser\r\n<\/pre>\n<p>feed\u30d1\u30e9\u30e1\u30fc\u30bf\u30fc\u306b\u30d5\u30a3\u30fc\u30c9\u306eURL\u3092\u6307\u5b9a\u3057\u3066GET\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u9001\u308b\u3068\u53d6\u5f97\u3057\u305f\u5185\u5bb9\u3092JSON\u3067\u8fd4\u3059\u3060\u3051\u306e\u30b5\u30fc\u30d0\u30fc\u3067\u3059\u3002(WEB API)<\/p>\n<pre class=\"lang:js decode:true \" >var FeedParser = require('feedparser')\r\n\t, request = require('request')\r\n\t, fs = require('fs')\r\n\t, http = require('http')\r\n\t, url = require('url');\r\n\r\nhttp.createServer(function (req, res) {\r\n\tvar pathname = url.parse(req.url).pathname;\r\n\tvar queryData = url.parse(req.url, true).query;\r\n\tvar feedurl = unescape(queryData.feed)\r\n\tvar article = [];\r\n\tif(pathname === \"\/favicon.ico\") return;\r\n\r\n\trequest(feedurl)\r\n\t\t.pipe(new FeedParser())\r\n\t\t.on('error', function(error) {\r\n\t\t\tconsole.error(error);\r\n\t\t})\r\n\t\t.on('meta', function (meta) {\r\n\t\t\t\/\/console.error(meta);\r\n\t\t})\r\n\t\t.on('readable', function () {\r\n\t\t\t\/\/ do something else, then do the next thing\r\n\t\t\tvar stream = this, item; \r\n\r\n\t\t\twhile (item = stream.read()) {\r\n\t\t\t\tarticle.push(item);\r\n\t\t\t}\r\n\t\t\tstream.end = function(){\r\n\t\t\t\tres.writeHead(200, {\r\n\t\t\t\t\t'Content-Type':'application\/json; charset=utf-8',\r\n\t\t\t\t\t'Access-Control-Allow-Origin':'*',\r\n\t\t\t\t\t'Pragma': 'no-cache',\r\n\t\t\t\t\t'Cache-Control' : 'no-cache'\r\n\t\t\t\t});\r\n\t\t\t\tif(queryData.limit) article = article.slice(0, queryData.limit);\r\n\t\t\t\tres.write(JSON.stringify(article));\r\n\t\t\t\tres.end();\t\t\t\r\n\t\t\t};\r\n\r\n\t\t});\r\n\r\n\r\n\tprocess.on('uncaughtException', function (err) {\r\n\t\tconsole.log('uncaughtException =&gt; ' + err);\r\n\t\tconsole.log(feedurl); \r\n\t\tres.writeHead(500);\r\n\t\tres.write(\"error!\");\r\n\t\tres.end(); \r\n\t});\r\n}).listen(1337, '127.0.0.1');\r\n\r\n\r\nfunction send(){\r\n\r\n}\r\nconsole.log('Server running at http:\/\/127.0.0.1:1337\/?feed=&lt;RSS URL&gt;&amp;limit=&lt;length&gt;');<\/pre>\n<p>\u30ed\u30fc\u30ab\u30eb\u3067\u52d5\u304b\u3059\u3053\u3068\u524d\u63d0\u306a\u306e\u3067\u3001\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u3068\u304b\u8003\u616e\u3057\u3066\u307e\u305b\u3093\u3002<\/p>\n<h2>D3.js\u3067RSS\u30ea\u30fc\u30c0\u30fc\u306e\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u3092\u4f5c\u6210<\/h2>\n<p>D3.js\u306f\u30b0\u30e9\u30d5\u306e\u4f5c\u6210\u3060\u3051\u3067\u306a\u304f\u3001\u30d5\u30ed\u30f3\u30c8\u30a8\u30f3\u30c9\u306eUI\u3092\u4f5c\u6210\u3059\u308b\u306e\u306b\u3082\u4f7f\u3048\u307e\u3059\u3002<\/p>\n<pre class=\"lang:xhtml decode:true \" >&lt;!DOCTYPE html&gt;\r\n&lt;html&gt;\r\n&lt;head&gt;\r\n&lt;meta charset=\"utf-8\" \/&gt;\r\n&lt;script src=\"http:\/\/d3js.org\/d3.v3.min.js\"&gt;&lt;\/script&gt;\r\n&lt;script&gt;\r\nvar list = [ \/\/\u8aad\u307f\u8fbc\u3080\u30d5\u30a3\u30fc\u30c9\u306e\u6307\u5b9a\r\n\t{title:\"GUNMA GIS GEEK\", url:\"https:\/\/gunmagisgeek.com\/blog\/feed\", limit:5},\r\n\t{title:\"\u306f\u3066\u30d6\", url:\"http:\/\/feeds.feedburner.com\/hatena\/b\/hotentry\", limit:5},\r\n\t{title:\"WIRED.jp\", url:\"http:\/\/wiredvision.jp\/feed\/atom.xml\", limit:5}\r\n]\r\n\r\n\/\/IE v.9\u4ee5\u4e0b\u975e\u5bfe\u5fdc\r\nlist.map(function(d){\r\n\td.id\u3000= d.title.replace(\/s|t|\u3000|.|,|\\\/g,\"\"); \/\/title\u304b\u3089DOM id\u3092\u4f5c\u6210\r\n});\r\n\r\nvar date = new Date();\r\n\r\nwindow.onload = function(){\r\n\tvar ul = d3.select('body').selectAll('ul').data(list)\r\n\t\t\t\t.enter()\r\n\t\t\t\t.append('div')\r\n\t\t\t\t.attr('id', function(d){ return d.id})\r\n\t\t\t\t.append('ul')\r\n\r\n\tul.html(function(d){ return \"&lt;h3&gt;\" + d.title + \"&lt;\/h3&gt;\"})\r\n\t\t.attr('class', function(d){ return d.id + \"_ul\"})\r\n\t\t.selectAll(append_li)\r\n\r\n\tfunction\u3000append_li(d){\r\n\t\t\tvar date = new Date();\r\n\t\t\tvar encUrl = escape(d.url);\r\n\t\t\td3.json('http:\/\/127.0.0.1:1337\/?feed='+encUrl+'&amp;limit='+d.limit + \"&amp;\"+date, function(json){\r\n\t\t\t\td3.select('.'+d.id +\"_ul\")\r\n\t\t\t\t  .selectAll(\".\"+d.id+\"_list\")\r\n\t\t\t\t  .data(json)\r\n\t\t\t\t  .enter()\r\n\t\t\t\t  .append('li')\r\n\t\t\t\t  .attr(\"class\", d.id+\"_list\")\r\n\t\t\t\t  .html(function(d){\r\n\t\t\t\t\treturn '&lt;a href=\"'+d.link+'\" target=\"_blank\"&gt;' + d.title + '&lt;\/a&gt;';\r\n\t\t\t\t  })\r\n\t\t\t});\r\n\t\t\treturn 'li';\r\n\t\t  }\r\n\r\n}\r\n&lt;\/script&gt;\r\n\r\n&lt;\/head&gt;\r\n&lt;body&gt;\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;<\/pre>\n<h2>\u5b9f\u884c<\/h2>\n<p>\u307e\u305a\u30b5\u30fc\u30d0\u30fc\u3092\u8d77\u52d5\u3057\u307e\u3059\u3002<\/p>\n<pre class=\"lang:sh decode:true \" >node server.js<\/pre>\n<p>\u30b5\u30fc\u30d0\u30fc\u304c\u8d77\u52d5\u3057\u305f\u3089\u3001reader.html\u3092\u958b\u304d\u307e\u3059\u3002\u4ee5\u4e0a\u3002<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2645\" alt=\"simplerssreader\" src=\"http:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/03\/simplerssreader.jpg\" width=\"590\" height=\"272\" srcset=\"https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/03\/simplerssreader.jpg 590w, https:\/\/gunmagisgeek.com\/blog\/wp-content\/uploads\/2013\/03\/simplerssreader-300x138.jpg 300w\" sizes=\"auto, (max-width: 590px) 100vw, 590px\" \/><\/p>\n<p><span class=\"removed_link\" title=\"https:\/\/github.com\/shimizu\/SimpleRssReader\">https:\/\/github.com\/shimizu\/SimpleRssReader<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>feedparser\u306e\u4ed5\u69d8\u304c\u5909\u308f\u3063\u305f\u305f\u3081\u73fe\u5728\u52d5\u304d\u307e&hellip;<\/p>\n","protected":false},"author":1,"featured_media":2644,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,5],"tags":[],"class_list":["post-2643","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-d3-js","category-node-js","has-post-thumbnail-archive"],"_links":{"self":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/2643","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/comments?post=2643"}],"version-history":[{"count":1,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/2643\/revisions"}],"predecessor-version":[{"id":2660,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/posts\/2643\/revisions\/2660"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media\/2644"}],"wp:attachment":[{"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/media?parent=2643"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/categories?post=2643"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gunmagisgeek.com\/blog\/wp-json\/wp\/v2\/tags?post=2643"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}