Adds version check dialog. Adds year and genre to Moby games scraper

This commit is contained in:
lantzelot-swe 2020-12-22 23:47:39 +01:00
parent baace0ff5f
commit 08f39e5fad
10 changed files with 234 additions and 36 deletions

View File

@ -12,4 +12,5 @@
<classpathentry kind="var" path="M2_REPO/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar" sourcepath="M2_REPO/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/org/jsoup/jsoup/1.11.3/jsoup-1.11.3.jar" sourcepath="M2_REPO/org/jsoup/jsoup/1.11.3/jsoup-1.11.3-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/com/jtattoo/JTattoo/1.6.7/JTattoo-1.6.7.jar" sourcepath="M2_REPO/com/jtattoo/JTattoo/1.6.7/JTattoo-1.6.7-sources.jar"/>
<classpathentry kind="var" path="M2_REPO/com/google/code/gson/gson/2.8.0/gson-2.8.0.jar" sourcepath="M2_REPO/com/google/code/gson/gson/2.8.0/gson-2.8.0-sources.jar"/>
</classpath>

View File

@ -37,6 +37,11 @@
<artifactId>JTattoo</artifactId>
<version>1.6.7</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.0</version>
</dependency>
</dependencies>
<build>
<plugins>

View File

@ -43,6 +43,7 @@ import se.lantz.model.MainViewModel;
import se.lantz.model.data.GameListData;
import se.lantz.util.ExceptionHandler;
import se.lantz.util.FileManager;
import se.lantz.util.VersionChecker;
public class MenuManager
{
@ -106,8 +107,8 @@ public class MenuManager
dbMenu.add(getCreateEmptyDbItem());
helpMenu = new JMenu("Help");
helpMenu.add(getHelpItem());
helpMenu.add(getCheckVersionItem());
helpMenu.add(getAboutItem());
helpMenu.add(getNewVersionItem());
}
public void intialize()
@ -234,10 +235,10 @@ public class MenuManager
});
return aboutItem;
}
private JMenuItem getNewVersionItem()
private JMenuItem getCheckVersionItem()
{
newVersionItem = new JMenuItem("Check for new version...");
newVersionItem = new JMenuItem("Check for updates");
newVersionItem.addActionListener(e -> {
checkForNewRelease();
});
@ -369,35 +370,25 @@ public class MenuManager
}
}
/**
* Fetches the latet version information from github
*/
private void checkForNewRelease()
{
try
VersionChecker.fetchLatestVersionFromGithub();
if (VersionChecker.isNewVersionAvailable())
{
URL url = new URL("https://lantzelot-swe@api.github.com/repos/lantzelot-swe/PCUGameManager/releases/latest");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("accept", "application/vnd.github.v3+json");
con.setRequestMethod("GET");
StringBuilder builder = new StringBuilder();
Scanner scanner = new Scanner(url.openStream());
while (scanner.hasNext())
VersionDownloadDialog dialog = new VersionDownloadDialog(MainWindow.getInstance());
dialog.pack();
dialog.setLocationRelativeTo(MainWindow.getInstance());
if (dialog.showDialog())
{
builder.append(scanner.nextLine() + "/n");
getExitItem().doClick();
}
scanner.close();
con.disconnect();
JsonReader reader = new JsonReader(new StringReader(builder.toString()));
reader.setLenient(true);
JsonElement root = new JsonParser().parse(reader);
String tagName = root.getAsJsonObject().get("tag_name").getAsString();
System.out.println("tagName = " + tagName);
}
catch (IOException ex)
else
{
ExceptionHandler.handleException(ex, "Could not check version");
JOptionPane.showMessageDialog(this.mainWindow,
"This is the latest version.",
"Version check",
JOptionPane.INFORMATION_MESSAGE);
}
}
}

View File

