Skip to content
This repository was archived by the owner on Dec 24, 2021. It is now read-only.

Decode body from response-specified charset to UTF8 #12

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 14 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
var request = require('request')
, sax = require('sax')
, _ = require('underscore');
, _ = require('underscore')
, iconv = require('iconv-lite');


// Public: Fetch the articles from the RSS or ATOM feed.
Expand Down Expand Up @@ -51,16 +52,26 @@ FeedRead.identify = function(xml) {
}
}


// Internal: Identify response charset.
//
// response - Response object (returned by request lib).
//
// Returns charset specified via Content-Type header or "utf-8" if nothing is specified.
FeedRead.identifyCharset = function(response) {
var contentType = response && response.headers && response.headers["content-type"];
var match = contentType && contentType.match(/charset=(\S+)/);
return match && match[1] || "utf-8";
}

// Internal: Get a single feed.
//
// feed_url - String url.
// callback - Receives `(err, articles)`.
//
FeedRead.get = function(feed_url, callback) {
request(feed_url, {timeout: 5000}, function(err, res, body) {
request(feed_url, {encoding: null, timeout: 5000}, function(err, res, body) {
if (err) return callback(err);
body = iconv.decode(body, FeedRead.identifyCharset(res));
var type = FeedRead.identify(body);
if (type == "atom") {
FeedRead.atom(body, feed_url, callback);
Expand Down
75 changes: 39 additions & 36 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
{ "name": "feed-read"
, "version": "0.0.1"
, "description": "a ATOM and RSS feed parser"
, "keywords": ["atom", "rss", "feed", "parser"]
, "homepage": "https://github.com/sentientwaffle/feed-read"
, "bugs":
{ "url": "https://github.com/sentientwaffle/feed-read/issues"
{
"name": "feed-read",
"version": "0.0.2",
"description": "a ATOM and RSS feed parser",
"keywords": [
"atom",
"rss",
"feed",
"parser"
],
"homepage": "https://github.com/sentientwaffle/feed-read",
"bugs": {
"url": "https://github.com/sentientwaffle/feed-read/issues"
},
"author": {
"name": "sentientwaffle",
"url": "http://sentientwaffle.github.com/"
},
"main": "./index",
"scripts": {
"test": "mocha"
},
"repository": {
"type": "git",
"url": "https://github.com/sentientwaffle/feed-read.git"
},
"dependencies": {
"iconv-lite": "^0.4.8",
"request": "2.x.x",
"sax": "0.3.x",
"underscore": "1.x.x"
},
"devDependencies": {
"should": "0.x.x",
"mocha": "0.x.x",
"connect": "1.x.x"
},
"engines": {
"node": ">=0.4.1"
}
, "author":
{ "name": "sentientwaffle"
, "url": "http://sentientwaffle.github.com/"
}

, "main": "./index"
, "scripts":
{ "test": "mocha"
}


, "repository":
{ "type": "git"
, "url": "https://github.com/sentientwaffle/feed-read.git"
}

, "dependencies":
{ "request": "2.x.x"
, "sax": "0.3.x"
, "underscore": "1.x.x"
}

, "devDependencies":
{ "should": "0.x.x"
, "mocha": "0.x.x"
, "connect": "1.x.x"
}

, "engines": { "node": ">=0.4.1" }
}