mirror of
https://github.com/fabianonline/telegram_backup.git
synced 2025-06-30 04:16:26 +00:00
Massively better exports. Stylesheets and Stats added.
This commit is contained in:
@ -414,24 +414,31 @@ public class Database {
|
||||
return list;
|
||||
} catch (SQLException e) { throw new RuntimeException(e); }
|
||||
}
|
||||
|
||||
|
||||
public HashMap<String, Integer> getMessageTypesWithCount() {
|
||||
return getMessageTypesWithCount(new GlobalChat());
|
||||
}
|
||||
|
||||
public HashMap<String, Integer> getMessageTypesWithCount(AbstractChat c) {
|
||||
HashMap<String, Integer> map = new HashMap<String, Integer>();
|
||||
try {
|
||||
ResultSet rs = stmt.executeQuery("SELECT message_type, COUNT(id) FROM messages GROUP BY message_type");
|
||||
ResultSet rs = stmt.executeQuery("SELECT message_type, COUNT(id) FROM messages WHERE " + c.getQuery() + " GROUP BY message_type");
|
||||
while (rs.next()) {
|
||||
map.put(rs.getString(1), rs.getInt(2));
|
||||
map.put("count.messages.type." + rs.getString(1), rs.getInt(2));
|
||||
}
|
||||
return map;
|
||||
} catch (Exception e) { throw new RuntimeException(e); }
|
||||
}
|
||||
|
||||
public HashMap<String, Integer> getMessageMediaTypesWithCount() {
|
||||
return getMessageMediaTypesWithCount(new GlobalChat());
|
||||
}
|
||||
|
||||
public HashMap<String, Integer> getMessageMediaTypesWithCount(AbstractChat c) {
|
||||
HashMap<String, Integer> map = new HashMap<String, Integer>();
|
||||
try {
|
||||
int count = 0;
|
||||
ResultSet rs = stmt.executeQuery("SELECT media_type, COUNT(id) FROM messages GROUP BY media_type");
|
||||
ResultSet rs = stmt.executeQuery("SELECT media_type, COUNT(id) FROM messages WHERE " + c.getQuery() + " GROUP BY media_type");
|
||||
while (rs.next()) {
|
||||
String s = rs.getString(1);
|
||||
if (s==null) {
|
||||
@ -439,19 +446,49 @@ public class Database {
|
||||
} else {
|
||||
count += rs.getInt(2);
|
||||
}
|
||||
map.put(s, rs.getInt(2));
|
||||
map.put("count.messages.media_type." + s, rs.getInt(2));
|
||||
}
|
||||
map.put("any", count);
|
||||
map.put("count.messages.media_type.any", count);
|
||||
return map;
|
||||
} catch (Exception e) { throw new RuntimeException(e); }
|
||||
}
|
||||
|
||||
public HashMap<String, Object> getMessageAuthorsWithCount() {
|
||||
return getMessageAuthorsWithCount(new GlobalChat());
|
||||
}
|
||||
|
||||
public HashMap<String, Object> getMessageAuthorsWithCount(AbstractChat c) {
|
||||
HashMap<String, Object> map = new HashMap<String, Object>();
|
||||
HashMap<User, Integer> user_map = new HashMap<User, Integer>();
|
||||
int count_others = 0;
|
||||
try {
|
||||
ResultSet rs = stmt.executeQuery("SELECT users.id, users.first_name, users.last_name, users.username, COUNT(messages.id) "+
|
||||
"FROM messages, users WHERE users.id=messages.sender_id AND " + c.getQuery() + " GROUP BY sender_id");
|
||||
while (rs.next()) {
|
||||
User u = new User(rs.getInt(1), rs.getString(2), rs.getString(3), rs.getString(4));
|
||||
if (u.isMe) {
|
||||
map.put("authors.count.me", rs.getInt(5));
|
||||
} else {
|
||||
user_map.put(u, rs.getInt(5));
|
||||
count_others += rs.getInt(5);
|
||||
}
|
||||
}
|
||||
map.put("authors.count.others", count_others);
|
||||
map.put("authors.all", user_map.entrySet());
|
||||
return map;
|
||||
} catch (Exception e) { throw new RuntimeException(e); }
|
||||
}
|
||||
|
||||
public int[][] getMessageTimesMatrix() {
|
||||
return getMessageTimesMatrix(new GlobalChat());
|
||||
}
|
||||
|
||||
public int[][] getMessageTimesMatrix(Integer dialog_id, Integer chat_id) {
|
||||
public int[][] getMessageTimesMatrix(AbstractChat c) {
|
||||
int result[][] = new int[7][24];
|
||||
try {
|
||||
ResultSet rs = stmt.executeQuery("SELECT STRFTIME('%w', time, 'unixepoch') as DAY, " +
|
||||
"STRFTIME('%H', time, 'unixepoch') AS hour, " +
|
||||
"COUNT(id) FROM messages GROUP BY hour, day " +
|
||||
"COUNT(id) FROM messages WHERE " + c.getQuery() + " GROUP BY hour, day " +
|
||||
"ORDER BY hour, day");
|
||||
while (rs.next()) {
|
||||
result[rs.getInt(1) == 0 ? 6 : rs.getInt(1)-1][rs.getInt(2)] = rs.getInt(3);
|
||||
@ -497,29 +534,25 @@ public class Database {
|
||||
}
|
||||
}
|
||||
|
||||
public LinkedList<HashMap<String, Object>> getMessagesForExport(Dialog d) {
|
||||
return getMessagesForExport("dialog_id", d.id);
|
||||
}
|
||||
|
||||
public LinkedList<HashMap<String, Object>> getMessagesForExport(Chat c) {
|
||||
return getMessagesForExport("chat_id", c.id);
|
||||
}
|
||||
|
||||
private LinkedList<HashMap<String, Object>> getMessagesForExport(String type, Integer id) {
|
||||
public LinkedList<HashMap<String, Object>> getMessagesForExport(AbstractChat c) {
|
||||
try {
|
||||
|
||||
ResultSet rs = stmt.executeQuery("SELECT messages.id as message_id, text, time*1000 as time, has_media, " +
|
||||
"media_type, media_file, media_size, users.first_name as user_first_name, users.last_name as user_last_name, " +
|
||||
"users.username as user_username, users.id as user_id, " +
|
||||
"users_fwd.first_name as user_fwd_first_name, users_fwd.last_name as user_fwd_last_name, users_fwd.username as user_fwd_username " +
|
||||
"FROM messages, users LEFT JOIN users AS users_fwd ON users_fwd.id=fwd_from_id WHERE " +
|
||||
"users.id=messages.sender_id AND " + type + "=" + id + " " +
|
||||
"users.id=messages.sender_id AND " + c.getQuery() + " " +
|
||||
"ORDER BY messages.id");
|
||||
SimpleDateFormat format_time = new SimpleDateFormat("HH:mm:ss");
|
||||
SimpleDateFormat format_date = new SimpleDateFormat("d MMM yy");
|
||||
ResultSetMetaData meta = rs.getMetaData();
|
||||
int columns = meta.getColumnCount();
|
||||
LinkedList<HashMap<String, Object>> list = new LinkedList<HashMap<String, Object>>();
|
||||
|
||||
Integer count=0;
|
||||
String old_date = null;
|
||||
Integer old_user = null;
|
||||
while (rs.next()) {
|
||||
HashMap<String, Object> h = new HashMap<String, Object>(columns);
|
||||
for (int i=1; i<=columns; i++) {
|
||||
@ -535,9 +568,13 @@ public class Database {
|
||||
}
|
||||
h.put("from_me", rs.getInt("user_id")==user_manager.getUser().getId());
|
||||
h.put("is_new_date", !date.equals(old_date));
|
||||
h.put("odd_even", (count%2==0) ? "even" : "odd");
|
||||
h.put("same_user", old_user!=null && rs.getInt("user_id")==old_user);
|
||||
old_user = rs.getInt("user_id");
|
||||
old_date = date;
|
||||
|
||||
list.add(h);
|
||||
count++;
|
||||
}
|
||||
rs.close();
|
||||
return list;
|
||||
@ -564,9 +601,11 @@ public class Database {
|
||||
|
||||
|
||||
|
||||
public abstract class AbstractChat {
|
||||
public abstract String getQuery();
|
||||
}
|
||||
|
||||
|
||||
public class Dialog {
|
||||
public class Dialog extends AbstractChat{
|
||||
public int id;
|
||||
public String first_name;
|
||||
public String last_name;
|
||||
@ -580,9 +619,11 @@ public class Database {
|
||||
this.username = username;
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public String getQuery() { return "dialog_id=" + id; }
|
||||
}
|
||||
|
||||
public class Chat {
|
||||
public class Chat extends AbstractChat {
|
||||
public int id;
|
||||
public String name;
|
||||
public int count;
|
||||
@ -592,5 +633,24 @@ public class Database {
|
||||
this.name = name;
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
public String getQuery() {return "chat_id=" + id; }
|
||||
}
|
||||
|
||||
public class User {
|
||||
public String name;
|
||||
public boolean isMe;
|
||||
|
||||
public User(int id, String first_name, String last_name, String username) {
|
||||
isMe = id==user_manager.getUser().getId();
|
||||
StringBuilder s = new StringBuilder();
|
||||
if (first_name!=null) s.append(first_name + " ");
|
||||
if (last_name!=null) s.append(last_name);
|
||||
name = s.toString().trim();
|
||||
}
|
||||
}
|
||||
|
||||
public class GlobalChat extends AbstractChat {
|
||||
public String getQuery() { return "1=1"; }
|
||||
}
|
||||
}
|
||||
|
@ -24,30 +24,41 @@ import java.io.PrintWriter;
|
||||
import java.io.BufferedWriter;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.net.URL;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import java.util.LinkedList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.github.mustachejava.DefaultMustacheFactory;
|
||||
import com.github.mustachejava.Mustache;
|
||||
import com.github.mustachejava.MustacheFactory;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class HTMLExporter {
|
||||
private static Logger logger = LoggerFactory.getLogger(HTMLExporter.class);
|
||||
|
||||
public void export(UserManager user) {
|
||||
try {
|
||||
Database db = new Database(user, null);
|
||||
|
||||
// Create base dir
|
||||
logger.debug("Creating base dir");
|
||||
String base = user.getFileBase() + "files" + File.separatorChar;
|
||||
new File(base).mkdirs();
|
||||
new File(base + "dialogs").mkdirs();
|
||||
|
||||
logger.debug("Fetching dialogs");
|
||||
LinkedList<Database.Dialog> dialogs = db.getListOfDialogsForExport();
|
||||
logger.debug("Fetching chats");
|
||||
LinkedList<Database.Chat> chats = db.getListOfChatsForExport();
|
||||
|
||||
|
||||
logger.debug("Generating index.html");
|
||||
HashMap<String, Object> scope = new HashMap<String, Object>();
|
||||
scope.put("user", user.getUser());
|
||||
scope.put("user", user);
|
||||
scope.put("dialogs", dialogs);
|
||||
scope.put("chats", chats);
|
||||
|
||||
@ -66,18 +77,11 @@ public class HTMLExporter {
|
||||
|
||||
scope.put("count.messages.from_me", db.getMessagesFromUserCount());
|
||||
|
||||
scope.put("heatmap_data", intArrayToString(db.getMessageTimesMatrix(null, null)));
|
||||
scope.put("heatmap_data", intArrayToString(db.getMessageTimesMatrix()));
|
||||
|
||||
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());
|
||||
}
|
||||
scope.putAll(db.getMessageAuthorsWithCount());
|
||||
scope.putAll(db.getMessageTypesWithCount());
|
||||
scope.putAll(db.getMessageMediaTypesWithCount());
|
||||
|
||||
MustacheFactory mf = new DefaultMustacheFactory();
|
||||
Mustache mustache = mf.compile("templates/html/index.mustache");
|
||||
@ -87,27 +91,44 @@ public class HTMLExporter {
|
||||
|
||||
mustache = mf.compile("templates/html/chat.mustache");
|
||||
|
||||
logger.debug("Generating dialog pages");
|
||||
for (Database.Dialog d : dialogs) {
|
||||
LinkedList<HashMap<String, Object>> messages = db.getMessagesForExport(d);
|
||||
scope.clear();
|
||||
scope.put("dialog", d);
|
||||
scope.put("messages", messages);
|
||||
|
||||
scope.putAll(db.getMessageAuthorsWithCount(d));
|
||||
scope.put("heatmap_data", intArrayToString(db.getMessageTimesMatrix(d)));
|
||||
scope.putAll(db.getMessageTypesWithCount(d));
|
||||
scope.putAll(db.getMessageMediaTypesWithCount(d));
|
||||
|
||||
w = new FileWriter(base + "dialogs" + File.separatorChar + "user_" + d.id + ".html");
|
||||
mustache.execute(w, scope);
|
||||
w.close();
|
||||
}
|
||||
|
||||
logger.debug("Generating chat pages");
|
||||
for (Database.Chat c : chats) {
|
||||
LinkedList<HashMap<String, Object>> messages = db.getMessagesForExport(c);
|
||||
scope.clear();
|
||||
scope.put("chat", c);
|
||||
scope.put("messages", messages);
|
||||
|
||||
scope.put("heatmap_data", intArrayToString(db.getMessageTimesMatrix(c)));
|
||||
scope.putAll(db.getMessageTypesWithCount(c));
|
||||
scope.putAll(db.getMessageMediaTypesWithCount(c));
|
||||
|
||||
w = new FileWriter(base + "dialogs" + File.separatorChar + "chat_" + c.id + ".html");
|
||||
mustache.execute(w, scope);
|
||||
w.close();
|
||||
}
|
||||
|
||||
logger.debug("Generating additional files");
|
||||
// Copy CSS
|
||||
URL cssFile = getClass().getResource("/templates/html/style.css");
|
||||
File dest = new File(base + "style.css");
|
||||
FileUtils.copyURLToFile(cssFile, dest);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
throw new RuntimeException("Exception above!");
|
||||
@ -126,4 +147,13 @@ public class HTMLExporter {
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private String mapToString(Map<String, Integer> map) {
|
||||
StringBuilder sb = new StringBuilder("[");
|
||||
for (Map.Entry<String, Integer> entry : map.entrySet()) {
|
||||
sb.append("['" + entry.getKey() + "', " + entry.getValue() + "],");
|
||||
}
|
||||
sb.append("]");
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
|
@ -1,85 +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.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