Welcome folks today in this blog post we will be using the twitter api
in node.js and express to download
posts and videos using url
and username in ejs. All the full source code of the application is shown below.
Get Started
In order to get started you need to make a new node.js
project and execute the below commands as shown below
npm init -y
npm i express
npm i ejs
npm i twdown-scrapper
npm i request
After that you will see the below directory
structure of the node.js express project as shown below
Now we need to make the index.js
file which will be the starting point of the application and copy paste the below code to start the express
server
index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
const express = require("express"); const request = require('request') const bodyParser = require("body-parser"); const twitterAPI= require("twdown-scrapper"); const app = express() const port = 3000 app.use(bodyParser.urlencoded({extended:false})) app.set('view engine','ejs') app.get('/',(req,res) => { res.render('index') }) app.listen(port,() => { console.log("App is listening on port 3000") }) |
As you can see we have imported all the libraries
at the top and started the express server at the port number and then we are writing the get
request at the /
route to load the index.ejs
template. And now we need to define this template inside the views
folder as shown below
views/index.ejs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> </head> <body> <div class="container"> <h1>Twitter Downloader</h1> <form method="post" action="/download" method="POST"> <div class="form-group"> <label for="url">Enter Twitter Video URL:</label> <input type="text" class="form-control" name="url" placeholder="Enter URL" required/> </div> <button type="submit" class="btn btn-primary">Download Video</button> </form> </div> </body> </html> |
And now we need to make the post
request inside the index.js
file as shown below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
const express = require("express"); const request = require('request') const bodyParser = require("body-parser"); const twitterAPI = require("twdown-scrapper"); const app = express() const port = 3000 app.use(bodyParser.urlencoded({extended:false})) app.set('view engine','ejs') app.get('/',(req,res) => { res.render('index') }) app.post('/download',async (req,res) => { const url = req.body.url; twitterAPI(url).then((response) => { console.log(response) if(response.sd == null){ downloadFile(res,response.url[0].url,"jpg") }else{ downloadFile(res,response.hd.url,"mp4") } }) }) function downloadFile(res, url, ext) { request(url, { encoding: null }, (error, response, body) => { if (error || response.statusCode !== 200) { res.status(400).send("Error downloading image"); return; } const fileName = Date.now() + "." + ext; res.setHeader("Content-Type", "application/octet-stream"); res.setHeader("Content-Disposition", `attachment; filename="${fileName}"`); res.send(body); }); } app.listen(port,() => { console.log("App is listening on port 3000") }) |
As you can see we are fetching the details
of the twitter link that you submitted and then we are downloading the file
as an attachment inside the browser with the help of request
module.