Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sitemaps limit on "bad url" log messages, fixes #145 #206

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Crawler-Commons Change Log

Current Development 0.10-SNAPSHOT (yyyy-mm-dd)
- [Sitemaps] Limit on "bad url" log messages (sebastian-nagel) #145
- EffectiveTldFinder to parse Internationalized Domain Names (sebastian-nagel) #179
- Add main() to EffectiveTldFinder (sebastian-nagel) #187
- Handle new suffixes in PaidLevelDomain (kkrugler) #183
Expand Down
58 changes: 18 additions & 40 deletions src/main/java/crawlercommons/sitemaps/SiteMapParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,24 @@ protected SiteMap processText(URL sitemapUrl, InputStream stream) throws IOExcep
BufferedReader reader = new BufferedReader(new InputStreamReader(bomIs, UTF_8));

String line;
int i = 1;
while ((line = reader.readLine()) != null) {
if (line.length() > 0 && i <= MAX_URLS) {
addUrlIntoSitemap(line, textSiteMap, null, null, null, i++);
int i = 0;
while ((line = reader.readLine()) != null && ++i <= MAX_URLS) {
line = line.trim();
if (line.isEmpty()) {
continue;
}
try {
URL url = new URL(line);
boolean valid = urlIsValid(textSiteMap.getBaseUrl(), url.toString());
if (valid || !strict) {
SiteMapURL sUrl = new SiteMapURL(url, valid);
textSiteMap.addSiteMapUrl(sUrl);
LOG.debug(" {}. {}", i, sUrl);
} else {
LOG.debug("URL: {} is excluded from the sitemap as it is not a valid url = not under the base url: {}", url.toExternalForm(), textSiteMap.getBaseUrl());
}
} catch (MalformedURLException e) {
LOG.debug("Bad url: [{}]", line.substring(0, Math.min(1024, line.length())));
}
}
textSiteMap.setProcessed(true);
Expand Down Expand Up @@ -410,42 +424,6 @@ public InputSource resolveEntity(String publicId, String systemId) {
}
}

/**
* Adds the given URL to the given sitemap while showing the relevant logs
*
* @param urlStr
* an URL string to add to the
* {@link crawlercommons.sitemaps.SiteMap}
* @param siteMap
* the sitemap to add URL(s) to
* @param lastMod
* last time the {@link crawlercommons.sitemaps.SiteMapURL} was
* modified
* @param changeFreq
* the {@link crawlercommons.sitemaps.SiteMapURL} change frquency
* @param priority
* priority of this {@link crawlercommons.sitemaps.SiteMapURL}
* @param urlIndex
* index position to which this entry has been added
*/
protected void addUrlIntoSitemap(String urlStr, SiteMap siteMap, String lastMod, String changeFreq, String priority, int urlIndex) {
try {
URL url = new URL(urlStr); // Checking the URL
boolean valid = urlIsValid(siteMap.getBaseUrl(), url.toString());

if (valid || !strict) {
SiteMapURL sUrl = new SiteMapURL(url.toString(), lastMod, changeFreq, priority, valid);
siteMap.addSiteMapUrl(sUrl);
LOG.debug(" {}. {}", urlIndex + 1, sUrl);
} else {
LOG.warn("URL: {} is excluded from the sitemap as it is not a valid url = not under the base url: {}", url.toExternalForm(), siteMap.getBaseUrl());
}
} catch (MalformedURLException e) {
LOG.warn("Bad url: [{}]", urlStr);
LOG.trace("Can't create a sitemap entry with a bad URL", e);
}
}

/**
* See if testUrl is under sitemapBaseUrl. Only URLs under sitemapBaseUrl
* are valid.
Expand Down