Imgur: Update title detection (Fixes #394); prepend user_ in title of

user rips.

* Simplify album title detection using proper .post-title element on page.
* Combine album and gallery handling (was duplicated code).
* Prepend user_ before username on user rips:
  * [username].imgur.com -> imgur_user_[username]
* Fix allowDuplicates() logic.
This commit is contained in:
MetaPrime 2016-12-21 04:16:42 -08:00
parent 3310bbef3d
commit 40adcf245f

View File

@ -52,7 +52,7 @@ public class ImgurRipper extends AlbumRipper {
*/ */
@Override @Override
public boolean allowDuplicates() { public boolean allowDuplicates() {
return true; return albumType == ALBUM_TYPE.USER;
} }
public boolean canRip(URL url) { public boolean canRip(URL url) {
@ -87,13 +87,44 @@ public class ImgurRipper extends AlbumRipper {
if (albumDoc == null) { if (albumDoc == null) {
albumDoc = Http.url(url).get(); albumDoc = Http.url(url).get();
} }
String title = albumDoc.title();
if (!title.contains(" - Imgur") Elements elems = null;
|| title.contains("'s albums")) {
throw new IOException("No title found"); /*
// TODO: Add config option for including username in album title.
// It's possible a lot of users would not be interested in that info.
String user = null;
elems = albumDoc.select(".post-account");
if (elems.size() > 0) {
Element postAccount = elems.get(0);
if (postAccount != null) {
user = postAccount.text();
}
} }
title = title.replaceAll(" - Imgur.*", ""); */
return "imgur_" + gid + " (" + title + ")";
String title = null;
elems = albumDoc.select(".post-title");
if (elems.size() > 0) {
Element postTitle = elems.get(0);
if (postTitle != null) {
title = postTitle.text();
}
}
String albumTitle = "imgur_";
/*
// TODO: Add config option (see above)
if (user != null) {
albumTitle += "user_" + user;
}
*/
albumTitle += gid;
if (title != null) {
albumTitle += " (" + title + ")";
}
return albumTitle;
} catch (IOException e) { } catch (IOException e) {
// Fall back to default album naming convention // Fall back to default album naming convention
} }
@ -417,19 +448,13 @@ public class ImgurRipper extends AlbumRipper {
@Override @Override
public String getGID(URL url) throws MalformedURLException { public String getGID(URL url) throws MalformedURLException {
Pattern p = Pattern.compile("^https?://(www\\.|m\\.)?imgur\\.com/a/([a-zA-Z0-9]{5,8}).*$"); Pattern p = null;
Matcher m = p.matcher(url.toExternalForm()); Matcher m = null;
if (m.matches()) {
// Imgur album p = Pattern.compile("^https?://(www\\.|m\\.)?imgur\\.com/(a|gallery)/([a-zA-Z0-9]{5,8}).*$");
albumType = ALBUM_TYPE.ALBUM;
String gid = m.group(m.groupCount());
this.url = new URL("http://imgur.com/a/" + gid);
return gid;
}
p = Pattern.compile("^https?://(www\\.|m\\.)?imgur\\.com/gallery/([a-zA-Z0-9]{5,8}).*$");
m = p.matcher(url.toExternalForm()); m = p.matcher(url.toExternalForm());
if (m.matches()) { if (m.matches()) {
// Imgur gallery // Imgur album or gallery
albumType = ALBUM_TYPE.ALBUM; albumType = ALBUM_TYPE.ALBUM;
String gid = m.group(m.groupCount()); String gid = m.group(m.groupCount());
this.url = new URL("http://imgur.com/a/" + gid); this.url = new URL("http://imgur.com/a/" + gid);
@ -444,7 +469,7 @@ public class ImgurRipper extends AlbumRipper {
throw new MalformedURLException("Cannot rip the www.imgur.com homepage"); throw new MalformedURLException("Cannot rip the www.imgur.com homepage");
} }
albumType = ALBUM_TYPE.USER; albumType = ALBUM_TYPE.USER;
return gid; return "user_" + gid;
} }
p = Pattern.compile("^https?://([a-zA-Z0-9\\-]{3,})\\.imgur\\.com/all.*$"); p = Pattern.compile("^https?://([a-zA-Z0-9\\-]{3,})\\.imgur\\.com/all.*$");
m = p.matcher(url.toExternalForm()); m = p.matcher(url.toExternalForm());
@ -546,4 +571,4 @@ public class ImgurRipper extends AlbumRipper {
} }
} }
} }