A simple JSON end point for Twitter tweets searches. This was developed to allow for the inclusion of Tweet search content on static web pages, available via a JavaScript AJAX GET request.
It runs as a Node.js app, currently hosted on Heroku. Feel free to fork and host your own version wherever suits you.
Clone the app to a local directory. You'll need to register a new application with Twitter. Once registered, create a file called .env
to store your app details for local testing. It should contain the following (replace the XXX with your details):
consumer_key=XXX
consumer_secret=XXX
access_token_key=XXX
access_token_secret=XXX
Since Twitter imposes quite strict usage limits, the app stores requests in Redis with a 15 minute expiry. Since I've deployed this to Heroku, I've added support for Redis Cloud. If using Heroku, activating Redis Cloud (currently with a 20MB free plan) will automatically work.
Ensure you have Node.js installed. Install any dependencies first:
$ npm install
Then run the app:
$ node app
If you have foreman
installed, run the app like so:
$ foreman start
Since I wanted it to work with Heroku, the app runs by default on port 5000
.
Currently the app will return a 404 unless the path contains either search
or stream
.
Search is as follows:
http://localhost:5000/search/foo
This will search Twitter for the phrase "foo" and return a JSON response containing the results. You can create more complex queries:
http://localhost:5000/search/this/AND/that
The search in this case will be "this AND that".
Adding a URL into the path won't work as the app splits the URL parts by /
. To get around this, you can structure a request like this to search for a specific URL:
http://localhost:5000/search/?url=example.com/your/path
http://localhost:5000/search/keyword/?url=example.com/your/path
I have deployed it to Heroku using these instructions. Since it's a light, database-free app, it could just as well live on a free Appfog instance also.
You'll need to add the environment variables listed above when setting up your app with Heroku etc.
This app relies on ntwitter.
Some tests would be a good idea. I threw this implementation together in a morning as a proof of concept, so before doing any more work on it I'll get some unit tests in place.
MIT license applied. Have fun. Pull requests and improvements much appreciated.