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

Finish twatt #4

Open
wants to merge 4 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
27 changes: 27 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const express = require('express');
const bodyParser = require('body-parser');
const OAuth = require('oauth');
const dotenv = require('dotenv').config();

const index = require('./routes/index');

const app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

app.use('/api/tweets', index);

const oauth = new OAuth.OAuth(
'https://api.twitter.com/oauth/request_token',
'https://api.twitter.com/oauth/access_token',
process.env.API_KEY,
process.env.API_SECRET,
'1.0A',
null,
'HMAC-SHA1'
);

app.locals.oauth = oauth;

app.listen(3000);
26 changes: 26 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"name": "twatt",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "nodemon app.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "git+https://github.com/yusufhacktiv8/twatt.git"
},
"author": "",
"license": "ISC",
"bugs": {
"url": "https://github.com/yusufhacktiv8/twatt/issues"
},
"homepage": "https://github.com/yusufhacktiv8/twatt#readme",
"dependencies": {
"body-parser": "^1.17.2",
"dotenv": "^4.0.0",
"express": "^4.15.3",
"oauth": "^0.9.15"
}
}
61 changes: 61 additions & 0 deletions routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const express = require('express');

const router = express.Router();
require('dotenv').config();

router.get('/test', (req, res) => {
req.app.locals.oauth.get(
'https://api.twitter.com/1.1/trends/place.json?id=23424977',
process.env.TOKEN,
process.env.TOKEN_SECRET,
function (e, data, resp) {
if (e) console.error(e);
console.log(data);
res.send(data);
});
});

router.get('/timelines', function(req, res) {
const url = 'https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=Yusuf_hacktiv8&count=2';
req.app.locals.oauth.get(
url,
process.env.TOKEN,
process.env.TOKEN_SECRET,
function (e, data, resp) {
if (e) console.error(e);
console.log(data);
res.send(data);
});
});

router.get('/search', function(req, res) {
const keyword = req.query.keyword ? req.query.keyword : '';
const url = `https://api.twitter.com/1.1/search/tweets.json?q=${keyword}&count=1`;
req.app.locals.oauth.get(
url,
process.env.TOKEN,
process.env.TOKEN_SECRET,
function (e, data, resp) {
if (e) console.error(e);
console.log(data);
res.send(data);
});
});

router.post('/addtweet', function(req, res) {
const tweet = req.body.tweet;
const url = `https://api.twitter.com/1.1/statuses/update.json?status=${tweet}`;
req.app.locals.oauth.post(
url,
process.env.TOKEN,
process.env.TOKEN_SECRET,
'',
'text/plain',
function (e, data, resp) {
if (e) console.error(e);
console.log(data);
res.send(data);
});
});

module.exports = router;