Skip to content
This repository was archived by the owner on Apr 27, 2020. It is now read-only.
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Telegram_Backup
* Copyright (C) 2016 Fabian Schlenz
* Copyright (C) 2016 Fabian Schlenz, 2019 Bohdan Horbeshko
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -25,6 +25,7 @@ import java.io.File
import java.io.IOException
import java.util.Scanner
import java.util.Vector
import java.util.LinkedList
import java.util.HashMap
import org.slf4j.LoggerFactory
import org.slf4j.Logger
Expand Down Expand Up @@ -133,10 +134,19 @@ class CommandLineController {
val d = DownloadManager(client, CommandLineDownloadProgress())
logger.debug("Calling DownloadManager.downloadMessages with limit {}", CommandLineOptions.val_limit_messages)
d.downloadMessages(CommandLineOptions.val_limit_messages)
logger.debug("CommandLineOptions.cmd_only_my_media: {}", CommandLineOptions.cmd_only_my_media)
logger.debug("CommandLineOptions.cmd_no_media: {}", CommandLineOptions.cmd_no_media)
logger.debug("CommandLineOptions.cmd_no_stickers: {}", CommandLineOptions.cmd_no_stickers)
if (!CommandLineOptions.cmd_no_media) {
logger.debug("Calling DownloadManager.downloadMedia")
d.downloadMedia()
var filters = LinkedList<MediaFilter>()
if (CommandLineOptions.cmd_only_my_media) {
filters.add(MediaFilter.ONLY_MY)
}
if (CommandLineOptions.cmd_no_stickers) {
filters.add(MediaFilter.NO_STICKERS)
}
d.downloadMedia(filters)
} else {
println("Skipping media download because --no-media is set.")
}
Expand Down Expand Up @@ -276,7 +286,9 @@ class CommandLineController {
println(" --trace-telegram Shows lots of debug messages from the library used to access Telegram.")
println(" -A, --list-accounts List all existing accounts ")
println(" --limit-messages <x> Downloads at most the most recent <x> messages.")
println(" --only-my-media Download only media files sent by this account.")
println(" --no-media Do not download media files.")
println(" --no-stickers Do not download stickers.")
println(" -t, --target <x> Target directory for the files.")
println(" -e, --export <format> Export the database. Valid formats are:")
println(" html - Creates HTML files.")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Telegram_Backup
* Copyright (C) 2016 Fabian Schlenz
* Copyright (C) 2016 Fabian Schlenz, 2019 Bohdan Horbeshko
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand All @@ -26,7 +26,9 @@ internal object CommandLineOptions {
var cmd_version = false
var cmd_license = false
var cmd_daemon = false
var cmd_only_my_media = false
var cmd_no_media = false
var cmd_no_stickers = false
var cmd_anonymize = false
var cmd_stats = false
var cmd_channels = false
Expand Down Expand Up @@ -86,7 +88,9 @@ internal object CommandLineOptions {
"--no-pagination" -> cmd_no_pagination = true
"--license" -> cmd_license = true
"-d", "--daemon" -> cmd_daemon = true
"--only-my-media" -> cmd_only_my_media = true
"--no-media" -> cmd_no_media = true
"--no-stickers" -> cmd_no_stickers = true
"--test" -> {
last_cmd = "--test"
continue@loop
Expand Down
13 changes: 10 additions & 3 deletions src/main/kotlin/de/fabianonline/telegram_backup/Database.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Telegram_Backup
* Copyright (C) 2016 Fabian Schlenz
* Copyright (C) 2016 Fabian Schlenz, 2019 Bohdan Horbeshko
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -98,10 +98,17 @@ class Database private constructor(var client: TelegramClient) {

}

fun getMessagesWithMedia(): LinkedList<TLMessage?> {
fun getMessagesWithMedia(filters: List<MediaFilter> = LinkedList<MediaFilter>()): LinkedList<TLMessage?> {
try {
val list = LinkedList<TLMessage?>()
val rs = stmt!!.executeQuery("SELECT data FROM messages WHERE has_media=1")
var query = "SELECT data FROM messages WHERE has_media=1"
for (filter in filters) {
when (filter) {
MediaFilter.ONLY_MY -> query += " AND sender_id=" + user_manager.user!!.getId()
MediaFilter.NO_STICKERS -> query += " AND media_type<>\"sticker\""
}
}
val rs = stmt!!.executeQuery(query)
while (rs.next()) {
list.add(bytesToTLMessage(rs.getBytes(1)))
}
Expand Down
22 changes: 16 additions & 6 deletions src/main/kotlin/de/fabianonline/telegram_backup/DownloadManager.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* Telegram_Backup
* Copyright (C) 2016 Fabian Schlenz
* Copyright (C) 2016 Fabian Schlenz, 2019 Bohdan Horbeshko
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
Expand Down Expand Up @@ -61,6 +61,11 @@ enum class MessageSource(val descr: String) {
SUPERGROUP("supergroup")
}

enum class MediaFilter {
ONLY_MY,
NO_STICKERS
}

class DownloadManager(internal var client: TelegramClient?, p: DownloadProgressInterface) {
internal var user: UserManager? = null
internal var db: Database? = null
Expand Down Expand Up @@ -308,13 +313,13 @@ class DownloadManager(internal var client: TelegramClient?, p: DownloadProgressI
}

@Throws(RpcErrorException::class, IOException::class)
fun downloadMedia() {
fun downloadMedia(filters: List<MediaFilter> = LinkedList<MediaFilter>()) {
download_client = client!!.getDownloaderClient()
var completed: Boolean
do {
completed = true
try {
_downloadMedia()
_downloadMedia(filters)
} catch (e: RpcErrorException) {
if (e.getCode() == 420) { // FLOOD_WAIT
completed = false
Expand All @@ -336,7 +341,7 @@ class DownloadManager(internal var client: TelegramClient?, p: DownloadProgressI
}

@Throws(RpcErrorException::class, IOException::class)
private fun _downloadMedia() {
private fun _downloadMedia(filters: List<MediaFilter>) {
logger.info("This is _downloadMedia")
logger.info("Checking if there are messages in the DB with a too old API layer")
val ids = db!!.getIdsFromQuery("SELECT id FROM messages WHERE has_media=1 AND api_layer<" + Kotlogram.API_LAYER)
Expand All @@ -346,18 +351,21 @@ class DownloadManager(internal var client: TelegramClient?, p: DownloadProgressI
downloadMessages(ids, null, source_type=MessageSource.NORMAL)
}

val messages = this.db!!.getMessagesWithMedia()
val messages = this.db!!.getMessagesWithMedia(filters)
logger.debug("Database returned {} messages with media", messages.size)
prog!!.onMediaDownloadStart(messages.size)
var mediaStats: MutableMap<String, Int> = mutableMapOf()
for (msg in messages) {
if (msg == null) continue
val m = FileManagerFactory.getFileManager(msg, user!!, client!!)
val simpleName = m!!.javaClass.getSimpleName()
logger.trace("message {}, {}, {}, {}, {}",
msg.getId(),
msg.getMedia().javaClass.getSimpleName().replace("TLMessageMedia", "…"),
m!!.javaClass.getSimpleName(),
simpleName,
if (m.isEmpty) "empty" else "non-empty",
if (m.downloaded) "downloaded" else "not downloaded")
mediaStats.merge(simpleName, 1, Int::plus)
if (m.isEmpty) {
prog!!.onMediaDownloadedEmpty()
} else if (m.downloaded) {
Expand All @@ -377,6 +385,8 @@ class DownloadManager(internal var client: TelegramClient?, p: DownloadProgressI

}
}
/*for ((key, value) in mediaStats)
println("$key => $value")*/
prog!!.onMediaDownloadFinished()
}

Expand Down