mirror of
https://github.com/fabianonline/telegram_backup.git
synced 2024-11-22 08:46:15 +00:00
The code is now compiling, but still largely untested. So it's still kinda WIP.
This commit is contained in:
parent
77efde1136
commit
ec4097e777
@ -41,7 +41,6 @@ class CommandLineController(val options: CommandLineOptions) {
|
||||
val handler: TelegramUpdateHandler
|
||||
var client: TelegramClient
|
||||
val user_manager: UserManager
|
||||
val inisettings: IniSettings
|
||||
val settings: Settings
|
||||
val database: Database
|
||||
logger.info("CommandLineController started. App version {}", Config.APP_APPVER)
|
||||
@ -63,7 +62,7 @@ class CommandLineController(val options: CommandLineOptions) {
|
||||
|
||||
// Setup file_base
|
||||
logger.debug("Target dir from Config: {}", Config.TARGET_DIR.anonymize())
|
||||
target_dir = options.values.get("target")?.last() ?: Config.TARGET_DIR
|
||||
target_dir = options.values.get("target") ?: Config.TARGET_DIR
|
||||
logger.debug("Target dir after options: {}", target_dir)
|
||||
println("Base directory for files: ${target_dir.anonymize()}")
|
||||
|
||||
@ -73,16 +72,16 @@ class CommandLineController(val options: CommandLineOptions) {
|
||||
}
|
||||
|
||||
if (options.booleans.contains("login")) {
|
||||
cmd_login(app, target_dir, options.values.get("account")?.last())
|
||||
cmd_login(app, target_dir, options.values.get("account"))
|
||||
}
|
||||
|
||||
logger.trace("Checking accounts")
|
||||
phone_number = try { selectAccount(target_dir, options.values.get("account")?.last())
|
||||
phone_number = try { selectAccount(target_dir, options.values.get("account"))
|
||||
} catch(e: AccountNotFoundException) {
|
||||
show_error("The specified account could not be found.")
|
||||
} catch(e: NoAccountsException) {
|
||||
println("No accounts found. Starting login process...")
|
||||
cmd_login(app, target_dir, options.values.get("account")?.last())
|
||||
cmd_login(app, target_dir, options.values.get("account"))
|
||||
}
|
||||
|
||||
// TODO: Create a new TelegramApp if the user set his/her own TelegramApp credentials
|
||||
@ -124,7 +123,7 @@ class CommandLineController(val options: CommandLineOptions) {
|
||||
System.exit(0)
|
||||
}
|
||||
|
||||
val export = options.values.get("export")?.last()
|
||||
val export = options.values["export"]
|
||||
logger.debug("options.val_export: {}", export)
|
||||
if (export != null) {
|
||||
if (export.toLowerCase() == "html") {
|
||||
@ -138,7 +137,7 @@ class CommandLineController(val options: CommandLineOptions) {
|
||||
println("You are logged in as ${user_manager.toString().anonymize()}")
|
||||
|
||||
logger.info("Initializing Download Manager")
|
||||
val d = DownloadManager(client, CommandLineDownloadProgress(), database, user_manager, inisettings)
|
||||
val d = DownloadManager(client, CommandLineDownloadProgress(), database, user_manager, settings, file_base)
|
||||
|
||||
if (options.booleans.contains("list_channels")) {
|
||||
val chats = d.getChats()
|
||||
@ -147,7 +146,7 @@ class CommandLineController(val options: CommandLineOptions) {
|
||||
var download: Boolean
|
||||
|
||||
println("Channels:")
|
||||
download = inisettings.download_channels
|
||||
download = settings.download_channels
|
||||
if (!download) println("Download of channels is disabled - see download_channels in config.ini")
|
||||
print_header(download)
|
||||
for (c in chats.channels) {
|
||||
@ -155,7 +154,7 @@ class CommandLineController(val options: CommandLineOptions) {
|
||||
}
|
||||
println()
|
||||
println("Supergroups:")
|
||||
download = inisettings.download_supergroups
|
||||
download = settings.download_supergroups
|
||||
if (!download) println("Download of supergroups is disabled - see download_supergroups in config.ini")
|
||||
print_header(download)
|
||||
for (c in chats.supergroups) {
|
||||
@ -166,17 +165,17 @@ class CommandLineController(val options: CommandLineOptions) {
|
||||
|
||||
logger.debug("Calling DownloadManager.downloadMessages with limit {}", options.values.get("limit_messages")?.last())
|
||||
d.downloadMessages(options.values.get("limit_messages")?.last()?.toInt())
|
||||
logger.debug("IniSettings#download_media: {}", inisettings.download_media)
|
||||
if (inisettings.download_media) {
|
||||
logger.debug("IniSettings#download_media: {}", settings.download_media)
|
||||
if (settings.download_media) {
|
||||
logger.debug("Calling DownloadManager.downloadMedia")
|
||||
d.downloadMedia()
|
||||
} else {
|
||||
println("Skipping media download because download_media is set to false.")
|
||||
}
|
||||
|
||||
if (options.boolean.contains("daemon")) {
|
||||
if (options.booleans.contains("daemon")) {
|
||||
logger.info("Initializing TelegramUpdateHandler")
|
||||
handler = TelegramUpdateHandler(user_manager, database)
|
||||
handler = TelegramUpdateHandler(user_manager, database, file_base, settings)
|
||||
client.close()
|
||||
logger.info("Creating new client")
|
||||
client = Kotlogram.getDefaultClient(app, storage, Kotlogram.PROD_DC4, handler)
|
||||
@ -203,7 +202,7 @@ class CommandLineController(val options: CommandLineOptions) {
|
||||
}
|
||||
|
||||
private fun selectAccount(file_base: String, requested_account: String?): String {
|
||||
var found_account: String? = null
|
||||
var found_account: String?
|
||||
val accounts = Utils.getAccounts(file_base)
|
||||
if (requested_account != null) {
|
||||
logger.debug("Account requested: {}", requested_account.anonymize())
|
||||
@ -218,7 +217,6 @@ class CommandLineController(val options: CommandLineOptions) {
|
||||
show_error(("You have more than one account but didn't specify which one to use.\n" +
|
||||
"Use '--account <x>' to use account <x>.\n" +
|
||||
"Use '--list-accounts' to see all available accounts."))
|
||||
System.exit(1)
|
||||
}
|
||||
|
||||
if (found_account == null) {
|
||||
|
@ -17,27 +17,24 @@ package de.fabianonline.telegram_backup
|
||||
|
||||
class CommandLineOptions(args: Array<String>) {
|
||||
val booleans = mutableListOf<String>()
|
||||
val values = mutableMapOf<String, MutableList<String>>()
|
||||
val values = mutableMapOf<String, String>()
|
||||
var last_key: String? = null
|
||||
|
||||
init {
|
||||
for(arg in args) {
|
||||
if (arg.starts_with("--")) {
|
||||
if (arg.startsWith("--")) {
|
||||
if (last_key!=null) {
|
||||
booleans.add(last_key)
|
||||
booleans.add(last_key!!)
|
||||
}
|
||||
last_key = arg.substr(2)
|
||||
last_key = arg.substring(2)
|
||||
} else {
|
||||
if (last_key==null) throw RuntimeException("Unexpected parameter without switch: $arg")
|
||||
var list = values.get(last_key)
|
||||
if (list==null) {
|
||||
list = mutableListOf<String>()
|
||||
values.add(last_key, list)
|
||||
if (last_key==null) {
|
||||
throw RuntimeException("Unexpected unnamed parameter ${arg}")
|
||||
}
|
||||
list.add(arg)
|
||||
values.put(last_key!!, arg)
|
||||
last_key = null
|
||||
}
|
||||
}
|
||||
if (last_key!=null) booleans.add(last_key)
|
||||
if (last_key!=null) booleans.add(last_key!!)
|
||||
}
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ class CommandLineRunner(args: Array<String>) {
|
||||
}
|
||||
|
||||
fun setupLogging() {
|
||||
if (options.booleans.hasKey('anonymize')) {
|
||||
if (options.booleans.contains("anonymize")) {
|
||||
Utils.anonymize = true
|
||||
}
|
||||
|
||||
@ -76,13 +76,13 @@ class CommandLineRunner(args: Array<String>) {
|
||||
rootLogger.addAppender(appender)
|
||||
rootLogger.setLevel(Level.OFF)
|
||||
|
||||
if (options.cmd_trace) {
|
||||
if (options.booleans.contains("trace")) {
|
||||
(LoggerFactory.getLogger("de.fabianonline.telegram_backup") as Logger).setLevel(Level.TRACE)
|
||||
} else if (options.cmd_debug) {
|
||||
} else if (options.booleans.contains("debug")) {
|
||||
(LoggerFactory.getLogger("de.fabianonline.telegram_backup") as Logger).setLevel(Level.DEBUG)
|
||||
}
|
||||
|
||||
if (options.cmd_trace_telegram) {
|
||||
if (options.booleans.contains("trace_telegram")) {
|
||||
(LoggerFactory.getLogger("com.github.badoualy") as Logger).setLevel(Level.TRACE)
|
||||
}
|
||||
}
|
||||
|
@ -245,14 +245,14 @@ class Database constructor(val file_base: String, val user_manager: UserManager)
|
||||
val map = mutableMapOf<String, String>()
|
||||
val rs = stmt.executeQuery(query)
|
||||
while(rs.next()) {
|
||||
map.add(rs.getString(1), rs.getString(2))
|
||||
map.put(rs.getString(1), rs.getString(2))
|
||||
}
|
||||
rs.close()
|
||||
return map
|
||||
}
|
||||
|
||||
@Synchronized
|
||||
fun saveMessages(all: TLVector<TLAbsMessage>, api_layer: Int, source_type: MessageSource = MessageSource.NORMAL) {
|
||||
fun saveMessages(all: TLVector<TLAbsMessage>, api_layer: Int, source_type: MessageSource = MessageSource.NORMAL, settings: Settings?) {
|
||||
//"(id, dialog_id, from_id, from_type, text, time, has_media, data, sticker, type) " +
|
||||
//"VALUES " +
|
||||
//"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||
@ -314,7 +314,7 @@ class Database constructor(val file_base: String, val user_manager: UserManager)
|
||||
}
|
||||
ps.setString(7, text)
|
||||
ps.setString(8, "" + msg.getDate())
|
||||
val f = FileManagerFactory.getFileManager(msg, user_manager)
|
||||
val f = FileManagerFactory.getFileManager(msg, user_manager, file_base, settings)
|
||||
if (f == null) {
|
||||
ps.setNull(9, Types.BOOLEAN)
|
||||
ps.setNull(10, Types.VARCHAR)
|
||||
@ -499,7 +499,7 @@ class Database constructor(val file_base: String, val user_manager: UserManager)
|
||||
ps_insert_or_replace.close()
|
||||
}
|
||||
|
||||
fun fetchSettings() = queryStringMap("SELECT key, value FROM settings WHERE key='${key}'")
|
||||
fun fetchSettings() = queryStringMap("SELECT key, value FROM settings")
|
||||
|
||||
fun saveSetting(key: String, value: String?) {
|
||||
val ps = conn.prepareStatement("INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)")
|
||||
|
@ -313,7 +313,7 @@ internal class DB_Update_6(conn: Connection, db: Database) : DatabaseUpdate(conn
|
||||
} else {
|
||||
ps.setInt(1, msg.getFwdFrom().getFromId())
|
||||
}
|
||||
val f = FileManagerFactory.getFileManager(msg, db.user_manager)
|
||||
val f = FileManagerFactory.getFileManager(msg, db.user_manager, db.file_base, settings = null)
|
||||
if (f == null) {
|
||||
ps.setNull(2, Types.VARCHAR)
|
||||
ps.setNull(3, Types.VARCHAR)
|
||||
@ -431,7 +431,7 @@ internal class DB_Update_9(conn: Connection, db: Database) : DatabaseUpdate(conn
|
||||
}
|
||||
}
|
||||
rs.close()
|
||||
db.saveMessages(messages, api_layer=53, source_type=MessageSource.SUPERGROUP)
|
||||
db.saveMessages(messages, api_layer=53, source_type=MessageSource.SUPERGROUP, settings=null)
|
||||
execute("DELETE FROM messages WHERE id IN (" + messages_to_delete.joinToString() + ")")
|
||||
print(".")
|
||||
|
||||
|
@ -1,12 +0,0 @@
|
||||
package de.fabianonline.telegram_backup
|
||||
|
||||
class DbSettings(val database: Database) {
|
||||
private fun fetchValue(name: String): String? = database.fetchSetting(name)
|
||||
private fun saveValue(name: String, value: String?) = database.saveSetting(name, value)
|
||||
|
||||
var pts: String?
|
||||
get() = fetchValue("pts")
|
||||
set(x: String?) = saveValue("pts", x)
|
||||
}
|
||||
|
||||
|
@ -60,7 +60,7 @@ enum class MessageSource(val descr: String) {
|
||||
SUPERGROUP("supergroup")
|
||||
}
|
||||
|
||||
class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInterface, val db: Database, val user_manager: UserManager, val inisettings: IniSettings) {
|
||||
class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInterface, val db: Database, val user_manager: UserManager, val settings: Settings, val file_base: String) {
|
||||
internal var has_seen_flood_wait_message = false
|
||||
|
||||
@Throws(RpcErrorException::class, IOException::class)
|
||||
@ -109,7 +109,7 @@ class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInte
|
||||
}
|
||||
}
|
||||
System.out.println("Top message ID is " + max_message_id)
|
||||
var max_database_id = db!!.getTopMessageID()
|
||||
var max_database_id = db.getTopMessageID()
|
||||
System.out.println("Top message ID in database is " + max_database_id)
|
||||
if (limit != null) {
|
||||
System.out.println("Limit is set to " + limit)
|
||||
@ -137,8 +137,8 @@ class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInte
|
||||
|
||||
logger.info("Searching for missing messages in the db")
|
||||
System.out.println("Checking message database for completeness...")
|
||||
val db_count = db!!.getMessageCount()
|
||||
val db_max = db!!.getTopMessageID()
|
||||
val db_count = db.getMessageCount()
|
||||
val db_max = db.getTopMessageID()
|
||||
logger.debug("db_count: {}", db_count)
|
||||
logger.debug("db_max: {}", db_max)
|
||||
|
||||
@ -163,12 +163,12 @@ class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInte
|
||||
}
|
||||
*/
|
||||
|
||||
if (inisettings.download_channels) {
|
||||
if (settings.download_channels) {
|
||||
println("Checking channels...")
|
||||
for (channel in chats.channels) { if (channel.download) downloadMessagesFromChannel(channel) }
|
||||
}
|
||||
|
||||
if (inisettings.download_supergroups) {
|
||||
if (settings.download_supergroups) {
|
||||
println("Checking supergroups...")
|
||||
for (supergroup in chats.supergroups) { if (supergroup.download) downloadMessagesFromChannel(supergroup) }
|
||||
}
|
||||
@ -176,7 +176,7 @@ class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInte
|
||||
|
||||
private fun downloadMessagesFromChannel(channel: Channel) {
|
||||
val obj = channel.obj
|
||||
val max_known_id = db!!.getTopMessageIDForChannel(channel.id)
|
||||
val max_known_id = db.getTopMessageIDForChannel(channel.id)
|
||||
if (obj.getTopMessage() > max_known_id) {
|
||||
val ids = makeIdList(max_known_id + 1, obj.getTopMessage())
|
||||
var channel_name = channel.title
|
||||
@ -196,7 +196,7 @@ class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInte
|
||||
} else {
|
||||
"${source_type.descr} $source_name"
|
||||
}
|
||||
prog!!.onMessageDownloadStart(ids.size, source_string)
|
||||
prog.onMessageDownloadStart(ids.size, source_string)
|
||||
|
||||
logger.debug("Entering download loop")
|
||||
while (ids.size > 0) {
|
||||
@ -221,9 +221,9 @@ class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInte
|
||||
tries++
|
||||
try {
|
||||
if (channel == null) {
|
||||
response = client!!.messagesGetMessages(vector)
|
||||
response = client.messagesGetMessages(vector)
|
||||
} else {
|
||||
response = client!!.channelsGetMessages(channel, vector)
|
||||
response = client.channelsGetMessages(channel, vector)
|
||||
}
|
||||
break
|
||||
} catch (e: RpcErrorException) {
|
||||
@ -241,10 +241,10 @@ class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInte
|
||||
CommandLineController.show_error("Requested ${vector.size} messages, but got ${response.getMessages().size}. That is unexpected. Quitting.")
|
||||
}
|
||||
|
||||
prog!!.onMessageDownloaded(response.getMessages().size)
|
||||
db!!.saveMessages(response.getMessages(), Kotlogram.API_LAYER, source_type=source_type)
|
||||
db!!.saveChats(response.getChats())
|
||||
db!!.saveUsers(response.getUsers())
|
||||
prog.onMessageDownloaded(response.getMessages().size)
|
||||
db.saveMessages(response.getMessages(), Kotlogram.API_LAYER, source_type=source_type, settings=settings)
|
||||
db.saveChats(response.getChats())
|
||||
db.saveUsers(response.getUsers())
|
||||
logger.trace("Sleeping")
|
||||
try {
|
||||
TimeUnit.MILLISECONDS.sleep(Config.DELAY_AFTER_GET_MESSAGES)
|
||||
@ -254,12 +254,12 @@ class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInte
|
||||
}
|
||||
logger.debug("Finished.")
|
||||
|
||||
prog!!.onMessageDownloadFinished()
|
||||
prog.onMessageDownloadFinished()
|
||||
}
|
||||
|
||||
@Throws(RpcErrorException::class, IOException::class)
|
||||
fun downloadMedia() {
|
||||
download_client = client!!.getDownloaderClient()
|
||||
download_client = client.getDownloaderClient()
|
||||
var completed: Boolean
|
||||
do {
|
||||
completed = true
|
||||
@ -289,19 +289,19 @@ class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInte
|
||||
private fun _downloadMedia() {
|
||||
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)
|
||||
val ids = db.getIdsFromQuery("SELECT id FROM messages WHERE has_media=1 AND api_layer<" + Kotlogram.API_LAYER)
|
||||
if (ids.size > 0) {
|
||||
System.out.println("You have ${ids.size} messages in your db that need an update. Doing that now.")
|
||||
logger.debug("Found {} messages", ids.size)
|
||||
downloadMessages(ids, null, source_type=MessageSource.NORMAL)
|
||||
}
|
||||
|
||||
val messages = this.db!!.getMessagesWithMedia()
|
||||
val messages = db.getMessagesWithMedia()
|
||||
logger.debug("Database returned {} messages with media", messages.size)
|
||||
prog!!.onMediaDownloadStart(messages.size)
|
||||
prog.onMediaDownloadStart(messages.size)
|
||||
for (msg in messages) {
|
||||
if (msg == null) continue
|
||||
val m = FileManagerFactory.getFileManager(msg, user_manager)
|
||||
val m = FileManagerFactory.getFileManager(msg, user_manager, file_base, settings=settings)
|
||||
logger.trace("message {}, {}, {}, {}, {}",
|
||||
msg.getId(),
|
||||
msg.getMedia().javaClass.getSimpleName().replace("TLMessageMedia", "…"),
|
||||
@ -309,25 +309,25 @@ class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInte
|
||||
if (m.isEmpty) "empty" else "non-empty",
|
||||
if (m.downloaded) "downloaded" else "not downloaded")
|
||||
if (m.isEmpty) {
|
||||
prog!!.onMediaDownloadedEmpty()
|
||||
prog.onMediaDownloadedEmpty()
|
||||
} else if (m.downloaded) {
|
||||
prog!!.onMediaAlreadyPresent(m)
|
||||
prog.onMediaAlreadyPresent(m)
|
||||
} else {
|
||||
try {
|
||||
val result = m.download()
|
||||
if (result) {
|
||||
prog!!.onMediaDownloaded(m)
|
||||
prog.onMediaDownloaded(m)
|
||||
} else {
|
||||
prog!!.onMediaSkipped()
|
||||
prog.onMediaSkipped()
|
||||
}
|
||||
} catch (e: TimeoutException) {
|
||||
// do nothing - skip this file
|
||||
prog!!.onMediaSkipped()
|
||||
prog.onMediaSkipped()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
prog!!.onMediaDownloadFinished()
|
||||
prog.onMediaDownloadFinished()
|
||||
}
|
||||
|
||||
private fun makeIdList(start: Int, end: Int): MutableList<Int> {
|
||||
@ -339,7 +339,7 @@ class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInte
|
||||
fun getChats(): ChatList {
|
||||
val cl = ChatList()
|
||||
logger.trace("Calling messagesGetDialogs")
|
||||
val dialogs = client!!.messagesGetDialogs(0, 0, TLInputPeerEmpty(), 100)
|
||||
val dialogs = client.messagesGetDialogs(0, 0, TLInputPeerEmpty(), 100)
|
||||
logger.trace("Got {} dialogs back", dialogs.getDialogs().size)
|
||||
|
||||
// Add dialogs
|
||||
@ -352,10 +352,10 @@ class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInte
|
||||
if (tl_peer_channel == null) continue
|
||||
|
||||
var download = true
|
||||
if (inisettings.whitelist_channels != null) {
|
||||
download = inisettings.whitelist_channels.contains(tl_channel.getId().toString())
|
||||
} else if (inisettings.blacklist_channels != null) {
|
||||
download = !inisettings.blacklist_channels.contains(tl_channel.getId().toString())
|
||||
if (settings.whitelist_channels != null) {
|
||||
download = settings.whitelist_channels.contains(tl_channel.getId().toString())
|
||||
} else if (settings.blacklist_channels != null) {
|
||||
download = !settings.blacklist_channels.contains(tl_channel.getId().toString())
|
||||
}
|
||||
val channel = Channel(id=tl_channel.getId(), access_hash=tl_channel.getAccessHash(), title=tl_channel.getTitle(), obj=tl_peer_channel, download=download)
|
||||
if (tl_channel.getMegagroup()) {
|
||||
|
@ -1,72 +0,0 @@
|
||||
package de.fabianonline.telegram_backup
|
||||
|
||||
import java.io.File
|
||||
import org.slf4j.LoggerFactory
|
||||
import org.slf4j.Logger
|
||||
|
||||
class IniSettings(val file_base: String) {
|
||||
val logger = LoggerFactory.getLogger(IniSettings::class.java)
|
||||
var settings = mutableMapOf<String, MutableList<String>>()
|
||||
|
||||
init {
|
||||
loadIni(file_base + "config.ini")
|
||||
copySampleIni(file_base + "config.sample.ini")
|
||||
}
|
||||
|
||||
private fun loadIni(filename: String) {
|
||||
val file = File(filename)
|
||||
logger.trace("Checking ini file {}", filename.anonymize())
|
||||
if (!file.exists()) return
|
||||
logger.debug("Loading ini file {}", filename.anonymize())
|
||||
file.forEachLine { parseLine(it) }
|
||||
}
|
||||
|
||||
private fun parseLine(original_line: String) {
|
||||
logger.trace("Parsing line: {}", original_line)
|
||||
var line = original_line.trim().replaceAfter("#", "").removeSuffix("#")
|
||||
logger.trace("After cleaning: {}", line)
|
||||
if (line == "") return
|
||||
val parts: List<String> = line.split("=", limit=2).map{it.trim()}
|
||||
|
||||
if (parts.size < 2) throw RuntimeException("Invalid config setting: $line")
|
||||
|
||||
val (key, value) = parts
|
||||
if (value=="") {
|
||||
settings.remove(key)
|
||||
} else {
|
||||
var map = settings.get(key)
|
||||
if (map == null) {
|
||||
map = mutableListOf<String>()
|
||||
settings.put(key, map)
|
||||
}
|
||||
map.add(value)
|
||||
}
|
||||
}
|
||||
|
||||
private fun copySampleIni(filename: String) {
|
||||
val stream = Config::class.java.getResourceAsStream("/config.sample.ini")
|
||||
File(filename).outputStream().use { stream.copyTo(it) }
|
||||
stream.close()
|
||||
}
|
||||
|
||||
fun println() = println(settings)
|
||||
|
||||
fun getString(key: String, default: String? = null): String? = settings.get(key)?.last() ?: default
|
||||
fun getStringList(key: String): List<String>? = settings.get(key)
|
||||
fun getInt(key: String, default: Int? = null): Int? = try { settings.get(key)?.last()?.toInt() } catch (e: NumberFormatException) { null } ?: default
|
||||
fun getBoolean(key: String, default: Boolean = false): Boolean {
|
||||
val value = settings.get(key)?.last()
|
||||
if (value==null) return default
|
||||
return value=="true"
|
||||
}
|
||||
fun getArray(key: String): List<String> = settings.get(key) ?: listOf<String>()
|
||||
|
||||
val gmaps_key = getString("gmaps_key", default=Config.SECRET_GMAPS)!!
|
||||
val pagination = getBoolean("pagination", default=true)
|
||||
val pagination_size = getInt("pagination_size", default=Config.DEFAULT_PAGINATION)!!
|
||||
val download_media = getBoolean("download_media", default=true)
|
||||
val download_channels = getBoolean("download_channels", default=false)
|
||||
val download_supergroups = getBoolean("download_supergroups", default=false)
|
||||
val whitelist_channels = getStringList("whitelist_channels")
|
||||
val blacklist_channels = getStringList("blacklist_channels")
|
||||
}
|
@ -5,25 +5,41 @@ import org.slf4j.LoggerFactory
|
||||
|
||||
class Settings(val file_base: String, val database: Database, val cli_settings: CommandLineOptions) {
|
||||
val logger = LoggerFactory.getLogger(Settings::class.java)
|
||||
val settings = mutableMapOf<String, Setting>()
|
||||
|
||||
val gmaps_key: Setting
|
||||
|
||||
private val db_settings: Map<String, String>
|
||||
|
||||
private var ini_settings: Map<String, List<String>>
|
||||
val ini_settings: Map<String, List<String>>
|
||||
|
||||
init {
|
||||
db_settings = database.fetchSettings()
|
||||
ini_settings = load_ini("config.ini")
|
||||
copy_sample_ini("config.sample.ini")
|
||||
|
||||
}
|
||||
// Merging CLI and INI settings
|
||||
gmaps_key = get_setting("gmaps_key", default=Config.SECRET_GMAPS)
|
||||
val gmaps_key = get_setting_string("gmaps_key", default=Config.SECRET_GMAPS)
|
||||
val pagination = get_setting_boolean("pagination", default=true)
|
||||
val pagination_size = get_setting_int("pagination_size", default=Config.DEFAULT_PAGINATION)
|
||||
val download_media = get_setting_boolean("download_media", default=true)
|
||||
val download_channels = get_setting_boolean("download_channels", default=false)
|
||||
val download_supergroups = get_setting_boolean("download_supergroups", default=false)
|
||||
val whitelist_channels = get_setting_list("whitelist_channels")
|
||||
val blacklist_channels = get_setting_list("blacklist_channels")
|
||||
|
||||
|
||||
private fun get_setting_string(name: String, default: String): String {
|
||||
return ini_settings[name]?.last() ?: cli_settings.values[name] ?: default
|
||||
}
|
||||
|
||||
private fun get_setting(name: String, default: String? = null): Setting {
|
||||
return Setting.build(name, ini_settings[name], cli_settings.values[name], default)
|
||||
private fun get_setting_int(name: String, default: Int): Int {
|
||||
return ini_settings[name]?.last()?.toInt() ?: cli_settings.values[name]?.toInt() ?: default
|
||||
}
|
||||
|
||||
private fun get_setting_boolean(name: String, default: Boolean): Boolean {
|
||||
return ini_settings[name]?.last()?.toBoolean() ?: cli_settings.values[name]?.toBoolean() ?: default
|
||||
}
|
||||
|
||||
private fun get_setting_list(name: String): List<String>? {
|
||||
return ini_settings[name]
|
||||
}
|
||||
|
||||
private fun load_ini(filename: String): Map<String, List<String>> {
|
||||
@ -63,39 +79,7 @@ class Settings(val file_base: String, val database: Database, val cli_settings:
|
||||
File(filename).outputStream().use { stream.copyTo(it) }
|
||||
stream.close()
|
||||
}
|
||||
|
||||
|
||||
open class Setting(name: String, value: List<String>?, source: SettingSource) {
|
||||
companion object {
|
||||
val all = mutableListOf<Setting>()
|
||||
|
||||
fun build(name: String, ini_value: List<String>?, cli_value: List<String>?, default: String?): Setting {
|
||||
var value: List<String>?
|
||||
var source: SettingSource
|
||||
if (cli_value != null) {
|
||||
value=cli_value
|
||||
source=SettingSource.CLI
|
||||
} else if (ini_value != null) {
|
||||
value=ini_value
|
||||
source=SettingSource.INI
|
||||
} else {
|
||||
if (default!=null) {
|
||||
value = listOf(default)
|
||||
} else {
|
||||
value = null
|
||||
}
|
||||
source=SettingSource.DEFAULT
|
||||
}
|
||||
return Setting(name, value, source);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum class SettingSource {
|
||||
INI,
|
||||
CLI,
|
||||
DEFAULT
|
||||
}
|
||||
}
|
||||
/*
|
||||
class DbSettings(val database: Database) {
|
||||
|
@ -28,14 +28,14 @@ import de.fabianonline.telegram_backup.mediafilemanager.AbstractMediaFileManager
|
||||
import de.fabianonline.telegram_backup.mediafilemanager.FileManagerFactory
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
internal class TelegramUpdateHandler(val user_manager: UserManager, val db: Database) : UpdateCallback {
|
||||
internal class TelegramUpdateHandler(val user_manager: UserManager, val db: Database, val file_base: String, val settings: Settings) : UpdateCallback {
|
||||
val logger = LoggerFactory.getLogger(TelegramUpdateHandler::class.java)
|
||||
|
||||
override fun onUpdates(client: TelegramClient, updates: TLUpdates) {
|
||||
|
||||
logger.debug("onUpdates - " + updates.getUpdates().size + " Updates, " + updates.getUsers().size + " Users, " + updates.getChats().size + " Chats")
|
||||
for (update in updates.getUpdates()) {
|
||||
processUpdate(update, client)
|
||||
processUpdate(update)
|
||||
logger.debug(" " + update.javaClass.getName())
|
||||
}
|
||||
db.saveUsers(updates.getUsers())
|
||||
@ -45,7 +45,7 @@ internal class TelegramUpdateHandler(val user_manager: UserManager, val db: Data
|
||||
override fun onUpdatesCombined(client: TelegramClient, updates: TLUpdatesCombined) {
|
||||
logger.debug("onUpdatesCombined")
|
||||
for (update in updates.getUpdates()) {
|
||||
processUpdate(update, client)
|
||||
processUpdate(update)
|
||||
}
|
||||
db.saveUsers(updates.getUsers())
|
||||
db.saveChats(updates.getChats())
|
||||
@ -53,7 +53,7 @@ internal class TelegramUpdateHandler(val user_manager: UserManager, val db: Data
|
||||
|
||||
override fun onUpdateShort(client: TelegramClient, update: TLUpdateShort) {
|
||||
logger.debug("onUpdateShort")
|
||||
processUpdate(update.getUpdate(), client)
|
||||
processUpdate(update.getUpdate())
|
||||
logger.debug(" " + update.getUpdate().javaClass.getName())
|
||||
}
|
||||
|
||||
@ -76,7 +76,7 @@ internal class TelegramUpdateHandler(val user_manager: UserManager, val db: Data
|
||||
message.getEntities(), null, null)
|
||||
val vector = TLVector<TLAbsMessage>(TLAbsMessage::class.java)
|
||||
vector.add(msg)
|
||||
db.saveMessages(vector, Kotlogram.API_LAYER)
|
||||
db.saveMessages(vector, Kotlogram.API_LAYER, settings=settings)
|
||||
System.out.print('.')
|
||||
}
|
||||
|
||||
@ -109,7 +109,7 @@ internal class TelegramUpdateHandler(val user_manager: UserManager, val db: Data
|
||||
m.getEntities(), null, null)
|
||||
val vector = TLVector<TLAbsMessage>(TLAbsMessage::class.java)
|
||||
vector.add(msg)
|
||||
db.saveMessages(vector, Kotlogram.API_LAYER)
|
||||
db.saveMessages(vector, Kotlogram.API_LAYER, settings=settings)
|
||||
System.out.print('.')
|
||||
}
|
||||
|
||||
@ -121,15 +121,15 @@ internal class TelegramUpdateHandler(val user_manager: UserManager, val db: Data
|
||||
logger.debug("onUpdateTooLong")
|
||||
}
|
||||
|
||||
private fun processUpdate(update: TLAbsUpdate, client: TelegramClient) {
|
||||
private fun processUpdate(update: TLAbsUpdate) {
|
||||
if (update is TLUpdateNewMessage) {
|
||||
val abs_msg = update.getMessage()
|
||||
val vector = TLVector<TLAbsMessage>(TLAbsMessage::class.java)
|
||||
vector.add(abs_msg)
|
||||
db.saveMessages(vector, Kotlogram.API_LAYER)
|
||||
db.saveMessages(vector, Kotlogram.API_LAYER, settings=settings)
|
||||
System.out.print('.')
|
||||
if (abs_msg is TLMessage) {
|
||||
val fm = FileManagerFactory.getFileManager(abs_msg, user_manager)
|
||||
val fm = FileManagerFactory.getFileManager(abs_msg, user_manager, file_base, settings)
|
||||
if (fm != null && !fm.isEmpty && !fm.downloaded) {
|
||||
try {
|
||||
fm.download()
|
||||
|
@ -37,11 +37,11 @@ import de.fabianonline.telegram_backup.*
|
||||
import org.slf4j.Logger
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
class HTMLExporter(val db: Database, val user: UserManager, val ini: IniSettings, val file_base: String) {
|
||||
class HTMLExporter(val db: Database, val user: UserManager, val settings: Settings, val file_base: String) {
|
||||
@Throws(IOException::class)
|
||||
fun export() {
|
||||
try {
|
||||
val pagination = if (ini.pagination) ini.pagination_size else -1
|
||||
val pagination = if (settings.pagination) settings.pagination_size else -1
|
||||
|
||||
// Create base dir
|
||||
logger.debug("Creating base dir")
|
||||
|
@ -21,6 +21,7 @@ import de.fabianonline.telegram_backup.Config
|
||||
|
||||
import com.github.badoualy.telegram.tl.api.*
|
||||
import com.github.badoualy.telegram.tl.exception.RpcErrorException
|
||||
import de.fabianonline.telegram_backup.Settings
|
||||
|
||||
import java.io.IOException
|
||||
import java.io.File
|
||||
|
@ -29,6 +29,7 @@ import com.github.badoualy.telegram.tl.api.*
|
||||
import com.github.badoualy.telegram.tl.api.upload.TLFile
|
||||
import com.github.badoualy.telegram.tl.exception.RpcErrorException
|
||||
import com.github.badoualy.telegram.tl.api.request.TLRequestUploadGetFile
|
||||
import de.fabianonline.telegram_backup.Settings
|
||||
|
||||
import java.io.IOException
|
||||
import java.io.File
|
||||
@ -41,7 +42,7 @@ import java.util.concurrent.TimeoutException
|
||||
import org.apache.commons.io.FileUtils
|
||||
|
||||
object FileManagerFactory {
|
||||
fun getFileManager(m: TLMessage?, u: UserManager, file_base: String): AbstractMediaFileManager? {
|
||||
fun getFileManager(m: TLMessage?, u: UserManager, file_base: String, settings: Settings?): AbstractMediaFileManager? {
|
||||
if (m == null) return null
|
||||
val media = m.getMedia() ?: return null
|
||||
|
||||
@ -53,7 +54,7 @@ object FileManagerFactory {
|
||||
StickerFileManager(m, u, file_base)
|
||||
} else d
|
||||
} else if (media is TLMessageMediaGeo) {
|
||||
return GeoFileManager(m, u, file_base)
|
||||
return GeoFileManager(m, u, file_base, settings)
|
||||
} else if (media is TLMessageMediaEmpty) {
|
||||
return UnsupportedFileManager(m, u, file_base, "empty")
|
||||
} else if (media is TLMessageMediaUnsupported) {
|
||||
|
@ -18,14 +18,15 @@ package de.fabianonline.telegram_backup.mediafilemanager
|
||||
|
||||
import de.fabianonline.telegram_backup.UserManager
|
||||
import de.fabianonline.telegram_backup.DownloadManager
|
||||
import de.fabianonline.telegram_backup.IniSettings
|
||||
|
||||
import com.github.badoualy.telegram.tl.api.*
|
||||
import de.fabianonline.telegram_backup.Config
|
||||
import de.fabianonline.telegram_backup.Settings
|
||||
|
||||
import java.io.IOException
|
||||
import java.io.File
|
||||
|
||||
class GeoFileManager(msg: TLMessage, user: UserManager, file_base: String) : AbstractMediaFileManager(msg, user, file_base) {
|
||||
class GeoFileManager(msg: TLMessage, user: UserManager, file_base: String, val settings: Settings?) : AbstractMediaFileManager(msg, user, file_base) {
|
||||
protected lateinit var geo: TLGeoPoint
|
||||
|
||||
// We don't know the size, so we just guess.
|
||||
@ -59,7 +60,7 @@ class GeoFileManager(msg: TLMessage, user: UserManager, file_base: String) : Abs
|
||||
"center=${geo.getLat()},${geo.getLong()}&" +
|
||||
"markers=color:red|${geo.getLat()},${geo.getLong()}&" +
|
||||
"zoom=14&size=300x150&scale=2&format=png&" +
|
||||
"key=" + IniSettings.gmaps_key
|
||||
"key=" + (settings?.gmaps_key ?: Config.SECRET_GMAPS)
|
||||
return DownloadManager.downloadExternalFile(targetPathAndFilename, url)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user