1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-23 01:06:17 +00:00

Add --only-my-media and --no-stickers keys.

This commit is contained in:
bodqhrohro 2019-04-06 22:04:00 +03:00
parent e3708c00ad
commit 82aae83839
4 changed files with 45 additions and 12 deletions

View File

@ -1,5 +1,5 @@
/* Telegram_Backup /* 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 * 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 * 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.io.IOException
import java.util.Scanner import java.util.Scanner
import java.util.Vector import java.util.Vector
import java.util.LinkedList
import java.util.HashMap import java.util.HashMap
import org.slf4j.LoggerFactory import org.slf4j.LoggerFactory
import org.slf4j.Logger import org.slf4j.Logger
@ -133,10 +134,19 @@ class CommandLineController {
val d = DownloadManager(client, CommandLineDownloadProgress()) val d = DownloadManager(client, CommandLineDownloadProgress())
logger.debug("Calling DownloadManager.downloadMessages with limit {}", CommandLineOptions.val_limit_messages) logger.debug("Calling DownloadManager.downloadMessages with limit {}", CommandLineOptions.val_limit_messages)
d.downloadMessages(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_media: {}", CommandLineOptions.cmd_no_media)
logger.debug("CommandLineOptions.cmd_no_stickers: {}", CommandLineOptions.cmd_no_stickers)
if (!CommandLineOptions.cmd_no_media) { if (!CommandLineOptions.cmd_no_media) {
logger.debug("Calling DownloadManager.downloadMedia") 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 { } else {
println("Skipping media download because --no-media is set.") 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(" --trace-telegram Shows lots of debug messages from the library used to access Telegram.")
println(" -A, --list-accounts List all existing accounts ") println(" -A, --list-accounts List all existing accounts ")
println(" --limit-messages <x> Downloads at most the most recent <x> messages.") 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-media Do not download media files.")
println(" --no-stickers Do not download stickers.")
println(" -t, --target <x> Target directory for the files.") println(" -t, --target <x> Target directory for the files.")
println(" -e, --export <format> Export the database. Valid formats are:") println(" -e, --export <format> Export the database. Valid formats are:")
println(" html - Creates HTML files.") println(" html - Creates HTML files.")

View File

@ -1,5 +1,5 @@
/* Telegram_Backup /* 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 * 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 * 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_version = false
var cmd_license = false var cmd_license = false
var cmd_daemon = false var cmd_daemon = false
var cmd_only_my_media = false
var cmd_no_media = false var cmd_no_media = false
var cmd_no_stickers = false
var cmd_anonymize = false var cmd_anonymize = false
var cmd_stats = false var cmd_stats = false
var cmd_channels = false var cmd_channels = false
@ -86,7 +88,9 @@ internal object CommandLineOptions {
"--no-pagination" -> cmd_no_pagination = true "--no-pagination" -> cmd_no_pagination = true
"--license" -> cmd_license = true "--license" -> cmd_license = true
"-d", "--daemon" -> cmd_daemon = true "-d", "--daemon" -> cmd_daemon = true
"--only-my-media" -> cmd_only_my_media = true
"--no-media" -> cmd_no_media = true "--no-media" -> cmd_no_media = true
"--no-stickers" -> cmd_no_stickers = true
"--test" -> { "--test" -> {
last_cmd = "--test" last_cmd = "--test"
continue@loop continue@loop

View File

@ -1,5 +1,5 @@
/* Telegram_Backup /* 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 * 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 * 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<TLMessage?> { fun getMessagesWithMedia(filters: List<MediaFilter> = LinkedList<MediaFilter>()): LinkedList<TLMessage?> {
try { try {
val list = LinkedList<TLMessage?>() 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()) { while (rs.next()) {
list.add(bytesToTLMessage(rs.getBytes(1))) list.add(bytesToTLMessage(rs.getBytes(1)))
} }

View File

@ -1,5 +1,5 @@
/* Telegram_Backup /* 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 * 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 * 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") SUPERGROUP("supergroup")
} }
enum class MediaFilter {
ONLY_MY,
NO_STICKERS
}
class DownloadManager(internal var client: TelegramClient?, p: DownloadProgressInterface) { class DownloadManager(internal var client: TelegramClient?, p: DownloadProgressInterface) {
internal var user: UserManager? = null internal var user: UserManager? = null
internal var db: Database? = null internal var db: Database? = null
@ -308,13 +313,13 @@ class DownloadManager(internal var client: TelegramClient?, p: DownloadProgressI
} }
@Throws(RpcErrorException::class, IOException::class) @Throws(RpcErrorException::class, IOException::class)
fun downloadMedia() { fun downloadMedia(filters: List<MediaFilter> = LinkedList<MediaFilter>()) {
download_client = client!!.getDownloaderClient() download_client = client!!.getDownloaderClient()
var completed: Boolean var completed: Boolean
do { do {
completed = true completed = true
try { try {
_downloadMedia() _downloadMedia(filters)
} catch (e: RpcErrorException) { } catch (e: RpcErrorException) {
if (e.getCode() == 420) { // FLOOD_WAIT if (e.getCode() == 420) { // FLOOD_WAIT
completed = false completed = false
@ -336,7 +341,7 @@ class DownloadManager(internal var client: TelegramClient?, p: DownloadProgressI
} }
@Throws(RpcErrorException::class, IOException::class) @Throws(RpcErrorException::class, IOException::class)
private fun _downloadMedia() { private fun _downloadMedia(filters: List<MediaFilter>) {
logger.info("This is _downloadMedia") logger.info("This is _downloadMedia")
logger.info("Checking if there are messages in the DB with a too old API layer") 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) 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) 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) logger.debug("Database returned {} messages with media", messages.size)
prog!!.onMediaDownloadStart(messages.size) prog!!.onMediaDownloadStart(messages.size)
var mediaStats: MutableMap<String, Int> = mutableMapOf()
for (msg in messages) { for (msg in messages) {
if (msg == null) continue if (msg == null) continue
val m = FileManagerFactory.getFileManager(msg, user!!, client!!) val m = FileManagerFactory.getFileManager(msg, user!!, client!!)
val simpleName = m!!.javaClass.getSimpleName()
logger.trace("message {}, {}, {}, {}, {}", logger.trace("message {}, {}, {}, {}, {}",
msg.getId(), msg.getId(),
msg.getMedia().javaClass.getSimpleName().replace("TLMessageMedia", ""), msg.getMedia().javaClass.getSimpleName().replace("TLMessageMedia", ""),
m!!.javaClass.getSimpleName(), simpleName,
if (m.isEmpty) "empty" else "non-empty", if (m.isEmpty) "empty" else "non-empty",
if (m.downloaded) "downloaded" else "not downloaded") if (m.downloaded) "downloaded" else "not downloaded")
mediaStats.merge(simpleName, 1, Int::plus)
if (m.isEmpty) { if (m.isEmpty) {
prog!!.onMediaDownloadedEmpty() prog!!.onMediaDownloadedEmpty()
} else if (m.downloaded) { } 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() prog!!.onMediaDownloadFinished()
} }