/** * Refresh the repository from the URL. * * @throws Exception */ public boolean refresh() { exception = null; try { resources.clear(); parseDocument(url); visited = null; return true; } catch (Exception e) { e.printStackTrace(); exception = e; } return false; }
/** * We have a referral to another repository. Just create another parser and read it inline. * * @param parser */ void referral(XmlPullParser parser) { // TODO handle depth! try { parser.require(XmlPullParser.START_TAG, null, "referral"); // String depth = parser.getAttributeValue(null, "depth"); String path = parser.getAttributeValue(null, "url"); URL url = new URL(this.url, path); parseDocument(url); parser.next(); parser.require(XmlPullParser.END_TAG, null, "referral"); } catch (Exception e) { e.printStackTrace(); } }
public static void main(String[] args) { try { MysqlBggRepository rep = new MysqlBggRepository(); try { for (BggGame game : rep.getCorrelatedGamesList()) { URL url = new URL( "http://www.boardgamegeek.com/xmlapi/boardgame/" + game.getBggId() + "?stats=1"); InputStream is = url.openStream(); XmlPullParser parser = XmlPullParserFactory.newInstance().newPullParser(); parser.setInput(is, "UTF8"); int result = parser.getEventType(); boolean ratingFound = false; while (result != XmlPullParser.END_DOCUMENT) { if (result == XmlPullParser.START_TAG) { if ("average".equals(parser.getName())) { Float rating = Float.parseFloat(parser.nextText()); game.setAvgBggRating(rating); rep.setAvgBggRating(game, rating); if (ratingFound) { throw new RuntimeException("Already found one rating"); } else { System.out.println( "Set (" + game.getBggId() + ") " + game.getName() + " to: " + rating); ratingFound = true; } } } result = parser.next(); } } } finally { try { rep.close(); } catch (Exception e) { } } } catch (Exception e) { e.printStackTrace(); } }