X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneDownloader.java;fp=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FSoneDownloader.java;h=4da122637fad01582ebb9ef3c488e18cee384298;hp=0812f668110836d39287639d47b0e0699c43e0cc;hb=0df5e91852f737d760c5a9f54c5667309fbadcc2;hpb=3b25d775c6066d65a5ab45844546552badd3d80d diff --git a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java index 0812f66..4da1226 100644 --- a/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java +++ b/src/main/java/net/pterodactylus/sone/core/SoneDownloader.java @@ -17,16 +17,19 @@ package net.pterodactylus.sone.core; -import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; +import java.util.ArrayList; import java.util.HashSet; +import java.util.List; import java.util.Set; import java.util.logging.Level; import java.util.logging.Logger; import net.pterodactylus.sone.core.Core.SoneStatus; +import net.pterodactylus.sone.data.Album; import net.pterodactylus.sone.data.Client; +import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Profile; import net.pterodactylus.sone.data.Reply; @@ -198,8 +201,8 @@ public class SoneDownloader extends AbstractService { } } return parsedSone; - } catch (IOException ioe1) { - logger.log(Level.WARNING, "Could not parse Sone from " + requestUri + "!", ioe1); + } catch (Exception e1) { + logger.log(Level.WARNING, "Could not parse Sone from " + requestUri + "!", e1); } finally { Closer.close(soneInputStream); soneBucket.free(); @@ -431,6 +434,63 @@ public class SoneDownloader extends AbstractService { } } + /* parse albums. */ + SimpleXML albumsXml = soneXml.getNode("albums"); + List topLevelAlbums = new ArrayList(); + if (albumsXml != null) { + for (SimpleXML albumXml : albumsXml.getNodes("album")) { + String id = albumXml.getValue("id", null); + String parentId = albumXml.getValue("parent", null); + String title = albumXml.getValue("title", null); + String description = albumXml.getValue("description", null); + if ((id == null) || (title == null) || (description == null)) { + logger.log(Level.WARNING, "Downloaded Sone %s contains invalid album!", new Object[] { sone }); + return null; + } + Album parent = null; + if (parentId != null) { + parent = core.getAlbum(parentId, false); + if (parent == null) { + logger.log(Level.WARNING, "Downloaded Sone %s has album with invalid parent!", new Object[] { sone }); + return null; + } + } + Album album = core.getAlbum(id).setSone(sone).setTitle(title).setDescription(description); + if (parent != null) { + parent.addAlbum(album); + } else { + topLevelAlbums.add(album); + } + SimpleXML imagesXml = albumXml.getNode("images"); + if (imagesXml != null) { + for (SimpleXML imageXml : imagesXml.getNodes("image")) { + String imageId = imageXml.getValue("id", null); + String imageCreationTimeString = imageXml.getValue("creation-time", null); + String imageKey = imageXml.getValue("key", null); + String imageTitle = imageXml.getValue("title", null); + String imageDescription = imageXml.getValue("description", ""); + String imageWidthString = imageXml.getValue("width", null); + String imageHeightString = imageXml.getValue("height", null); + if ((imageId == null) || (imageCreationTimeString == null) || (imageKey == null) || (imageTitle == null) || (imageWidthString == null) || (imageHeightString == null)) { + logger.log(Level.WARNING, "Downloaded Sone %s contains invalid images!", new Object[] { sone }); + return null; + } + long creationTime = Numbers.safeParseLong(imageCreationTimeString, 0L); + int imageWidth = Numbers.safeParseInteger(imageWidthString, 0); + int imageHeight = Numbers.safeParseInteger(imageHeightString, 0); + if ((imageWidth < 1) || (imageHeight < 1)) { + logger.log(Level.WARNING, "Downloaded Sone %s contains image %s with invalid dimensions (%s, %s)!", new Object[] { sone, imageId, imageWidthString, imageHeightString }); + return null; + } + Image image = core.getImage(imageId).setSone(sone).setKey(imageKey).setCreationTime(creationTime); + image.setTitle(imageTitle).setDescription(imageDescription); + image.setWidth(imageWidth).setHeight(imageHeight); + album.addImage(image); + } + } + } + } + /* okay, apparently everything was parsed correctly. Now import. */ /* atomic setter operation on the Sone. */ synchronized (sone) { @@ -439,6 +499,7 @@ public class SoneDownloader extends AbstractService { sone.setReplies(replies); sone.setLikePostIds(likedPostIds); sone.setLikeReplyIds(likedReplyIds); + sone.setAlbums(topLevelAlbums); } return sone;