Last active
December 5, 2016 22:33
-
-
Save pietgeursen/4ef6c4c4b8a631c88aa408aa0483b66b to your computer and use it in GitHub Desktop.
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
| 'use strict'; | |
| const dotenv = require('dotenv') | |
| const geocoder = require('geocoder'); | |
| const Rx = require('rx') | |
| const {Observable} = Rx | |
| const {create, fromEvent, fromNodeCallback} = Observable | |
| // load environment variables | |
| dotenv.load() | |
| // config api | |
| const googleTanslate = GoogleTranslate(process.env.GOOGLE_TRANSLATE_API_KEY) | |
| const Twitter = require('twitter') | |
| const client = new Twitter({ | |
| consumer_key: process.env.TWITTER_CONSUMER_KEY, | |
| consumer_secret: process.env.TWITTER_CONSUMER_SECRET, | |
| access_token_key: process.env.TWITTER_ACCESS_TOKEN_KEY, | |
| access_token_secret: process.env.TWITTER_ACCESS_TOKEN_SECRET, | |
| }) | |
| var stream = client.stream('statuses/filter', {track: 'trump'}); | |
| var tweets = fromEvent(stream, 'data') | |
| var simpleTweets = tweets | |
| .map(({text, coordinates, user: {location}}) => {return {text, coordinates, location}}) | |
| var [locatedTweets, unlocatedTweets] = simpleTweets | |
| .partition(({coordinates}) => coordinates) | |
| var geocodes = fromNodeCallback(geocoder.geocode, geocoder) | |
| var geocodedTweets = unlocatedTweets | |
| .filter(({location}) => location) | |
| .flatMap(tweet => { | |
| return geocodes(tweet.location) | |
| .filter(location => location.results.length > 0) | |
| .filter(location => location.results[0].geometry) | |
| .map(location => location.results[0].geometry.location) | |
| .map(geometry => { | |
| tweet.coordinates = geometry | |
| return tweet | |
| }) | |
| }) | |
| Observable.merge(geocodedTweets, locatedTweets) | |
| .subscribe(console.log) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment