diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineController.kt b/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineController.kt index 70969bc..2808548 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineController.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineController.kt @@ -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 @@ -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 @@ -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() + 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.") } @@ -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 Downloads at most the most recent 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 Target directory for the files.") println(" -e, --export Export the database. Valid formats are:") println(" html - Creates HTML files.") diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineOptions.kt b/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineOptions.kt index 32ba425..9059dfd 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineOptions.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/CommandLineOptions.kt @@ -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 @@ -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 @@ -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 diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt b/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt index 20cbda6..cd758b0 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/Database.kt @@ -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 @@ -98,10 +98,17 @@ class Database private constructor(var client: TelegramClient) { } - fun getMessagesWithMedia(): LinkedList { + fun getMessagesWithMedia(filters: List = LinkedList()): LinkedList { try { val list = LinkedList() - 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))) } diff --git a/src/main/kotlin/de/fabianonline/telegram_backup/DownloadManager.kt b/src/main/kotlin/de/fabianonline/telegram_backup/DownloadManager.kt index 9766ae4..7e50ea5 100644 --- a/src/main/kotlin/de/fabianonline/telegram_backup/DownloadManager.kt +++ b/src/main/kotlin/de/fabianonline/telegram_backup/DownloadManager.kt @@ -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 @@ -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 @@ -308,13 +313,13 @@ class DownloadManager(internal var client: TelegramClient?, p: DownloadProgressI } @Throws(RpcErrorException::class, IOException::class) - fun downloadMedia() { + fun downloadMedia(filters: List = LinkedList()) { 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 @@ -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) { 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) @@ -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 = 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) { @@ -377,6 +385,8 @@ class DownloadManager(internal var client: TelegramClient?, p: DownloadProgressI } } + /*for ((key, value) in mediaStats) + println("$key => $value")*/ prog!!.onMediaDownloadFinished() }