Skip to content

Instantly share code, notes, and snippets.

@senthilmpro
Created March 10, 2024 15:32
Show Gist options
  • Save senthilmpro/ab3b3966dd9c435aada221f949d01b9b to your computer and use it in GitHub Desktop.
Save senthilmpro/ab3b3966dd9c435aada221f949d01b9b to your computer and use it in GitHub Desktop.
telegram-channel-dl.js
// download
let counter = 0;
let prefix = "TamilFreakers";
const LAST_INDEX = 1210; // get the message Id of last message (right click on the latest post, "copy message link".. this should have latest message id")
let messages = Array(LAST_INDEX).fill(0).map((x, i) => `message${i + 1}`).reverse();
let textContents = [];
let htmlContents = [];
const sleep = (delay) => new Promise(res => setTimeout(res, delay));
let obj = {};
//messages = messages.slice(0, 370);
const start = async () => {
await messages.reduce(async (memo, message) => {
await memo;
try {
let id = `#${message}`;
let el = document.querySelector(id);
if (el) {
const { content, rawContent } = getElementContent(el);
if (!obj[id]) {
await sleep(500);
obj[message] = { content };
console.log(message);
await copyTextToArray(id);
//await downloadElementAsCanvas(id);
//await downloadElementAsTxt(id);
console.log(`processing ${id}`);
} else {
console.log("ID Already present , ", id);
}
}
} catch (err) {
console.log("ERROR : ", err);
}
}, undefined);
console.log("COMPLETED ")
}
const getElementContent = (el) => {
const divEl = el.querySelectorAll('.message-date-group div');
return {
rawContent: divEl?.innerText,
content: divEl?.innerText?.replaceAll("\n", " ")
}
}
const downloadElementAsCanvas = async (id) => {
let el = document.querySelector(id);
if (el) {
el.scrollIntoView();
await sleep(2500);
html2canvas(el.querySelector('.message-content-wrapper .message-content')).then(canvas => {
var link = document.createElement('a');
let filename = id.replace("#", '');
link.download = `${prefix}-${filename}.png`;
link.href = canvas.toDataURL()
link.click();
});
}
}
const downloadElementAsTxt = async (id) => {
let el = document.querySelector(id);
if (el) {
el.scrollIntoView();
}
let text = document.querySelector(id).innerText;
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
let filename = id.replace("#", '');
element.setAttribute('download', `${prefix}-${filename}.txt`);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
const copyTextToArray = async (id) => {
const el = document.querySelector(id);
let text = el.innerText;
let html = el.innerHTML;
if (el) {
el.scrollIntoView();
await sleep(500);
}
textContents.push(text);
htmlContents.push(html);
const num = parseInt(id.replace("#message", ""));
if (num % 100 == 0) {
console.log(textContents);
await downloadTextAsFile(textContents, num, prefix, true);
await downloadTextAsFile(htmlContents, num, prefix, false);
textContents = [];
htmlContents = [];
}
return true;
}
const downloadTextAsFile = async (text, num, prefix, isText) => {
const isTextStr = isText ? "text": "html";
var element = document.createElement('a');
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', `${prefix}-${num}-${isTextStr}.txt`);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
await sleep(500);
console.log(`Downloaded ${prefix}-${num}-${isTextStr}.txt`);
return;
}
start();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment