1
0
mirror of https://github.com/fabianonline/telegram_backup.git synced 2024-11-22 16:56:16 +00:00

More and nicer exports. Mustache is nice.

This commit is contained in:
Fabian Schlenz 2016-07-08 14:21:11 +02:00
parent 4e227e55c8
commit 8b361d5547
3 changed files with 81 additions and 17 deletions

View File

@ -36,8 +36,10 @@ import java.io.IOException;
import java.util.LinkedList;
import java.util.LinkedHashMap;
import java.util.HashMap;
import java.util.Date;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.text.SimpleDateFormat;
import de.fabianonline.telegram_backup.mediafilemanager.AbstractMediaFileManager;
import de.fabianonline.telegram_backup.mediafilemanager.FileManagerFactory;
@ -586,20 +588,36 @@ public class Database {
private LinkedList<HashMap<String, Object>> getMessagesForExport(String type, Integer id) {
try {
ResultSet rs = stmt.executeQuery("SELECT messages.id as mid, text, time*1000 as t, has_media, " +
"media_type, media_file, media_size, users.first_name, users.last_name, users.username, " +
"users_fwd.first_name, users_fwd.last_name, users_fwd.username " +
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 + " " +
"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>>();
String old_date = null;
while (rs.next()) {
HashMap<String, Object> h = new HashMap<String, Object>(columns);
for (int i=1; i<=columns; i++) {
h.put(meta.getColumnName(i), rs.getObject(i));
}
// Additional values to make up for Mustache's inability to format dates
Date d = rs.getTime("time");
String date = format_date.format(d);
h.put("formatted_time", format_time.format(d));
h.put("formatted_date", date);
if (rs.getString("media_type")!=null) {
h.put("media_" + rs.getString("media_type"), true);
}
h.put("from_me", rs.getInt("user_id")==user_manager.getUser().getId());
h.put("is_new_date", !date.equals(old_date));
old_date = date;
list.add(h);
}
rs.close();

View File

@ -56,25 +56,32 @@ public class HTMLExporter {
mustache.execute(w, scope);
w.close();
for (Database.Dialog d : dialogs) {
//LinkedList<HashMap<String, Object>> messages = db.getMessagesForTemplate(dialog);
}
System.exit(0);
/*
String filename = base + "dialogs" + File.separatorChar + "user_" + dialog.id + ".html";
final PrintWriter chatfile = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
db.getMessagesForExport(dialog, new ChatLineWriter(chatfile));
chatfile.close();
mustache = mf.compile("templates/html/chat.mustache");
String filename = base + "dialogs" + File.separatorChar + "chat_" + chat.id + ".html";
final PrintWriter chatfile = new PrintWriter(new BufferedWriter(new FileWriter(filename)));
db.getMessagesForExport(chat, new ChatLineWriter(chatfile));
chatfile.close();
*/
for (Database.Dialog d : dialogs) {
LinkedList<HashMap<String, Object>> messages = db.getMessagesForExport(d);
scope.clear();
scope.put("dialog", d);
scope.put("messages", messages);
w = new FileWriter(base + "dialogs" + File.separatorChar + "user_" + d.id + ".html");
mustache.execute(w, scope);
w.close();
}
for (Database.Chat c : chats) {
LinkedList<HashMap<String, Object>> messages = db.getMessagesForExport(c);
scope.clear();
scope.put("chat", c);
scope.put("messages", messages);
w = new FileWriter(base + "dialogs" + File.separatorChar + "chat_" + c.id + ".html");
mustache.execute(w, scope);
w.close();
}
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Exception above!");
}
}
// w.println("" + String.format("%1$tF %1$tT", msg.time) + " - " + msg.text + "<br>");
}

View File

@ -0,0 +1,39 @@
<!DOCTYPE html>
<html>
<head>
<title>Telegram Backup for {{user.getUserString}}</title>
</head>
<body>
<h1>Telegram Backup</h1>
{{#dialog}}
<h2>Dialog with {{first_name}} {{last_name}} {{#username}}(@{{username}}){{/username}}</h2>
{{/dialog}}
{{#chat}}
<h2>Chat {{name}}</h2>
{{/chat}}
<a href="../index.html">Back to the overview</a>
<ul class="messages">
{{#messages}}
{{#is_new_date}}
<li class="date">
{{formatted_date}}
</li>
{{/is_new_date}}
<li class="message {{#from_me}}from-me{{/from_me}}" data-message-id="{{message_id}}" data-media="{{media_type}}">
<span class="sender">{{user_first_name}}</span>
<span class="time">{{formatted_time}}</span>
{{#text}}<span class="text">{{text}}</span>{{/text}}
{{#media_sticker}}<span class="sticker"><img src="../../../../stickers/{{media_file}}" /></span>{{/media_sticker}}
{{#media_photo}}<span class="photo"><img src="../../../files/{{media_file}}" /></span>{{/media_photo}}
{{#media_document}}<span class="document"><a href="../../../files/{{media_file}}">{{media_file}}</a></span>{{/media_document}}
</li>
{{/messages}}
</ul>
<a href="../index.html">Back to the overview</a>
</body>
</html>