1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-12-25 22:35:35 +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 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); }
}

View File

@ -14,6 +14,7 @@ import java.io.File;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.LinkedList;
import java.sql.Array;
import de.fabianonline.telegram_backup.UserManager;
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) {
try {
PreparedStatement ps = conn.prepareStatement(

View File

@ -50,35 +50,58 @@ class DownloadManager {
System.out.println("New top message id 'in database' is " + max_database_id);
}
int start_id = max_database_id + 1;
int current_start_id = start_id;
int end_id = max_message_id;
if (start_id > end_id) {
if (max_database_id == max_message_id) {
System.out.println("No new messages to download.");
return;
}
prog.onMessageDownloadStart(end_id - current_start_id + 1);
while (current_start_id <= end_id) {
int my_end_id = Math.min(current_start_id+99, end_id);
ArrayList<Integer> a = makeIdList(current_start_id, my_end_id);
TLIntVector ids = new TLIntVector();
ids.addAll(a);
my_end_id = ids.get(ids.size()-1);
current_start_id = my_end_id + 1;
} 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 current_start_id = start_id;
int end_id = max_message_id;
TLAbsMessages response = client.messagesGetMessages(ids);
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.onMessageDownloadStart(end_id - current_start_id + 1);
while (current_start_id <= end_id) {
int my_end_id = Math.min(current_start_id+99, end_id);
ArrayList<Integer> a = makeIdList(current_start_id, my_end_id);
TLIntVector ids = new TLIntVector();
ids.addAll(a);
my_end_id = ids.get(ids.size()-1);
current_start_id = my_end_id + 1;
TLAbsMessages response = client.messagesGetMessages(ids);
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();
}
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 {