UNPKG

6.27 kBMarkdownView Raw
1[![Build Status](https://secure.travis-ci.org/michaelnisi/pickup.svg)](http://travis-ci.org/michaelnisi/pickup)
2[![Coverage Status](https://coveralls.io/repos/github/michaelnisi/pickup/badge.svg?branch=master)](https://coveralls.io/github/michaelnisi/pickup?branch=master)
3
4# pickup - transform feeds
5
6The **pickup** [Node](http://nodejs.org/) package transforms XML feeds to JSON or objects. Exporting a [Transform](http://nodejs.org/api/stream.html#stream_class_stream_transform) stream, it parses [RSS 2.0](http://cyber.law.harvard.edu/rss/rss.html), including [iTunes](https://www.apple.com/itunes/podcasts/specs.html) namespace extensions, and [Atom 1.0](http://atomenabled.org/developers/syndication/) formatted XML, producing newline separated JSON strings or objects.
7
8## Usage
9
10### Command-line
11
12```
13$ export URL=https://www.newyorker.com/feed/posts
14$ curl -sS $URL | node ./example/stdin.js
15```
16
17The Standard Input example is more or less the CLI binary of **pickup**, `./bin/cli.js`, which you can install globally if you want to.
18
19```
20npm i -g pickup
21```
22
23đź’ˇ *You can use `pickup`, `node ./example/stdin.js`, or `node ./bin/cli.js` in these examples.*
24
25Piping data to **pickup**, you can now, for example, parse feeds from the network using [curl](https://curl.haxx.se) on the command-line.
26
27```
28$ curl -sS $URL | pickup
29```
30
31For convenient JSON massaging, I use [json](https://github.com/trentm/json), available on [npm](https://www.npmjs.com).
32
33```
34$ npm install -g json
35```
36
37Now you can extend your pipe for correct formatting…
38
39```
40$ curl -sS $URL | pickup | json -g
41```
42
43…and handy filtering.
44
45```
46$ curl -sS $URL | pickup | json -ga title
47```
48
49### REPL
50
51Being a library user of this module, my preferred interface for exploring, and testing while working on it, is its REPL.
52
53```
54$ ./repl.js
55pickup> let x = get('https://www.newyorker.com/feed/posts')
56pickup> read(x, Entry, 'title')
57pickup> 'An Asylum Seeker’s Quest to Get Her Toddler Back'
58'At N.H.L.’s All-Star Weekend, Female Players Prove Their Excellence, but NBC Can’t Keep Up'
59'Cliff Sims Is Proud to Have Served Trump'
60'The Slimmed-Down and Soothing New “Conan”'
61'Donald Trump’s Chance to Bring Peace to Afghanistan and End America’s Longest War'
62'The First Days of Disco'
63'How Various Government Agencies Celebrated the End of the Shutdown'
64'Why We Care (and Don’t Care) About the New Rules of Golf'
65'Howard Schultz Against the Hecklers'
66'Daily Cartoon: Tuesday, January 29th'
67ok
68pickup>
69```
70
71Look at `./repl.js` to see what it can do. Not much, but enough for exploring exotic feeds.
72
73### Library
74
75#### Transforming from stdin to stdout
76
77Here’s the Standard Input example from before again.
78
79```js
80const pickup = require('pickup')
81
82process.stdin
83 .pipe(pickup())
84 .pipe(process.stdout)
85```
86
87You can run this example from the command-line:
88
89```
90$ curl -sS $URL | node example/stdin.js | json -g
91```
92
93#### Proxy server
94
95```js
96const http = require('http')
97const https = require('https')
98const pickup = require('../')
99
100http.createServer((req, res) => {
101 https.get('https:/'.concat(req.url), (feed) => {
102 feed.pipe(pickup()).pipe(res)
103 })
104}).listen(8080)
105```
106
107To try the proxy server:
108
109```
110$ node example/proxy.js &
111$ curl -sS http://localhost:8080/www.newyorker.com/feed/posts | json -ga title
112```
113
114## Types
115
116### str()
117
118This can either be a `String()`, `null`, or `undefined`.
119
120### opts()
121
122The options [`Object`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object) is passed to the `Transform` stream constructor.
123
124**pickup** uses following additional options:
125
126- `eventMode` [`Boolean`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean) defaults to `false`, if `true` readable state buffers are not filled and no `'data'`, but `'feed'` and `'entry'` events are emitted.
127
128- `charset` `str()` | `'UTF-8'` | `'ISO-8859-1'` An optional string to specify the encoding of input data. In the common use case you received this string in the headers of your HTTP response before you began parsing. If you, not so commonly, cannot provide the encoding upfront, **pickup** tries to detect the encoding, and eventually defaults to `'UTF-8'`. The `charset` option is corresponding to the optional `charset` MIME type parameter found in `Content-Type` HTTP headers. It's OK to pass any string, **pickup** will fall back on `'UTF-8'` when confused.
129
130### url()
131
132An `undefined` property, not populated by the parser, to identify feeds and entries in upstream systems, without prompting V8 to create new [hidden classes](https://github.com/v8/v8/wiki/Design%20Elements#fast-property-access).
133
134### feed()
135
136- `author` `str()`
137- `copyright` `str()`
138- `id` `str()`
139- `image` `str()`
140- `language` `str()`
141- `link` `str()`
142- `originalURL` `url()`
143- `payment` `str()`
144- `subtitle` `str()`
145- `summary` `str()`
146- `title` `str()`
147- `ttl` `str()`
148- `updated` `str()`
149- `url` `url()`
150
151### enclosure()
152
153- `length` `str()`
154- `type` `str()`
155- `url` `str()`
156
157### entry()
158
159- `author` `str()`
160- `duration` `str()`
161- `enclosure` `enclosure()` or `undefined`
162- `id` `str()`
163- `image` `str()`
164- `link` `str()`
165- `originalURL` `url()`
166- `subtitle` `str()`
167- `summary` `str()`
168- `title` `str()`
169- `updated` `str()`
170- `url` `url()`
171
172### Event:'feed'
173
174```js
175feed()
176```
177Emitted when the meta information of the feed gets available.
178
179### Event:'entry'
180
181```js
182entry()
183```
184Emitted for each entry.
185
186## Exports
187
188```js
189pickup(opts())
190```
191
192**pickup** exports a function that returns a [Transform](http://nodejs.org/api/stream.html#stream_class_stream_transform) stream which emits newline separated JSON strings, in `objectMode` the `'data'` event contains `entry()` or `feed()` objects. As per XML's structure the last `'data'` event usually contains the `feed()` object. In `eventMode` neither `'readable'` nor `'data'` events are emitted, instead `'feed'` and `'entry'` events are fired.
193
194## Installation
195
196With [npm](https://npmjs.org/package/pickup), do:
197
198```
199$ npm install pickup
200```
201
202To use the CLI (as above):
203
204```
205$ npm install -g pickup
206```
207
208## Contribute
209
210Please create an issue if you encounter a feed that **pickup** fails to parse.
211
212## License
213
214[MIT license](https://raw.github.com/michaelnisi/pickup/master/LICENSE)