From ecbca30a10184c47e6151c6f0c93b16b0e6c4cfa Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Thu, 7 Jul 2016 06:40:00 +0200 Subject: [PATCH] WIP: Using mustache as template engine for HTML exporter. --- README.md | 8 ++- build.gradle | 5 ++ .../CommandLineController.java | 1 + .../telegram_backup/Database.java | 5 +- .../telegram_backup/UserManager.java | 2 +- .../{ => exporter}/HTMLExporter.java | 55 ++++++++++--------- .../resources/templates/html/index.mustache | 36 ++++++++++++ 7 files changed, 78 insertions(+), 34 deletions(-) rename src/main/java/de/fabianonline/telegram_backup/{ => exporter}/HTMLExporter.java (66%) create mode 100644 src/main/resources/templates/html/index.mustache diff --git a/README.md b/README.md index 74079d0..7f3ee42 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/build.gradle b/build.gradle index 31def36..f18e7bd 100644 --- a/build.gradle +++ b/build.gradle @@ -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" + ] } diff --git a/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java b/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java index ed26734..1be9c11 100644 --- a/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java +++ b/src/main/java/de/fabianonline/telegram_backup/CommandLineController.java @@ -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; diff --git a/src/main/java/de/fabianonline/telegram_backup/Database.java b/src/main/java/de/fabianonline/telegram_backup/Database.java index 894b4b1..f01d7c1 100644 --- a/src/main/java/de/fabianonline/telegram_backup/Database.java +++ b/src/main/java/de/fabianonline/telegram_backup/Database.java @@ -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; diff --git a/src/main/java/de/fabianonline/telegram_backup/UserManager.java b/src/main/java/de/fabianonline/telegram_backup/UserManager.java index 2baef43..16599af 100644 --- a/src/main/java/de/fabianonline/telegram_backup/UserManager.java +++ b/src/main/java/de/fabianonline/telegram_backup/UserManager.java @@ -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; diff --git a/src/main/java/de/fabianonline/telegram_backup/HTMLExporter.java b/src/main/java/de/fabianonline/telegram_backup/exporter/HTMLExporter.java similarity index 66% rename from src/main/java/de/fabianonline/telegram_backup/HTMLExporter.java rename to src/main/java/de/fabianonline/telegram_backup/exporter/HTMLExporter.java index 57ecafa..1ec511a 100644 --- a/src/main/java/de/fabianonline/telegram_backup/HTMLExporter.java +++ b/src/main/java/de/fabianonline/telegram_backup/exporter/HTMLExporter.java @@ -14,7 +14,7 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ -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(""); - index.println(""); - index.println(""); - index.println("Telegram Backup for " + user.getUserString() + ""); - index.println(""); - - index.println(""); - index.println("

Telegram Backup

"); - index.println("

" + user.getUserString() + "

"); LinkedList dialogs = db.getListOfDialogsForExport(); - index.println("

Dialogs

"); - index.println("
    "); - for (Database.Dialog dialog : dialogs) { - index.println("
  • " + dialog.first_name + " " + (dialog.last_name!=null ? dialog.last_name : "") + " (" + dialog.count + ")
  • "); + LinkedList chats = db.getListOfChatsForExport(); + + + HashMap scope = new HashMap(); + 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> 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 chats = db.getListOfChatsForExport(); - index.println("

    Group chats

    "); - index.println("
      "); - for (Database.Chat chat : chats) { - index.println("
    • " + chat.name + " (" + chat.count + ")
    • "); + 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("
    "); - index.println(""); - index.println(""); - index.close(); + */ } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("Exception above!"); diff --git a/src/main/resources/templates/html/index.mustache b/src/main/resources/templates/html/index.mustache new file mode 100644 index 0000000..7c22174 --- /dev/null +++ b/src/main/resources/templates/html/index.mustache @@ -0,0 +1,36 @@ + + + + Telegram Backup for {{user.getUserString}} + + + +

    Telegram Backup

    +

    {{user.getUserString}}

    + +

    Dialogs

    + + +

    Chats

    + + +