Skip to content

Instantly share code, notes, and snippets.

@heapwolf
Last active December 24, 2015 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heapwolf/6808425 to your computer and use it in GitHub Desktop.
Save heapwolf/6808425 to your computer and use it in GitHub Desktop.
foursquare-streams

SYNOPSIS

A simple streaming abstraction for foursquare's REST APIs

DESCRIPTION

If you for some reason want to collect the garbage that is foursquare's data, but you want to do it with streams, here's a module for it. Zing!

USAGE

var fsconn = {
  clientId: '2DSWUN',
  clientSecret: 'YCSISJ'
}

var foursquare = require('foursquare-streams').createClient(fsconn)

var opts = {
  'near': 'NYC',
  'limit': 50,
}

foursquare.getVenues(opts).pipe(...) // pipe that stream, dog!

INSTALL

$ npm install foursquare-streams
var querystring = require('querystring')
var hyperquest = require('hyperquest')
var JSONStream = require('JSONStream')
var through = require('through')
var mapStream = require('map-stream')
exports.createClient = function (opts) {
if (!opts.clientId || !opts.clientSecret) {
throw new Error('options object requires `clientId` and `clientSecret` members.')
}
var clientId = opts.clientId
var clientSecret = opts.clientSecret
var today = new Date()
var month = today.getMonth() + 1
if (month < 10) {
month = '0' + month
}
var day = today.getDate()
if (day < 10) {
day = '0' + day
}
var date = today.getFullYear() + '' + month + '' + day
var credentials = {
'v': date,
'client_id': clientId,
'client_secret': clientSecret
}
var client = {}
client.getVenues = function getVenues(params) {
var url = [
'https://api.foursquare.com/v2/venues/search?',
querystring.stringify(params) + '&',
querystring.stringify(credentials)
].join('')
return hyperquest(url)
.pipe(JSONStream.parse('response.venues.*'))
.pipe(mapStream(function (venueRef, callback) {
var that = this
client.getVenue({
venueId: venueRef.id
}).pipe(through(function(venue) {
callback(null, venue)
}))
}))
}
client.exploreVenues = function exploreVenues(params, callback) {
var url = [
'https://api.foursquare.com/v2/venues/explore?',
querystring.stringify(params) + '&',
querystring.stringify(credentials)
].join('')
return hyperquest(url).pipe(JSONStream.parse())
}
client.getVenue = function getVenue(params, callback) {
var url = [
'https://api.foursquare.com/v2/venues/',
params.venueId + '?',
querystring.stringify(credentials)
].join('')
return hyperquest(url).pipe(JSONStream.parse())
}
return client
}
Copyright (c) 2013 hij1nx. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
{
"name": "foursquare-streams",
"version": "0.0.1",
"description": "various foursquare APIs, streaming",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": "https://gist.github.com/6808425.git",
"author": "",
"license": "BSD",
"readme": "ERROR: No README data found!",
"dist": {
"shasum": "da6ea75dc5b55692332f878ea489001d00e33257"
},
"dependencies": {
"hyperquest": "~0.1.6",
"JSONStream": "~0.6.4",
"through": "~2.3.4",
"map-stream": "0.0.2"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment