Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Timeline Done #5

Open
wants to merge 2 commits 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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.DS_Store
node_modules
.env
12 changes: 12 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const index = require('./routes/index');
const twatt = require('./routes/api');

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use('/', index);
app.use('/api/', twatt);

app.listen(3000, () => console.log("Listening on port 3000"));
58 changes: 58 additions & 0 deletions controllers/twatt_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const OAuth = require('oauth');
require('dotenv').config();

function index(req, res) {
res.send('Welcome to the Twitter API');
}

function search(req, res) {
var oauth = new OAuth.OAuth(
'https://api.twitter.com/oauth/request_token',
'https://api.twitter.com/oauth/access_token',
process.env.CONSUMER_KEY, //consumer key
process.env.APPLICATION_SECRET, //application secret
'1.0A',
null,
'HMAC-SHA1'
);
oauth.get(
'https://api.twitter.com/1.1/search/tweets.json?q=hacktiv8&src=typd',
process.env.USER_TOKEN, //user token
process.env.USER_SECRET, //user secret
function (e, data) {
let result = [];
data = JSON.parse(data);
if (e) console.error(e);
data.statuses.forEach(element => {
result.push(element.user.name);
result.push(element.text);
});
console.log(result);
res.send(result);
});
}

function timeline(req, res) {
var oauth = new OAuth.OAuth(
'https://api.twitter.com/oauth/request_token',
'https://api.twitter.com/oauth/access_token',
process.env.CONSUMER_KEY, //consumer key
process.env.APPLICATION_SECRET, //application secret
'1.0A',
null,
'HMAC-SHA1'
);
oauth.get(
'https://api.twitter.com/1.1/statuses/user_timeline.json',
process.env.USER_TOKEN, //user token
process.env.USER_SECRET, //user secret
function (e, data) {
data = JSON.parse(data);
if (e) console.error(e);
res.send(data);
});
}

module.exports = {
index, search, timeline
}
30 changes: 30 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"name": "twatt-search",
"version": "1.0.0",
"description": "Learn Twitter API",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/raynormw/twatt-search.git"
},
"keywords": [
"twitter",
"api",
"hacktiv8"
],
"author": "raynormw",
"license": "ISC",
"bugs": {
"url": "https://github.com/raynormw/twatt-search/issues"
},
"homepage": "https://github.com/raynormw/twatt-search#readme",
"dependencies": {
"body-parser": "^1.17.2",
"dotenv": "^4.0.0",
"express": "^4.15.3",
"oauth": "^0.9.15"
}
}
9 changes: 9 additions & 0 deletions routes/api/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const express = require('express');
const router = express.Router();
const twatt = require('../../controllers/twatt_controller');

router.get('/', twatt.index);
router.get('/search', twatt.search);
router.get('/timeline', twatt.timeline);

module.exports = router;
8 changes: 8 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const express = require('express');
const router = express.Router();

router.get('/', function(req, res) {
res.send("Our site is alive, yeay..");
});

module.exports = router;