From d773a39a1f07ae4b8021222a47c16b048d7fd435 Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Tue, 17 Aug 2010 01:56:11 +0200 Subject: initial commit --- src/de/animux/android/andmal/api/anime/Anime.java | 197 +++++++++++++++++++++ .../animux/android/andmal/api/anime/AnimeList.java | 117 ++++++++++++ src/de/animux/android/andmal/api/anime/State.java | 36 ++++ 3 files changed, 350 insertions(+) create mode 100644 src/de/animux/android/andmal/api/anime/Anime.java create mode 100644 src/de/animux/android/andmal/api/anime/AnimeList.java create mode 100644 src/de/animux/android/andmal/api/anime/State.java (limited to 'src/de/animux/android/andmal/api') diff --git a/src/de/animux/android/andmal/api/anime/Anime.java b/src/de/animux/android/andmal/api/anime/Anime.java new file mode 100644 index 0000000..d4f542f --- /dev/null +++ b/src/de/animux/android/andmal/api/anime/Anime.java @@ -0,0 +1,197 @@ +package de.animux.android.andmal.api.anime; + +public class Anime implements Comparable { + + private int id; + private String title; + private String synonyms; + private int type; + private int episodes; + private int status; + private String start; + private String end; + private String image; + private int myId; + private int watchedEpisodes; + private String myStart; + private String myEnd; + private int myScore; + private int myStatus; + private int rewatching; + private int rewatchingEpisodes; + private int lastUpdate; + private String tags; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public String getSynonyms() { + return synonyms; + } + + public void setSynonyms(String synonyms) { + this.synonyms = synonyms; + } + + public int getType() { + return type; + } + + public void setType(int type) { + this.type = type; + } + + public int getEpisodes() { + return episodes; + } + + public void setEpisodes(int episodes) { + this.episodes = episodes; + } + + public int getStatus() { + return status; + } + + public void setStatus(int status) { + this.status = status; + } + + public String getStart() { + return start; + } + + public void setStart(String start) { + this.start = start; + } + + public String getEnd() { + return end; + } + + public void setEnd(String end) { + this.end = end; + } + + public String getImage() { + return image; + } + + public void setImage(String image) { + this.image = image; + } + + public int getMyId() { + return myId; + } + + public void setMyId(int myId) { + this.myId = myId; + } + + public int getWatchedEpisodes() { + return watchedEpisodes; + } + + public void setWatchedEpisodes(int watchedEpisodes) { + this.watchedEpisodes = watchedEpisodes; + } + + public String getMyStart() { + return myStart; + } + + public void setMyStart(String myStart) { + this.myStart = myStart; + } + + public String getMyEnd() { + return myEnd; + } + + public void setMyEnd(String myEnd) { + this.myEnd = myEnd; + } + + public int getMyScore() { + return myScore; + } + + public void setMyScore(int myScore) { + this.myScore = myScore; + } + + public int getMyStatus() { + return myStatus; + } + + public void setMyStatus(int myStatus) { + this.myStatus = myStatus; + } + + public int getRewatching() { + return rewatching; + } + + public void setRewatching(int rewatching) { + this.rewatching = rewatching; + } + + public int getRewatchingEpisodes() { + return rewatchingEpisodes; + } + + public void setRewatchingEpisodes(int rewatchingEpisodes) { + this.rewatchingEpisodes = rewatchingEpisodes; + } + + public int getLastUpdate() { + return lastUpdate; + } + + public void setLastUpdate(int lastUpdate) { + this.lastUpdate = lastUpdate; + } + + public String getTags() { + return tags; + } + + public void setTags(String tags) { + this.tags = tags; + } + + @Override + public String toString() { + return getTitle(); + } + + @Override + public int compareTo(Anime arg0) { + int compareTitle = arg0.getTitle().compareTo(getTitle()); + if (compareTitle != 0) { + return compareTitle; + } + else { + if (arg0.getId() != getId()) { + return getId() - arg0.getId(); + } + } + + return 0; + } + +} diff --git a/src/de/animux/android/andmal/api/anime/AnimeList.java b/src/de/animux/android/andmal/api/anime/AnimeList.java new file mode 100644 index 0000000..78a1e09 --- /dev/null +++ b/src/de/animux/android/andmal/api/anime/AnimeList.java @@ -0,0 +1,117 @@ +package de.animux.android.andmal.api.anime; + +import java.io.IOException; +import java.io.InputStream; + +import java.net.URL; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import javax.xml.parsers.FactoryConfigurationError; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.parsers.SAXParser; +import javax.xml.parsers.SAXParserFactory; + +import org.xml.sax.Attributes; +import org.xml.sax.SAXException; +import org.xml.sax.helpers.DefaultHandler; + +import de.animux.android.andmal.api.anime.State; +import de.animux.android.andmal.util.SortedLinkedList; + +public class AnimeList extends DefaultHandler { + + private Anime currentAnime; + private StringBuffer currentValue; + + private HashMap> animes; + + public AnimeList(String user) throws IOException, + ParserConfigurationException, SAXException, + FactoryConfigurationError { + currentAnime = null; + currentValue = new StringBuffer(); + + animes = new HashMap>(); + animes.put(State.WATCHING, new SortedLinkedList()); + animes.put(State.COMPLETED, new SortedLinkedList()); + animes.put(State.ONHOLD, new SortedLinkedList()); + animes.put(State.DROPPED, new SortedLinkedList()); + animes.put(State.PLANTOWATCH, new SortedLinkedList()); + + URL url = new URL("http://myanimelist.net/malappinfo.php?status=all&type=anime&u=" + user); + InputStream stream = url.openStream(); + + SAXParser sax = SAXParserFactory.newInstance().newSAXParser(); + sax.parse(stream, this); + } + + public Anime getCurrentAnime() { + return currentAnime; + } + + public StringBuffer getCurrentValue() { + return currentValue; + } + + public Map> getAnimes() { + return animes; + } + + @Override + public void characters(char[] ch, int start, int length) + throws SAXException { + currentValue.append(new String(ch).substring(start, length - start) + .replaceAll("&", "&")); + } + + @Override + public void endElement(String uri, String localName, String qName) + throws SAXException { + + if (localName.equals("anime")) { + if (animes.containsKey(State.valueOf(currentAnime.getMyStatus()))) { + animes.get(State.valueOf(currentAnime.getMyStatus())).add(currentAnime); + } + + } else if (localName.equals("series_animedb_id")) { + currentAnime.setId(Integer.valueOf(currentValue.toString())); + } else if (localName.equals("series_title")) { + currentAnime.setTitle(currentValue.toString()); + } else if (localName.equals("series_synonyms")) { + currentAnime.setSynonyms(currentValue.toString()); + } else if (localName.equals("series_type")) { + currentAnime.setType(Integer.valueOf(currentValue.toString())); + } else if (localName.equals("series_episodes")) { + currentAnime.setEpisodes(Integer.valueOf(currentValue.toString())); + } else if (localName.equals("series_status")) { + currentAnime.setStatus(Integer.valueOf(currentValue.toString())); + } else if (localName.equals("series_start")) { + currentAnime.setStart(currentValue.toString()); + } else if (localName.equals("series_end")) { + currentAnime.setEnd(currentValue.toString()); + } else if (localName.equals("series_image")) { + currentAnime.setImage(currentValue.toString()); + } else if (localName.equals("my_id")) { + currentAnime.setMyId(Integer.valueOf(currentValue.toString())); + } else if (localName.equals("my_watched_episodes")) { + currentAnime.setWatchedEpisodes(Integer.valueOf(currentValue + .toString())); + } else if (localName.equals("my_status")) { + currentAnime.setMyStatus(Integer.valueOf(currentValue.toString())); + } + + currentValue.setLength(0); + } + + @Override + public void startElement(String uri, String localName, String qName, + Attributes attributes) throws SAXException { + + currentValue.setLength(0); + if (localName.equals("anime")) { + currentAnime = new Anime(); + } + } +} diff --git a/src/de/animux/android/andmal/api/anime/State.java b/src/de/animux/android/andmal/api/anime/State.java new file mode 100644 index 0000000..639b761 --- /dev/null +++ b/src/de/animux/android/andmal/api/anime/State.java @@ -0,0 +1,36 @@ +package de.animux.android.andmal.api.anime; + +public enum State { + WATCHING (1, "Watching"), + COMPLETED (2, "Completed"), + ONHOLD (3, "On Hold"), + DROPPED (4, "Dropped"), + PLANTOWATCH (6, "Plan to watch"); + + private final int id; + private final String name; + + State(int id, String name) { + this.id = id; + this.name = name; + } + + @Override + public String toString() { + return name; + } + + public int getId() { + return id; + } + + static State valueOf(int id) { + for (State s : State.values()) { + if (s.getId() == id) { + return s; + } + } + + return null; + } +} \ No newline at end of file -- cgit v1.2.3