Changeset 104

Show
Ignore:
Timestamp:
10/19/08 20:06:15 (2 years ago)
Author:
chris
Message:

r7962@ctilden-laptop: chris | 2008-10-19 20:04:57 -0700

r7958@ctilden-laptop: chris | 2008-10-19 19:26:36 -0700
version for working with cached submissions
r7959@ctilden-laptop: chris | 2008-10-19 19:29:05 -0700
merge of Maksim Liauchuk's patch from 7/6/2008
r7960@ctilden-laptop: chris | 2008-10-19 19:30:46 -0700
applied a code-format
r7961@ctilden-laptop: chris | 2008-10-19 19:43:58 -0700
fixes for minor javadoc errors

Files:

Legend:

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

    r76 r104  
    4444     * Submits the tracks to Last.fm 
    4545     * @param userInterface  The application's user interface. 
     46     * @param online  submission type: 
     47     *                  true - online, false - offline (to Last.fm client cache) 
    4648     * @return  A status message upon completion. 
    4749     */ 
    48     Object submitTracks(UI userInterface); 
     50    Object submitTracks(UI userInterface, boolean online); 
    4951} 
  • trunk/src/main/org/lastpod/ModelImpl.java

    r93 r104  
    135135     * Submits the tracks to Last.fm 
    136136     * @param userInterface  The application's user interface. 
     137     * @param online  submission type: 
     138     *                  true - online, false - offline (to Last.fm client cache) 
    137139     * @return  A status message upon completion. 
    138140     */ 
    139     public Object submitTracks(UI userInterface) { 
     141    public Object submitTracks(UI userInterface, boolean online) { 
    140142        Logger logger = Logger.getLogger(LastPod.class.getPackage().getName()); 
    141143        Preferences fPrefs = Preferences.userRoot().node("ws/afterglo/audioPod"); 
     
    167169        String backupUrl = fPrefs.get("backupUrl", ""); 
    168170 
     171        String submitCachePath = fPrefs.get("submitCachePath", ""); 
     172 
    169173        try { 
    170             Scrobbler scrobbler = new Scrobbler(username, encryptedPassword, backupUrl); 
     174            Scrobbler scrobbler = 
     175                new Scrobbler(username, encryptedPassword, backupUrl, submitCachePath); 
    171176 
    172177            List activeRecentPlayed = onlyActiveTrackItems(recentlyPlayed); 
     
    176181            scrobbler.setTracksToSubmit(activeRecentPlayed); 
    177182            scrobbler.addInactiveToHistories(inactiveRecentPlayed); 
    178             scrobbler.handshake(); 
    179             scrobbler.submitTracks(); 
     183 
     184            if (online) { 
     185                scrobbler.handshake(); 
     186                scrobbler.submitTracks(); 
     187            } else { 
     188                scrobbler.submitTracksToCache(); 
     189            } 
    180190 
    181191            /* Refresh track list. */ 
  • trunk/src/main/org/lastpod/Scrobbler.java

    r85 r104  
    2424import org.lastpod.util.IoUtils; 
    2525import org.lastpod.util.MiscUtilities; 
     26import org.lastpod.util.XmlUtils; 
     27 
     28import org.w3c.dom.Document; 
     29import org.w3c.dom.Element; 
     30import org.w3c.dom.NodeList; 
     31 
     32import org.xml.sax.SAXException; 
    2633 
    2734import java.io.BufferedReader; 
     35import java.io.File; 
     36import java.io.FileOutputStream; 
    2837import java.io.IOException; 
    2938import java.io.InputStream; 
     
    5564import javax.security.auth.login.FailedLoginException; 
    5665 
     66import javax.xml.parsers.DocumentBuilder; 
     67import javax.xml.parsers.DocumentBuilderFactory; 
     68import javax.xml.parsers.ParserConfigurationException; 
     69import javax.xml.transform.TransformerConfigurationException; 
     70import javax.xml.transform.TransformerException; 
     71 
    5772/** 
    5873 * @author muti 
     
    6984     */ 
    7085    private static final int MIN_TRACK_SECONDS = 30; 
     86 
     87    /** 
     88     * Last.fm client cache version. 
     89     */ 
     90    private static final String CACHE_VERSION = "1.2"; 
     91 
     92    /** 
     93     * Last.fm client product name. 
     94     */ 
     95    private static final String PRODUCT_NAME = "Audioscrobbler"; 
    7196    private String username; 
    7297    private String encryptedPassword; 
     
    76101    private Integer submitPort; 
    77102    private String submitUrl; 
     103    private String submitCachePath; 
    78104 
    79105    /** 
     
    94120    private Logger logger; 
    95121 
    96     public Scrobbler(String username, String encryptedPassword, String backupUrl) { 
     122    public Scrobbler(String username, String encryptedPassword, String backupUrl, 
     123        String submitCachePath) { 
    97124        this.username = username; 
    98125        this.encryptedPassword = encryptedPassword; 
    99126        this.backupUrl = backupUrl; 
     127        this.submitCachePath = submitCachePath; 
    100128        logger = Logger.getLogger(getClass().getPackage().getName()); 
    101129    } 
     
    336364    } 
    337365 
     366    public void submitTracksToCache() 
     367            throws UnsupportedEncodingException, NoSuchAlgorithmException, MalformedURLException, 
     368                IOException, FailedLoginException, ParserConfigurationException, SAXException, 
     369                TransformerConfigurationException, TransformerException, Exception { 
     370        String statusMessage = "Submitting tracks to cache..."; 
     371        chunkProgress.setSubmitStatusMessage(statusMessage); 
     372        logger.log(Level.INFO, statusMessage); 
     373 
     374        if (trackChunks.size() == 0) { 
     375            statusMessage = "No tracks to submit"; 
     376            chunkProgress.setSubmitStatusMessage(statusMessage); 
     377            throw new RuntimeException(statusMessage); 
     378        } 
     379 
     380        File cacheFile = new File(submitCachePath); 
     381 
     382        if (cacheFile.createNewFile()) { 
     383            FileOutputStream fos = new FileOutputStream(cacheFile); 
     384            String cacheString = ""; 
     385            cacheString += "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n"; 
     386            cacheString += ("<submissions version=\"" + CACHE_VERSION + "\" product=\"" 
     387            + PRODUCT_NAME + "\">\r\n"); 
     388            cacheString += "</submissions>"; 
     389            fos.write(cacheString.getBytes()); 
     390            fos.close(); 
     391        } 
     392 
     393        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 
     394        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 
     395        Document doc = docBuilder.parse(cacheFile); 
     396 
     397        NodeList submissions = doc.getElementsByTagName("submissions"); 
     398 
     399        Chunk chunk = null; 
     400 
     401        for (int i = 0; i < trackChunks.size(); i++) { 
     402            pauseIfRequired(); 
     403            chunk = (Chunk) trackChunks.get(i); 
     404 
     405            int tracknum = 0; 
     406 
     407            for (int j = 0; j < chunk.getChunkSize(); j++) { 
     408                TrackItem track = (TrackItem) chunk.getContent().get(j); 
     409 
     410                if (track.getLength() < MIN_TRACK_SECONDS) { 
     411                    continue; 
     412                } 
     413 
     414                Element item = doc.createElement("item"); 
     415                XmlUtils.addChild(doc, item, "artist", track.getArtist()); 
     416                XmlUtils.addChild(doc, item, "album", track.getAlbum()); 
     417                XmlUtils.addChild(doc, item, "track", track.getTrack()); 
     418                XmlUtils.addChild(doc, item, "duration", Long.toString(track.getLength())); 
     419                XmlUtils.addChild(doc, item, "timestamp", Long.toString(track.getLastplayed())); 
     420                XmlUtils.addChild(doc, item, "playcount", "0"); 
     421                XmlUtils.addChild(doc, item, "source", "1"); 
     422                XmlUtils.addChild(doc, item, "userActionFlags", "8"); 
     423                XmlUtils.addChild(doc, item, "playerId", "foo"); 
     424                submissions.item(0).appendChild(item); 
     425 
     426                tracknum++; 
     427            } 
     428 
     429            for (int j = 0; j < chunk.getChunkSize(); j++) { 
     430                TrackItem track = (TrackItem) chunk.getContent().get(j); 
     431                track.setActive(Boolean.FALSE); 
     432            } 
     433 
     434            addHistories(chunk.getContent()); 
     435 
     436            chunkProgress.updateCurrentChunk(i + 2); 
     437        } 
     438 
     439        XmlUtils.xmlToFile(doc, submitCachePath); 
     440 
     441        chunkProgress.setSubmitStatusMessage("Done. You may now sync your iPod."); 
     442        logger.log(Level.INFO, "Tracks submitted"); 
     443        logger.log(Level.INFO, 
     444            "You may now sync your iPod with your music management software " 
     445            + "or delete 'Play Counts' from the iTunes folder!"); 
     446 
     447        chunkProgress.setCompletionStatus(true); 
     448    } 
     449 
    338450    /** 
    339451     * Last.fm will informs this client if it needs to pause.  This occurs when 
  • trunk/src/main/org/lastpod/action/SubmitTracks.java

    r77 r104  
    139139                        busyIconTimer.start(); 
    140140 
    141                         return model.submitTracks(userInterface); 
     141                        // TODO 
     142                        return model.submitTracks(userInterface, false); 
    142143                    } 
    143144