Adds cover to scraper. Small fixes for version checker

This commit is contained in:
lantzelot-swe 2020-12-23 23:20:02 +01:00
parent 64efc3e5e0
commit e240ee4e12
6 changed files with 71 additions and 32 deletions

View File

@ -53,6 +53,7 @@ public class ScreenshotsPanel extends JPanel
private String currentCoverFile = "";
private String currentScreen1File = "";
private String currentScreen2File = "";
private BufferedImage currentCoverImage = null;
private ImageIcon missingSceenshotIcon = null;
private ImageIcon missingCoverIcon = null;
@ -124,15 +125,29 @@ public class ScreenshotsPanel extends JPanel
private void reloadScreens()
{
String modelCoverFile = model.getCoverFile();
if (modelCoverFile.isEmpty())
BufferedImage coverImage = model.getCoverImage();
if (coverImage != null)
{
getCoverImageLabel().setIcon(getMissingCoverImageIcon());
if (!coverImage.equals(currentCoverImage))
{
logger.debug("SETTING NEW COVER IMAGE");
Image newImage = coverImage.getScaledInstance(130, 200, Image.SCALE_DEFAULT);
getCoverImageLabel().setIcon(new ImageIcon(newImage));
currentCoverImage = coverImage;
}
}
else if (!modelCoverFile.equals(currentCoverFile))
else
{
loadCover(modelCoverFile);
currentCoverFile = modelCoverFile;
String modelCoverFile = model.getCoverFile();
if (modelCoverFile.isEmpty())
{
getCoverImageLabel().setIcon(getMissingCoverImageIcon());
}
else if (!modelCoverFile.equals(currentCoverFile))
{
loadCover(modelCoverFile);
currentCoverFile = modelCoverFile;
}
}
String modelScreen1File = model.getScreens1File();
if (modelScreen1File.isEmpty())

View File

@ -12,6 +12,8 @@ public class VersionDownloadDialog extends BaseDialog
setTitle("New version available");
addContent(getVersionDownloadPanel());
getOkButton().setText("Exit");
getCancelButton().setText("Continue");
this.setResizable(false);
}
private VersionDownloadPanel getVersionDownloadPanel() {

View File

@ -40,7 +40,7 @@ public class VersionDownloadPanel extends JPanel
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>" +
"</a> and download PCUGameManager.exe.<p>" +
"Exit PCU Game Manager and replace the existing PCUGameManager.exe with the downloaded file to upgrade.</html>";
editorPane = new JEditorPane("text/html", info);

View File

@ -457,6 +457,10 @@ public class MainViewModel extends AbstractModel
infoModel.setGenre(genre);
}
}
if (fields.isCover())
{
infoModel.setCoverImage(scraper.scrapeCover());
}
//TODO: cover, screenshots
}
}

View File

@ -202,7 +202,7 @@ public class MobyGamesScraper
return value;
}
private void scrapeCover()
public BufferedImage scrapeCover()
{
Document doc;
try
@ -214,29 +214,53 @@ public class MobyGamesScraper
if (coverElements.first() != null)
{
Element coverElement = coverElements.first();
String absoluteUrl = coverElement.absUrl("src");
String srcValue = coverElement.attr("src");
URL url = new URL(absoluteUrl);
BufferedImage c = ImageIO.read(url);
ImageIcon image = new ImageIcon(c);
saveImage(absoluteUrl, game + ".jpg");
//TODO: big cover:
String bigCoverUrl = coverElement.parent().attr("href");
scrapeBigCover(bigCoverUrl);
System.out.println("Cover art: " + absoluteUrl);
return scrapeBigCover(bigCoverUrl);
}
}
catch (IOException e)
{
e.printStackTrace();
ExceptionHandler.handleException(e, "Could not scrape cover");
}
return null;
}
// private void scrapeCoverTest()
// {
// Document doc;
// try
// {
// Connection.Response result = Jsoup.connect(mobyGamesGameUrl).method(Connection.Method.GET).execute();
// doc = result.parse();
// //Fetch the right element
// Elements coverElements = doc.select(coverCssQuery);
// if (coverElements.first() != null)
// {
// Element coverElement = coverElements.first();
// String absoluteUrl = coverElement.absUrl("src");
// String srcValue = coverElement.attr("src");
//
// URL url = new URL(absoluteUrl);
// BufferedImage c = ImageIO.read(url);
// ImageIcon image = new ImageIcon(c);
//
// saveImage(absoluteUrl, game + ".jpg");
//
// //TODO: big cover:
//
// String bigCoverUrl = coverElement.parent().attr("href");
// return scrapeBigCover(bigCoverUrl);
// System.out.println("Cover art: " + absoluteUrl);
// }
// }
// catch (IOException e)
// {
// e.printStackTrace();
// }
// }
private void scrapeBigCover(String url)
private BufferedImage scrapeBigCover(String url)
{
String cssQuery = "#main > div > div:eq(1) > center > img"; //*[@id="main"]/div/div[2]/center/img
Document doc;
@ -252,18 +276,14 @@ public class MobyGamesScraper
String absoluteUrl = coverElement.absUrl("src");
URL imageUrl = new URL(absoluteUrl);
BufferedImage c = ImageIO.read(imageUrl);
ImageIcon image = new ImageIcon(c);
saveImage(absoluteUrl, game + "-large.jpg");
System.out.println("Big Cover art: " + absoluteUrl);
return ImageIO.read(imageUrl);
}
}
catch (IOException e)
{
e.printStackTrace();
ExceptionHandler.handleException(e, "Could not scrape cover");
}
return null;
}
public static void saveImage(String imageUrl, String destinationFile) throws IOException

View File

@ -40,9 +40,7 @@ public class VersionChecker
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)
{