mirror of
https://github.com/tiennm99/telegram-exporter.git
synced 2026-10-11 03:13:49 +00:00
feat: accept chat ids, usernames and links, and keep exports per chat
-c already passed through to tdl, which resolves a numeric argument as an MTProto id and anything else through gotd's resolver (@name, name, t.me and tg:// links). Two forms needed handling: a Bot API '-100' prefixed id, which MTProto does not use and which tdl never strips, is now converted; and a message link, which names a message rather than a chat, is rejected with a clear error instead of a resolver failure. The export JSON now defaults to export-<chat>.json when -c is given, so exporting a second chat from the same directory cannot silently download the first chat again from a stale export.json. An existing file is still reused, but the script now says which file it is using.
This commit is contained in:
1 parent
4ae19e32ec
commit
40bdec66d0
2 files changed
+70
-5
No files matched your search
@@ -62,13 +62,43 @@ and warnings go to stderr; press Ctrl-C at any point and it stops cleanly.
|
||||
| Flag | Meaning |
|
||||
|------|---------|
|
||||
| `-r REMOTE:PATH` | **Required.** rclone destination, e.g. `gdrive:telegram/media`, `s3:bucket/tg`, `dav:tg-export` |
|
||||
| `-c CHAT` | Chat to export when the JSON does not exist yet: `@username` or a numeric chat id |
|
||||
| `-f FILE` | Export JSON to download from (default `export.json`) |
|
||||
| `-c CHAT` | Chat to export when the JSON does not exist yet — id, username, or link (see below) |
|
||||
| `-f FILE` | Export JSON to download from (default `export-<chat>.json` with `-c`, else `export.json`) |
|
||||
| `-d DIR` | Staging directory (default `./staging`) |
|
||||
| `-i SECONDS` | Seconds between rclone sweeps (default `60`) |
|
||||
| `-a AGE` | rclone `--min-age`, a second guard against moving files still being written (default `2m`) |
|
||||
| `-h` | Help |
|
||||
|
||||
### Identifying the chat
|
||||
|
||||
`-c` accepts every form tdl understands, plus one it doesn't:
|
||||
|
||||
| Form | Example |
|
||||
|------|---------|
|
||||
| Numeric id, as printed by `tdl chat ls` | `-c 1697797156` |
|
||||
| Username, with or without `@` | `-c @mygroup` / `-c mygroup` |
|
||||
| Public link | `-c https://t.me/mygroup` / `-c t.me/mygroup` |
|
||||
| Deep link | `-c 'tg://resolve?domain=mygroup'` |
|
||||
| **Bot API id** (converted for you) | `-c -1001697797156` → `1697797156` |
|
||||
|
||||
tdl resolves a numeric argument as an MTProto id and anything else through
|
||||
gotd's resolver. MTProto has no `-100` prefix, so a Bot API id would otherwise
|
||||
fail to resolve; the script strips it and logs the conversion.
|
||||
|
||||
A **message** link is rejected — `-c` names a chat, not a message:
|
||||
|
||||
```
|
||||
$ ./run.sh -r gdrive:tg -c https://t.me/mygroup/123
|
||||
error: -c takes a chat, not a message link — pass the chat's username or id
|
||||
```
|
||||
|
||||
Run `tdl chat ls` to see ids and usernames side by side.
|
||||
|
||||
Each chat gets its own export file by default (`export-mygroup.json`,
|
||||
`export-1697797156.json`), so exporting a second chat from the same directory
|
||||
never reuses the first one's JSON. When the file already exists it is reused and
|
||||
the script says so — delete it to re-export.
|
||||
|
||||
Anything after `--` is passed straight to `tdl dl`:
|
||||
|
||||
```bash
|
||||
|
||||
@@ -22,7 +22,7 @@ readonly PROG=${0##*/}
|
||||
readonly TEMP_GLOB='*.tmp'
|
||||
|
||||
# Defaults
|
||||
export_file='export.json'
|
||||
export_file='' # decided after parsing: per-chat when -c is given
|
||||
chat=''
|
||||
staging='./staging'
|
||||
remote=''
|
||||
@@ -38,8 +38,12 @@ Required:
|
||||
-r REMOTE:PATH rclone destination, e.g. gdrive:telegram/media
|
||||
|
||||
Options:
|
||||
-f FILE tdl export JSON (default: $export_file)
|
||||
-c CHAT chat to export when FILE does not exist, e.g. @mygroup
|
||||
-f FILE tdl export JSON (default: export-<chat>.json with -c,
|
||||
otherwise export.json)
|
||||
-c CHAT chat to export when FILE does not exist. Accepts a numeric
|
||||
id as printed by 'tdl chat ls', a username with or without
|
||||
'@', or a t.me/tg:// link. A Bot API '-100...' id is
|
||||
converted to the plain id tdl expects.
|
||||
-d DIR staging directory (default: $staging)
|
||||
-i SECONDS seconds between rclone sweeps (default: $interval)
|
||||
-a AGE rclone --min-age, a second guard against moving files still
|
||||
@@ -77,6 +81,35 @@ tdl_extra=("$@")
|
||||
[[ $min_age =~ ^[0-9]+(\.[0-9]+)?(ms|s|m|h|d|w|M|y)?$ ]] \
|
||||
|| die "-a must be an rclone duration, e.g. 2m"
|
||||
|
||||
# tdl resolves a numeric argument as an MTProto id and anything else through
|
||||
# gotd's resolver, which handles '@name', 'name' and t.me/tg:// links. Two forms
|
||||
# still need help: Bot API ids carry a '-100' prefix that MTProto does not use,
|
||||
# and a message link is not a chat.
|
||||
if [[ -n $chat ]]; then
|
||||
read -r chat <<<"$chat" # trim stray whitespace
|
||||
case $chat in
|
||||
'') die "-c requires a chat" ;;
|
||||
*t.me/c/*|*t.me/*/[0-9]*)
|
||||
die "-c takes a chat, not a message link ($chat) — pass the chat's username or id" ;;
|
||||
-100[0-9]*)
|
||||
log "converting Bot API id $chat to MTProto id ${chat#-100}"
|
||||
chat=${chat#-100} ;;
|
||||
esac
|
||||
fi
|
||||
|
||||
# Keep each chat's export in its own file, so switching -c never silently
|
||||
# downloads the previous chat again from a stale export.json.
|
||||
if [[ -z $export_file ]]; then
|
||||
if [[ -n $chat ]]; then
|
||||
slug=${chat#@}
|
||||
slug=${slug##*/}
|
||||
slug=$(printf '%s' "$slug" | tr -c 'A-Za-z0-9._-' '_')
|
||||
export_file="export-$slug.json"
|
||||
else
|
||||
export_file='export.json'
|
||||
fi
|
||||
fi
|
||||
|
||||
for tool in tdl rclone; do
|
||||
command -v "$tool" >/dev/null || die "$tool is not installed or not on PATH"
|
||||
done
|
||||
@@ -103,6 +136,8 @@ if [[ ! -f $export_file ]]; then
|
||||
[[ -n $chat ]] || die "$export_file not found; pass -c CHAT to export it first"
|
||||
log "exporting $chat metadata to $export_file"
|
||||
tdl chat export -c "$chat" --all --with-content -o "$export_file"
|
||||
else
|
||||
log "using existing $export_file (delete it to re-export)"
|
||||
fi
|
||||
|
||||
mkdir -p "$staging"
|
||||
|
||||
Reference in new issue
Block a user