Skip to content

Commit

Permalink
Fix 'null (FP)' problems for most (all?) items not visible on ebay.com.
Browse files Browse the repository at this point in the history
[#1298 state:resolved tagged:committed]
  • Loading branch information
cyberfox committed Oct 27, 2010
1 parent 040a613 commit 450795e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 25 deletions.
52 changes: 39 additions & 13 deletions src/com/jbidwatcher/auction/server/ebay/ebayAuction.java
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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<String> 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()) {
Expand Down Expand Up @@ -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")) ||
Expand Down Expand Up @@ -898,6 +899,31 @@ private int getBidCount(JHTML doc, int quantity) {
return bidCount;
}

private String getRawBidCount(JHTML doc) {
List<String> 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());
Expand Down
27 changes: 19 additions & 8 deletions src/com/jbidwatcher/util/Currency.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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.
Expand Down Expand Up @@ -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);
Expand Down
12 changes: 8 additions & 4 deletions src/com/jbidwatcher/util/html/JHTML.java
Expand Up @@ -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<String> findSequence(String... sequence) {
List<String> contentSequence = new LinkedList<String>();
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;
}

//------------------------------------------------------------
Expand Down

0 comments on commit 450795e

Please sign in to comment.