List of usage examples for org.jsoup.nodes Element child
public Element child(int index)
From source file:de.geeksfactory.opacclient.apis.IOpac.java
static void parseMediaList(List<LentItem> media, Document doc, JSONObject data) { if (doc.select("a[name=AUS]").size() == 0) return;/*w w w . ja va 2 s .c o m*/ Elements copytrs = doc.select("a[name=AUS] ~ table, a[name=AUS] ~ form table").first().select("tr"); doc.setBaseUri(data.optString("baseurl")); DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN); int trs = copytrs.size(); if (trs < 2) { return; } assert (trs > 0); JSONObject copymap = new JSONObject(); try { if (data.has("accounttable")) { copymap = data.getJSONObject("accounttable"); } } catch (JSONException e) { } Pattern datePattern = Pattern.compile("\\d{2}\\.\\d{2}\\.\\d{4}"); for (int i = 1; i < trs; i++) { Element tr = copytrs.get(i); LentItem item = new LentItem(); if (copymap.optInt("title", 0) >= 0) { item.setTitle(tr.child(copymap.optInt("title", 0)).text().trim().replace("\u00a0", "")); } if (copymap.optInt("author", 1) >= 0) { item.setAuthor(tr.child(copymap.optInt("author", 1)).text().trim().replace("\u00a0", "")); } if (copymap.optInt("format", 2) >= 0) { item.setFormat(tr.child(copymap.optInt("format", 2)).text().trim().replace("\u00a0", "")); } int prolongCount = 0; if (copymap.optInt("prolongcount", 3) >= 0) { prolongCount = Integer .parseInt(tr.child(copymap.optInt("prolongcount", 3)).text().trim().replace("\u00a0", "")); item.setStatus(String.valueOf(prolongCount) + "x verl."); } if (data.optInt("maxprolongcount", -1) != -1) { item.setRenewable(prolongCount < data.optInt("maxprolongcount", -1)); } if (copymap.optInt("returndate", 4) >= 0) { String value = tr.child(copymap.optInt("returndate", 4)).text().trim().replace("\u00a0", ""); Matcher matcher = datePattern.matcher(value); if (matcher.find()) { try { item.setDeadline(fmt.parseLocalDate(matcher.group())); } catch (IllegalArgumentException e1) { e1.printStackTrace(); } } } if (copymap.optInt("prolongurl", 5) >= 0) { if (tr.children().size() > copymap.optInt("prolongurl", 5)) { Element cell = tr.child(copymap.optInt("prolongurl", 5)); if (cell.select("input[name=MedNrVerlAll]").size() > 0) { // new iOPAC Version 1.45 - checkboxes to prolong multiple items // internal convention: We add "NEW" to the media ID to show that we have // the new iOPAC version Element input = cell.select("input[name=MedNrVerlAll]").first(); String value = input.val(); item.setProlongData("NEW" + value); item.setId(value.split(";")[0]); if (input.hasAttr("disabled")) item.setRenewable(false); } else { // previous versions - link for prolonging on every medium String link = cell.select("a").attr("href"); item.setProlongData(link); // find media number with regex Pattern pattern = Pattern.compile("mednr=([^&]*)&"); Matcher matcher = pattern.matcher(link); if (matcher.find() && matcher.group() != null) item.setId(matcher.group(1)); } } } media.add(item); } assert (media.size() == trs - 1); }
From source file:com.example.muzei.muzeiapod.ApodNasaArtSource.java
@Override protected void onTryUpdate(int reason) throws RetryException { URI topUri;//from w ww . j a va 2s .co m try { topUri = new URI("http://apod.nasa.gov/"); } catch (URISyntaxException e) { return; } URI mainUri = topUri.resolve("/apod/astropix.html"); String bodyStr = getURLContent(mainUri.toString()); /* TODO code below should go to a separate method/class */ /* start parsing page */ Document doc = Jsoup.parse(bodyStr); Element body = doc.body(); /* get image URI */ Element firstCenterTag = body.child(0); Element imgAnchor = firstCenterTag.getElementsByTag("a").last(); Element img = imgAnchor.getElementsByTag("img").first(); URI bigImageUri = topUri.resolve("/apod/" + img.attr("src")); String uri = bigImageUri.toString(); /* get title */ Element secondCenterTag = body.child(1); Element titleElem = secondCenterTag.child(0); String title = titleElem.text(); /* get byline */ String secondCenterText = secondCenterTag.text(); /* byline: everything after 'title' above */ int idx = secondCenterText.lastIndexOf(title) + title.length(); String byline = secondCenterText.substring(idx).trim(); /* TODO figure out the permanent link */ String link = "http://apod.nasa.gov/apod/astropix.html"; publishArtwork(new Artwork.Builder().title(title).byline(byline).imageUri(Uri.parse(uri)).token(title) .viewIntent(new Intent(Intent.ACTION_VIEW, Uri.parse(link))).build()); scheduleUpdate(System.currentTimeMillis() + ROTATE_TIME_MILLIS); }
From source file:com.liato.bankdroid.banking.banks.Bioklubben.java
@Override public void update() throws BankException, LoginException, BankChoiceException { super.update(); if (username == null || password == null || username.length() == 0 || password.length() == 0) { throw new LoginException(res.getText(R.string.invalid_username_password).toString()); }//from ww w . j ava 2 s .c o m urlopen = login(); try { Document d = Jsoup .parse(urlopen.open("http://bioklubben.sf.se/MyPurchases.aspx?ParentTreeID=1&TreeID=1")); Element e = d.getElementById("ctl00_ContentPlaceHolder1_BonusPointsLabel"); if (e == null) { throw new BankException(res.getText(R.string.unable_to_find).toString() + " points element."); } BigDecimal b = Helpers.parseBalance(e.text()); Account a = new Account("Pong", b, "1"); a.setCurrency(context.getString(R.string.points)); accounts.add(a); balance = balance.add(a.getBalance()); Elements es = d.select(".GridViewStd_Item,.GridViewStd_ItemAlt"); List<Transaction> transactions = new ArrayList<Transaction>(); if (es != null) { for (Element el : es) { transactions.add(new Transaction(el.child(0).text().trim(), el.child(1).text().trim(), Helpers.parseBalance(el.child(2).text()))); } } a.setTransactions(transactions); } catch (IOException e) { if (e == null) { throw new BankException(e.getMessage()); } } if (accounts.isEmpty()) { throw new BankException(res.getText(R.string.no_accounts_found).toString()); } super.updateComplete(); }
From source file:io.github.carlomicieli.footballdb.starter.parsers.DraftParser.java
private DraftedPlayer mapToDraftedPlayer(Element e) { if (!e.tagName().equals("tr")) { throw new IllegalArgumentException("Invalid tag"); }//from w ww . j a v a 2 s . c o m int round = TryConvert.toIntegerOrGet(e.child(0).text(), -1); int pick = TryConvert.toIntegerOrGet(e.child(1).text(), -1); String team = e.child(2).text(); String name = e.child(3).text(); String pos = e.child(4).text(); String college = e.child(27).text(); return Draft.newPick().college(college).name(name).round(round).number(pick).position(pos).team(team) .build(); }
From source file:com.anhao.spring.service.impl.PhotosServiceImpl.java
@Override public void process(Document doc) { Elements links = doc.select("section ul li"); for (Element li : links) { Element figure = li.child(0); String wallpaperId = figure.attr("data-wallpaper-id"); /**/* w w w.j a v a 2 s.c om*/ * ??? */ String tempUUID = jobPhotosDAO.findByWallpaperId(wallpaperId); if (StringUtils.isNotEmpty(tempUUID)) { logger.info("wallpapers id {} thumbnail exist.", wallpaperId); //??wallpaperID? ??? continue; } String thumbnail = "http://alpha.wallhaven.cc/wallpapers/thumb/small/th-" + wallpaperId + ".jpg"; String full = "http://wallpapers.wallhaven.cc/wallpapers/full/wallhaven-" + wallpaperId + ".jpg"; /** * fastdfs?fastdfs?/tmp/ */ boolean smallStatus = download(thumbnail, "/tmp/small" + wallpaperId + ".jpg"); boolean fullStatus = download(full, "/tmp/full" + wallpaperId + ".jpg"); if (smallStatus && fullStatus) { File thumbnailFile = new File("/tmp/small" + wallpaperId + ".jpg"); String thumbnailPath = storageService.upload(thumbnailFile); thumbnailFile.delete(); File sourceFile = new File("/tmp/full" + wallpaperId + ".jpg"); String sourceFilePath = storageService.upload(sourceFile); EasyImage easyImage = new EasyImage(sourceFile); //??? //sourceFile.delete(); String uuid = UUID.randomUUID().toString(); Photos photos = new Photos(); photos.setId(uuid); photos.setTitle(wallpaperId); photos.setWidth(easyImage.getWidth()); photos.setHeight(easyImage.getHeight()); photos.setSize(sourceFile.length()); photos.setCreate_date(new Date()); photos.setModify_date(new Date()); photos.setLarge(sourceFilePath); photos.setMedium(thumbnailPath); photos.setOrders(1); photos.setSource(sourceFilePath); photos.setThumbnail(thumbnailPath); photos.setAlbum_id("ff8081814f7e13d8014f7e18a95a0000"); photos.setMember_id("1"); photos.setWallhaven(wallpaperId); photos.setStorage_host("http://123.57.240.11"); jobPhotosDAO.add(photos); //2015-10-18 ? getWallpaperTags(wallpaperId); //2015-10-18 ? ? //? photosColorsService.generateColors(sourceFile, uuid); } else { logger.info("wallpapers id {} thumbnail or fullImage not exist.", wallpaperId); } } }
From source file:org.manalith.ircbot.plugin.uriinfo.UriInfoPlugin.java
private String getSiteSpecificTitle(String uri, Document document) { if (uri.startsWith("http://mlbpark.donga.com/bbs/view.php?") || uri.startsWith("http://mlbpark.donga.com/mbs/articleV.php?")) { // MLB Park article Element element = document.getElementsByClass("D14").first(); if (element != null) { try { return element.child(0).text(); } catch (IndexOutOfBoundsException e) { return null; }//from w w w. j a v a 2 s . c om } } else if (uri.startsWith("http://www.slrclub.com/bbs/vx2.php?")) { Element element = document.getElementsByClass("sbj").first(); if (element != null) { return element.text(); } } return null; }
From source file:de.geeksfactory.opacclient.apis.BiBer1992.java
static List<ReservedItem> parseResList(Document doc, JSONObject data) throws JSONException { List<ReservedItem> reservations = new ArrayList<>(); if (doc == null) { // error message as html result return reservations; }//from w w w . j av a 2 s . c o m // parse result list JSONObject copymap; if (!data.has("reservationtable")) { // reservations not specifically supported, let's just try it // with default values but fail silently copymap = new JSONObject(); copymap.put("author", 3); copymap.put("availability", 6); copymap.put("branch", -1); copymap.put("cancelurl", -1); copymap.put("expirationdate", 5); copymap.put("title", 3); } else { copymap = data.getJSONObject("reservationtable"); } DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN); Elements rowElements = doc.select("form[name=vorml] table tr"); // rows: skip 1st row -> title row for (int i = 1; i < rowElements.size(); i++) { Element tr = rowElements.get(i); if (tr.child(0).tagName().equals("th")) { continue; } ReservedItem item = new ReservedItem(); item.setCancelData(tr.select("input[type=checkbox]").attr("name")); // columns: all elements of one media Iterator<?> keys = copymap.keys(); while (keys.hasNext()) { String key = (String) keys.next(); int index = copymap.getInt(key); if (index >= 0) { String value = tr.child(index).text().trim(); switch (key) { case "author": value = findTitleAndAuthor(value)[1]; break; case "title": value = findTitleAndAuthor(value)[0]; break; case "availability": try { value = fmt.parseLocalDate(value).toString(); } catch (IllegalArgumentException e1) { key = "status"; } break; case "expirationdate": try { value = fmt.parseLocalDate(value).toString(); } catch (IllegalArgumentException e1) { key = "status"; } break; } if (value != null && value.length() != 0) { item.set(key, value); } } } reservations.add(item); } return reservations; }
From source file:de.geeksfactory.opacclient.apis.BiBer1992.java
static List<LentItem> parseMediaList(AccountData res, Document doc, JSONObject data) throws JSONException { List<LentItem> media = new ArrayList<>(); if (doc == null) { return media; }//www . j a va2 s .c o m // parse result list JSONObject copymap = data.getJSONObject("accounttable"); Pattern expire = Pattern.compile("Ausweisg.ltigkeit: ([0-9.]+)"); Pattern fees = Pattern.compile("([0-9,.]+) ."); for (Element td : doc.select(".td01x09n")) { String text = td.text().trim(); if (expire.matcher(text).matches()) { res.setValidUntil(expire.matcher(text).replaceAll("$1")); } else if (fees.matcher(text).matches()) { res.setPendingFees(text); } } DateTimeFormatter fmt = DateTimeFormat.forPattern("dd.MM.yyyy").withLocale(Locale.GERMAN); Elements rowElements = doc.select("form[name=medkl] table tr"); // rows: skip 1st row -> title row for (int i = 1; i < rowElements.size(); i++) { Element tr = rowElements.get(i); if (tr.child(0).tagName().equals("th")) { continue; } LentItem item = new LentItem(); Pattern itemIdPat = Pattern.compile("javascript:smAcc\\('[a-z]+','[a-z]+','([A-Za-z0-9]+)'\\)"); // columns: all elements of one media Iterator<?> keys = copymap.keys(); while (keys.hasNext()) { String key = (String) keys.next(); int index; try { index = copymap.has(key) ? copymap.getInt(key) : -1; } catch (JSONException e1) { index = -1; } if (index >= 0) { String value = tr.child(index).text().trim().replace("\u00A0", ""); switch (key) { case "author": value = findTitleAndAuthor(value)[1]; break; case "title": value = findTitleAndAuthor(value)[0]; break; case "returndate": try { value = fmt.parseLocalDate(value).toString(); } catch (IllegalArgumentException e1) { e1.printStackTrace(); } break; } if (tr.child(index).select("a").size() == 1) { Matcher matcher = itemIdPat.matcher(tr.child(index).select("a").attr("href")); if (matcher.find()) item.setId(matcher.group(1)); } if (value != null && value.length() != 0) item.set(key, value); } } if (tr.select("input[type=checkbox][value=YES]").size() > 0) { item.setProlongData(tr.select("input[type=checkbox][value=YES]").attr("name")); } media.add(item); } return media; }
From source file:com.liato.bankdroid.banking.banks.Hemkop.java
@Override public void update() throws BankException, LoginException, BankChoiceException { super.update(); if (username == null || password == null || username.length() == 0 || password.length() == 0) { throw new LoginException(res.getText(R.string.invalid_username_password).toString()); }/*from w w w . j av a 2 s. c om*/ urlopen = login(); Document d = Jsoup.parse(response); Elements amounts = d.select(".bonusStatement .amount"); Elements names = d.select(".bonusStatement .label"); for (int i = 0; i < Math.min(amounts.size(), names.size()); i++) { Element amount = amounts.get(i); Element name = names.get(i); BigDecimal accountBalance = Helpers.parseBalance(amount.ownText()); Account account = new Account(name.ownText().replace(":", "").trim(), accountBalance, String.format("acc_%d", i)); if (i > 0) { account.setAliasfor("acc_0"); } accounts.add(account); balance = balance.add(accountBalance); } if (accounts.isEmpty()) { throw new BankException(res.getText(R.string.no_accounts_found).toString()); } Account account = accounts.get(0); try { response = urlopen.open("https://www.hemkop.se/Mina-sidor/Kontoutdrag/"); d = Jsoup.parse(response); Elements es = d.select(".transactions tbody tr"); ArrayList<Transaction> transactions = new ArrayList<Transaction>(); for (Element e : es) { Transaction t = new Transaction(e.child(1).ownText().trim(), e.child(0).ownText().trim(), Helpers.parseBalance(e.child(3).ownText())); if (!TextUtils.isEmpty(e.child(2).ownText())) { t.setCurrency(Helpers.parseCurrency(e.child(2).ownText().trim(), "SEK")); } transactions.add(t); } account.setTransactions(transactions); es = d.select(".currentBalance,.disposable"); int i = 0; for (Element e : es) { Account a = new Account(e.child(0).ownText().trim(), Helpers.parseBalance(e.child(1).ownText()), String.format("acc_cc_%d", i)); a.setAliasfor("acc_0"); accounts.add(a); i++; } } catch (ClientProtocolException e) { e.printStackTrace(); Log.e(TAG, e.getMessage() != null ? e.getMessage() : ""); } catch (IOException e) { e.printStackTrace(); Log.e(TAG, e.getMessage() != null ? e.getMessage() : ""); } super.updateComplete(); }
From source file:com.cognifide.aet.job.common.comparators.w3chtml5.WarningNodeToW3cHtml5IssueFunction.java
@Override public W3cHtml5Issue apply(Node child) { if (!(child instanceof Element)) { return null; }//from w ww.ja v a 2 s.c o m Element element = (Element) child; W3cHtml5IssueType issueType = W3cHtml5IssueType .valueOf(StringUtils.removeStart(element.attr("class"), "msg_").toUpperCase()); String message = element.getElementsByAttributeValue("class", "msg").html(); String additionalInfo = element.child(1).html(); return new W3cHtml5Issue(0, 0, message, StringUtils.EMPTY, StringUtils.EMPTY, StringUtils.EMPTY, additionalInfo, issueType); }