Package org.socialmusicdiscovery.server.plugins.mediaimport

Examples of org.socialmusicdiscovery.server.plugins.mediaimport.TrackData


public class Mp3TagReaderTest {
    @Test
    public void testSimple() throws IOException {
        String filename = BaseTestCase.getTestResourceDiretory() + "org/socialmusicdiscovery/server/plugins/mediaimport/filesystem/testfile1.mp3";
        TrackData data = new Mp3TagReader(null).getTrackData(new File(filename));
        assert data != null;
        assert data.getFile().equals(filename);
        assert data.getUrl().startsWith("file:/");
        assert data.getUrl().endsWith("testfile1.mp3");
        assert data.getFormat().equals("mp3");
        assert data.getSmdID().equals("ab2dbe29c46c1668b89cf4bd17e34a59-000004e4");
        assert data.getTags() != null;
        assert data.getTags().size() == 8;
        boolean foundAlbumArtist = false;
        boolean foundArtist = false;
        for (TagData tagData : data.getTags()) {
            if (tagData.getName().equals("ARTIST") && tagData.getValue().equals("EPMD")) {
                foundArtist = true;
            } else if (tagData.getName().equals("BAND") && tagData.getValue().equals("Kenny \"Dope\" Gonzalez")) {
                foundAlbumArtist = true;
            }
View Full Code Here


    }

    @Test(enabled = false)
    public void testSimplev1() throws IOException {
        String filename = BaseTestCase.getTestResourceDiretory() + "org/socialmusicdiscovery/server/plugins/mediaimport/filesystem/testfile1v1.mp3";
        TrackData data = new Mp3TagReader(null).getTrackData(new File(filename));
        assert data != null;
        assert data.getFile().equals(filename);
        assert data.getUrl().startsWith("file:/");
        assert data.getUrl().endsWith("testfile1v1.mp3");
        assert data.getFormat().equals("mp3");
        assert data.getSmdID().equals("ab2dbe29c46c1668b89cf4bd17e34a59-000004e4");
        assert data.getTags() != null;
        assert data.getTags().size() == 8;
        boolean foundAlbumArtist = false;
        boolean foundArtist = false;
        for (TagData tagData : data.getTags()) {
            if (tagData.getName().equals("ARTIST") && tagData.getValue().equals("EPMD")) {
                foundArtist = true;
            } else if (tagData.getName().equals("BAND") && tagData.getValue().equals("Kenny \"Dope\" Gonzalez")) {
                foundAlbumArtist = true;
            }
View Full Code Here

                    if (!isAborted()) {
                        if (i == 0) {
                            entityManager.getTransaction().begin();
                        }

                        TrackData track = new TrackData();
                        track.setFile(trackData.getUri());
                        track.setFormat("spotify");
                        track.setSmdID(trackData.getUri());
                        track.setUrl(trackData.getUri());
                        track.setTags(new ArrayList<TagData>());
                        track.getTags().add(new TagData(TagData.ALBUM, trackData.getAlbum()));
                        track.getTags().add(new TagData(TagData.TITLE, trackData.getName()));
                        track.getTags().add(new TagData(TagData.SPOTIFY_TRACK_ID, trackData.getUri()));
                        track.getTags().add(new TagData("SPOTIFYIMAGE", trackData.getCover()));
                        track.getTags().add(new TagData("DURATION", "" + trackData.getDuration()));

                        if (trackData.getIndex() != null) {
                            if (trackData.getIndex() != null && trackData.getIndex() != 0) {
                                track.getTags().add(new TagData(TagData.TRACKNUM, "" + trackData.getIndex()));
                            }
                            if (trackData.getDisc() != null && trackData.getDisc() != 0) {
                                track.getTags().add(new TagData(TagData.DISC, "" + trackData.getDisc()));
                            }
                            if (trackData.getAlbumuri() != null) {
                                track.getTags().add(new TagData(TagData.SPOTIFY_ALBUM_ID, trackData.getAlbumuri()));
                            }
                            if (trackData.getArtists() != null) {
                                for (SpotifyArtistData artistData : trackData.getArtists()) {
                                    track.getTags().add(new TagData(TagData.ARTIST, artistData.getName()));
                                    track.getTags().add(new TagData(TagData.SPOTIFY_ARTIST_ID, artistData.getUri()));
                                }
                            }
                        } else {
                            //TODO: Remove this as soon as 2.2.7 of spotifyd has been officially released
                            response = c.resource(SERVICE_URL + "/" + trackData.getUri() + "/browse.json").accept("application/json").get(JSONObject.class);
                            try {
                                if (response.has("index") && !response.getString("index").equals("0")) {
                                    track.getTags().add(new TagData(TagData.TRACKNUM, response.getString("index")));
                                }
                                if (response.has("disc") && !response.getString("disc").equals("0")) {
                                    track.getTags().add(new TagData(TagData.DISC, response.getString("disc")));
                                }
                                if (response.has("albumuri") && response.getString("albumuri").length() > 0) {
                                    track.getTags().add(new TagData(TagData.SPOTIFY_ALBUM_ID, response.getString("albumuri")));
                                }
                                if (response.has("artists")) {
                                    JSONArray artistsArray = response.getJSONArray("artists");
                                    for (int j = 0; j < artistsArray.length(); j++) {
                                        JSONObject artistObject = artistsArray.getJSONObject(j);
                                        track.getTags().add(new TagData(TagData.ARTIST, artistObject.getString("name")));
                                        track.getTags().add(new TagData(TagData.SPOTIFY_ARTIST_ID, artistObject.getString("uri")));
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                                //TODO: Implement error handling
                            }
                        }

                        progressHandler.progress(getId(), trackData.getAlbum() + "/" + trackData.getName(), offset + 1, (long) noOfTracks);
                        if (track != null) {
                            try {
                                importNewPlayableElement(track);
                            } catch (ConstraintViolationException e) {
                                //TODO: Change this so it uses the logging framework
                                System.err.println("ERROR when importing: " + track.getFile() + ": ");
                                for (ConstraintViolation<?> violation : e.getConstraintViolations()) {
                                    System.err.println("- " + violation.getLeafBean().getClass().getSimpleName() + "." + violation.getPropertyPath().toString() + ": " + violation.getMessage());
                                }
                            }
                        } else {
View Full Code Here

    public TrackData getTrackData(File file) throws IOException {
        if (!file.exists() || !isSupportedFileType(file)) {
            return null;
        }
        try {
            TrackData result = new TrackData();
            result.setFile(file.getCanonicalPath());
            result.setUrl(file.toURI().toString());
            result.setFormat(getFormat(file));
            result.setSmdID(getSmdId(file));
            result.setTags(new ArrayList<TagData>());
            Map<String, List<String>> normalTags = new HashMap<String, List<String>>();
            Map<String, List<String>> sortTags = new HashMap<String, List<String>>();
            AudioFile f = AudioFileIO.read(file);
            Tag tag = f.getTag();
            Iterator<TagField> it = tag.getFields();
            while (it.hasNext()) {
                TagField tf = it.next();
                TagData tagData = getTagData(tf);
                if (tagData != null) {
                    String[] values;
                    if (separatorCharacters != null && separatorCharacters.trim().length() > 0) {
                        values = tagData.getValue().split("[" + separatorCharacters + "]");
                    } else {
                        values = new String[]{tagData.getValue()};
                    }
                    for (String value : values) {
                        if (value != null && value.trim().length() > 0) {
                            String name = getMappedTagName(tagData.getName());
                            if (name != null) {
                                if (normalTags.containsKey(name)) {
                                    normalTags.get(name).add(value);
                                } else {
                                    normalTags.put(name, new ArrayList<String>(Arrays.asList(value)));
                                }
                            } else if (getSortTag(tagData.getName()) != null) {
                                String sortName = getSortTag(tagData.getName());
                                if (sortTags.containsKey(sortName)) {
                                    sortTags.get(sortName).add(value);
                                } else {
                                    sortTags.put(sortName, new ArrayList<String>(Arrays.asList(value)));
                                }
                            }
                        }
                    }
                }
            }
            for (Map.Entry<String, List<String>> entry : normalTags.entrySet()) {
                List<String> values = entry.getValue();
                List<String> sortValues = null;
                if (sortTags.containsKey(entry.getKey())) {
                    sortValues = sortTags.get(entry.getKey());
                }
                int i = 0;
                for (String value : values) {
                    String sortValue = null;
                    if (sortValues != null && sortValues.size() > i) {
                        sortValue = sortValues.get(i);
                    }
                    result.getTags().add(new TagData(entry.getKey(), value, sortValue));
                }
            }
            return result;
        } catch (TagException e) {
            throw new IOException(e);
View Full Code Here

                if (shouldScan) {
                    if (i == 0) {
                        entityManager.getTransaction().begin();
                    }
                    try {
                        TrackData track = scanFile(file);
                        progressHandler.progress(getId(), track.getFile(), offset + 1, (long) files.size());
                        if (track != null) {
                            try {
                                importNewPlayableElement(track);
                            } catch (ConstraintViolationException e) {
                                //TODO: Change this so it uses the logging framework
                                System.err.println("ERROR when importing: " + track.getFile() + ": ");
                                for (ConstraintViolation<?> violation : e.getConstraintViolations()) {
                                    System.err.println("- " + violation.getLeafBean().getClass().getSimpleName() + "." + violation.getPropertyPath().toString() + ": " + violation.getMessage());
                                }
                            }
                        } else {
View Full Code Here

     */
    TrackData scanFile(File file) throws IOException {
        String separatorCharacters = getConfiguration().getStringParameter("separatorCharacters");
        TagReader[] tagReaders = {new FlacTagReader(separatorCharacters), new Mp3TagReader(separatorCharacters)};
        for (TagReader tagReader : tagReaders) {
            TrackData data = tagReader.getTrackData(file);
            if (data != null) {
                data.setLastModified(file.lastModified());
                return data;
            }
        }
        return null;
    }
View Full Code Here

TOP

Related Classes of org.socialmusicdiscovery.server.plugins.mediaimport.TrackData

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.