this was alot simpler than with c++
This commit is contained in:
parent
5552a20eb6
commit
f6da878ac0
@ -15,6 +15,7 @@ import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@ -34,10 +35,8 @@ public class DataMapper {
|
||||
public static DataMapper instance = new DataMapper();
|
||||
private volatile boolean called;
|
||||
private final ConcurrentMap<Integer, StringObject> soEventContentCache;
|
||||
private final Stopwatch stopwatch;
|
||||
|
||||
public DataMapper() {
|
||||
this.stopwatch = Stopwatch.createStarted();
|
||||
this.soEventContentCache = new MapMaker().concurrencyLevel(2).makeMap();
|
||||
}
|
||||
|
||||
@ -46,6 +45,47 @@ public class DataMapper {
|
||||
return MapColoumns;
|
||||
}
|
||||
|
||||
public void insertIntoDBEventContent(ConcurrentMap<Integer, String> EventContentMap) {
|
||||
String SQL_Delete = "delete from Maps";
|
||||
String l_sSQL = "INSERT INTO `EventContent` (`title`,`Rewards`,`Leaders`,`datum`,`hours`) VALUES (?, ?, ?, ?, ?)";
|
||||
String l_SQLMaps = "INSERT INTO `Maps` (`MapNames`) VALUES (?)";
|
||||
List<String> maps = new ArrayList();
|
||||
try (Connection l_cCon = DBCPDataSource.getConnection()) {
|
||||
try (PreparedStatement s = l_cCon.prepareStatement(SQL_Delete)) {
|
||||
s.executeUpdate();
|
||||
}
|
||||
SQL_Delete = "delete from EventContent";
|
||||
try (PreparedStatement s = l_cCon.prepareStatement(SQL_Delete)) {
|
||||
s.executeUpdate();
|
||||
}
|
||||
try (PreparedStatement s = l_cCon.prepareStatement(l_sSQL)) {
|
||||
int i = 1;
|
||||
for (String str : EventContentMap.values()) {
|
||||
if (i < 6) {
|
||||
System.out.println("str: " + str + "\n");
|
||||
s.setString(i, str);
|
||||
i++;
|
||||
} else {
|
||||
maps.add(str);
|
||||
}
|
||||
}
|
||||
s.executeUpdate();
|
||||
}
|
||||
try (PreparedStatement s = l_cCon.prepareStatement(l_SQLMaps)) {
|
||||
for (String strMap : maps) {
|
||||
if (!maps.isEmpty()) {
|
||||
s.setString(1, strMap);
|
||||
s.addBatch();
|
||||
}
|
||||
}
|
||||
s.executeBatch();
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
Logger.getLogger(DataMapper.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
called = false;
|
||||
}
|
||||
|
||||
private Map<Integer, StringObject> getCache() throws SQLException, IOException, CustomError {
|
||||
List<StringObject> stro;
|
||||
stro = GetEventContentFromDBTable();
|
||||
@ -66,21 +106,6 @@ public class DataMapper {
|
||||
}
|
||||
}
|
||||
|
||||
public void checkUpdateTime() {
|
||||
new Thread(() -> {
|
||||
if (stopwatch.elapsed(TimeUnit.SECONDS) >= EXPIRE_TIME_IN_SECONDS) {
|
||||
try {
|
||||
soEventContentCache.clear();
|
||||
called = false;
|
||||
initialization();
|
||||
stopwatch.reset();
|
||||
} catch (SQLException | IOException | CustomError ex) {
|
||||
Logger.getLogger(DataMapper.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
}
|
||||
|
||||
public static String modifyEventContent(String str, int i) {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
switch (i) {
|
||||
|
@ -0,0 +1,147 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package FunctionLayer;
|
||||
//ps ax | grep EventNotfierDiscordBot-1.0
|
||||
//kill $pid (number)
|
||||
/*
|
||||
screen -d -m -S nonRoot java -jar /home/sourcemodextensionstest/EventNotfierDiscordBot/EventNotfierDiscordBot-1.0.jar
|
||||
screen -ls (number1)
|
||||
screen -X -S (number1) quit
|
||||
nohup screen -d -m -S nonroot java -jar /home/sourcemodextensionstest/EventNotfierDiscordBot/EventNotfierDiscordBot-1.0.jar
|
||||
*/
|
||||
|
||||
import DataLayer.DataMapper;
|
||||
import com.google.common.base.Stopwatch;
|
||||
import com.google.common.collect.MapMaker;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStreamReader;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.MalformedURLException;
|
||||
import java.net.ProtocolException;
|
||||
import java.net.URL;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
import org.javacord.api.DiscordApi;
|
||||
import org.javacord.api.DiscordApiBuilder;
|
||||
import org.javacord.api.event.message.MessageCreateEvent;
|
||||
import org.jsoup.Jsoup;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author install1
|
||||
*/
|
||||
public class EventNotifier {
|
||||
|
||||
//public static EventNotifier instance = new EventNotifier();
|
||||
public static final long EXPIRE_TIME_IN_SECONDS = TimeUnit.SECONDS.convert(15, TimeUnit.MINUTES);
|
||||
private static Stopwatch stopwatch = Stopwatch.createUnstarted();
|
||||
private static String eventURL = "https://unloze.com/forums/events.76/";
|
||||
|
||||
private static void checkIfNewEventThread() {
|
||||
try {
|
||||
URL url = new URL(eventURL);
|
||||
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
||||
con.setRequestMethod("GET");
|
||||
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
||||
String inputLine;
|
||||
StringBuffer content = new StringBuffer();
|
||||
int firstEventForZEFinder = 0;
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
content.append(inputLine).append("\n");
|
||||
if (inputLine.contains(">Event #") && firstEventForZEFinder == 0) {
|
||||
if (!inputLine.contains("Nomination") && !inputLine.contains("Poll")) {
|
||||
int diff = content.length() - inputLine.length();
|
||||
firstEventForZEFinder = diff > 0 ? diff : 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
String str = content.toString();
|
||||
String hRef = "<a href=\"";
|
||||
str = str.substring(firstEventForZEFinder);
|
||||
str = str.substring(0, str.indexOf(">Event #"));
|
||||
str = str.substring(str.lastIndexOf(hRef), str.lastIndexOf("\" title="));
|
||||
str = str.replace(hRef, "");
|
||||
String urlFirstEvent = "https://unloze.com/" + str;
|
||||
System.out.println(urlFirstEvent);
|
||||
url = new URL(urlFirstEvent);
|
||||
con = (HttpURLConnection) url.openConnection();
|
||||
con.setRequestMethod("GET");
|
||||
in = new BufferedReader(new InputStreamReader(con.getInputStream()));
|
||||
content = new StringBuffer();
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
content.append(inputLine).append("\n");
|
||||
}
|
||||
str = content.toString();
|
||||
if (str.contains("Server :") && str.contains("Maps :") && str.contains("Prize :")) {
|
||||
ConcurrentMap<Integer, String> eventContentMap = new MapMaker().concurrencyLevel(2).makeMap();
|
||||
String html2text = html2text(str);
|
||||
System.out.println(html2text);
|
||||
String title = html2text.substring(0, html2text.indexOf("| UNLOZE"));
|
||||
eventContentMap.put(eventContentMap.size(), title);
|
||||
String rewards = html2text.substring(html2text.indexOf("Prize :") + 7, html2text.lastIndexOf("won"));
|
||||
rewards = rewards + "won";
|
||||
eventContentMap.put(eventContentMap.size(), rewards);
|
||||
String leaders = html2text.substring(html2text.indexOf("Leader :") + 7, html2text.indexOf("Prize :"));
|
||||
eventContentMap.put(eventContentMap.size(), leaders);
|
||||
String date = html2text.substring(html2text.indexOf("Date :") + 6, html2text.indexOf("Time :"));
|
||||
eventContentMap.put(eventContentMap.size(), date);
|
||||
String Time = html2text.substring(html2text.indexOf("Time :") + 6, html2text.indexOf("Leader :"));
|
||||
eventContentMap.put(eventContentMap.size(), Time);
|
||||
String maps = html2text.substring(html2text.indexOf("Maps : -") + 6, html2text.indexOf("Date :"));
|
||||
String[] split = maps.split("-");
|
||||
for (String strsplit : split) {
|
||||
eventContentMap.put(eventContentMap.size(), strsplit);
|
||||
}
|
||||
DataMapper.instance.insertIntoDBEventContent(eventContentMap);
|
||||
}
|
||||
con.disconnect();
|
||||
in.close();
|
||||
} catch (MalformedURLException | ProtocolException ex) {
|
||||
Logger.getLogger(EventNotifier.class.getName()).log(Level.SEVERE, null, ex);
|
||||
} catch (IOException ex) {
|
||||
Logger.getLogger(EventNotifier.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
|
||||
public static String html2text(String html) {
|
||||
return Jsoup.parse(html).text();
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
String token = "NTI5MDIyODgwOTU3MDcxMzk5.Dwqy6w.sgbRejEzZOLdTqyqzDmktSTBDpM";
|
||||
DiscordApi api = new DiscordApiBuilder().setToken(token).login().join();
|
||||
api.addMessageCreateListener((MessageCreateEvent event) -> {
|
||||
if (stopwatch.elapsed(TimeUnit.SECONDS) >= EXPIRE_TIME_IN_SECONDS || !stopwatch.isRunning()) {
|
||||
if (!stopwatch.isRunning()) {
|
||||
stopwatch.start();
|
||||
} else {
|
||||
stopwatch.reset();
|
||||
}
|
||||
checkIfNewEventThread();
|
||||
}
|
||||
if (event.getMessageContent().equalsIgnoreCase("!event")) {
|
||||
try {
|
||||
DataMapper.instance.initialization();
|
||||
List<StringObject> EventContent = DataMapper.instance.getAllMapValues();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append("Event Information: \n");
|
||||
for (StringObject SO : EventContent) {
|
||||
sb.append(SO.getEventContent()).append("\n");
|
||||
}
|
||||
event.getChannel().sendMessage(sb.toString());
|
||||
} catch (SQLException | IOException | CustomError ex) {
|
||||
Logger.getLogger(EventNotifier.class.getName()).log(Level.SEVERE, null, ex);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user