JSON Parsing

- Add JSON Album parsing
- Add JSON Album's tracks parsing

Convert project to Maven project to add org.json from repos

- Update abstract classes to fit XML/JSON parsing

- Add JSON Album parsing JUnit
- Add JSON Album's tracks parsing JUnit
This commit is contained in:
Tanguy Herbron 2018-10-14 17:17:30 +02:00
parent c35879a65f
commit c6c7005be8
5 changed files with 166 additions and 4 deletions

View File

@ -25,4 +25,11 @@
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<dependencies>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180813</version>
</dependency>
</dependencies>
</project> </project>

View File

@ -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<Album> readAlbums(InputStream in) throws Exception
{
LOG.debug(">>readAlbums");
List<Album> listAlbums = new ArrayList<Album>();
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("<<readAlbums");
return listAlbums;
}
}

View File

@ -0,0 +1,91 @@
package fr.ensim.json;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import fr.ensim.xml.deezer.data.Track;
public class JSONSearchAlbumTracks {
private static Logger LOG = Logger.getLogger(JSONSearchAlbumTracks.class);
public static List<Track> 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("<<albums");
return null;
}
protected static List<Track> find(InputStream in) throws SAXException, ParserConfigurationException, IOException {
LOG.debug(">>find");
ArrayList<Track> listTracks = new ArrayList<Track>();
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;
}
}

View File

@ -29,7 +29,7 @@ public abstract class AbstractSearchAlbum {
* @throws ParserConfigurationException * @throws ParserConfigurationException
* @throws SAXException * @throws SAXException
*/ */
public List<Album> find(String author) throws IOException, public List<Album> find(String author, boolean isXML) throws IOException,
ParserConfigurationException, ParserConfigurationException,
SAXException { SAXException {
LOG.debug(">>find author="+author); LOG.debug(">>find author="+author);
@ -38,7 +38,7 @@ public abstract class AbstractSearchAlbum {
StringBuilder sUrl = new StringBuilder(); StringBuilder sUrl = new StringBuilder();
sUrl.append("http://api.deezer.com/2.0/search/album?q="); sUrl.append("http://api.deezer.com/2.0/search/album?q=");
sUrl.append(author); sUrl.append(author);
sUrl.append("&output=xml"); if(isXML) sUrl.append("&output=xml");
URL url = new URL(sUrl.toString()); URL url = new URL(sUrl.toString());
LOG.debug(url); LOG.debug(url);

View File

@ -24,14 +24,14 @@ public abstract class AbstractSearchAlbumTracks {
* @return la liste des chansons. * @return la liste des chansons.
* @throws Exception * @throws Exception
*/ */
public List<Track> find(String id) throws Exception { public List<Track> find(String id, boolean isXML) throws Exception {
log.debug(">>find"); log.debug(">>find");
// Constitution de l'URL // Constitution de l'URL
StringBuilder sUrl = new StringBuilder(); StringBuilder sUrl = new StringBuilder();
sUrl.append("http://api.deezer.com/2.0/album/"); sUrl.append("http://api.deezer.com/2.0/album/");
sUrl.append(id); sUrl.append(id);
sUrl.append("?output=xml"); if(isXML) sUrl.append("&output=xml");
URL url = new URL(sUrl.toString()); URL url = new URL(sUrl.toString());