1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-10 19:36:15 +00:00

Resorted the code in DownloadManager - no more need for downloadMessages calling _downloadMessages or downloadMedia calling _downloadMedia.

This commit is contained in:
Fabian Schlenz 2018-04-10 06:33:15 +02:00
parent 9affb47130
commit 069799cbaf

View File

@ -63,18 +63,18 @@ enum class MessageSource(val descr: String) {
class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInterface, val db: Database, val user_manager: UserManager, val settings: Settings, val file_base: String) { class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInterface, val db: Database, val user_manager: UserManager, val settings: Settings, val file_base: String) {
@Throws(RpcErrorException::class, IOException::class) @Throws(RpcErrorException::class, IOException::class)
fun downloadMessages(limit: Int?) { fun downloadMessages(limit: Int?) {
Utils.obeyFloodWait() { logger.info("This is downloadMessages with limit {}", limit)
_downloadMessages(limit)
}
}
@Throws(RpcErrorException::class, IOException::class, TimeoutException::class)
fun _downloadMessages(limit: Int?) {
logger.info("This is _downloadMessages with limit {}", limit)
logger.info("Downloading the last dialogs") logger.info("Downloading the last dialogs")
System.out.println("Downloading most recent dialogs... ") System.out.println("Downloading most recent dialogs... ")
var max_message_id = 0 var max_message_id = 0
val chats = getChats() var result: ChatList? = null
Utils.obeyFloodWait() {
result = getChats()
}
val chats = result!!
logger.debug("Got {} dialogs, {} supergoups, {} channels", chats.dialogs.size, chats.supergroups.size, chats.channels.size) logger.debug("Got {} dialogs, {} supergoups, {} channels", chats.dialogs.size, chats.supergroups.size, chats.channels.size)
for (d in chats.dialogs) { for (d in chats.dialogs) {
@ -101,7 +101,7 @@ class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInte
if (max_database_id == max_message_id) { if (max_database_id == max_message_id) {
System.out.println("No new messages to download.") System.out.println("No new messages to download.")
} else if (max_database_id > max_message_id) { } else if (max_database_id > max_message_id) {
throw RuntimeException("max_database_id is bigger then max_message_id. This shouldn't happen. But the telegram api nonetheless does that sometimes. Just ignore this error, wait a few seconds and then try again.") throw RuntimeException("max_database_id is bigger than max_message_id. This shouldn't happen. But the telegram api nonetheless does that sometimes. Just ignore this error, wait a few seconds and then try again.")
} else { } else {
val start_id = max_database_id + 1 val start_id = max_database_id + 1
val end_id = max_message_id val end_id = max_message_id
@ -209,11 +209,7 @@ class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInte
db.saveChats(response.getChats()) db.saveChats(response.getChats())
db.saveUsers(response.getUsers()) db.saveUsers(response.getUsers())
logger.trace("Sleeping") logger.trace("Sleeping")
try { try { TimeUnit.MILLISECONDS.sleep(Config.DELAY_AFTER_GET_MESSAGES) } catch (e: InterruptedException) { }
TimeUnit.MILLISECONDS.sleep(Config.DELAY_AFTER_GET_MESSAGES)
} catch (e: InterruptedException) {
}
} }
logger.debug("Finished.") logger.debug("Finished.")
@ -223,13 +219,6 @@ class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInte
@Throws(RpcErrorException::class, IOException::class) @Throws(RpcErrorException::class, IOException::class)
fun downloadMedia() { fun downloadMedia() {
download_client = client.getDownloaderClient() download_client = client.getDownloaderClient()
Utils.obeyFloodWait() {
_downloadMedia()
}
}
@Throws(RpcErrorException::class, IOException::class)
private fun _downloadMedia() {
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)
@ -359,15 +348,13 @@ class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInte
} }
logger.trace("offset before the loop is {}", offset) logger.trace("offset before the loop is {}", offset)
fos = FileOutputStream(temp_filename, true) fos = FileOutputStream(temp_filename, true)
var response: TLFile? = null
var try_again: Boolean
do { do {
try_again = false
logger.trace("offset: {} block_size: {} size: {}", offset, size, size) logger.trace("offset: {} block_size: {} size: {}", offset, size, size)
val req = TLRequestUploadGetFile(loc, offset, size) val req = TLRequestUploadGetFile(loc, offset, size)
var resp: TLFile? = null
try { try {
Utils.obeyFloodWait() { Utils.obeyFloodWait() {
response = download_client!!.executeRpcQuery(req, dcID) as TLFile resp = download_client!!.executeRpcQuery(req, dcID) as TLFile
} }
} catch (e: RpcErrorException) { } catch (e: RpcErrorException) {
if (e.getCode() == 400) { if (e.getCode() == 400) {
@ -377,19 +364,16 @@ class DownloadManager(val client: TelegramClient, val prog: DownloadProgressInte
throw e throw e
} }
val resp = response!! val response = resp!!
offset += resp.getBytes().getData().size offset += response.getBytes().getData().size
logger.trace("response: {} total size: {}", resp.getBytes().getData().size, offset) logger.trace("response: {} total size: {}", response.getBytes().getData().size, offset)
fos.write(resp.getBytes().getData()) fos.write(response.getBytes().getData())
fos.flush() fos.flush()
try { try { TimeUnit.MILLISECONDS.sleep(Config.DELAY_AFTER_GET_FILE) } catch (e: InterruptedException) { }
TimeUnit.MILLISECONDS.sleep(Config.DELAY_AFTER_GET_FILE)
} catch (e: InterruptedException) {
}
} while (offset < size && (try_again || resp.getBytes().getData().size > 0)) } while (offset < size && response.getBytes().getData().size > 0)
fos.close() fos.close()
if (offset < size) { if (offset < size) {
System.out.println("Requested file $target with $size bytes, but got only $offset bytes.") System.out.println("Requested file $target with $size bytes, but got only $offset bytes.")