use of me.devsaki.hentoid.json.sources.PixivIllustMetadata in project Hentoid by avluis.
the class PixivParser method parseSeries.
private List<ImageFile> parseSeries(@NonNull Content onlineContent, @Nullable Content storedContent, @NonNull String cookieStr) throws Exception {
String[] seriesIdParts = onlineContent.getUniqueSiteId().split("/");
String seriesId = seriesIdParts[seriesIdParts.length - 1];
if (seriesId.contains("?")) {
seriesId = seriesId.substring(0, seriesId.indexOf("?"));
}
// Retrieve the number of Illusts
String nbChaptersStr = ContentHelper.parseDownloadParams(onlineContent.getDownloadParams()).get(KEY_DL_PARAMS_NB_CHAPTERS);
if (null == nbChaptersStr || !StringHelper.isNumeric(nbChaptersStr))
throw new IllegalArgumentException("Chapter count not saved");
int nbChapters = Integer.parseInt(nbChaptersStr);
// List all Illust IDs (API is paged, hence the loop)
List<Chapter> chapters = new ArrayList<>();
while (chapters.size() < nbChapters) {
if (processHalted.get())
break;
int chaptersToRead = Math.min(nbChapters - chapters.size(), MAX_QUERY_WINDOW);
PixivSeriesIllustMetadata seriesContentMetadata = PixivServer.api.getSeriesIllusts(seriesId, chaptersToRead, chapters.size(), cookieStr).execute().body();
if (null == seriesContentMetadata || seriesContentMetadata.isError()) {
String message = "Unreachable series illust";
if (seriesContentMetadata != null)
message = seriesContentMetadata.getMessage();
throw new IllegalArgumentException(message);
}
chapters.addAll(seriesContentMetadata.getChapters(onlineContent.getId()));
}
// Put back chapters in reading order
Collections.reverse(chapters);
// If the stored content has chapters already, save them for comparison
List<Chapter> storedChapters = null;
if (storedContent != null) {
storedChapters = storedContent.getChapters();
if (storedChapters != null)
// Work on a copy
storedChapters = Stream.of(storedChapters).toList();
}
if (null == storedChapters)
storedChapters = Collections.emptyList();
// Use chapter folder as a differentiator (as the whole URL may evolve)
List<Chapter> extraChapters = ParseHelper.getExtraChaptersbyUrl(storedChapters, chapters);
progressStart(onlineContent, storedContent, extraChapters.size());
// Start numbering extra images right after the last position of stored and chaptered images
int imgOffset = ParseHelper.getMaxImageOrder(storedChapters) + 1;
// Retrieve all Illust detailed info
List<ImageFile> result = new ArrayList<>();
result.add(ImageFile.newCover(onlineContent.getCoverImageUrl(), StatusContent.SAVED));
Set<Attribute> attrs = new HashSet<>();
for (Chapter ch : extraChapters) {
if (processHalted.get())
break;
PixivIllustMetadata illustMetadata = PixivServer.api.getIllustMetadata(ch.getUniqueId(), cookieStr).execute().body();
if (null == illustMetadata || illustMetadata.isError()) {
String message = "Unreachable illust";
if (illustMetadata != null)
message = illustMetadata.getMessage();
throw new IllegalArgumentException(message);
}
List<Attribute> chapterAttrs = illustMetadata.getAttributes();
attrs.addAll(chapterAttrs);
List<ImageFile> chapterImages = illustMetadata.getImageFiles();
for (ImageFile img : chapterImages) img.setOrder(imgOffset++).computeName(4).setChapter(ch);
result.addAll(chapterImages);
progressPlus();
}
// If the process has been halted manually, the result is incomplete and should not be returned as is
if (processHalted.get())
throw new PreparationInterruptedException();
onlineContent.putAttributes(attrs);
onlineContent.setUpdatedProperties(true);
return result;
}
use of me.devsaki.hentoid.json.sources.PixivIllustMetadata in project Hentoid by avluis.
the class PixivContent method update.
@Nullable
public Content update(@NonNull final Content content, @Nonnull String url, boolean updateImages) {
String[] urlParts = url.split("/");
String id = urlParts[urlParts.length - 1];
if (id.contains("?")) {
id = id.substring(0, id.indexOf("?"));
}
String entity = urlParts[urlParts.length - 2];
Uri uri = Uri.parse(url);
switch(entity) {
// fetch Call / single gallery
case "artworks":
case // fetch Call / single gallery
"illust":
if (!StringHelper.isNumeric(id))
id = uri.getQueryParameter("illust_id");
break;
// User
case "user":
case "users":
if (!StringHelper.isNumeric(id))
id = uri.getQueryParameter("id");
break;
default:
}
try {
if (id != null && !id.isEmpty()) {
String cookieStr = HttpHelper.getCookies(url, null, Site.PIXIV.useMobileAgent(), Site.PIXIV.useHentoidAgent(), Site.PIXIV.useWebviewAgent());
switch(entity) {
case "artworks":
case "illust":
PixivIllustMetadata metadata = PixivServer.api.getIllustMetadata(id, cookieStr).execute().body();
if (metadata != null)
return metadata.update(content, url, updateImages);
break;
case "series_content":
case "series":
PixivSeriesMetadata seriesData = PixivServer.api.getSeriesMetadata(id, cookieStr).execute().body();
if (seriesData != null)
return seriesData.update(content, url, updateImages);
break;
case "user":
case "users":
PixivUserMetadata userData = PixivServer.api.getUserMetadata(id, cookieStr).execute().body();
if (userData != null)
return userData.update(content, url, updateImages);
break;
default:
}
}
} catch (IOException e) {
Timber.e(e, "Error parsing content.");
}
return new Content().setSite(Site.PIXIV).setStatus(StatusContent.IGNORED);
}
use of me.devsaki.hentoid.json.sources.PixivIllustMetadata in project Hentoid by avluis.
the class PixivParser method parseUser.
private List<ImageFile> parseUser(@NonNull Content onlineContent, @Nullable Content storedContent, @NonNull String cookieStr) throws Exception {
String[] userIdParts = onlineContent.getUniqueSiteId().split("/");
String userId = userIdParts[userIdParts.length - 1];
if (userId.contains("?")) {
userId = userId.substring(0, userId.indexOf("?"));
}
// Retrieve the list of Illusts IDs (=chapters)
PixivUserIllustMetadata userIllustsMetadata = PixivServer.api.getUserIllusts(userId, cookieStr).execute().body();
if (null == userIllustsMetadata || userIllustsMetadata.isError()) {
String message = "Unreachable user illusts";
if (userIllustsMetadata != null)
message = userIllustsMetadata.getMessage();
throw new IllegalArgumentException(message);
}
// Detect extra chapters
List<String> illustIds = userIllustsMetadata.getIllustIds();
List<Chapter> storedChapters = null;
if (storedContent != null) {
storedChapters = storedContent.getChapters();
if (storedChapters != null)
storedChapters = Stream.of(storedChapters).toList();
}
if (null == storedChapters)
storedChapters = Collections.emptyList();
else
illustIds = ParseHelper.getExtraChaptersbyId(storedChapters, illustIds);
// Work on detected extra chapters
progressStart(onlineContent, storedContent, illustIds.size());
// Start numbering extra images & chapters right after the last position of stored and chaptered images & chapters
int imgOffset = ParseHelper.getMaxImageOrder(storedChapters) + 1;
int chpOffset = ParseHelper.getMaxChapterOrder(storedChapters) + 1;
// Cycle through all Illusts
List<ImageFile> result = new ArrayList<>();
result.add(ImageFile.newCover(onlineContent.getCoverImageUrl(), StatusContent.SAVED));
Set<Attribute> attrs = new HashSet<>();
for (String illustId : illustIds) {
if (processHalted.get())
break;
PixivIllustMetadata illustMetadata = PixivServer.api.getIllustMetadata(illustId, cookieStr).execute().body();
if (null == illustMetadata || illustMetadata.isError()) {
String message = "Unreachable illust";
if (illustMetadata != null)
message = illustMetadata.getMessage();
throw new IllegalArgumentException(message);
}
List<Attribute> chapterAttrs = illustMetadata.getAttributes();
attrs.addAll(chapterAttrs);
Chapter chp = new Chapter(chpOffset++, illustMetadata.getUrl(), illustMetadata.getTitle()).setUniqueId(illustMetadata.getId()).setContentId(onlineContent.getId());
List<ImageFile> chapterImages = illustMetadata.getImageFiles();
for (ImageFile img : chapterImages) img.setOrder(imgOffset++).computeName(4).setChapter(chp);
result.addAll(chapterImages);
progressPlus();
}
// If the process has been halted manually, the result is incomplete and should not be returned as is
if (processHalted.get())
throw new PreparationInterruptedException();
onlineContent.putAttributes(attrs);
onlineContent.setUpdatedProperties(true);
return result;
}
Aggregations