mirror of
https://github.com/fabianonline/telegram_backup.git
synced 2024-11-22 16:56:16 +00:00
Updated Kotlogram to the current HEAD. This also added a small database change and the need to update database entries for messages with media that were downloaded with api layer 51.
This commit is contained in:
parent
b4c50a0163
commit
fc7d3fdcbc
@ -14,7 +14,7 @@ repositories {
|
|||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile 'com.github.badoualy:kotlogram:0.0.6'
|
compile 'com.github.badoualy:kotlogram:497e5dd62d9bcb341f584164b04b4b537ce9d295'
|
||||||
compile 'org.xerial:sqlite-jdbc:3.8.11.2'
|
compile 'org.xerial:sqlite-jdbc:3.8.11.2'
|
||||||
compile 'com.github.spullara.mustache.java:compiler:0.8.18'
|
compile 'com.github.spullara.mustache.java:compiler:0.8.18'
|
||||||
}
|
}
|
||||||
|
@ -17,8 +17,9 @@
|
|||||||
package de.fabianonline.telegram_backup;
|
package de.fabianonline.telegram_backup;
|
||||||
|
|
||||||
import com.github.badoualy.telegram.api.TelegramApiStorage;
|
import com.github.badoualy.telegram.api.TelegramApiStorage;
|
||||||
import com.github.badoualy.telegram.mtproto.DataCenter;
|
import com.github.badoualy.telegram.mtproto.model.DataCenter;
|
||||||
import com.github.badoualy.telegram.mtproto.auth.AuthKey;
|
import com.github.badoualy.telegram.mtproto.auth.AuthKey;
|
||||||
|
import com.github.badoualy.telegram.mtproto.model.MTSession;
|
||||||
|
|
||||||
import org.apache.commons.io.FileUtils;
|
import org.apache.commons.io.FileUtils;
|
||||||
|
|
||||||
@ -132,7 +133,8 @@ class ApiStorage implements TelegramApiStorage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void saveServerSalt(long salt) {}
|
public void saveSession(MTSession session) {
|
||||||
|
}
|
||||||
|
|
||||||
public Long loadServerSalt() { return null; }
|
public MTSession loadSession() { return null; }
|
||||||
}
|
}
|
||||||
|
@ -65,8 +65,12 @@ public class CommandLineController {
|
|||||||
if (CommandLineOptions.cmd_list_accounts) this.list_accounts();
|
if (CommandLineOptions.cmd_list_accounts) this.list_accounts();
|
||||||
|
|
||||||
Log.debug("Initializing TelegramApp");
|
Log.debug("Initializing TelegramApp");
|
||||||
|
if (CommandLineOptions.cmd_debug_telegram) {
|
||||||
|
System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "DEBUG");
|
||||||
|
} else {
|
||||||
|
System.setProperty(org.slf4j.impl.SimpleLogger.DEFAULT_LOG_LEVEL_KEY, "ERROR");
|
||||||
|
}
|
||||||
app = new TelegramApp(Config.APP_ID, Config.APP_HASH, Config.APP_MODEL, Config.APP_SYSVER, Config.APP_APPVER, Config.APP_LANG);
|
app = new TelegramApp(Config.APP_ID, Config.APP_HASH, Config.APP_MODEL, Config.APP_SYSVER, Config.APP_APPVER, Config.APP_LANG);
|
||||||
if (CommandLineOptions.cmd_debug_telegram) Kotlogram.setDebugLogEnabled(true);
|
|
||||||
|
|
||||||
Log.debug("Checking accounts");
|
Log.debug("Checking accounts");
|
||||||
Log.up();
|
Log.up();
|
||||||
|
@ -37,14 +37,8 @@ public class Config {
|
|||||||
public static final String FILE_FILES_BASE = "files";
|
public static final String FILE_FILES_BASE = "files";
|
||||||
public static final String FILE_STICKER_BASE = "stickers";
|
public static final String FILE_STICKER_BASE = "stickers";
|
||||||
|
|
||||||
public static final int[] FILE_DOWNLOAD_BLOCK_SIZES = new int[]{
|
public static int DELAY_AFTER_GET_MESSAGES = 100;
|
||||||
1*1024*1024,
|
public static int DELAY_AFTER_GET_FILE = 100;
|
||||||
512*1024,
|
|
||||||
2*1024*1024,
|
|
||||||
4*1024*1024};
|
|
||||||
|
|
||||||
public static int DELAY_AFTER_GET_MESSAGES = 200;
|
|
||||||
public static int DELAY_AFTER_GET_FILE = 1000;
|
|
||||||
|
|
||||||
public static final String SECRET_GMAPS = "AIzaSyBEtUDhCQKEH6i2Mn1GAiQ9M_tLN0vxHIs";
|
public static final String SECRET_GMAPS = "AIzaSyBEtUDhCQKEH6i2Mn1GAiQ9M_tLN0vxHIs";
|
||||||
|
|
||||||
|
@ -161,16 +161,16 @@ public class Database {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized void saveMessages(TLVector<TLAbsMessage> all) {
|
public synchronized void saveMessages(TLVector<TLAbsMessage> all, Integer api_layer) {
|
||||||
try {
|
try {
|
||||||
//"(id, dialog_id, from_id, from_type, text, time, has_media, data, sticker, type) " +
|
//"(id, dialog_id, from_id, from_type, text, time, has_media, data, sticker, type) " +
|
||||||
//"VALUES " +
|
//"VALUES " +
|
||||||
//"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
//"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
|
||||||
String columns =
|
String columns =
|
||||||
"(id, message_type, dialog_id, chat_id, sender_id, fwd_from_id, text, time, has_media, media_type, media_file, media_size, data) "+
|
"(id, message_type, dialog_id, chat_id, sender_id, fwd_from_id, text, time, has_media, media_type, media_file, media_size, data, api_layer) "+
|
||||||
"VALUES " +
|
"VALUES " +
|
||||||
"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
|
||||||
//1 2 3 4 5 6 7 8 9 10 11 12 13
|
//1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
||||||
PreparedStatement ps = conn.prepareStatement("INSERT OR REPLACE INTO messages " + columns);
|
PreparedStatement ps = conn.prepareStatement("INSERT OR REPLACE INTO messages " + columns);
|
||||||
PreparedStatement ps_insert_or_ignore = conn.prepareStatement("INSERT OR IGNORE INTO messages " + columns);
|
PreparedStatement ps_insert_or_ignore = conn.prepareStatement("INSERT OR IGNORE INTO messages " + columns);
|
||||||
|
|
||||||
@ -194,11 +194,13 @@ public class Database {
|
|||||||
throw new RuntimeException("Unexpected Peer type: " + peer.getClass().getName());
|
throw new RuntimeException("Unexpected Peer type: " + peer.getClass().getName());
|
||||||
}
|
}
|
||||||
ps.setInt(5, msg.getFromId());
|
ps.setInt(5, msg.getFromId());
|
||||||
if (msg.getFwdFromId()!=null && msg.getFwdFromId() instanceof TLPeerUser) {
|
|
||||||
ps.setInt(6, ((TLPeerUser)msg.getFwdFromId()).getUserId());
|
if (msg.getFwdFrom() != null) {
|
||||||
|
ps.setInt(6, msg.getFwdFrom().getFromId());
|
||||||
} else {
|
} else {
|
||||||
ps.setNull(6, Types.INTEGER);
|
ps.setNull(6, Types.INTEGER);
|
||||||
}
|
}
|
||||||
|
|
||||||
String text = msg.getMessage();
|
String text = msg.getMessage();
|
||||||
if ((text==null || text.equals("")) && msg.getMedia()!=null) {
|
if ((text==null || text.equals("")) && msg.getMedia()!=null) {
|
||||||
if (msg.getMedia() instanceof TLMessageMediaDocument) {
|
if (msg.getMedia() instanceof TLMessageMediaDocument) {
|
||||||
@ -224,6 +226,7 @@ public class Database {
|
|||||||
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
ByteArrayOutputStream stream = new ByteArrayOutputStream();
|
||||||
msg.serializeBody(stream);
|
msg.serializeBody(stream);
|
||||||
ps.setBytes(13, stream.toByteArray());
|
ps.setBytes(13, stream.toByteArray());
|
||||||
|
ps.setInt(14, api_layer);
|
||||||
ps.addBatch();
|
ps.addBatch();
|
||||||
} else if (abs instanceof TLMessageService) {
|
} else if (abs instanceof TLMessageService) {
|
||||||
ps_insert_or_ignore.setInt(1, abs.getId());
|
ps_insert_or_ignore.setInt(1, abs.getId());
|
||||||
@ -239,6 +242,7 @@ public class Database {
|
|||||||
ps_insert_or_ignore.setNull(11, Types.VARCHAR);
|
ps_insert_or_ignore.setNull(11, Types.VARCHAR);
|
||||||
ps_insert_or_ignore.setNull(12, Types.INTEGER);
|
ps_insert_or_ignore.setNull(12, Types.INTEGER);
|
||||||
ps_insert_or_ignore.setNull(13, Types.BLOB);
|
ps_insert_or_ignore.setNull(13, Types.BLOB);
|
||||||
|
ps_insert_or_ignore.setInt(14, api_layer);
|
||||||
ps_insert_or_ignore.addBatch();
|
ps_insert_or_ignore.addBatch();
|
||||||
} else if (abs instanceof TLMessageEmpty) {
|
} else if (abs instanceof TLMessageEmpty) {
|
||||||
ps_insert_or_ignore.setInt(1, abs.getId());
|
ps_insert_or_ignore.setInt(1, abs.getId());
|
||||||
@ -254,6 +258,7 @@ public class Database {
|
|||||||
ps_insert_or_ignore.setNull(11, Types.VARCHAR);
|
ps_insert_or_ignore.setNull(11, Types.VARCHAR);
|
||||||
ps_insert_or_ignore.setNull(12, Types.INTEGER);
|
ps_insert_or_ignore.setNull(12, Types.INTEGER);
|
||||||
ps_insert_or_ignore.setNull(13, Types.BLOB);
|
ps_insert_or_ignore.setNull(13, Types.BLOB);
|
||||||
|
ps_insert_or_ignore.setInt(14, api_layer);
|
||||||
ps_insert_or_ignore.addBatch();
|
ps_insert_or_ignore.addBatch();
|
||||||
} else {
|
} else {
|
||||||
throw new RuntimeException("Unexpected Message type: " + abs.getClass().getName());
|
throw new RuntimeException("Unexpected Message type: " + abs.getClass().getName());
|
||||||
@ -397,6 +402,17 @@ public class Database {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public LinkedList<Integer> getIdsFromQuery(String query) {
|
||||||
|
try {
|
||||||
|
LinkedList<Integer> list = new LinkedList<Integer>();
|
||||||
|
ResultSet rs = stmt.executeQuery(query);
|
||||||
|
while(rs.next()) { list.add(rs.getInt(1)); }
|
||||||
|
rs.close();
|
||||||
|
return list;
|
||||||
|
} catch (SQLException e) { throw new RuntimeException(e); }
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public HashMap<String, Integer> getMessageTypesWithCount() {
|
public HashMap<String, Integer> getMessageTypesWithCount() {
|
||||||
HashMap<String, Integer> map = new HashMap<String, Integer>();
|
HashMap<String, Integer> map = new HashMap<String, Integer>();
|
||||||
try {
|
try {
|
||||||
|
@ -29,6 +29,7 @@ public class DatabaseUpdates {
|
|||||||
register(new DB_Update_4(conn, db));
|
register(new DB_Update_4(conn, db));
|
||||||
register(new DB_Update_5(conn, db));
|
register(new DB_Update_5(conn, db));
|
||||||
register(new DB_Update_6(conn, db));
|
register(new DB_Update_6(conn, db));
|
||||||
|
register(new DB_Update_7(conn, db));
|
||||||
Log.down();
|
Log.down();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -97,7 +98,7 @@ public class DatabaseUpdates {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private DatabaseUpdate getUpdateToVersion(int i) { return updates.get(i); }
|
private DatabaseUpdate getUpdateToVersion(int i) { return updates.get(i-1); }
|
||||||
|
|
||||||
private int getMaxPossibleVersion() {
|
private int getMaxPossibleVersion() {
|
||||||
return updates.size();
|
return updates.size();
|
||||||
@ -205,7 +206,7 @@ class DB_Update_4 extends DatabaseUpdate {
|
|||||||
class DB_Update_5 extends DatabaseUpdate {
|
class DB_Update_5 extends DatabaseUpdate {
|
||||||
public int getVersion() { return 5; }
|
public int getVersion() { return 5; }
|
||||||
public DB_Update_5(Connection conn, Database db) { super(conn, db); }
|
public DB_Update_5(Connection conn, Database db) { super(conn, db); }
|
||||||
|
|
||||||
protected void _doUpdate() throws SQLException {
|
protected void _doUpdate() throws SQLException {
|
||||||
stmt.executeUpdate("CREATE TABLE runs (id INTEGER PRIMARY KEY ASC, time INTEGER, start_id INTEGER, end_id INTEGER, count_missing INTEGER)");
|
stmt.executeUpdate("CREATE TABLE runs (id INTEGER PRIMARY KEY ASC, time INTEGER, start_id INTEGER, end_id INTEGER, count_missing INTEGER)");
|
||||||
}
|
}
|
||||||
@ -268,10 +269,10 @@ class DB_Update_6 extends DatabaseUpdate {
|
|||||||
while (rs.next()) {
|
while (rs.next()) {
|
||||||
ps.setInt(5, rs.getInt(1));
|
ps.setInt(5, rs.getInt(1));
|
||||||
TLMessage msg = db.bytesToTLMessage(rs.getBytes(2));
|
TLMessage msg = db.bytesToTLMessage(rs.getBytes(2));
|
||||||
if (msg==null || msg.getFwdFromId()==null || ! (msg.getFwdFromId() instanceof TLPeerUser)) {
|
if (msg==null || msg.getFwdFrom()==null) {
|
||||||
ps.setNull(1, Types.INTEGER);
|
ps.setNull(1, Types.INTEGER);
|
||||||
} else {
|
} else {
|
||||||
ps.setInt(1, ((TLPeerUser)msg.getFwdFromId()).getUserId());
|
ps.setInt(1, msg.getFwdFrom().getFromId());
|
||||||
}
|
}
|
||||||
AbstractMediaFileManager f = FileManagerFactory.getFileManager(msg, db.user_manager, db.client);
|
AbstractMediaFileManager f = FileManagerFactory.getFileManager(msg, db.user_manager, db.client);
|
||||||
if (f==null) {
|
if (f==null) {
|
||||||
@ -294,3 +295,15 @@ class DB_Update_6 extends DatabaseUpdate {
|
|||||||
stmt.executeUpdate("ALTER TABLE messages_new RENAME TO messages");
|
stmt.executeUpdate("ALTER TABLE messages_new RENAME TO messages");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class DB_Update_7 extends DatabaseUpdate {
|
||||||
|
public int getVersion() { return 7; }
|
||||||
|
public boolean needsBackup() { return true; }
|
||||||
|
public DB_Update_7(Connection conn, Database db) { super(conn, db); }
|
||||||
|
|
||||||
|
protected void _doUpdate() throws SQLException {
|
||||||
|
stmt.executeUpdate("ALTER TABLE messages ADD COLUMN api_layer INTEGER");
|
||||||
|
|
||||||
|
stmt.executeUpdate("UPDATE messages SET api_layer=51");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -24,6 +24,7 @@ import de.fabianonline.telegram_backup.mediafilemanager.FileManagerFactory;
|
|||||||
import de.fabianonline.telegram_backup.mediafilemanager.AbstractMediaFileManager;
|
import de.fabianonline.telegram_backup.mediafilemanager.AbstractMediaFileManager;
|
||||||
|
|
||||||
import com.github.badoualy.telegram.api.TelegramClient;
|
import com.github.badoualy.telegram.api.TelegramClient;
|
||||||
|
import com.github.badoualy.telegram.api.Kotlogram;
|
||||||
import com.github.badoualy.telegram.tl.core.TLIntVector;
|
import com.github.badoualy.telegram.tl.core.TLIntVector;
|
||||||
import com.github.badoualy.telegram.tl.core.TLObject;
|
import com.github.badoualy.telegram.tl.core.TLObject;
|
||||||
import com.github.badoualy.telegram.tl.api.messages.TLAbsMessages;
|
import com.github.badoualy.telegram.tl.api.messages.TLAbsMessages;
|
||||||
@ -38,6 +39,7 @@ import java.io.File;
|
|||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.concurrent.TimeoutException;
|
import java.util.concurrent.TimeoutException;
|
||||||
@ -96,7 +98,7 @@ public class DownloadManager {
|
|||||||
dialog_limit);
|
dialog_limit);
|
||||||
Log.debug("Got %d dialogs", dialogs.getDialogs().size());
|
Log.debug("Got %d dialogs", dialogs.getDialogs().size());
|
||||||
Log.up();
|
Log.up();
|
||||||
for (TLAbsDialog d : dialogs.getDialogs()) {
|
for (TLDialog d : dialogs.getDialogs()) {
|
||||||
if (d.getTopMessage() > max_message_id) {
|
if (d.getTopMessage() > max_message_id) {
|
||||||
Log.debug("Updating top message id: %d => %d", max_message_id, d.getTopMessage());
|
Log.debug("Updating top message id: %d => %d", max_message_id, d.getTopMessage());
|
||||||
max_message_id = d.getTopMessage();
|
max_message_id = d.getTopMessage();
|
||||||
@ -119,42 +121,10 @@ public class DownloadManager {
|
|||||||
throw new 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 new 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.");
|
||||||
} else {
|
} else {
|
||||||
int start_id = max_database_id + 1;
|
int start_id = max_database_id + 1;
|
||||||
int current_start_id = start_id;
|
|
||||||
int end_id = max_message_id;
|
int end_id = max_message_id;
|
||||||
|
|
||||||
prog.onMessageDownloadStart(end_id - current_start_id + 1);
|
List<Integer> ids = makeIdList(start_id, end_id);
|
||||||
|
downloadMessages(ids);
|
||||||
Log.debug("Entering download loop");
|
|
||||||
Log.up();
|
|
||||||
while (current_start_id <= end_id) {
|
|
||||||
Log.debug("Loop");
|
|
||||||
Log.up();
|
|
||||||
Log.debug("current_start_id: %d", current_start_id);
|
|
||||||
Log.debug("end_id: %d", end_id);
|
|
||||||
int my_end_id = Math.min(current_start_id+99, end_id);
|
|
||||||
Log.debug("my_end_id: %d", my_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);
|
|
||||||
Log.debug("my_end_id: %d", my_end_id);
|
|
||||||
current_start_id = my_end_id + 1;
|
|
||||||
Log.debug("current_start_id: %d", current_start_id);
|
|
||||||
TLAbsMessages response = client.messagesGetMessages(ids);
|
|
||||||
prog.onMessageDownloaded(response.getMessages().size());
|
|
||||||
db.saveMessages(response.getMessages());
|
|
||||||
db.saveChats(response.getChats());
|
|
||||||
db.saveUsers(response.getUsers());
|
|
||||||
Log.debug("Sleeping");
|
|
||||||
try {
|
|
||||||
Thread.sleep(Config.DELAY_AFTER_GET_MESSAGES);
|
|
||||||
} catch (InterruptedException e) {}
|
|
||||||
Log.down();
|
|
||||||
}
|
|
||||||
Log.down();
|
|
||||||
Log.debug("Finished.");
|
|
||||||
|
|
||||||
prog.onMessageDownloadFinished();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Log.debug("Searching for missing messages in the db");
|
Log.debug("Searching for missing messages in the db");
|
||||||
@ -173,38 +143,49 @@ public class DownloadManager {
|
|||||||
LinkedList<Integer> ids = db.getMissingIDs();
|
LinkedList<Integer> ids = db.getMissingIDs();
|
||||||
count_missing = ids.size();
|
count_missing = ids.size();
|
||||||
System.out.println("Downloading " + ids.size() + " messages that are missing in your database.");
|
System.out.println("Downloading " + ids.size() + " messages that are missing in your database.");
|
||||||
prog.onMessageDownloadStart(ids.size());
|
|
||||||
Log.debug("Entering download loop");
|
downloadMessages(ids);
|
||||||
Log.up();
|
|
||||||
while (ids.size()>0) {
|
|
||||||
Log.debug("Loop");
|
|
||||||
Log.up();
|
|
||||||
TLIntVector vector = new TLIntVector();
|
|
||||||
for (int i=0; i<100; i++) {
|
|
||||||
if (ids.size()==0) break;
|
|
||||||
vector.add(ids.remove());
|
|
||||||
}
|
|
||||||
Log.debug("vector.size(): %d", vector.size());
|
|
||||||
Log.debug("ids.size(): %d", ids.size());
|
|
||||||
TLAbsMessages response = client.messagesGetMessages(vector);
|
|
||||||
prog.onMessageDownloaded(response.getMessages().size());
|
|
||||||
db.saveMessages(response.getMessages());
|
|
||||||
db.saveChats(response.getChats());
|
|
||||||
db.saveUsers(response.getUsers());
|
|
||||||
Log.debug("sleep");
|
|
||||||
try { Thread.sleep(Config.DELAY_AFTER_GET_MESSAGES); } catch (InterruptedException e) {}
|
|
||||||
Log.down();
|
|
||||||
}
|
|
||||||
Log.down();
|
|
||||||
prog.onMessageDownloadFinished();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Log.down();
|
|
||||||
Log.debug("Logging this run");
|
Log.debug("Logging this run");
|
||||||
db.logRun(Math.min(max_database_id + 1, max_message_id), max_message_id, count_missing);
|
db.logRun(Math.min(max_database_id + 1, max_message_id), max_message_id, count_missing);
|
||||||
Log.down();
|
Log.down();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void downloadMessages(List<Integer> ids) throws RpcErrorException, IOException {
|
||||||
|
prog.onMessageDownloadStart(ids.size());
|
||||||
|
|
||||||
|
Log.debug("Entering download loop");
|
||||||
|
Log.up();
|
||||||
|
while (ids.size()>0) {
|
||||||
|
Log.debug("Loop");
|
||||||
|
Log.up();
|
||||||
|
TLIntVector vector = new TLIntVector();
|
||||||
|
for (int i=0; i<100; i++) {
|
||||||
|
if (ids.size()==0) break;
|
||||||
|
vector.add(ids.remove(0));
|
||||||
|
}
|
||||||
|
Log.debug("vector.size(): %d", vector.size());
|
||||||
|
Log.debug("ids.size(): %d", ids.size());
|
||||||
|
|
||||||
|
TLAbsMessages response = client.messagesGetMessages(vector);
|
||||||
|
prog.onMessageDownloaded(response.getMessages().size());
|
||||||
|
db.saveMessages(response.getMessages(), Config.API_LAYER);
|
||||||
|
db.saveChats(response.getChats());
|
||||||
|
db.saveUsers(response.getUsers());
|
||||||
|
Log.debug("Sleeping");
|
||||||
|
try {
|
||||||
|
Thread.sleep(Config.DELAY_AFTER_GET_MESSAGES);
|
||||||
|
} catch (InterruptedException e) {}
|
||||||
|
Log.down();
|
||||||
|
}
|
||||||
|
Log.down();
|
||||||
|
Log.debug("Finished.");
|
||||||
|
|
||||||
|
prog.onMessageDownloadFinished();
|
||||||
|
}
|
||||||
|
|
||||||
public void downloadMedia() throws RpcErrorException, IOException {
|
public void downloadMedia() throws RpcErrorException, IOException {
|
||||||
boolean completed = true;
|
boolean completed = true;
|
||||||
do {
|
do {
|
||||||
@ -232,6 +213,14 @@ public class DownloadManager {
|
|||||||
|
|
||||||
private void _downloadMedia() throws RpcErrorException, IOException, TimeoutException {
|
private void _downloadMedia() throws RpcErrorException, IOException, TimeoutException {
|
||||||
Log.debug("This is _downloadMedia");
|
Log.debug("This is _downloadMedia");
|
||||||
|
Log.debug("Checking if there are messages in the DB with a too old API layer");
|
||||||
|
LinkedList<Integer> 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.");
|
||||||
|
Log.debug("Found %d messages", ids.size());
|
||||||
|
downloadMessages(ids);
|
||||||
|
}
|
||||||
|
|
||||||
LinkedList<TLMessage> messages = this.db.getMessagesWithMedia();
|
LinkedList<TLMessage> messages = this.db.getMessagesWithMedia();
|
||||||
Log.debug("Database returned %d messages with media", messages.size());
|
Log.debug("Database returned %d messages with media", messages.size());
|
||||||
prog.onMediaDownloadStart(messages.size());
|
prog.onMediaDownloadStart(messages.size());
|
||||||
@ -259,10 +248,9 @@ public class DownloadManager {
|
|||||||
prog.onMediaDownloadFinished();
|
prog.onMediaDownloadFinished();
|
||||||
}
|
}
|
||||||
|
|
||||||
private ArrayList<Integer> makeIdList(int start, int end) {
|
private List<Integer> makeIdList(int start, int end) {
|
||||||
if (start > end) throw new RuntimeException("start and end reversed");
|
LinkedList<Integer> a = new LinkedList<Integer>();
|
||||||
ArrayList<Integer> a = new ArrayList<Integer>(end - start + 1);
|
for (int i=start; i<=end; i++) a.add(i);
|
||||||
for (int i=0; i<=end-start; i++) a.add(start+i);
|
|
||||||
return a;
|
return a;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,15 +65,15 @@ class TelegramUpdateHandler implements UpdateCallback {
|
|||||||
if (db==null) return;
|
if (db==null) return;
|
||||||
if (debug) System.out.println("onShortChatMessage - " + m.getMessage());
|
if (debug) System.out.println("onShortChatMessage - " + m.getMessage());
|
||||||
TLMessage msg = new TLMessage(
|
TLMessage msg = new TLMessage(
|
||||||
m.getUnread(),
|
|
||||||
m.getOut(),
|
m.getOut(),
|
||||||
m.getMentioned(),
|
m.getMentioned(),
|
||||||
m.getMediaUnread(),
|
m.getMediaUnread(),
|
||||||
|
m.getSilent(),
|
||||||
|
false,
|
||||||
m.getId(),
|
m.getId(),
|
||||||
m.getFromId(),
|
m.getFromId(),
|
||||||
new TLPeerChat(m.getChatId()),
|
new TLPeerChat(m.getChatId()),
|
||||||
m.getFwdFromId(),
|
m.getFwdFrom(),
|
||||||
m.getFwdDate(),
|
|
||||||
m.getViaBotId(),
|
m.getViaBotId(),
|
||||||
m.getReplyToMsgId(),
|
m.getReplyToMsgId(),
|
||||||
m.getDate(),
|
m.getDate(),
|
||||||
@ -81,10 +81,11 @@ class TelegramUpdateHandler implements UpdateCallback {
|
|||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
m.getEntities(),
|
m.getEntities(),
|
||||||
|
null,
|
||||||
null);
|
null);
|
||||||
TLVector<TLAbsMessage> vector = new TLVector<TLAbsMessage>(TLAbsMessage.class);
|
TLVector<TLAbsMessage> vector = new TLVector<TLAbsMessage>(TLAbsMessage.class);
|
||||||
vector.add(msg);
|
vector.add(msg);
|
||||||
db.saveMessages(vector);
|
db.saveMessages(vector, Config.API_LAYER);
|
||||||
System.out.print('.');
|
System.out.print('.');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -100,15 +101,15 @@ class TelegramUpdateHandler implements UpdateCallback {
|
|||||||
from_id = m.getUserId();
|
from_id = m.getUserId();
|
||||||
}
|
}
|
||||||
TLMessage msg = new TLMessage(
|
TLMessage msg = new TLMessage(
|
||||||
m.getUnread(),
|
|
||||||
m.getOut(),
|
m.getOut(),
|
||||||
m.getMentioned(),
|
m.getMentioned(),
|
||||||
m.getMediaUnread(),
|
m.getMediaUnread(),
|
||||||
|
m.getSilent(),
|
||||||
|
false,
|
||||||
m.getId(),
|
m.getId(),
|
||||||
from_id,
|
from_id,
|
||||||
new TLPeerUser(to_id),
|
new TLPeerUser(to_id),
|
||||||
m.getFwdFromId(),
|
m.getFwdFrom(),
|
||||||
m.getFwdDate(),
|
|
||||||
m.getViaBotId(),
|
m.getViaBotId(),
|
||||||
m.getReplyToMsgId(),
|
m.getReplyToMsgId(),
|
||||||
m.getDate(),
|
m.getDate(),
|
||||||
@ -116,10 +117,11 @@ class TelegramUpdateHandler implements UpdateCallback {
|
|||||||
null,
|
null,
|
||||||
null,
|
null,
|
||||||
m.getEntities(),
|
m.getEntities(),
|
||||||
|
null,
|
||||||
null);
|
null);
|
||||||
TLVector<TLAbsMessage> vector = new TLVector<TLAbsMessage>(TLAbsMessage.class);
|
TLVector<TLAbsMessage> vector = new TLVector<TLAbsMessage>(TLAbsMessage.class);
|
||||||
vector.add(msg);
|
vector.add(msg);
|
||||||
db.saveMessages(vector);
|
db.saveMessages(vector, Config.API_LAYER);
|
||||||
System.out.print('.');
|
System.out.print('.');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,7 +133,7 @@ class TelegramUpdateHandler implements UpdateCallback {
|
|||||||
TLAbsMessage abs_msg = ((TLUpdateNewMessage)update).getMessage();
|
TLAbsMessage abs_msg = ((TLUpdateNewMessage)update).getMessage();
|
||||||
TLVector<TLAbsMessage> vector = new TLVector<TLAbsMessage>(TLAbsMessage.class);
|
TLVector<TLAbsMessage> vector = new TLVector<TLAbsMessage>(TLAbsMessage.class);
|
||||||
vector.add(abs_msg);
|
vector.add(abs_msg);
|
||||||
db.saveMessages(vector);
|
db.saveMessages(vector, Config.API_LAYER);
|
||||||
System.out.print('.');
|
System.out.print('.');
|
||||||
if (abs_msg instanceof TLMessage) {
|
if (abs_msg instanceof TLMessage) {
|
||||||
AbstractMediaFileManager fm = FileManagerFactory.getFileManager((TLMessage)abs_msg, user, client);
|
AbstractMediaFileManager fm = FileManagerFactory.getFileManager((TLMessage)abs_msg, user, client);
|
||||||
|
@ -17,7 +17,7 @@
|
|||||||
package de.fabianonline.telegram_backup;
|
package de.fabianonline.telegram_backup;
|
||||||
|
|
||||||
import com.github.badoualy.telegram.api.TelegramClient;
|
import com.github.badoualy.telegram.api.TelegramClient;
|
||||||
import com.github.badoualy.telegram.tl.api.auth.TLAbsSentCode;
|
import com.github.badoualy.telegram.tl.api.auth.TLSentCode;
|
||||||
import com.github.badoualy.telegram.tl.api.auth.TLAuthorization;
|
import com.github.badoualy.telegram.tl.api.auth.TLAuthorization;
|
||||||
import com.github.badoualy.telegram.tl.api.TLUser;
|
import com.github.badoualy.telegram.tl.api.TLUser;
|
||||||
import com.github.badoualy.telegram.tl.api.TLUserFull;
|
import com.github.badoualy.telegram.tl.api.TLUserFull;
|
||||||
@ -36,7 +36,7 @@ public class UserManager {
|
|||||||
public String phone = null;
|
public String phone = null;
|
||||||
private String code = null;
|
private String code = null;
|
||||||
private TelegramClient client = null;
|
private TelegramClient client = null;
|
||||||
private TLAbsSentCode sent_code = null;
|
private TLSentCode sent_code = null;
|
||||||
private TLAuthorization auth = null;
|
private TLAuthorization auth = null;
|
||||||
private boolean password_needed = false;
|
private boolean password_needed = false;
|
||||||
|
|
||||||
@ -54,7 +54,7 @@ public class UserManager {
|
|||||||
|
|
||||||
public void sendCodeToPhoneNumber(String number) throws RpcErrorException, IOException {
|
public void sendCodeToPhoneNumber(String number) throws RpcErrorException, IOException {
|
||||||
this.phone = number;
|
this.phone = number;
|
||||||
this.sent_code = this.client.authSendCode(this.phone, 5);
|
this.sent_code = this.client.authSendCode(false, this.phone, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void verifyCode(String code) throws RpcErrorException, IOException {
|
public void verifyCode(String code) throws RpcErrorException, IOException {
|
||||||
@ -72,7 +72,7 @@ public class UserManager {
|
|||||||
|
|
||||||
public void verifyPassword(String pw) throws RpcErrorException, IOException {
|
public void verifyPassword(String pw) throws RpcErrorException, IOException {
|
||||||
byte[] password = pw.getBytes("UTF-8");
|
byte[] password = pw.getBytes("UTF-8");
|
||||||
byte[] salt = ((TLPassword)client.executeRpcQuery(new TLRequestAccountGetPasswordWithCurrentSalt())).getCurrentSalt().getData();
|
byte[] salt = ((TLPassword)client.accountGetPassword()).getCurrentSalt().getData();
|
||||||
MessageDigest md = null;
|
MessageDigest md = null;
|
||||||
try {
|
try {
|
||||||
md = MessageDigest.getInstance("SHA-256");
|
md = MessageDigest.getInstance("SHA-256");
|
||||||
|
@ -1,73 +0,0 @@
|
|||||||
/* Telegram_Backup
|
|
||||||
* Copyright (C) 2016 Fabian Schlenz
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
||||||
|
|
||||||
package de.fabianonline.telegram_backup.mediafilemanager;
|
|
||||||
|
|
||||||
import de.fabianonline.telegram_backup.UserManager;
|
|
||||||
import de.fabianonline.telegram_backup.Database;
|
|
||||||
import de.fabianonline.telegram_backup.StickerConverter;
|
|
||||||
import de.fabianonline.telegram_backup.DownloadProgressInterface;
|
|
||||||
import de.fabianonline.telegram_backup.DownloadManager;
|
|
||||||
|
|
||||||
import com.github.badoualy.telegram.api.TelegramClient;
|
|
||||||
import com.github.badoualy.telegram.tl.core.TLIntVector;
|
|
||||||
import com.github.badoualy.telegram.tl.core.TLObject;
|
|
||||||
import com.github.badoualy.telegram.tl.api.messages.TLAbsMessages;
|
|
||||||
import com.github.badoualy.telegram.tl.api.messages.TLAbsDialogs;
|
|
||||||
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 java.io.IOException;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.concurrent.TimeoutException;
|
|
||||||
|
|
||||||
import org.apache.commons.io.FileUtils;
|
|
||||||
|
|
||||||
public class AudioFileManager extends AbstractMediaFileManager {
|
|
||||||
protected TLAudio audio;
|
|
||||||
|
|
||||||
public AudioFileManager(TLMessage msg, UserManager user, TelegramClient client) {
|
|
||||||
super(msg, user, client);
|
|
||||||
TLAbsAudio a = ((TLMessageMediaAudio)msg.getMedia()).getAudio();
|
|
||||||
if (a instanceof TLAudio) {
|
|
||||||
this.audio = (TLAudio) a;
|
|
||||||
} else if (a instanceof TLAudioEmpty) {
|
|
||||||
this.isEmpty = true;
|
|
||||||
} else {
|
|
||||||
throwUnexpectedObjectError(a);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getSize() { return audio.getSize(); }
|
|
||||||
|
|
||||||
public String getExtension() {
|
|
||||||
return extensionFromMimetype(audio.getMimeType());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void download() throws RpcErrorException, IOException {
|
|
||||||
DownloadManager.downloadFile(client, getTargetPathAndFilename(), getSize(), audio.getDcId(), audio.getId(), audio.getAccessHash());
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getLetter() { return "a"; }
|
|
||||||
public String getName() { return "audio"; }
|
|
||||||
public String getDescription() { return "Audio"; }
|
|
||||||
}
|
|
@ -55,10 +55,6 @@ public class FileManagerFactory {
|
|||||||
return new StickerFileManager(m, u, c);
|
return new StickerFileManager(m, u, c);
|
||||||
}
|
}
|
||||||
return d;
|
return d;
|
||||||
} else if (media instanceof TLMessageMediaVideo) {
|
|
||||||
return new VideoFileManager(m, u, c);
|
|
||||||
} else if (media instanceof TLMessageMediaAudio) {
|
|
||||||
return new AudioFileManager(m, u, c);
|
|
||||||
} else if (media instanceof TLMessageMediaGeo) {
|
} else if (media instanceof TLMessageMediaGeo) {
|
||||||
return new GeoFileManager(m, u, c);
|
return new GeoFileManager(m, u, c);
|
||||||
} else if (media instanceof TLMessageMediaEmpty) {
|
} else if (media instanceof TLMessageMediaEmpty) {
|
||||||
|
@ -1,73 +0,0 @@
|
|||||||
/* Telegram_Backup
|
|
||||||
* Copyright (C) 2016 Fabian Schlenz
|
|
||||||
*
|
|
||||||
* 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
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
|
||||||
|
|
||||||
package de.fabianonline.telegram_backup.mediafilemanager;
|
|
||||||
|
|
||||||
import de.fabianonline.telegram_backup.UserManager;
|
|
||||||
import de.fabianonline.telegram_backup.Database;
|
|
||||||
import de.fabianonline.telegram_backup.StickerConverter;
|
|
||||||
import de.fabianonline.telegram_backup.DownloadProgressInterface;
|
|
||||||
import de.fabianonline.telegram_backup.DownloadManager;
|
|
||||||
|
|
||||||
import com.github.badoualy.telegram.api.TelegramClient;
|
|
||||||
import com.github.badoualy.telegram.tl.core.TLIntVector;
|
|
||||||
import com.github.badoualy.telegram.tl.core.TLObject;
|
|
||||||
import com.github.badoualy.telegram.tl.api.messages.TLAbsMessages;
|
|
||||||
import com.github.badoualy.telegram.tl.api.messages.TLAbsDialogs;
|
|
||||||
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 java.io.IOException;
|
|
||||||
import java.io.File;
|
|
||||||
import java.io.FileOutputStream;
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.LinkedList;
|
|
||||||
import java.net.URL;
|
|
||||||
import java.util.concurrent.TimeoutException;
|
|
||||||
|
|
||||||
import org.apache.commons.io.FileUtils;
|
|
||||||
|
|
||||||
public class VideoFileManager extends AbstractMediaFileManager {
|
|
||||||
protected TLVideo video;
|
|
||||||
|
|
||||||
public VideoFileManager(TLMessage msg, UserManager user, TelegramClient client) {
|
|
||||||
super(msg, user, client);
|
|
||||||
TLAbsVideo v = ((TLMessageMediaVideo)msg.getMedia()).getVideo();
|
|
||||||
if (v instanceof TLVideo) {
|
|
||||||
this.video = (TLVideo) v;
|
|
||||||
} else if (v instanceof TLVideoEmpty) {
|
|
||||||
this.isEmpty = true;
|
|
||||||
} else {
|
|
||||||
throwUnexpectedObjectError(v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public int getSize() { return video.getSize(); }
|
|
||||||
|
|
||||||
public String getExtension() {
|
|
||||||
return extensionFromMimetype(video.getMimeType());
|
|
||||||
}
|
|
||||||
|
|
||||||
public void download() throws RpcErrorException, IOException {
|
|
||||||
DownloadManager.downloadFile(client, getTargetPathAndFilename(), getSize(), video.getDcId(), video.getId(), video.getAccessHash());
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getLetter() { return "v"; }
|
|
||||||
public String getName() { return "video"; }
|
|
||||||
public String getDescription() { return "Video"; }
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user