WIP: Using mustache as template engine for HTML exporter.

This commit is contained in:
Fabian Schlenz 2016-07-07 06:40:00 +02:00
parent c62e6db3d3
commit ecbca30a10
7 changed files with 78 additions and 34 deletions

View File

@ -94,5 +94,9 @@ least the folder `export` contains exported data.
## Attribution
This tool uses libraries from other developers which are covered by other licenses,
which are:
* [Kotlogram](https://github.com/badoualy/kotlogram) by Yannick Badoual, licensed under MIT License.
* [SQLite JDBC](https://bitbucket.org/xerial/sqlite-jdbc) by Taro L. Saito, licensed under Apache License version 2.0.
* [Kotlogram](https://github.com/badoualy/kotlogram) by Yannick Badoual, licensed
under MIT License.
* [SQLite JDBC](https://bitbucket.org/xerial/sqlite-jdbc) by Taro L. Saito,
licensed under Apache License version 2.0.
* [Mustache.java](https://github.com/spullara/mustache.java) by RightTime,
Inc., licensed under Apache License version 2.0.

View File

@ -16,6 +16,7 @@ repositories {
dependencies {
compile 'com.github.badoualy:kotlogram:0.0.6'
compile 'org.xerial:sqlite-jdbc:3.8.11.2'
compile 'com.github.spullara.mustache.java:compiler:0.8.18'
}
run {
@ -30,4 +31,8 @@ jar {
from {
configurations.compile.collect { it.isDirectory() ? it : zipTree(it) }
}
includes [
"*.mustache"
]
}

View File

@ -17,6 +17,7 @@
package de.fabianonline.telegram_backup;
import de.fabianonline.telegram_backup.TelegramUpdateHandler;
import de.fabianonline.telegram_backup.exporter.HTMLExporter;
import com.github.badoualy.telegram.api.Kotlogram;
import com.github.badoualy.telegram.api.TelegramApp;

View File

@ -32,10 +32,7 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.LinkedList;
import de.fabianonline.telegram_backup.UserManager;
import de.fabianonline.telegram_backup.StickerConverter;
class Database {
public class Database {
private Connection conn;
private Statement stmt;
private UserManager user_manager;

View File

@ -31,7 +31,7 @@ import java.security.NoSuchAlgorithmException;
import java.io.IOException;
import java.io.File;
class UserManager {
public class UserManager {
public TLUser user = null;
public String phone = null;
private String code = null;

View File

@ -14,7 +14,7 @@
* 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;
package de.fabianonline.telegram_backup.exporter;
import de.fabianonline.telegram_backup.UserManager;
import de.fabianonline.telegram_backup.Database;
@ -25,8 +25,13 @@ import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.HashMap;
class HTMLExporter {
import com.github.mustachejava.DefaultMustacheFactory;
import com.github.mustachejava.Mustache;
import com.github.mustachejava.MustacheFactory;
public class HTMLExporter {
public void export(UserManager user) {
try {
Database db = new Database(user);
@ -36,40 +41,36 @@ class HTMLExporter {
new File(base).mkdirs();
new File(base + "dialogs").mkdirs();
PrintWriter index = new PrintWriter(new BufferedWriter(new FileWriter(base + "index.html")));
index.println("<!DOCTYPE html>");
index.println("<html>");
index.println("<head>");
index.println("<title>Telegram Backup for " + user.getUserString() + "</title>");
index.println("</head>");
index.println("<body>");
index.println("<h1>Telegram Backup</h1>");
index.println("<h2>" + user.getUserString() + "</h2>");
LinkedList<Database.Dialog> dialogs = db.getListOfDialogsForExport();
index.println("<h3>Dialogs</h3>");
index.println("<ul>");
for (Database.Dialog dialog : dialogs) {
index.println("<li><a href='dialogs/user_" + dialog.id + ".html'>" + dialog.first_name + " " + (dialog.last_name!=null ? dialog.last_name : "") + "</a> <span class='count'>(" + dialog.count + ")</span></li>");
LinkedList<Database.Chat> chats = db.getListOfChatsForExport();
HashMap<String, Object> scope = new HashMap<String, Object>();
scope.put("user", user.getUser());
scope.put("dialogs", dialogs);
scope.put("chats", chats);
MustacheFactory mf = new DefaultMustacheFactory();
Mustache mustache = mf.compile("templates/html/index.mustache");
FileWriter w = new FileWriter(base + "index.html");
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();
}
LinkedList<Database.Chat> chats = db.getListOfChatsForExport();
index.println("<h3>Group chats</h3>");
index.println("<ul>");
for (Database.Chat chat : chats) {
index.println("<li><a href='dialogs/chat_" + chat.id + ".html'>" + chat.name + "</a> <span class='count'>(" + chat.count + ")</span></li>");
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();
}
index.println("</ul>");
index.println("</body>");
index.println("</html>");
index.close();
*/
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("Exception above!");

View File

@ -0,0 +1,36 @@
<!DOCTYPE html>
<html>
<head>
<title>Telegram Backup for {{user.getUserString}}</title>
</head>
<body>
<h1>Telegram Backup</h1>
<h2>{{user.getUserString}}</h2>
<h3>Dialogs</h3>
<ul class="dialogs">
{{#dialogs}}
<li>
<a href="dialogs/user_{{id}}.html">
<span class="name">{{first_name}} {{last_name}}</span>
{{#username}}<span class="username">(@{{username}})</span>{{/username}}
<span class="count">({{count}})</span>
</a>
</li>
{{/dialogs}}
</ul>
<h3>Chats</h3>
<ul class="chats">
{{#chats}}
<li>
<a href="dialogs/chat_{{id}}.html">
<span class="name">{{name}}</span>
<span class="count">({{count}})</span>
</a>
</li>
{{/chats}}
</ul>
</body>
</html>