diff --git a/ENSIM/Java/tp-deezer3-HERBRON_Tanguy/pom.xml b/ENSIM/Java/tp-deezer3-HERBRON_Tanguy/pom.xml index 82ad24f..5b0e62f 100644 --- a/ENSIM/Java/tp-deezer3-HERBRON_Tanguy/pom.xml +++ b/ENSIM/Java/tp-deezer3-HERBRON_Tanguy/pom.xml @@ -25,4 +25,11 @@ + + + org.json + json + 20180813 + + \ No newline at end of file diff --git a/ENSIM/Java/tp-deezer3-HERBRON_Tanguy/src/fr/ensim/json/JSONSearchAlbum.java b/ENSIM/Java/tp-deezer3-HERBRON_Tanguy/src/fr/ensim/json/JSONSearchAlbum.java new file mode 100644 index 0000000..a3b2fc9 --- /dev/null +++ b/ENSIM/Java/tp-deezer3-HERBRON_Tanguy/src/fr/ensim/json/JSONSearchAlbum.java @@ -0,0 +1,64 @@ +package fr.ensim.json; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; +import org.apache.log4j.Logger; +import org.json.JSONArray; +import org.json.JSONObject; + +import fr.ensim.xml.deezer.AbstractSearchAlbum; +import fr.ensim.xml.deezer.data.Album; +import fr.ensim.xml.deezer.data.Artist; + +public class JSONSearchAlbum extends AbstractSearchAlbum { + private static final Logger LOG = Logger.getLogger(JSONSearchAlbum.class); + + public List readAlbums(InputStream in) throws Exception + { + LOG.debug(">>readAlbums"); + + List listAlbums = new ArrayList(); + + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + + String file = ""; + String line; + + while((line = reader.readLine()) != null) + { + file += line; + } + + JSONObject jsonObject = new JSONObject(file); + JSONArray dataArray = jsonObject.getJSONArray("data"); + + for(int i = 0; i < dataArray.length(); i++) + { + JSONObject albumObject = dataArray.getJSONObject(i); + JSONObject artistObject = albumObject.getJSONObject("artist"); + + Album album = new Album(); + + album.setId(String.valueOf(albumObject.getInt("id"))); + album.setTitle(albumObject.getString("title")); + album.setCover(albumObject.getString("cover")); + + Artist artist = new Artist(); + + artist.setId(String.valueOf(artistObject.getInt("id"))); + artist.setName(artistObject.getString("name")); + artist.setLink(artistObject.getString("link")); + artist.setPicture(artistObject.getString("picture")); + + album.setArtist(artist); + + listAlbums.add(album); + } + + LOG.debug("< find(String id) throws IOException, ParserConfigurationException, SAXException { + LOG.debug(">>albums"); + + // Constitution de l'URL + StringBuilder sUrl = new StringBuilder(); + sUrl.append("http://api.deezer.com/2.0/album/"); + sUrl.append(id); + + URL url = new URL(sUrl.toString()); + + LOG.debug(url); + + HttpURLConnection cnx = (HttpURLConnection) url.openConnection(); + cnx.setConnectTimeout(5000); + cnx.setReadTimeout(5000); + cnx.setRequestMethod("GET"); + cnx.setDoInput(true); + cnx.addRequestProperty("Accept-Language", "en;q=0.6,en-us;q=0.4,sv;q=0.2"); + + try { + if (cnx.getResponseCode() == HttpURLConnection.HTTP_OK) { + return find(cnx.getInputStream()); + } + } finally { + cnx.disconnect(); + } + + LOG.debug("< find(InputStream in) throws SAXException, ParserConfigurationException, IOException { + LOG.debug(">>find"); + + ArrayList listTracks = new ArrayList(); + + BufferedReader reader = new BufferedReader(new InputStreamReader(in)); + + String file = ""; + String line; + + while((line = reader.readLine()) != null) + { + file += line; + } + + JSONObject jsonObject = new JSONObject(file); + JSONObject tracksDataObject = jsonObject.getJSONObject("tracks"); + JSONArray dataArray = tracksDataObject.getJSONArray("data"); + + for(int i = 0; i < dataArray.length(); i++) + { + JSONObject trackObject = dataArray.getJSONObject(i); + Track track = new Track(); + + track.setPreview(trackObject.getString("preview")); + track.setTitle(trackObject.getString("title")); + + listTracks.add(track); + } + + LOG.debug(">>find"); + return listTracks; + } +} diff --git a/ENSIM/Java/tp-deezer3-HERBRON_Tanguy/src/fr/ensim/xml/deezer/AbstractSearchAlbum.java b/ENSIM/Java/tp-deezer3-HERBRON_Tanguy/src/fr/ensim/xml/deezer/AbstractSearchAlbum.java index c02d158..ebb5b6a 100644 --- a/ENSIM/Java/tp-deezer3-HERBRON_Tanguy/src/fr/ensim/xml/deezer/AbstractSearchAlbum.java +++ b/ENSIM/Java/tp-deezer3-HERBRON_Tanguy/src/fr/ensim/xml/deezer/AbstractSearchAlbum.java @@ -29,7 +29,7 @@ public abstract class AbstractSearchAlbum { * @throws ParserConfigurationException * @throws SAXException */ - public List find(String author) throws IOException, + public List find(String author, boolean isXML) throws IOException, ParserConfigurationException, SAXException { LOG.debug(">>find author="+author); @@ -38,7 +38,7 @@ public abstract class AbstractSearchAlbum { StringBuilder sUrl = new StringBuilder(); sUrl.append("http://api.deezer.com/2.0/search/album?q="); sUrl.append(author); - sUrl.append("&output=xml"); + if(isXML) sUrl.append("&output=xml"); URL url = new URL(sUrl.toString()); LOG.debug(url); diff --git a/ENSIM/Java/tp-deezer3-HERBRON_Tanguy/src/fr/ensim/xml/deezer/AbstractSearchAlbumTracks.java b/ENSIM/Java/tp-deezer3-HERBRON_Tanguy/src/fr/ensim/xml/deezer/AbstractSearchAlbumTracks.java index fcd0f85..28fdfbe 100644 --- a/ENSIM/Java/tp-deezer3-HERBRON_Tanguy/src/fr/ensim/xml/deezer/AbstractSearchAlbumTracks.java +++ b/ENSIM/Java/tp-deezer3-HERBRON_Tanguy/src/fr/ensim/xml/deezer/AbstractSearchAlbumTracks.java @@ -24,14 +24,14 @@ public abstract class AbstractSearchAlbumTracks { * @return la liste des chansons. * @throws Exception */ - public List find(String id) throws Exception { + public List find(String id, boolean isXML) throws Exception { log.debug(">>find"); // Constitution de l'URL StringBuilder sUrl = new StringBuilder(); sUrl.append("http://api.deezer.com/2.0/album/"); sUrl.append(id); - sUrl.append("?output=xml"); + if(isXML) sUrl.append("&output=xml"); URL url = new URL(sUrl.toString());