koztv-blog-tools 1.1.0 → 1.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/export-telegram.js +121 -0
- package/package.json +6 -2
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* CLI for exporting Telegram channel posts
|
|
4
|
+
*
|
|
5
|
+
* Usage:
|
|
6
|
+
* npx koztv-blog-tools export --channel @channelname --out ./export
|
|
7
|
+
*
|
|
8
|
+
* Environment variables:
|
|
9
|
+
* TELEGRAM_API_ID - API ID from https://my.telegram.org
|
|
10
|
+
* TELEGRAM_API_HASH - API Hash from https://my.telegram.org
|
|
11
|
+
* TELEGRAM_SESSION - Session string (for re-authentication)
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
const { exportTelegramChannel } = require('../dist/index.js');
|
|
15
|
+
const fs = require('fs');
|
|
16
|
+
const path = require('path');
|
|
17
|
+
const readline = require('readline');
|
|
18
|
+
|
|
19
|
+
// Parse arguments
|
|
20
|
+
const args = process.argv.slice(2);
|
|
21
|
+
const getArg = (name) => {
|
|
22
|
+
const idx = args.indexOf(`--${name}`);
|
|
23
|
+
return idx !== -1 ? args[idx + 1] : null;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
const CHANNEL = getArg('channel') || process.env.TELEGRAM_CHANNEL;
|
|
27
|
+
const OUTPUT_DIR = getArg('out') || './export';
|
|
28
|
+
const LIMIT = parseInt(getArg('limit') || '0', 10);
|
|
29
|
+
const SINCE = getArg('since');
|
|
30
|
+
const UNTIL = getArg('until');
|
|
31
|
+
const NO_MEDIA = args.includes('--no-media');
|
|
32
|
+
|
|
33
|
+
const API_ID = parseInt(process.env.TELEGRAM_API_ID || '0', 10);
|
|
34
|
+
const API_HASH = process.env.TELEGRAM_API_HASH || '';
|
|
35
|
+
const SESSION = process.env.TELEGRAM_SESSION || '';
|
|
36
|
+
const SESSION_FILE = path.join(OUTPUT_DIR, '.telegram-session');
|
|
37
|
+
|
|
38
|
+
if (!CHANNEL) {
|
|
39
|
+
console.error('Error: Channel is required');
|
|
40
|
+
console.error('');
|
|
41
|
+
console.error('Usage:');
|
|
42
|
+
console.error(' npx koztv-blog-tools export --channel @channelname --out ./export');
|
|
43
|
+
console.error('');
|
|
44
|
+
console.error('Environment variables:');
|
|
45
|
+
console.error(' TELEGRAM_API_ID - API ID from https://my.telegram.org');
|
|
46
|
+
console.error(' TELEGRAM_API_HASH - API Hash');
|
|
47
|
+
console.error(' TELEGRAM_SESSION - Session string (optional, for re-auth)');
|
|
48
|
+
console.error(' TELEGRAM_CHANNEL - Default channel (optional)');
|
|
49
|
+
console.error('');
|
|
50
|
+
console.error('Options:');
|
|
51
|
+
console.error(' --channel @name Target channel');
|
|
52
|
+
console.error(' --out ./path Output directory (default: ./export)');
|
|
53
|
+
console.error(' --limit N Max posts to export');
|
|
54
|
+
console.error(' --since YYYY-MM-DD Export posts since date');
|
|
55
|
+
console.error(' --until YYYY-MM-DD Export posts until date');
|
|
56
|
+
console.error(' --no-media Skip media download');
|
|
57
|
+
process.exit(1);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
if (!API_ID || !API_HASH) {
|
|
61
|
+
console.error('Error: TELEGRAM_API_ID and TELEGRAM_API_HASH are required');
|
|
62
|
+
console.error('Get them from https://my.telegram.org');
|
|
63
|
+
process.exit(1);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Read saved session if exists
|
|
67
|
+
let savedSession = SESSION;
|
|
68
|
+
if (!savedSession && fs.existsSync(SESSION_FILE)) {
|
|
69
|
+
savedSession = fs.readFileSync(SESSION_FILE, 'utf-8').trim();
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function main() {
|
|
73
|
+
console.log('Telegram Channel Exporter');
|
|
74
|
+
console.log('=========================');
|
|
75
|
+
console.log(`Channel: ${CHANNEL}`);
|
|
76
|
+
console.log(`Output: ${OUTPUT_DIR}`);
|
|
77
|
+
if (LIMIT) console.log(`Limit: ${LIMIT}`);
|
|
78
|
+
if (SINCE) console.log(`Since: ${SINCE}`);
|
|
79
|
+
if (UNTIL) console.log(`Until: ${UNTIL}`);
|
|
80
|
+
console.log(`Media: ${NO_MEDIA ? 'skip' : 'download'}`);
|
|
81
|
+
console.log('');
|
|
82
|
+
|
|
83
|
+
try {
|
|
84
|
+
const result = await exportTelegramChannel({
|
|
85
|
+
apiId: API_ID,
|
|
86
|
+
apiHash: API_HASH,
|
|
87
|
+
session: savedSession,
|
|
88
|
+
target: CHANNEL,
|
|
89
|
+
outputDir: OUTPUT_DIR,
|
|
90
|
+
limit: LIMIT,
|
|
91
|
+
since: SINCE ? new Date(SINCE) : undefined,
|
|
92
|
+
until: UNTIL ? new Date(UNTIL) : undefined,
|
|
93
|
+
downloadMedia: !NO_MEDIA,
|
|
94
|
+
onProgress: (current, total, msg) => {
|
|
95
|
+
process.stdout.write(`\r${msg} (${current}/${total})`);
|
|
96
|
+
},
|
|
97
|
+
onSession: (session) => {
|
|
98
|
+
// Save session for future use
|
|
99
|
+
fs.mkdirSync(OUTPUT_DIR, { recursive: true });
|
|
100
|
+
fs.writeFileSync(SESSION_FILE, session);
|
|
101
|
+
console.log('\nSession saved to', SESSION_FILE);
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
console.log('\n');
|
|
106
|
+
console.log('=========================');
|
|
107
|
+
console.log(`Exported ${result.posts.length} posts from "${result.channelMeta.title}"`);
|
|
108
|
+
console.log(`Output: ${OUTPUT_DIR}`);
|
|
109
|
+
|
|
110
|
+
// Output session string if new
|
|
111
|
+
if (result.session && result.session !== savedSession) {
|
|
112
|
+
console.log('\nNew session string (save to TELEGRAM_SESSION):');
|
|
113
|
+
console.log(result.session);
|
|
114
|
+
}
|
|
115
|
+
} catch (error) {
|
|
116
|
+
console.error('\nError:', error.message);
|
|
117
|
+
process.exit(1);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
main();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "koztv-blog-tools",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Shared utilities for Telegram-based blog sites",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -12,8 +12,12 @@
|
|
|
12
12
|
"require": "./dist/index.js"
|
|
13
13
|
}
|
|
14
14
|
},
|
|
15
|
+
"bin": {
|
|
16
|
+
"tg-export": "./bin/export-telegram.js"
|
|
17
|
+
},
|
|
15
18
|
"files": [
|
|
16
|
-
"dist"
|
|
19
|
+
"dist",
|
|
20
|
+
"bin"
|
|
17
21
|
],
|
|
18
22
|
"scripts": {
|
|
19
23
|
"build": "tsup src/index.ts --format cjs,esm --dts --clean",
|