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

finishing and fixing code while error on SSL url #7

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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
.env
19 changes: 19 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const express = require('express');
const bodyParser = require('body-parser');
const cookieParser = require('cookie-parser');
require('dotenv').config();

var twatts = require('./routes/twatts');

var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());

app.use('/api/twitter', twatts);

app.listen(process.env.PORT, ()=>{
console.log('success to running app on port: ', process.env.PORT);
})

module.exports = app;
38 changes: 38 additions & 0 deletions controllers/twatts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
const twitt = require('../helpers/twattprocess');

var methods = {}

methods.postStatus = function(req, res){
let text = req.body.status
twitt.posting(text, (data)=>{
res.send({
msg: 'posting status to twitter is success!',
data: data
})
})
}

methods.searchTwitt = function(req, res){
let text = req.params.search
twitt.search(text, (data)=>{
res.send({
msg: 'search status on twitter is success!',
data: data
})
})
}

methods.searchTrends = function(req, res){
let username = req.params.name
twitt.searching(username, (data)=>{
res.send(data)
})
}

methods.trends = function(req, res){
twitt.trends((data)=>{
res.send(data)
})
}

module.exports = methods;
54 changes: 54 additions & 0 deletions helpers/twattprocess.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const twitt = require('../models/twatts');
require('dotenv').config();

module.exports = {
posting : function(text, callback){
twitt.post(
'https://api.twitter.com/1.1/statuses/update.json?status='+text,
process.env.ACCESS_TOKEN,
process.env.ACCESS_TOKEN_SECRET,
text,
'text',
function(e, data){
if(e){
console.log(e)
} else {
callback(data);
}
}
);
},
search : function(text, callback){
twitt.get(
`https://api.twitter.com/1.1/search/tweets.json?q=${text}`,
process.env.ACCESS_TOKEN,
process.env.ACCESS_TOKEN_SECRET,
function(e, data){
if(e) console.error(e);
callback(data);
}
)
},
searching: function(text, callback){
twitt.get(
`https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=${text}&count=4`,
process.env.ACCESS_TOKEN,
process.env.ACCESS_TOKEN_SECRET,
function(e, data){
if(e) console.error(e);
callback(data);
}
)
},
trends: function(callback){
twitt.get(
`https://api.twitter.com/1.1/trends/user_timeline.json`,
process.env.ACCESS_TOKEN,
process.env.ACCESS_TOKEN_SECRET,
function(e, data){
if(e) console.error(e);
callback(data);
}
)
}
}
14 changes: 14 additions & 0 deletions models/twatts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const OAuth = require('oauth');
require('dotenv').config();

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

module.exports = oauth;
28 changes: 28 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"name": "twatt",
"version": "1.0.0",
"description": "",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon app.js"
},
"repository": {
"type": "git",
"url": "git+https://github.com/DeriKurniawan/twatt.git"
},
"author": "deri kurniawan",
"license": "ISC",
"bugs": {
"url": "https://github.com/DeriKurniawan/twatt/issues"
},
"homepage": "https://github.com/DeriKurniawan/twatt#readme",
"dependencies": {
"body-parser": "^1.17.2",
"cookie-parser": "^1.4.3",
"dotenv": "^4.0.0",
"express": "^4.15.3",
"nodemon": "^1.11.0",
"oauth": "^0.9.15"
}
}
9 changes: 9 additions & 0 deletions routes/twatts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const twitter = require('../controllers/twatts');
const router = require('express').Router();

router.post('/status', twitter.postStatus);
router.get('/:search', twitter.searchTwitt);
router.get('/', twitter.trends);
router.get('/:name', twitter.searchTrends);

module.exports = router;