Skip to content
This repository has been archived by the owner on Feb 12, 2020. It is now read-only.

Commit

Permalink
Merge pull request #18 from Russia9/dev
Browse files Browse the repository at this point in the history
Release 1.4
  • Loading branch information
Russia9 authored Jan 13, 2020
2 parents 22a7a8b + ee594a0 commit b8a8a72
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 93 deletions.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM store/oracle/serverjre:8
LABEL maintainer="[email protected]"
COPY ./target/trainpix-1.3-shaded.jar /trainpix/trainpix.jar
COPY ./target/trainpix-1.4-shaded.jar /trainpix/trainpix.jar
ENV LEVEL INFO
WORKDIR /trainpix
CMD java -jar trainpix.jar
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dev.russia9</groupId>
<artifactId>trainpix</artifactId>
<version>1.3</version>
<version>1.4</version>
<name>TrainPix</name>

<dependencies>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/dev/russia9/trainpix/i18n/LocaleManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import java.util.ResourceBundle;

public class LocaleManager {
private ResourceBundle[] resourceBundles;
private final ResourceBundle[] resourceBundles;

public LocaleManager() {
ClassLoader classloader = Thread.currentThread().getContextClassLoader();
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/dev/russia9/trainpix/lib/Lib.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dev.russia9.trainpix.lib;

public class Lib {
public static String getSearchQuery(String[] message) {
StringBuilder searchQuery = new StringBuilder();
for (int i = 1; i < message.length; i++) {
if (!message[i].equals(" ")) {
searchQuery.append(message[i].trim()).append(" ");
}
}
return searchQuery.toString().trim();
}
}
2 changes: 1 addition & 1 deletion src/main/java/dev/russia9/trainpix/lib/Reference.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Reference {
/**
* Bot version
*/
public static final String botVersion = "1.3";
public static final String botVersion = "1.4";

/**
* TrainPix image
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import dev.russia9.trainpix.i18n.LocaleManager;
import dev.russia9.trainpix.lib.Reference;
import dev.russia9.trainpix.modules.BotModule;
import dev.russia9.trainpix.modules.HelpModule;
import dev.russia9.trainpix.modules.ListModule;
import dev.russia9.trainpix.modules.PhotoModule;
import dev.russia9.trainpix.modules.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.javacord.api.event.message.MessageCreateEvent;
Expand All @@ -18,13 +15,14 @@
*/
public class MessageCreateListener implements org.javacord.api.listener.message.MessageCreateListener {
private static final Logger logger = LogManager.getLogger("TrainPix");
private BotModule[] modules;
private final BotModule[] modules;

public MessageCreateListener(LocaleManager localeManager) {
modules = new BotModule[]{
new ListModule(localeManager),
new PhotoModule(localeManager),
new HelpModule(localeManager)
new HelpModule(localeManager),
new RandomModule(localeManager)
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/main/java/dev/russia9/trainpix/modules/HelpModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
*/
public class HelpModule implements BotModule {
private static final Logger logger = LogManager.getLogger("TrainPix");
private LocaleManager localeManager;
private String[] aliases = {
private final LocaleManager localeManager;
private final String[] aliases = {
"help"
};

Expand Down
13 changes: 7 additions & 6 deletions src/main/java/dev/russia9/trainpix/modules/ListModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import dev.russia9.trainpix.i18n.LocaleManager;
import dev.russia9.trainpix.lib.Lib;
import dev.russia9.trainpix.lib.Reference;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -11,7 +12,6 @@
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.awt.*;
import java.io.IOException;
import java.net.URLEncoder;

Expand All @@ -25,8 +25,8 @@
*/
public class ListModule implements BotModule {
private static final Logger logger = LogManager.getLogger("TrainPix");
private LocaleManager localeManager;
private String[] aliases = {
private final LocaleManager localeManager;
private final String[] aliases = {
"list",
"l"
};
Expand All @@ -43,8 +43,9 @@ public String[] getAliases() {
@Override
public void process(MessageCreateEvent event) {
String[] message = event.getMessageContent().split(" ");
if (message.length == 2) { // First page
String searchQuery = message[1];
if (message.length >= 2) { // First page
String searchQuery = Lib.getSearchQuery(message);

EmbedBuilder reply = new EmbedBuilder();

String lang = "en";
Expand All @@ -66,7 +67,7 @@ public void process(MessageCreateEvent event) {
int i = 0;

for (Element trainRow : trains) {
if(i >= Reference.maxListSize) {
if (i >= Reference.maxListSize) {
break;
}
StringBuilder head = new StringBuilder();
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/dev/russia9/trainpix/modules/PhotoModule.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package dev.russia9.trainpix.modules;

import dev.russia9.trainpix.i18n.LocaleManager;
import dev.russia9.trainpix.lib.Lib;
import dev.russia9.trainpix.lib.Reference;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -24,8 +25,8 @@
*/
public class PhotoModule implements BotModule {
private static final Logger logger = LogManager.getLogger("TrainPix");
private LocaleManager localeManager;
private String[] aliases = {
private final LocaleManager localeManager;
private final String[] aliases = {
"photo",
"p"
};
Expand All @@ -42,8 +43,9 @@ public String[] getAliases() {
@Override
public void process(MessageCreateEvent event) {
String[] message = event.getMessageContent().split(" ");
if (message.length == 2) { // First page
String searchQuery = message[1];
if (message.length >= 2) { // First page
String searchQuery = Lib.getSearchQuery(message);

EmbedBuilder reply = new EmbedBuilder();

String lang = "en";
Expand All @@ -53,7 +55,7 @@ public void process(MessageCreateEvent event) {
logger.trace("Detected LANG:" + lang);

try {
String searchUrl = "https://trainpix.org/vsearch.php?&order=2&strict=on&num=" + URLEncoder.encode(searchQuery, "UTF-8");
String searchUrl = "https://trainpix.org/vsearch.php?&order=2&num=" + URLEncoder.encode(searchQuery, "UTF-8");
Document document = getPage(searchUrl, lang);

String trainLink = "https://trainpix.org/" + document.getElementsByClass("main").get(0)
Expand Down
153 changes: 153 additions & 0 deletions src/main/java/dev/russia9/trainpix/modules/RandomModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package dev.russia9.trainpix.modules;

import dev.russia9.trainpix.i18n.LocaleManager;
import dev.russia9.trainpix.lib.Reference;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.javacord.api.entity.message.embed.EmbedBuilder;
import org.javacord.api.event.message.MessageCreateEvent;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.awt.*;
import java.io.IOException;

import static dev.russia9.trainpix.lib.ParseHelper.getPage;

/**
* /photo command module
*
* @author Russia9
* @since 0.1
*/
public class RandomModule implements BotModule {
private static final Logger logger = LogManager.getLogger("TrainPix");
private final LocaleManager localeManager;
private final String[] aliases = {
"random",
"rd"
};

public RandomModule(LocaleManager localeManager) {
this.localeManager = localeManager;
}

@Override
public String[] getAliases() {
return aliases;
}

@Override
public void process(MessageCreateEvent event) {
EmbedBuilder reply = new EmbedBuilder();

String lang = "en";
if (event.isServerMessage() && event.getServer().get().getRegion().getKey().equals("russia")) {
lang = "ru";
}
logger.trace("Detected LANG:" + lang);

try {
String photoLink = "https://trainpix.org/ph.php";
Document photo = getPage(photoLink, lang);
String photoImage = "https://trainpix.org" + photo.getElementById("ph").attr("src");

String trainLink = "https://trainpix.org/" + photo.getElementsByAttributeValueStarting("href", "/vehicle").get(0).attr("href");
Document train = getPage(trainLink, lang);

String trainName = train.getElementsByTag("h1").get(0).text();

String authorName = photo.getElementsByClass("cmt_aname").get(0).getElementsByTag("a").text();

// Build date detection
Elements built = train.getElementsContainingOwnText(localeManager.getString(lang, "train.built"));
String buildDate = localeManager.getString(lang, "train.built") + " " + localeManager.getString(lang, "unknown");

if (!built.isEmpty() && built.parents().get(0).children().size() > 0) {
buildDate = localeManager.getString(lang, "train.built") + " " + built.parents().get(0).getElementsByTag("b").text();
}

// Depot detection
Elements depot = train.getElementsContainingOwnText(localeManager.getString(lang, "train.depot"));
String depotName = localeManager.getString(lang, "train.depot") + " " + localeManager.getString(lang, "unknown");

if (!depot.isEmpty() && depot.parents().get(0).children().size() > 0) {
depotName = depot.parents().get(0).getElementsByTag("a").text();
}

// Road detection
Elements road = train.getElementsContainingOwnText(localeManager.getString(lang, "train.road"));
String roadName = localeManager.getString(lang, "train.road") + " " + localeManager.getString(lang, "unknown");

if (!road.isEmpty() && road.parents().get(0).children().size() > 0) {
roadName = road.parents().get(0).getElementsByTag("a").text();
}

// Category detection
Elements category = train.getElementsContainingOwnText(localeManager.getString(lang, "train.category"));
String categoryName = "Other";

if (!category.isEmpty() && road.parents().get(0).children().size() > 0) {
categoryName = category.parents().get(0).child(1).text();
}

// Condition and color detection
Color color = new Color(220, 220, 220);
Element state = photo.getElementsByClass("state").first();
String stateText = state.text();
if ("ru".equals(lang)) {
if (stateText.contains("Новый")) {
color = new Color(108, 220, 53);
}
if (stateText.contains("Списан")) {
color = new Color(220, 122, 110);
}
if (stateText.contains("Не работает")) {
color = new Color(220, 217, 76);
}
if (stateText.contains("Нынешнее местонахождение и судьба неизвестны")) {
color = new Color(220, 138, 104);
}
if (stateText.contains("Памятник/музейный экспонат/тренажёр")) {
color = new Color(220, 53, 37);
}
} else {
if (stateText.contains("New")) {
color = new Color(108, 220, 53);
}
if (stateText.contains("Written off")) {
color = new Color(220, 122, 110);
}
if (stateText.contains("Out of order")) {
color = new Color(220, 217, 76);
}
if (stateText.contains("Current location and condition are unknown")) {
color = new Color(220, 138, 104);
}
if (stateText.contains("Monument/Museum exhibit/Trainer")) {
color = new Color(220, 53, 37);
}
}

reply.setTitle(trainName);
reply.setAuthor(Reference.botName, trainLink, Reference.botImageLink);
reply.addField(roadName + " | " + depotName, categoryName + " | " + stateText);
reply.setImage(photoImage);
reply.setColor(color);
reply.setFooter(authorName + " | " + buildDate);
} catch (IOException e) {
logger.trace(e);
reply.setAuthor(Reference.botName);
reply.setTitle(localeManager.getString(lang, "errors.500.header"));
reply.addField(localeManager.getString(lang, "errors.500.title"), localeManager.getString(lang, "errors.500.description"));
} catch (IndexOutOfBoundsException e) {
logger.trace(e);
reply.setAuthor(Reference.botName);
reply.setTitle(localeManager.getString(lang, "errors.404.header"));
reply.addField(localeManager.getString(lang, "errors.404.title"), localeManager.getString(lang, "errors.404.description"));
}

event.getChannel().sendMessage(reply);
}
}
Loading

0 comments on commit b8a8a72

Please sign in to comment.