Skip to content

Commit

Permalink
Bugfixes
Browse files Browse the repository at this point in the history
- Fix Date fail
- Fix Update/ New File Bug
- Fix one Testmode Bug

Signed-off-by: Keno Oelrichs.Garcia <[email protected]>
  • Loading branch information
MrDrache333 committed Oct 22, 2019
1 parent 1c7f172 commit ec44634
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 21 deletions.
9 changes: 4 additions & 5 deletions src/Hauptklasse.java
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,11 @@ private static void login(String user, String pass) {
String newfiles = "";
String updatedfiles = "";
for (StudIPFile file : Files) {
//TODO Änderungsdatum vergleichen
File studipfile = new File(DownloadPath.getPath() + "/" + kursname.replace(" ", "_") + "/" + file.getPath() + file.getName());
boolean aktuell = studipfile.exists() && studipfile.lastModified() >= file.getLastChanged().getTime();
if (!studipfile.exists() || !aktuell) {
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm dd.MM.yyyy");
if (!aktuell) {
if (studipfile.exists()) {
Sout("Datei " + file.getName() + " ist nicht mehr aktuell. Datei: " + sdf.format(studipfile.lastModified()) + " / StudIP: " + sdf.format(file.getLastChanged().getTime()) + "Aktualisiere...");
updatedfiles += "[" + file.getName() + "](" + file.getLink() + ")\n";
} else
Expand All @@ -222,9 +221,9 @@ private static void login(String user, String pass) {
if (!updatedfiles.equals("")) {
updatedfiles = "\n*Aktualisierte Dateien*\n" + updatedfiles;
}
if (!newfiles.equals("") && !updatedfiles.equals("")) {
if (!newfiles.equals("") || !updatedfiles.equals("")) {
String header = "📄 _" + kursname + "_ 📄";
telegramBot.sendMessage(telegramChatId, header + updatedfiles + newfiles, !TESTING);
telegramBot.sendMessage(telegramChatId, header + updatedfiles + newfiles, telegramBot.parseMode.MARKDOWN, true);
}
} else {
Sout("Modul " + kursname + " übersprungen!");
Expand All @@ -239,7 +238,7 @@ private static void login(String user, String pass) {
for (News news : newNews) {
//Neue News an Telegram senden
//System.out.println("\t" + news.getTitle() + "\n\t\t" + news.getText());
telegramBot.sendMessage(telegramChatId, "📰 _" + kursname + "_ 📰\n*" + news.getTitle() + "*\n" + news.getText(), !TESTING);
telegramBot.sendMessage(telegramChatId, "📰 _" + kursname + "_ 📰\n*" + news.getTitle() + "*\n" + news.getText(), telegramBot.parseMode.MARKDOWN, true);
sendMessages++;
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/objects/Kurs.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,11 @@ public ArrayList<News> getNews(WebClient webClient, String url, boolean markAsRe
Document Content = Jsoup.parse(page.asXml());
//System.out.println(page.asText());
//Neue News filtern
Elements articles = Content.select(".studip .new");
Elements articles = Content.select(".studip .toggle .new"); //.new
for (Element article : articles) {
//News auslesen und als gelesen markieren
News news = new News(article.select("h1").text(), article.select(".formatted-content").text(), article.getElementsByAttribute("id").get(0).attr("id"));
news.setHtml(article.select(".formatted-content").html());
if (markAsRead)
webClient.getPage(url + ID + "&contentbox_type=news&contentbox_open=" + news.getID() + "#" + news.getID());
newNews.add(news);
Expand All @@ -182,7 +183,6 @@ public ArrayList<News> getNews(WebClient webClient, String url, boolean markAsRe
} catch (Exception e) {
e.printStackTrace();
}
//TODO
return newNews;
}

Expand Down
19 changes: 19 additions & 0 deletions src/objects/News.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ public class News {
private String Title;
private String Text;
private String ID;
private String html;

/**
* Instantiates a new News.
Expand All @@ -22,6 +23,24 @@ public News(String tilte, String text, String id) {
this.ID = id;
}

/**
* Gets html.
*
* @return the html
*/
public String getHtml() {
return html;
}

/**
* Sets html.
*
* @param html the html
*/
public void setHtml(String html) {
this.html = html;
}

/**
* Gets Title.
*
Expand Down
39 changes: 25 additions & 14 deletions src/utils/telegramBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,35 @@ public class telegramBot {

private static String TOKEN;

/**
* Instantiates a new Telegram bot.
*
* @param token the token
*/
public telegramBot(String token) {
TOKEN = token;

}

/**
* Send message.
*
* @param chat_id the chat id
* @param text the text
* @param send the send
*/
public static void sendMessage(String chat_id, String text, boolean send) {
public static void sendMessage(String chat_id, String text, parseMode type, boolean send) {
if (chat_id.equals("")) return;
String data = "{\"chat_id\":\"" + chat_id + "\",\"text\":\"" + text + "\",\"parse_mode\":\"Markdown\"}";
text = text.replace("\"", "\\\"");
String data = "{\"chat_id\":\"" + chat_id + "\"" + (type != parseMode.TEXT ? ",\"parse_mode\":\"" + type.name() + "\"" : "") + ",\"text\":\"" + text + "\"}";
if (type == parseMode.HTML) data = data.replaceAll("\n", "<br>");
if (send) {
if (TOKEN.equals("")) throw new NullPointerException("Telegram BotToken must not be Null");
httpPost(data, "sendMessage", true);
} else
System.out.println(data);
}

/**
* Instantiates a new Telegram bot.
*
* @param token the token
*/
public telegramBot(String token) {
TOKEN = token;

}

/**
* Function to Push the Content of the given Data to ioBroker
*
Expand All @@ -59,6 +61,7 @@ private static void httpPost(String Data, String methodname, boolean send) {
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setConnectTimeout(5000);
conn.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
conn.setFixedLengthStreamingMode(Data.getBytes(StandardCharsets.UTF_8).length);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
Expand All @@ -76,16 +79,24 @@ private static void httpPost(String Data, String methodname, boolean send) {
conn.disconnect();
trys = 0;
} catch (Exception e) {
if (trys > 1)
if (trys > 2) {
System.err.println("Failed to send Message. Probably to long?");
System.err.println(e.getMessage());
}
trys++;
}
} while (trys > 0 && trys < 3);
} while (trys > 0 && trys < 4);
}


}

public enum parseMode {
HTML,
MARKDOWN,
TEXT
}

//Konvertiert die Eingabe eines InputStreams in einen String und gibt ihn zurück
private static String convertStreamToString(InputStream is)
throws IOException {
Expand Down

0 comments on commit ec44634

Please sign in to comment.