diff --git a/src/com/jbidwatcher/auction/server/ebay/ebayAuction.java b/src/com/jbidwatcher/auction/server/ebay/ebayAuction.java index a2059f01..51e5471e 100644 --- a/src/com/jbidwatcher/auction/server/ebay/ebayAuction.java +++ b/src/com/jbidwatcher/auction/server/ebay/ebayAuction.java @@ -12,6 +12,7 @@ import com.jbidwatcher.util.Constants; import java.util.Date; +import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -270,7 +271,8 @@ private void loadBuyNow() { setBuyNowUS(zeroDollars); String altBuyNowString1 = mDocument.getNextContentAfterRegexIgnoring(T.s("ebayServer.price"), "([Ii]tem.[Nn]umber|^\\s*[0-9]+\\s*$)"); - if(mDocument.getPrevContent(3).equals("Estimated delivery*")) return; + String deliveryCheck = mDocument.getPrevContent(3); + if(deliveryCheck != null && deliveryCheck.matches("(?i)(standard|estimated) delivery.*")) return; if(altBuyNowString1 != null) { altBuyNowString1 = altBuyNowString1.trim(); } @@ -730,11 +732,19 @@ private static void setMaxBidFromServer(AuctionEntry ae, Currency maxBid) { } private Currency establishCurrentBid(AuctionEntry ae) { + Currency cvtCur = null; + // The set of tags that indicate the current/starting/lowest/winning // bid are 'Current bid', 'Starting bid', 'Lowest bid', // 'Winning bid' so far. - String cvtCur = mDocument.getNextContentAfterRegex(T.s("ebayServer.currentBid")); - setCurBid(Currency.getCurrency(cvtCur)); + List curBidSequence = mDocument.findSequence(T.s("ebayServer.currentBid"), Currency.NAME_REGEX, Currency.VALUE_REGEX); + if (curBidSequence != null) { + cvtCur = Currency.getCurrency(curBidSequence.get(1), curBidSequence.get(2)); + } else { + String foundBid = mDocument.getNextContentAfterRegex(T.s("ebayServer.currentBid")); + if(foundBid != null) cvtCur = Currency.getCurrency(foundBid); + } + if(cvtCur != null) setCurBid(cvtCur); setUSCur(getUSCurrency(getCurBid(), mDocument)); if(getCurBid() == null || getCurBid().isNull()) { @@ -852,16 +862,7 @@ private void checkReserve() { } private int getBidCount(JHTML doc, int quantity) { - String rawBidCount = doc.getNextContentAfterRegex(T.s("ebayServer.bidCount")); - if (rawBidCount == null) { - rawBidCount = doc.getContentBeforeContent("See history"); - if(rawBidCount != null && rawBidCount.matches("^(Purchased|Bid).*")) { - if (rawBidCount.matches("^Purchased.*")) setFixedPrice(true); - rawBidCount = doc.getPrevContent(); - } - if(rawBidCount != null && !StringTools.isNumberOnly(rawBidCount)) rawBidCount = null; - } - + String rawBidCount = getRawBidCount(doc); int bidCount = 0; if(rawBidCount != null) { if(rawBidCount.equals(T.s("ebayServer.purchasesBidCount")) || @@ -898,6 +899,31 @@ private int getBidCount(JHTML doc, int quantity) { return bidCount; } + private String getRawBidCount(JHTML doc) { + List bidSequence = doc.findSequence(T.s("ebayServer.currentBid"), ".*", ".*", "[0-9]+", "bids"); + String rawBidCount; + if(bidSequence == null) { + bidSequence = doc.findSequence(T.s("ebayServer.currentBid"), ".*", "[0-9]+", "bids"); + if(bidSequence != null) { + rawBidCount = bidSequence.get(2); + } else { + rawBidCount = doc.getNextContentAfterRegex(T.s("ebayServer.bidCount")); + } + } else { + rawBidCount = bidSequence.get(3); + } + + if(rawBidCount == null) { + rawBidCount = doc.getContentBeforeContent("See history"); + if(rawBidCount != null && rawBidCount.matches("^(Purchased|Bid).*")) { + if (rawBidCount.matches("^Purchased.*")) setFixedPrice(true); + rawBidCount = doc.getPrevContent(); + } + if(rawBidCount != null && !StringTools.isNumberOnly(rawBidCount)) rawBidCount = null; + } + return rawBidCount; + } + public String getThumbnailURL() { if(potentialThumbnail != null) return potentialThumbnail; return getThumbnailById(getIdentifier()); diff --git a/src/com/jbidwatcher/util/Currency.java b/src/com/jbidwatcher/util/Currency.java index a9494c3d..73390321 100644 --- a/src/com/jbidwatcher/util/Currency.java +++ b/src/com/jbidwatcher/util/Currency.java @@ -15,6 +15,9 @@ import java.beans.DefaultPersistenceDelegate; public class Currency implements Comparable { + public static final String VALUE_REGEX="^[0-9]+([,.0-9]*)$"; + public static final String NAME_REGEX = "(USD|GBP|JPY|CHF|FRF|EUR|CAD|AUD|NTD|TWD|HKD|MYR|SGD|INR)"; + private static NumberFormat df = NumberFormat.getNumberInstance(Locale.US); // We create a lot of these, so minimizing memory usage is good. public static final int NONE=0, US_DOLLAR=1, UK_POUND=2, JP_YEN=3, GER_MARK=4, FR_FRANC=5, CAN_DOLLAR=6; public static final int EURO=7, AU_DOLLAR=8, CH_FRANC=9, NT_DOLLAR=10, TW_DOLLAR=10, HK_DOLLAR=11; @@ -224,7 +227,20 @@ public Currency(String symbol, double startValue) { } public Currency(String symbol, String startValue) { - setValues(symbol, Double.parseDouble(startValue)); + setValues(symbol, Double.parseDouble(cleanCommas(startValue))); + } + + // Convert [###.###.]###,## to [###,###,]###.## + private static String cleanCommas(String startValue) { + int decimalPos = startValue.length()-3; + if(decimalPos > 0) { + if (startValue.charAt(decimalPos) == '.') { + startValue = startValue.replaceAll(",", ""); + } else if(startValue.charAt(decimalPos) == ',') { + startValue = startValue.replaceAll("\\.", "").replaceAll(",", "."); + } + } + return startValue; } private int checkLengthMatchStart(String value, String currencyName) { @@ -242,7 +258,7 @@ private int checkLengthMatchStart(String value, String currencyName) { return 0; } - /** + /** * @brief Provided an entire string containing a currency prefix and * an amount, extract the two and set this object's value to equal * the result. @@ -360,12 +376,7 @@ private void setValues(String wholeValue) { if(valuePortion.length() != 0) { double actualValue; try { - String cvt = valuePortion; - // Convert [###.###.]###,## to [###,###,]###.## - if(cvt.length() > 2 && cvt.charAt(cvt.length()-3) == ',') { - cvt = cvt.substring(0, cvt.length()-3).replaceAll("\\.",",") + '.' + cvt.substring(cvt.length()-2); -// System.out.println("Converting '" + cvt + "': " + df.parse(cvt).doubleValue()); - } + String cvt = cleanCommas(valuePortion); actualValue = df.parse(cvt).doubleValue(); } catch(java.text.ParseException e) { JConfig.log().handleException("currency parse!", e); diff --git a/src/com/jbidwatcher/util/html/JHTML.java b/src/com/jbidwatcher/util/html/JHTML.java index 31096c01..36a23850 100644 --- a/src/com/jbidwatcher/util/html/JHTML.java +++ b/src/com/jbidwatcher/util/html/JHTML.java @@ -517,20 +517,24 @@ public String getNextContentAfterRegexIgnoring(String match, String ignore) { * @return - True if that sequence was found, false otherwise. (Probably should be the contents that matched the last regex.) */ public boolean hasSequence(String... sequence) { - if(sequence.length == 0) return false; + return sequence.length != 0 && findSequence(sequence) != null; + } + public List findSequence(String... sequence) { + List contentSequence = new LinkedList(); int index = 0; for (String contentStep : contentList) { if (contentStep.matches(sequence[index])) { + contentSequence.add(contentStep); index++; - if (index == sequence.length) return true; + if (index == sequence.length) return contentSequence; } else { + contentSequence.clear(); index = 0; } } - - return false; + return null; } //------------------------------------------------------------