feat: adds c64com as selectable when scraping game info

This commit is contained in:
mikael.lantz 2021-01-12 16:31:28 +01:00
parent b286dd8e5e
commit 840fa79d10
8 changed files with 685 additions and 43 deletions

View File

@ -0,0 +1,387 @@
package se.lantz.gui.scraper;
import java.awt.Cursor;
import java.awt.Desktop;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.net.URISyntaxException;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.event.HyperlinkEvent;
import se.lantz.manager.ScraperManager;
import se.lantz.model.data.ScraperFields;
import se.lantz.util.ExceptionHandler;
public class C64comOptionsPanel extends JPanel
{
private JEditorPane infoEditorPane;
private JTextField urlTextField;
private JPanel fieldsPanel;
private JLabel fieldsInfoLabel;
private JCheckBox titleCheckBox;
private JCheckBox authorCheckBox;
private JCheckBox yearCheckBox;
private JCheckBox descriptionCheckBox;
private JCheckBox coverCheckBox;
private JCheckBox screensCheckBox;
private ScraperManager scraper;
private JButton connectButton;
private JLabel connectionStatusLabel;
private Cursor waitCursor = new Cursor(Cursor.WAIT_CURSOR);
private Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);
private JButton okButton;
private JCheckBox genreCheckBox;
private JCheckBox composerCheckBox;
public C64comOptionsPanel(ScraperManager scraper, JButton okButton)
{
this.scraper = scraper;
this.okButton = okButton;
GridBagLayout gridBagLayout = new GridBagLayout();
setLayout(gridBagLayout);
GridBagConstraints gbc_infoLabel = new GridBagConstraints();
gbc_infoLabel.gridwidth = 2;
gbc_infoLabel.weightx = 1.0;
gbc_infoLabel.fill = GridBagConstraints.HORIZONTAL;
gbc_infoLabel.insets = new Insets(10, 10, 5, 10);
gbc_infoLabel.gridx = 0;
gbc_infoLabel.gridy = 0;
add(getInfoEditorPane(), gbc_infoLabel);
GridBagConstraints gbc_urlTextField = new GridBagConstraints();
gbc_urlTextField.weightx = 1.0;
gbc_urlTextField.insets = new Insets(0, 10, 10, 5);
gbc_urlTextField.fill = GridBagConstraints.HORIZONTAL;
gbc_urlTextField.gridx = 0;
gbc_urlTextField.gridy = 1;
add(getUrlTextField(), gbc_urlTextField);
GridBagConstraints gbc_connectButton = new GridBagConstraints();
gbc_connectButton.anchor = GridBagConstraints.NORTHWEST;
gbc_connectButton.insets = new Insets(0, 0, 10, 10);
gbc_connectButton.gridx = 1;
gbc_connectButton.gridy = 1;
add(getConnectButton(), gbc_connectButton);
GridBagConstraints gbc_connectionStatusLabel = new GridBagConstraints();
gbc_connectionStatusLabel.gridwidth = 2;
gbc_connectionStatusLabel.insets = new Insets(0, 10, 10, 5);
gbc_connectionStatusLabel.gridx = 0;
gbc_connectionStatusLabel.gridy = 2;
add(getConnectionStatusLabel(), gbc_connectionStatusLabel);
GridBagConstraints gbc_fieldsPanel = new GridBagConstraints();
gbc_fieldsPanel.insets = new Insets(0, 0, 10, 0);
gbc_fieldsPanel.gridwidth = 2;
gbc_fieldsPanel.anchor = GridBagConstraints.NORTH;
gbc_fieldsPanel.weighty = 1.0;
gbc_fieldsPanel.weightx = 1.0;
gbc_fieldsPanel.gridx = 0;
gbc_fieldsPanel.gridy = 3;
add(getFieldsPanel(), gbc_fieldsPanel);
okButton.setEnabled(false);
}
private JEditorPane getInfoEditorPane()
{
if (infoEditorPane == null)
{
String info = "<html>To scrape information from c64.com you need to specify the URL for a specific game." +
"<ol><li>Go to <a href='http:/www.c64.com/'>http:/www.c64.com/</a> and search for the game you want to<br>scrape information for.</li>" +
"<li>Select the game and click on the \"direct link\" for it. Copy the URL to the<br>field below." +
"(Example: http://www.c64.com/games/53)</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);
infoEditorPane.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 infoEditorPane;
}
JTextField getUrlTextField()
{
if (urlTextField == null)
{
urlTextField = new JTextField();
urlTextField.setText("http://www.c64.com/games/");
urlTextField.addKeyListener(new KeyAdapter()
{
@Override
public void keyPressed(KeyEvent e)
{
if (e.getKeyCode() == KeyEvent.VK_ENTER)
{
getConnectButton().doClick();
}
}
});
}
return urlTextField;
}
private JPanel getFieldsPanel()
{
if (fieldsPanel == null)
{
fieldsPanel = new JPanel();
GridBagLayout gbl_fieldsPanel = new GridBagLayout();
fieldsPanel.setLayout(gbl_fieldsPanel);
GridBagConstraints gbc_fieldsInfoLabel = new GridBagConstraints();
gbc_fieldsInfoLabel.gridwidth = 3;
gbc_fieldsInfoLabel.weightx = 1.0;
gbc_fieldsInfoLabel.insets = new Insets(0, 0, 5, 0);
gbc_fieldsInfoLabel.anchor = GridBagConstraints.NORTH;
gbc_fieldsInfoLabel.gridx = 0;
gbc_fieldsInfoLabel.gridy = 0;
fieldsPanel.add(getFieldsInfoLabel(), gbc_fieldsInfoLabel);
GridBagConstraints gbc_titleCheckBox = new GridBagConstraints();
gbc_titleCheckBox.insets = new Insets(0, 0, 5, 5);
gbc_titleCheckBox.anchor = GridBagConstraints.WEST;
gbc_titleCheckBox.gridx = 0;
gbc_titleCheckBox.gridy = 1;
fieldsPanel.add(getTitleCheckBox(), gbc_titleCheckBox);
GridBagConstraints gbc_authorCheckBox = new GridBagConstraints();
gbc_authorCheckBox.insets = new Insets(0, 0, 5, 5);
gbc_authorCheckBox.anchor = GridBagConstraints.WEST;
gbc_authorCheckBox.gridx = 0;
gbc_authorCheckBox.gridy = 2;
fieldsPanel.add(getAuthorCheckBox(), gbc_authorCheckBox);
GridBagConstraints gbc_composerCheckBox = new GridBagConstraints();
gbc_composerCheckBox.anchor = GridBagConstraints.WEST;
gbc_composerCheckBox.insets = new Insets(0, 0, 5, 5);
gbc_composerCheckBox.gridx = 1;
gbc_composerCheckBox.gridy = 2;
fieldsPanel.add(getComposerCheckBox(), gbc_composerCheckBox);
GridBagConstraints gbc_coverCheckBox = new GridBagConstraints();
gbc_coverCheckBox.anchor = GridBagConstraints.WEST;
gbc_coverCheckBox.insets = new Insets(0, 0, 5, 5);
gbc_coverCheckBox.gridx = 1;
gbc_coverCheckBox.gridy = 3;
fieldsPanel.add(getCoverCheckBox(), gbc_coverCheckBox);
GridBagConstraints gbc_yearCheckBox = new GridBagConstraints();
gbc_yearCheckBox.insets = new Insets(0, 0, 5, 5);
gbc_yearCheckBox.anchor = GridBagConstraints.WEST;
gbc_yearCheckBox.gridx = 0;
gbc_yearCheckBox.gridy = 3;
fieldsPanel.add(getYearCheckBox(), gbc_yearCheckBox);
GridBagConstraints gbc_descriptionCheckBox = new GridBagConstraints();
gbc_descriptionCheckBox.insets = new Insets(0, 0, 5, 5);
gbc_descriptionCheckBox.anchor = GridBagConstraints.WEST;
gbc_descriptionCheckBox.gridx = 1;
gbc_descriptionCheckBox.gridy = 1;
fieldsPanel.add(getDescriptionCheckBox(), gbc_descriptionCheckBox);
GridBagConstraints gbc_genreCheckBox = new GridBagConstraints();
gbc_genreCheckBox.insets = new Insets(0, 0, 0, 5);
gbc_genreCheckBox.anchor = GridBagConstraints.WEST;
gbc_genreCheckBox.gridx = 0;
gbc_genreCheckBox.gridy = 4;
fieldsPanel.add(getGenreCheckBox(), gbc_genreCheckBox);
GridBagConstraints gbc_screensCheckBox = new GridBagConstraints();
gbc_screensCheckBox.insets = new Insets(0, 0, 0, 5);
gbc_screensCheckBox.anchor = GridBagConstraints.WEST;
gbc_screensCheckBox.gridx = 1;
gbc_screensCheckBox.gridy = 4;
fieldsPanel.add(getScreensCheckBox(), gbc_screensCheckBox);
}
return fieldsPanel;
}
private JLabel getFieldsInfoLabel()
{
if (fieldsInfoLabel == null)
{
fieldsInfoLabel = new JLabel("Select fields to scrape:");
}
return fieldsInfoLabel;
}
private JCheckBox getTitleCheckBox()
{
if (titleCheckBox == null)
{
titleCheckBox = new JCheckBox("Title");
titleCheckBox.setSelected(true);
titleCheckBox.setEnabled(false);
}
return titleCheckBox;
}
private JCheckBox getAuthorCheckBox()
{
if (authorCheckBox == null)
{
authorCheckBox = new JCheckBox("Author");
authorCheckBox.setSelected(true);
authorCheckBox.setEnabled(false);
}
return authorCheckBox;
}
private JCheckBox getYearCheckBox()
{
if (yearCheckBox == null)
{
yearCheckBox = new JCheckBox("Year");
yearCheckBox.setSelected(true);
yearCheckBox.setEnabled(false);
}
return yearCheckBox;
}
private JCheckBox getDescriptionCheckBox()
{
if (descriptionCheckBox == null)
{
descriptionCheckBox = new JCheckBox("Description");
descriptionCheckBox.setSelected(false);
descriptionCheckBox.setEnabled(false);
}
return descriptionCheckBox;
}
private JCheckBox getCoverCheckBox()
{
if (coverCheckBox == null)
{
coverCheckBox = new JCheckBox("Cover");
coverCheckBox.setSelected(true);
coverCheckBox.setEnabled(false);
}
return coverCheckBox;
}
private JCheckBox getScreensCheckBox()
{
if (screensCheckBox == null)
{
screensCheckBox = new JCheckBox("Screenshots");
screensCheckBox.setSelected(true);
screensCheckBox.setEnabled(false);
}
return screensCheckBox;
}
private void enableCheckBoxes(boolean enable)
{
titleCheckBox.setEnabled(enable);
authorCheckBox.setEnabled(enable);
yearCheckBox.setEnabled(enable);
// descriptionCheckBox.setEnabled(enable);
coverCheckBox.setEnabled(enable);
screensCheckBox.setEnabled(enable);
genreCheckBox.setEnabled(enable);
composerCheckBox.setEnabled(enable);
}
public ScraperFields getScraperFields()
{
ScraperFields returnValue = new ScraperFields();
returnValue.setTitle(titleCheckBox.isSelected());
returnValue.setAuthor(authorCheckBox.isSelected());
returnValue.setYear(yearCheckBox.isSelected());
returnValue.setGenre(genreCheckBox.isSelected());
returnValue.setDescription(false);
returnValue.setCover(coverCheckBox.isSelected());
returnValue.setScreenshots(screensCheckBox.isSelected());
returnValue.setComposer(composerCheckBox.isSelected());
return returnValue;
}
JButton getConnectButton()
{
if (connectButton == null)
{
connectButton = new JButton("Connect");
connectButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
try
{
C64comOptionsPanel.this.setCursor(waitCursor);
scraper.connectScraper(urlTextField.getText());
getConnectionStatusLabel().setText("Connection status: OK");
getConnectionStatusLabel().setIcon(new ImageIcon(this.getClass().getResource("/se/lantz/check.png")));
enableCheckBoxes(true);
okButton.setEnabled(true);
C64comOptionsPanel.this.setCursor(defaultCursor);
SwingUtilities.invokeLater(() -> SwingUtilities.getRootPane(connectButton).setDefaultButton(okButton));
}
catch (Exception e2)
{
getConnectionStatusLabel().setText("Connection status: Error. Invalid URL?");
getConnectionStatusLabel().setIcon(new ImageIcon(this.getClass().getResource("/se/lantz/failure.png")));
enableCheckBoxes(false);
okButton.setEnabled(false);
C64comOptionsPanel.this.setCursor(defaultCursor);
SwingUtilities.invokeLater(() -> SwingUtilities.getRootPane(connectButton).setDefaultButton(connectButton));
}
}
});
}
return connectButton;
}
private JLabel getConnectionStatusLabel()
{
if (connectionStatusLabel == null)
{
connectionStatusLabel = new JLabel(" ");
connectionStatusLabel.setFont(connectionStatusLabel.getFont().deriveFont(Font.BOLD));
connectionStatusLabel.setHorizontalTextPosition(SwingConstants.LEADING);
}
return connectionStatusLabel;
}
private JCheckBox getGenreCheckBox()
{
if (genreCheckBox == null)
{
genreCheckBox = new JCheckBox("Genre");
genreCheckBox.setSelected(true);
genreCheckBox.setEnabled(false);
}
return genreCheckBox;
}
private JCheckBox getComposerCheckBox()
{
if (composerCheckBox == null)
{
composerCheckBox = new JCheckBox("Composer");
composerCheckBox.setSelected(true);
composerCheckBox.setEnabled(false);
}
return composerCheckBox;
}
}

