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

Added automatic checks for missing messages in the database complete with download of these mesages.

This commit is contained in:
Fabian Schlenz 2016-07-04 13:33:17 +02:00
parent 29b963ec25
commit 07ba594a15
3 changed files with 86 additions and 26 deletions

View File

@ -31,7 +31,7 @@ class CommandLineDownloadProgress implements DownloadProgressInterface {
public void onMediaDownloadedEmpty(boolean n) { show(true, 'e'); } public void onMediaDownloadedEmpty(boolean n) { show(true, 'e'); }
public void onMediaDownloadFinished() { showNewLine(); System.out.println("Done."); } public void onMediaDownloadFinished() { showNewLine(); System.out.println("Done."); }
private void show(boolean n, char letter) { System.out.print(n ? letter : '.'); i++; if (i % 50 == 0) showNewLine();} private void show(boolean n, char letter) { System.out.print(n ? letter : '.'); i++; if (i % 100 == 0) showNewLine();}
private void showNewLine() { System.out.println(" - " + i + "/" + mediaCount); } private void showNewLine() { System.out.println(" - " + i + "/" + mediaCount); }
} }

View File

@ -14,6 +14,7 @@ import java.io.File;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.util.LinkedList; import java.util.LinkedList;
import java.sql.Array;
import de.fabianonline.telegram_backup.UserManager; import de.fabianonline.telegram_backup.UserManager;
import de.fabianonline.telegram_backup.StickerConverter; import de.fabianonline.telegram_backup.StickerConverter;
@ -115,6 +116,42 @@ class Database {
} }
} }
public int getMessageCount() {
try {
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM messages");
rs.next();
return rs.getInt(1);
} catch (SQLException e) {
throw new RuntimeException("Could not get count of messages.");
}
}
public LinkedList<Integer> getMissingIDs() {
try {
LinkedList<Integer> missing = new LinkedList<Integer>();
int max = getTopMessageID();
ResultSet rs = stmt.executeQuery("SELECT id FROM messages ORDER BY id");
rs.next();
int id=rs.getInt(1);
for (int i=1; i<=max; i++) {
if (i==id) {
rs.next();
if (rs.isClosed()) {
id = Integer.MAX_VALUE;
} else {
id=rs.getInt(1);
}
} else if (i<id) {
missing.add(i);
}
}
return missing;
} catch(SQLException e) {
e.printStackTrace();
throw new RuntimeException("Could not get list of ids.");
}
}
public void saveMessages(TLVector<TLAbsMessage> all) { public void saveMessages(TLVector<TLAbsMessage> all) {
try { try {
PreparedStatement ps = conn.prepareStatement( PreparedStatement ps = conn.prepareStatement(

View File

@ -50,13 +50,14 @@ class DownloadManager {
System.out.println("New top message id 'in database' is " + max_database_id); System.out.println("New top message id 'in database' is " + max_database_id);
} }
if (max_database_id == max_message_id) {
System.out.println("No new messages to download.");
} else if (max_database_id > max_message_id) {
throw new RuntimeException("max_database_id is bigger then max_message_id. This shouldn't be aple to happen. Ever.");
} else {
int start_id = max_database_id + 1; int start_id = max_database_id + 1;
int current_start_id = start_id; int current_start_id = start_id;
int end_id = max_message_id; int end_id = max_message_id;
if (start_id > end_id) {
System.out.println("No new messages to download.");
return;
}
prog.onMessageDownloadStart(end_id - current_start_id + 1); prog.onMessageDownloadStart(end_id - current_start_id + 1);
@ -81,6 +82,28 @@ class DownloadManager {
prog.onMessageDownloadFinished(); prog.onMessageDownloadFinished();
} }
System.out.println("Checking message database for completeness...");
if (db.getMessageCount() != db.getTopMessageID()) {
LinkedList<Integer> ids = db.getMissingIDs();
System.out.println("Downloading " + ids.size() + " messages that are missing in your database.");
prog.onMessageDownloadStart(ids.size());
while (ids.size()>0) {
TLIntVector vector = new TLIntVector();
for (int i=0; i<100; i++) {
if (ids.size()==0) break;
vector.add(ids.remove());
}
TLAbsMessages response = client.messagesGetMessages(vector);
prog.onMessageDownloaded(response.getMessages().size());
db.saveMessages(response.getMessages());
db.saveChats(response.getChats());
db.saveUsers(response.getUsers());
try { Thread.sleep(Config.DELAY_AFTER_GET_MESSAGES); } catch (InterruptedException e) {}
}
prog.onMessageDownloadFinished();
}
}
public void downloadMedia() throws RpcErrorException, IOException { public void downloadMedia() throws RpcErrorException, IOException {
LinkedList<TLMessage> messages = this.db.getMessagesWithMedia(); LinkedList<TLMessage> messages = this.db.getMessagesWithMedia();
prog.onMediaDownloadStart(messages.size()); prog.onMediaDownloadStart(messages.size());