mirror of
https://github.com/fabianonline/telegram_backup.git
synced 2025-06-30 04:16:26 +00:00
WIP: Added StatsExporter.
This commit is contained in:
@ -18,6 +18,7 @@ package de.fabianonline.telegram_backup;
|
||||
|
||||
import de.fabianonline.telegram_backup.TelegramUpdateHandler;
|
||||
import de.fabianonline.telegram_backup.exporter.HTMLExporter;
|
||||
import de.fabianonline.telegram_backup.exporter.StatsExporter;
|
||||
|
||||
import com.github.badoualy.telegram.api.Kotlogram;
|
||||
import com.github.badoualy.telegram.api.TelegramApp;
|
||||
@ -95,6 +96,9 @@ public class CommandLineController {
|
||||
if (options.export.toLowerCase().equals("html")) {
|
||||
(new HTMLExporter()).export(user);
|
||||
System.exit(0);
|
||||
} else if (options.export.toLowerCase().equals("stats")) {
|
||||
(new StatsExporter()).export(user);
|
||||
System.exit(0);
|
||||
} else {
|
||||
show_error("Unknown export format.");
|
||||
}
|
||||
|
@ -543,6 +543,41 @@ public class Database {
|
||||
}
|
||||
}
|
||||
|
||||
public int getMessagesFromUserCount() {
|
||||
try {
|
||||
ResultSet rs = stmt.executeQuery("SELECT COUNT(*) FROM messages WHERE sender_id=" + user_manager.getUser().getId());
|
||||
rs.next();
|
||||
return rs.getInt(1);
|
||||
} catch (SQLException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public HashMap<String, Integer> getMessageTypesWithCount() {
|
||||
HashMap<String, Integer> map = new HashMap<String, Integer>();
|
||||
try {
|
||||
ResultSet rs = stmt.executeQuery("SELECT message_type, COUNT(id) FROM messages GROUP BY message_type");
|
||||
while (rs.next()) {
|
||||
map.put(rs.getString(1), rs.getInt(2));
|
||||
}
|
||||
return map;
|
||||
} catch (Exception e) { throw new RuntimeException(e); }
|
||||
}
|
||||
|
||||
public HashMap<String, Integer> getMessageMediaTypesWithCount() {
|
||||
HashMap<String, Integer> map = new HashMap<String, Integer>();
|
||||
try {
|
||||
ResultSet rs = stmt.executeQuery("SELECT media_type, COUNT(id) FROM messages GROUP BY media_type");
|
||||
while (rs.next()) {
|
||||
String s = rs.getString(1);
|
||||
if (s==null) s="null";
|
||||
map.put(s, rs.getInt(2));
|
||||
}
|
||||
return map;
|
||||
} catch (Exception e) { throw new RuntimeException(e); }
|
||||
}
|
||||
|
||||
|
||||
public LinkedList<Chat> getListOfChatsForExport() {
|
||||
LinkedList<Chat> list = new LinkedList<Chat>();
|
||||
try {
|
||||
@ -560,6 +595,7 @@ public class Database {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public LinkedList<Dialog> getListOfDialogsForExport() {
|
||||
LinkedList<Dialog> list = new LinkedList<Dialog>();
|
||||
try {
|
||||
|
@ -0,0 +1,85 @@
|
||||
/* 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.exporter;
|
||||
|
||||
import de.fabianonline.telegram_backup.UserManager;
|
||||
import de.fabianonline.telegram_backup.Database;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.PrintWriter;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import com.github.mustachejava.DefaultMustacheFactory;
|
||||
import com.github.mustachejava.Mustache;
|
||||
import com.github.mustachejava.MustacheFactory;
|
||||
|
||||
public class StatsExporter {
|
||||
public void export(UserManager user) {
|
||||
try {
|
||||
Database db = new Database(user, null);
|
||||
|
||||
// Create base dir
|
||||
String base = user.getFileBase() + "export" + File.separatorChar + "stats" + File.separatorChar;
|
||||
new File(base).mkdirs();
|
||||
|
||||
HashMap<String, Object> scope = new HashMap<String, Object>();
|
||||
|
||||
// Collect data
|
||||
LinkedList<Database.Dialog> dialogs = db.getListOfDialogsForExport();
|
||||
LinkedList<Database.Chat> chats = db.getListOfChatsForExport();
|
||||
|
||||
scope.put("count.chats", chats.size());
|
||||
scope.put("count.dialogs", dialogs.size());
|
||||
|
||||
int count_messages_chats = 0;
|
||||
int count_messages_dialogs = 0;
|
||||
for (Database.Chat c : chats) count_messages_chats += c.count;
|
||||
for (Database.Dialog d : dialogs) count_messages_dialogs += d.count;
|
||||
|
||||
scope.put("count.messages", count_messages_chats + count_messages_dialogs);
|
||||
scope.put("count.messages.chats", count_messages_chats);
|
||||
scope.put("count.messages.dialogs", count_messages_dialogs);
|
||||
|
||||
scope.put("count.messages.from_me", db.getMessagesFromUserCount());
|
||||
|
||||
HashMap<String, Integer> types;
|
||||
types = db.getMessageTypesWithCount();
|
||||
for (Map.Entry<String, Integer> entry : types.entrySet()) {
|
||||
scope.put("count.messages.type." + entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
types = db.getMessageMediaTypesWithCount();
|
||||
for (Map.Entry<String, Integer> entry : types.entrySet()) {
|
||||
scope.put("count.messages.media_type." + entry.getKey(), entry.getValue());
|
||||
}
|
||||
|
||||
MustacheFactory mf = new DefaultMustacheFactory();
|
||||
Mustache mustache = mf.compile("templates/stats/index.mustache");
|
||||
FileWriter w = new FileWriter(base + "index.html");
|
||||
mustache.execute(w, scope);
|
||||
w.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("Exception above!");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user