View File

@ -1,50 +1,139 @@
package se.lantz.gui.scraper;
import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import se.lantz.gui.BaseDialog;
import se.lantz.manager.ScraperManager;
import se.lantz.manager.ScraperManager.SCRAPER;
import se.lantz.model.data.ScraperFields;
public class ScraperDialog extends BaseDialog
{
private MobyGamesOptionsPanel mbyGamesPanel;
private MobyGamesOptionsPanel mobyGamesPanel;
private C64comOptionsPanel c64comPanel;
private final ScraperManager scraper;
private CardLayout cardLayout = new CardLayout();
private JPanel scraperSelectionPanel;
private JLabel scraperInfoLabel;
private JComboBox scraperComboBox;
private JPanel cardPanel;
public ScraperDialog(Frame owner, ScraperManager scraper)
{
super(owner);
setTitle("Scrape game information");
this.scraper = scraper;
JPanel content = new JPanel();
content.setLayout(new BorderLayout());
content.add(getMobyGamesPanel(), BorderLayout.CENTER);
cardPanel = new JPanel();
cardPanel.setLayout(cardLayout);
cardPanel.add(getMobyGamesPanel(), "moby");
cardPanel.add(getC64comPanel(), "c64com");
content.add(cardPanel, BorderLayout.CENTER);
addContent(content);
setTitle("Scrape game information");
content.add(getScraperSelectionPanel(), BorderLayout.NORTH);
this.setResizable(false);
}
private MobyGamesOptionsPanel getMobyGamesPanel()
{
if (mbyGamesPanel == null)
if (mobyGamesPanel == null)
{
mbyGamesPanel = new MobyGamesOptionsPanel(scraper, getOkButton());
mobyGamesPanel = new MobyGamesOptionsPanel(scraper, getOkButton());
}
return mbyGamesPanel;
return mobyGamesPanel;
}
private C64comOptionsPanel getC64comPanel()
{
if (c64comPanel == null)
{
c64comPanel = new C64comOptionsPanel(scraper, getOkButton());
}
return c64comPanel;
}
public ScraperFields getScraperFields()
{
return getMobyGamesPanel().getScraperFields();
}
@Override
public boolean showDialog()
{
getMobyGamesPanel().getUrlTextField().requestFocusInWindow();
this.getRootPane().setDefaultButton(getMobyGamesPanel().getConnectButton());
return super.showDialog();
}
private JPanel getScraperSelectionPanel()
{
if (scraperSelectionPanel == null)
{
scraperSelectionPanel = new JPanel();
GridBagLayout gbl_scraperSelectionPanel = new GridBagLayout();
scraperSelectionPanel.setLayout(gbl_scraperSelectionPanel);
GridBagConstraints gbc_scraperInfoLabel = new GridBagConstraints();
gbc_scraperInfoLabel.insets = new Insets(10, 10, 5, 5);
gbc_scraperInfoLabel.gridx = 0;
gbc_scraperInfoLabel.gridy = 0;
scraperSelectionPanel.add(getScraperInfoLabel(), gbc_scraperInfoLabel);
GridBagConstraints gbc_scraperComboBox = new GridBagConstraints();
gbc_scraperComboBox.insets = new Insets(10, 0, 5, 5);
gbc_scraperComboBox.gridx = 1;
gbc_scraperComboBox.gridy = 0;
scraperSelectionPanel.add(getScraperComboBox(), gbc_scraperComboBox);
}
return scraperSelectionPanel;
}
private JLabel getScraperInfoLabel()
{
if (scraperInfoLabel == null)
{
scraperInfoLabel = new JLabel("Select a site to scrape:");
}
return scraperInfoLabel;
}
private JComboBox getScraperComboBox()
{
if (scraperComboBox == null)
{
scraperComboBox = new JComboBox();
scraperComboBox.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
if (scraperComboBox.getSelectedItem().equals("www.mobygames.com"))
{
cardLayout.show(cardPanel, "moby");
scraper.setScrapertoUse(SCRAPER.moby);
getRootPane().setDefaultButton(getMobyGamesPanel().getConnectButton());
}
else
{
cardLayout.show(cardPanel, "c64com");
scraper.setScrapertoUse(SCRAPER.c64com);
getRootPane().setDefaultButton(getC64comPanel().getConnectButton());
}
}
});
scraperComboBox.addItem("www.mobygames.com");
scraperComboBox.addItem("www.c64.com");
}
return scraperComboBox;
}
}

