Skip to content

[script add]: Add a news paper downloading script #1326

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

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
1 change: 1 addition & 0 deletions JavaScript/News_Downloader/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
10 changes: 10 additions & 0 deletions JavaScript/News_Downloader/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# News Downloader

Download the latest news from THE TELEGRAPH

## One time install
- Make sure you have node.js installed
- Run `npm i` to install all the dependency

## How to download ?
- Download using `npm run download`
54 changes: 54 additions & 0 deletions JavaScript/News_Downloader/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import fetch from "node-fetch";
import { parse } from 'node-html-parser';
import PDFDocument from "pdfkit";
import fs from 'fs';

console.log("Downloading...");
const dateTime = new Date();
const paperName ="telegraphindia";
const year = `${dateTime.getFullYear()}`;
const month = `${dateTime.getMonth()+1}`;
const date = `${dateTime.getDate()}`;
const URL = `https://epaper.${paperName}.com/calcutta/${year}-${month}-${date}/71/Page-1.html`;

const getRawData = async (URL) => {
const response = await fetch(URL);
const data = await response.text();
return data;
};

const gettelegraphindia = async () => {
const telegraphindia = await getRawData(URL);
const root = parse(telegraphindia);
const totalPage = root.querySelector('#totalpages').rawAttributes.value
const totalDate = `${date.length==2?date:"0"+date}${month.length==2?month:"0"+month}${year}`
const doc = new PDFDocument({
size: [419, 673],
});
doc.pipe(fs.createWriteStream(`${paperName} ${date}-${month}-${year}.pdf`));
const newsPage = [];
for(let i=1; i<=totalPage; i++){
newsPage.push(fetch(`https://epaper.${paperName}.com/epaperimages////${totalDate}////${totalDate}-md-hr-${i}.jpg`, {
method: 'GET'
})
)
}
const newsPageResp = await Promise.all(newsPage);
for(let i=0; i<newsPageResp.length; i++){
await newsPageResp[i].buffer().then(buffer => {
doc.image(buffer, 0, 0, {
width: 419,
height: 673
});
});
if(i<newsPageResp.length) {
doc.addPage({
size: [419, 673],
});
}
}
doc.end();
console.log("Download Finished");
};

gettelegraphindia();
Loading