Importing SoundCloud favorites into Ex.Fm using Java and spring-social-music
I’m loving Ex.Fm and wanted to import my SoundCloud favorites into my Ex.Fm profile as loved tracks. Spent this afternoon making some changes to spring-social-exfm and spring-social-soundcloud modules to allow me to do this with a few lines of Java code.
Here’s a snippet from my new spring-social-music sample utils module that does the job…
// Construct a new SoundCloudTemplate only authorized by api key, as user-level authorization is not needed
// to retrieve favorite tracks
SoundCloud soundCloud = new SoundCloudTemplate(soundCloudApiKey);
// Construct a new ExFmTemplate authorized for a specific user, specifying api base url, username and password
ExFm exFm = new ExFmTemplate("http://ex.fm/api/v3",exFmUserId,exFmPassword);
// Retrieve the first page of SoundCloud favorite tracks
Page<Track> soundCloudFavorites = soundCloud.usersOperations().userOperations(soundCloudUserId).getFavorites(new PageRequest(0,soundCloudFetchPageSize));
// For each page of SoundCloud favorite tracks...
for (int pageNumber = 0; pageNumber < soundCloudFavorites.getTotalPages();pageNumber++)
{
// For each SoundCloud favorite track on current page, love the track on Ex.Fm using the stream url
for (Track soundCloudTrack : soundCloudFavorites)
{
sleepForLimitRate();
try
{
exFm.songOperations().loveSongBySourceUrl(soundCloudTrack.getStreamUrl());
}
catch (ResourceNotFoundException e)
{
// Failed to love song as Ex.Fm can't find information about the soundcloud track with this url
}
}
// Get next page of tracks from SoundCloud if available
if (soundCloudFavorites.hasNextPage()) soundCloudFavorites = soundCloud.usersOperations().userOperations(soundCloudUserId).getFavorites(new PageRequest(pageNumber + 1,soundCloudFetchPageSize));
}
Checkout the spring-social-music module to see the full source code and dependencies used by the snippet above, or add the following repository and dependency to your maven project in order to use or modify the simple utility class for your project.
<repositories>
<repository>
<id>opensourceagility-snapshots</id>
<url>http://repo.opensourceagility.com/snapshots</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.springsocialmusic</groupId>
<artifactId>spring-social-music</artifactId>
<version>1.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
The utility class to import SoundCloud favorites into Ex.Fm as loved tracks could then be called as shown below:
SoundCloudToExFmMigrator soundCloudToExFmMigrator = new SoundCloudToExFmMigrator(mySoundCloudApiKey); soundCloudToExFmMigrator.loveSoundCloudFavoritesOnExFm(fromSoundCloudUserName, exFmUserName,exFmPassword);