View File

@ -8,65 +8,87 @@ import se.lantz.model.InfoModel;
import se.lantz.model.MainViewModel;
import se.lantz.model.SystemModel;
import se.lantz.model.data.ScraperFields;
import se.lantz.scraper.C64comScraper;
import se.lantz.scraper.MobyGamesScraper;
import se.lantz.scraper.Scraper;
public class ScraperManager
{
MobyGamesScraper scraper = new MobyGamesScraper();
public enum SCRAPER {
moby, c64com
}
Scraper mobyScraper = new MobyGamesScraper();
Scraper c64comScraper = new C64comScraper();
Scraper usedScraper = mobyScraper;
List<BufferedImage> screenshotsList = new ArrayList<>();
private InfoModel infoModel;
private SystemModel systemModel;
private ScraperFields fields;
public ScraperManager(MainViewModel model)
{
this.infoModel = model.getInfoModel();
this.systemModel = model.getSystemModel();
}
public void setScrapertoUse(SCRAPER scraper)
{
switch (scraper)
{
case moby:
usedScraper = mobyScraper;
break;
case c64com:
usedScraper = c64comScraper;
break;
default:
break;
}
}
public void connectScraper(String url) throws Exception
{
scraper.connectToMobyGames(url);
usedScraper.connect(url);
}
public void scrapeGameInformation(ScraperFields fields)
{
this.fields = fields;
scraper.scrapeInformation(fields);
usedScraper.scrapeInformation(fields);
}
public void scrapeScreenshots()
{
screenshotsList = scraper.scrapeScreenshots();
screenshotsList = usedScraper.scrapeScreenshots();
}
public List<BufferedImage> getScreenshots()
{
return screenshotsList;
}
public void updateModelWithGamesInfo()
{
if (fields.isTitle())
{
infoModel.setTitle(scraper.getTitle());
infoModel.setTitle(usedScraper.getTitle());
}
if (fields.isAuthor())
{
infoModel.setAuthor(scraper.getAuthor());
infoModel.setAuthor(usedScraper.getAuthor());
}
if (fields.isYear())
{
infoModel.setYear(scraper.getYear());
{
infoModel.setYear(usedScraper.getYear());
}
if (fields.isDescription())
{
infoModel.setDescription(scraper.getDescription());
infoModel.setDescription(usedScraper.getDescription());
}
if (fields.isGenre())
{
String genre = scraper.getGenre();
String genre = usedScraper.getGenre();
if (!genre.isEmpty())
{
infoModel.setGenre(genre);
@ -74,14 +96,14 @@ public class ScraperManager
}
if (fields.isComposer())
{
infoModel.setComposer(scraper.getComposer());
infoModel.setComposer(usedScraper.getComposer());
}
if (fields.isCover())
{
infoModel.setCoverImage(scraper.getCover());
infoModel.setCoverImage(usedScraper.getCover());
}
//Set system based on the scraped URL
if (scraper.isC64())
if (usedScraper.isC64())
{
systemModel.setC64(true);
}
@ -90,7 +112,7 @@ public class ScraperManager
systemModel.setVic(true);
}
}
public void updateModelWithScreenshotImages(BufferedImage screen1, BufferedImage screen2)
{
infoModel.setScreen1Image(screen1);

View File

@ -21,6 +21,7 @@ import se.lantz.model.data.GameListData;
import se.lantz.model.data.GameView;
import se.lantz.model.data.ScraperFields;
import se.lantz.scraper.MobyGamesScraper;
import se.lantz.scraper.Scraper;
import se.lantz.util.FileManager;
public class MainViewModel extends AbstractModel
@ -49,7 +50,7 @@ public class MainViewModel extends AbstractModel
private GameListData selectedData;
MobyGamesScraper scraper = new MobyGamesScraper();
Scraper scraper = new MobyGamesScraper();
public void initialize()
{

View File

@ -10,73 +10,100 @@ public class ScraperFields
private boolean cover = true;
private boolean screenshots = true;
private boolean composer = true;
private boolean game = false;
public ScraperFields()
{
//Empty
}
public boolean isTitle()
{
return title;
}
public void setTitle(boolean title)
{
this.title = title;
}
public boolean isAuthor()
{
return author;
}
public void setAuthor(boolean author)
{
this.author = author;
}
public boolean isYear()
{
return year;
}
public void setYear(boolean year)
{
this.year = year;
}
public boolean isDescription()
{
return description;
}
public void setDescription(boolean description)
{
this.description = description;
}
public boolean isCover()
{
return cover;
}
public void setCover(boolean cover)
{
this.cover = cover;
}
public boolean isScreenshots()
{
return screenshots;
}
public void setScreenshots(boolean screenshots)
{
this.screenshots = screenshots;
}
public boolean isGenre()
{
return genre;
}
public void setGenre(boolean genre)
{
this.genre = genre;
}
public boolean isComposer()
{
return composer;
}
public void setComposer(boolean composer)
{
this.composer = composer;
}
public boolean isGame()
{
return game;
}
public void setGame(boolean game)
{
this.game = game;
}
}

View File

@ -16,12 +16,13 @@ import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import se.lantz.model.data.ScraperFields;
import se.lantz.util.ExceptionHandler;
public class C64comScraper
public class C64comScraper implements Scraper
{
private static final Logger logger = LoggerFactory.getLogger(C64comScraper.class);
private String c64comGameUrl = "http://www.c64.com/games/259";
private String c64comGameUrl = "http://www.c64.com/games/53";
private String screenshotCssQuery = "html > body > table > tbody > tr > td:eq(0) > table > tbody > tr:eq(1) > td > table:eq(1) > tbody > tr > td:eq(4) > table > tbody > tr:eq(0) > td > img";
@ -34,16 +35,17 @@ public class C64comScraper
private String scrapedTitle;
private String scrapedYear;
private int scrapedYear;
private String scrapedAuthor;
private List<String> scrapedMusicList = new ArrayList<>();
private String scrapedGenre;
private BufferedImage scrapedCover;
public static void main(String[] args)
{
C64comScraper scraper = new C64comScraper();
scraper.scrapeInformation();
scraper.scrapeInformation(null);
scraper.scrapeScreenshots();
}
public C64comScraper()
@ -51,14 +53,16 @@ public class C64comScraper
// TODO Auto-generated constructor stub
}
public void connectToC64Com(String url) throws IOException
@Override
public void connect(String url) throws IOException
{
this.c64comGameUrl = "";
Jsoup.connect(url).method(Connection.Method.GET).execute();
this.c64comGameUrl = url;
}
public void scrapeInformation()//ScraperFields fields)
@Override
public void scrapeInformation(ScraperFields fields)
{
Document doc;
try
@ -82,7 +86,7 @@ public class C64comScraper
Elements yearElements = mainFrameDocument.select(yearCssQuery);
if (yearElements.first() != null)
{
scrapedYear = yearElements.first().text();
scrapedYear = Integer.parseInt(yearElements.first().text().trim());
}
logger.debug("scraped year: {}", scrapedYear);
@ -110,11 +114,27 @@ public class C64comScraper
String music = child.select("td:eq(1)").first().text();
scrapedMusicList.add(music);
logger.debug("scraped music: {}", music);
continue;
}
else if (info.equalsIgnoreCase("Genre:"))
if (info.equalsIgnoreCase("Genre:"))
{
scrapedGenre = child.select("td:eq(1)").first().text();
logger.debug("scraped genre: {}", scrapedGenre);
continue;
}
if (info.startsWith("Inlay"))
{
String url = child.select("td:eq(1) > a").first().attr("href");
//Select the right part
url = url.substring(url.indexOf("'")+1);
url = url.substring(0, url.indexOf("'"));
url = url.substring(url.indexOf("=")+1);
url = "http://www.c64.com/games/" + url;
URL imageUrl = new URL(url);
scrapedCover = ImageIO.read(imageUrl);
logger.debug("Cover url: {}", url);
// http://www.c64.com/games/inlay.php?url=inlays/a/arkanoid.png
}
}
}
@ -125,7 +145,8 @@ public class C64comScraper
}
}
private List<BufferedImage> scrapeScreenshots()
@Override
public List<BufferedImage> scrapeScreenshots()
{
List<BufferedImage> screensList = new ArrayList<>();
Document doc;
@ -154,8 +175,58 @@ public class C64comScraper
}
catch (IOException e)
{
ExceptionHandler.handleException(e, "Could not scrape screenshot");
logger.warn("Could not scrape all six screenshots", e);
}
return screensList;
}
@Override
public String getTitle()
{
return scrapedTitle;
}
@Override
public String getAuthor()
{
return scrapedAuthor;
}
@Override
public int getYear()
{
return scrapedYear;
}
@Override
public String getDescription()
{
//Not supported, no description on c64.com
return "";
}
@Override
public String getGenre()
{
return scrapedGenre;
}
@Override
public String getComposer()
{
return String.join(", ", scrapedMusicList);
}
@Override
public BufferedImage getCover()
{
return scrapedCover;
}
@Override
public boolean isC64()
{
// Only C64 games available
return true;
}
}