@ -0,0 +1,24 @@
package se.lantz.gui;
import java.awt.Frame;
public class VersionDownloadDialog extends BaseDialog
{
private VersionDownloadPanel panel;
public VersionDownloadDialog(Frame owner)
{
super(owner);
setTitle("Version check");
addContent(getVersionDownloadPanel());
getOkButton().setText("Exit");
}
private VersionDownloadPanel getVersionDownloadPanel() {
if (panel == null) {
panel = new VersionDownloadPanel();
}
return panel;
}
}

View File

@ -0,0 +1,68 @@
package se.lantz.gui;
import java.awt.Desktop;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.io.IOException;
import java.net.URISyntaxException;
import javax.swing.JEditorPane;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.event.HyperlinkEvent;
import se.lantz.util.ExceptionHandler;
import se.lantz.util.VersionChecker;
public class VersionDownloadPanel extends JPanel
{
private JEditorPane editorPane;
public VersionDownloadPanel()
{
GridBagLayout gridBagLayout = new GridBagLayout();
setLayout(gridBagLayout);
GridBagConstraints gbc_editorPane = new GridBagConstraints();
gbc_editorPane.weighty = 1.0;
gbc_editorPane.weightx = 1.0;
gbc_editorPane.insets = new Insets(10, 10, 10, 10);
gbc_editorPane.fill = GridBagConstraints.BOTH;
gbc_editorPane.gridx = 0;
gbc_editorPane.gridy = 0;
add(getEditorPane(), gbc_editorPane);
}
private JEditorPane getEditorPane()
{
if (editorPane == null)
{
String downloadUrl = VersionChecker.getDownloadUrl();
String info = "<html>There is a new version of PCU Game Manager available: <b>" +
VersionChecker.getLatestVersion() + "</b><p>" + "Go to <a href='" + downloadUrl + "'>" + downloadUrl +
"</a> and download PCUGameManager.exe.<br>" +
"Exit PCU Game Manager and replace the existing PCUGameManager.exe with the downloaded file to upgrade.</html>";
editorPane = new JEditorPane("text/html", info);
editorPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
editorPane.setFont(UIManager.getDefaults().getFont("Label.font"));
editorPane.setEditable(false);
editorPane.setOpaque(false);
editorPane.addHyperlinkListener((hle) -> {
if (HyperlinkEvent.EventType.ACTIVATED.equals(hle.getEventType()) && Desktop.isDesktopSupported() &&
Desktop.getDesktop().isSupported(Desktop.Action.BROWSE))
{
try
{
Desktop.getDesktop().browse(hle.getURL().toURI());
}
catch (IOException | URISyntaxException e)
{
ExceptionHandler.handleException(e, "Could not open default browser");
}
}
});
}
return editorPane;
}
}

View File

@ -101,6 +101,7 @@ public class MobyGamesOptionsPanel extends JPanel
"(Example: https://www.mobygames.com/game/c64/arkanoid)</li></ol></html>";
infoEditorPane = new JEditorPane("text/html", info);
infoEditorPane.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES, Boolean.TRUE);
infoEditorPane.setFont(UIManager.getDefaults().getFont("Label.font"));
infoEditorPane.setEditable(false);
infoEditorPane.setOpaque(false);

View File

@ -8,7 +8,6 @@ import javax.swing.JPanel;
import se.lantz.gui.BaseDialog;
import se.lantz.model.MainViewModel;
import se.lantz.model.data.ScraperFields;
import se.lantz.util.MobyGamesScraper;
public class ScraperDialog extends BaseDialog
{
@ -23,6 +22,7 @@ public class ScraperDialog extends BaseDialog
content.add(getMobyGamesPanel(), BorderLayout.CENTER);
addContent(content);
setTitle("Scrape game information");
this.setResizable(false);
}
private MobyGamesOptionsPanel getMobyGamesPanel()

View File

