Changeset 50

Show
Ignore:
Timestamp:
05/06/07 13:32:57 (3 years ago)
Author:
chris
Message:

r7685@flan: chris | 2007-05-06 13:32:38 -0700
Completes trac ticket #5 "Submit 10 tracks per request"

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/org/lastpod/Scrobbler.java

    r49 r50  
    1919package org.lastpod; 
    2020 
     21import org.lastpod.chunk.Chunk; 
     22import org.lastpod.chunk.ChunkUtil; 
     23 
    2124import java.io.BufferedReader; 
    2225import java.io.IOException; 
     
    5457 */ 
    5558public class Scrobbler { 
     59    /** 
     60     * AudioScrobbler suggests a maximum number of tracks per chunk. 
     61     */ 
     62    private static final int MAX_TRACKS_PER_CHUNK = 10; 
     63 
    5664    /** 
    5765     * The minimum length (in seconds) of a track that meets Last.fm guidelines. 
     
    178186        String urlEncodedChallange = URLEncoder.encode(md5chal, "UTF-8"); 
    179187 
    180         pauseIfRequired(); 
    181  
    182         String queryString = "u=" + urlEncodedUsername + "&" + "s=" + urlEncodedChallange; 
    183  
    184         int tracknum = 0; 
    185  
    186         for (int i = 0; i < recentPlayed.size(); i++) { 
    187             TrackItem track = (TrackItem) recentPlayed.get(i); 
    188  
    189             /* Per Last.fm guidelines; do not submit tracks that are less 
    190              * than 30 characters in length. 
     188        /* Converts the recentPlayed List into a List of Chunk objects.  Each 
     189         * chunk stores at most 10 tracks.  Each chunk will be submitted to 
     190         * Last.fm individually, per their guidelines. 
     191         */ 
     192        List chunks = ChunkUtil.createChunks(recentPlayed, MAX_TRACKS_PER_CHUNK); 
     193 
     194        Chunk chunk = null; 
     195 
     196        for (int i = 0; i < chunks.size(); i++) { 
     197            pauseIfRequired(); 
     198 
     199            chunk = (Chunk) chunks.get(i); 
     200 
     201            String queryString = "u=" + urlEncodedUsername + "&" + "s=" + urlEncodedChallange; 
     202 
     203            int tracknum = 0; 
     204 
     205            for (int j = 0; j < chunk.getChunkSize(); j++) { 
     206                TrackItem track = (TrackItem) chunk.getContent().get(j); 
     207 
     208                /* Per Last.fm guidelines; do not submit tracks that are less 
     209                 * than 30 characters in length. 
     210                 */ 
     211                if (track.getLength() < MIN_TRACK_SECONDS) { 
     212                    continue; 
     213                } 
     214 
     215                queryString += buildTrackQueryString(track, tracknum); 
     216 
     217                tracknum++; 
     218            } 
     219 
     220            String content = null; 
     221 
     222            /* If a backup URL is specified then two submits will take place.  A 
     223             * backup URL can be used to send your information to another server. 
    191224             */ 
    192             if (track.getLength() < MIN_TRACK_SECONDS) { 
    193                 continue; 
    194             } 
    195  
    196             queryString += buildTrackQueryString(track, tracknum); 
    197  
    198             tracknum++; 
    199         } 
    200  
    201         String content = null; 
    202  
    203         /* If a backup URL is specified then two submits will take place.  A 
    204          * backup URL can be used to send your information to another server. 
    205          */ 
    206         if ((backupUrl != null) && !backupUrl.equals("")) { 
    207             content = fetchContent(backupUrl, queryString); 
    208             logger.log(Level.FINE, "Received from server:\n" + content); 
    209         } 
    210  
    211         String urlString = "http://" + submitHost + ":" + submitPort + submitUrl; 
    212         content = fetchContent(urlString, queryString); 
    213  
    214         String[] lines = content.split("\n"); 
    215  
    216         if ((lines[0].length() >= 6) && lines[0].substring(0, 6).equals("FAILED")) { 
    217             throw new RuntimeException(lines[0].substring(7)); 
    218         } 
    219  
    220         if ((lines[0].length() >= 7) && lines[0].substring(0, 7).equals("BADAUTH")) { 
    221             throw new FailedLoginException("Invalid username/password"); 
    222         } 
    223  
    224         if ((lines[0].length() >= 2) && !lines[0].substring(0, 2).equals("OK")) { 
    225             throw new RuntimeException("Unknown error submitting tracks"); 
     225            if ((backupUrl != null) && !backupUrl.equals("")) { 
     226                content = fetchContent(backupUrl, queryString); 
     227                logger.log(Level.FINE, "Received from server:\n" + content); 
     228            } 
     229 
     230            String urlString = "http://" + submitHost + ":" + submitPort + submitUrl; 
     231            content = fetchContent(urlString, queryString); 
     232 
     233            String[] lines = content.split("\n"); 
     234 
     235            /* Sets the interval, if it is present in the response. */ 
     236            if ((lines.length >= 2) && (lines[1].length() >= 10)) { 
     237                String wait = lines[1].substring(9); 
     238                interval = Integer.parseInt(wait); 
     239            } 
     240 
     241            if ((lines[0].length() >= 6) && lines[0].substring(0, 6).equals("FAILED")) { 
     242                throw new RuntimeException(lines[0].substring(7)); 
     243            } 
     244 
     245            if ((lines[0].length() >= 7) && lines[0].substring(0, 7).equals("BADAUTH")) { 
     246                throw new FailedLoginException("Invalid username/password"); 
     247            } 
     248 
     249            if ((lines[0].length() >= 2) && !lines[0].substring(0, 2).equals("OK")) { 
     250                throw new RuntimeException("Unknown error submitting tracks"); 
     251            } 
    226252        } 
    227253