View File

@ -25,7 +25,7 @@ import org.slf4j.LoggerFactory;
import se.lantz.model.data.ScraperFields;
import se.lantz.util.ExceptionHandler;
public class MobyGamesScraper
public class MobyGamesScraper implements Scraper
{
private static final Logger logger = LoggerFactory.getLogger(MobyGamesScraper.class);
@ -72,13 +72,15 @@ public class MobyGamesScraper
genreMap.put("Sports", "sport");
}
public void connectToMobyGames(String url) throws IOException
@Override
public void connect(String url) throws IOException
{
this.mobyGamesGameUrl = "";
Jsoup.connect(url).method(Connection.Method.GET).execute();
this.mobyGamesGameUrl = url;
}
@Override
public void scrapeInformation(ScraperFields fields)
{
Document doc;
@ -142,36 +144,43 @@ public class MobyGamesScraper
}
}
@Override
public String getTitle()
{
return scrapedTitle;
}
@Override
public String getAuthor()
{
return scrapedAuthor;
}
@Override
public int getYear()
{
return scrapedYear;
}
@Override
public String getDescription()
{
return scrapedDescription;
}
@Override
public String getGenre()
{
return scrapedGenre;
}
@Override
public String getComposer()
{
return scrapedComposer;
}
@Override
public BufferedImage getCover()
{
return scrapedCover;
@ -305,6 +314,7 @@ public class MobyGamesScraper
return null;
}
@Override
public List<BufferedImage> scrapeScreenshots()
{
List<BufferedImage> returnList = new ArrayList<>();
@ -359,6 +369,7 @@ public class MobyGamesScraper
return null;
}
@Override
public boolean isC64()
{
return mobyGamesGameUrl.contains("c64");

View File

@ -0,0 +1,34 @@
package se.lantz.scraper;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.List;
import se.lantz.model.data.ScraperFields;
public interface Scraper
{
void connect(String url) throws IOException;
void scrapeInformation(ScraperFields fields);
String getTitle();
String getAuthor();
int getYear();
String getDescription();
String getGenre();
String getComposer();
BufferedImage getCover();
List<BufferedImage> scrapeScreenshots();
boolean isC64();
}