@ -6,6 +6,8 @@ import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ListModel;
@ -436,13 +438,25 @@ public class MainViewModel extends AbstractModel
}
if (fields.isYear())
{
//TODO: Parse properly
// infoModel.setYear(Integer.parseInt(scraper.scrapeYear()));
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(scraper.scrapeYear());
if (m.find())
{
infoModel.setYear(Integer.parseInt(m.group()));
}
}
if (fields.isDescription())
{
infoModel.setDescription(scraper.scrapeDescription());
}
//TODO: Genre, cover, screenshots
if (fields.isGenre())
{
String genre = scraper.scrapeGenre();
if (!genre.isEmpty())
{
infoModel.setGenre(genre);
}
}
//TODO: cover, screenshots
}
}

View File

@ -6,7 +6,9 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
@ -44,6 +46,8 @@ public class MobyGamesScraper
private long startTime = 0L;
Map<String, String> genreMap = new HashMap<>();
public MobyGamesScraper()
{
// TODO Auto-generated constructor stub
@ -53,6 +57,14 @@ public class MobyGamesScraper
//*[@id="main"]/div/div[3]/div[1]/h2[1]
//*[@id="coreGameCover"]/a/img
//Keys are Genres defined on MobyGames, values are supported genres in the tool
genreMap.put("Adventure, Role-Playing (RPG)", "adventure");
genreMap.put("Racing / driving", "driving");
genreMap.put("Puzzle, Strategy / tactics", "puzzle");
genreMap.put("Educational", "programming");
genreMap.put("Simulation", "simulation");
genreMap.put("Sports", "sport");
}
public static void main(String[] args)
@ -100,12 +112,7 @@ public class MobyGamesScraper
{
return scarpeElementValue(yearCssQuery);
}
public String scrapeGenre()
{
return scarpeElementValue(genreCssQuery);
}
private void scrapeMobyGames()
{
startTime = System.currentTimeMillis();
@ -150,6 +157,25 @@ public class MobyGamesScraper
}
return "";
}
public String scrapeGenre()
{
String genreFromMobyGames = scarpeElementValue(genreCssQuery);
String[] split = genreFromMobyGames.split(", ");
for (int i = 0; i < split.length; i++)
{
//Map towards available genres, return first one found
for (Map.Entry<String, String> entry : genreMap.entrySet()) {
if (entry.getKey().contains(split[i]))
{
System.out.println(entry.getKey() + "/" + entry.getValue());
return entry.getValue();
}
}
}
return "";
}
private String scarpeElementValue(String cssQuery)
{

View File

@ -0,0 +1,68 @@
package se.lantz.util;
import java.io.IOException;
import java.io.StringReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
public class VersionChecker
{
private static String latestVersion = "";
private static String downloadUrl = "";
private VersionChecker()
{
//Empty
}
public static void fetchLatestVersionFromGithub()
{
try
{
URL url = new URL("https://lantzelot-swe@api.github.com/repos/lantzelot-swe/PCUGameManager/releases/latest");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestProperty("accept", "application/vnd.github.v3+json");
con.setRequestMethod("GET");
StringBuilder builder = new StringBuilder();
Scanner scanner = new Scanner(url.openStream());
while (scanner.hasNext())
{
builder.append(scanner.nextLine() + "/n");
}
scanner.close();
con.disconnect();
JsonReader reader = new JsonReader(new StringReader(builder.toString()));
reader.setLenient(true);
JsonElement root = new JsonParser().parse(reader);
latestVersion = root.getAsJsonObject().get("tag_name").getAsString();
System.out.println("tagName = " + latestVersion);
downloadUrl = root.getAsJsonObject().get("html_url").getAsString();
System.out.println("html url = " + downloadUrl);
}
catch (IOException ex)
{
ExceptionHandler.handleException(ex, "Could not check version");
}
}
public static boolean isNewVersionAvailable()
{
return !FileManager.getPcuVersionFromManifest().equals(latestVersion);
}
public static String getLatestVersion()
{
return latestVersion;
}
public static String getDownloadUrl()
{
return downloadUrl;
}
}