Changeset 104
- Timestamp:
- 10/19/08 20:06:15 (2 years ago)
- Files:
-
- trunk/src/main/org/lastpod/Model.java (modified) (1 diff)
- trunk/src/main/org/lastpod/ModelImpl.java (modified) (3 diffs)
- trunk/src/main/org/lastpod/Scrobbler.java (modified) (6 diffs)
- trunk/src/main/org/lastpod/action/SubmitTracks.java (modified) (1 diff)
- trunk/src/main/org/lastpod/util/XmlUtils.java (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/main/org/lastpod/Model.java
r76 r104 44 44 * Submits the tracks to Last.fm 45 45 * @param userInterface The application's user interface. 46 * @param online submission type: 47 * true - online, false - offline (to Last.fm client cache) 46 48 * @return A status message upon completion. 47 49 */ 48 Object submitTracks(UI userInterface );50 Object submitTracks(UI userInterface, boolean online); 49 51 } trunk/src/main/org/lastpod/ModelImpl.java
r93 r104 135 135 * Submits the tracks to Last.fm 136 136 * @param userInterface The application's user interface. 137 * @param online submission type: 138 * true - online, false - offline (to Last.fm client cache) 137 139 * @return A status message upon completion. 138 140 */ 139 public Object submitTracks(UI userInterface ) {141 public Object submitTracks(UI userInterface, boolean online) { 140 142 Logger logger = Logger.getLogger(LastPod.class.getPackage().getName()); 141 143 Preferences fPrefs = Preferences.userRoot().node("ws/afterglo/audioPod"); … … 167 169 String backupUrl = fPrefs.get("backupUrl", ""); 168 170 171 String submitCachePath = fPrefs.get("submitCachePath", ""); 172 169 173 try { 170 Scrobbler scrobbler = new Scrobbler(username, encryptedPassword, backupUrl); 174 Scrobbler scrobbler = 175 new Scrobbler(username, encryptedPassword, backupUrl, submitCachePath); 171 176 172 177 List activeRecentPlayed = onlyActiveTrackItems(recentlyPlayed); … … 176 181 scrobbler.setTracksToSubmit(activeRecentPlayed); 177 182 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 } 180 190 181 191 /* Refresh track list. */ trunk/src/main/org/lastpod/Scrobbler.java
r85 r104 24 24 import org.lastpod.util.IoUtils; 25 25 import org.lastpod.util.MiscUtilities; 26 import org.lastpod.util.XmlUtils; 27 28 import org.w3c.dom.Document; 29 import org.w3c.dom.Element; 30 import org.w3c.dom.NodeList; 31 32 import org.xml.sax.SAXException; 26 33 27 34 import java.io.BufferedReader; 35 import java.io.File; 36 import java.io.FileOutputStream; 28 37 import java.io.IOException; 29 38 import java.io.InputStream; … … 55 64 import javax.security.auth.login.FailedLoginException; 56 65 66 import javax.xml.parsers.DocumentBuilder; 67 import javax.xml.parsers.DocumentBuilderFactory; 68 import javax.xml.parsers.ParserConfigurationException; 69 import javax.xml.transform.TransformerConfigurationException; 70 import javax.xml.transform.TransformerException; 71 57 72 /** 58 73 * @author muti … … 69 84 */ 70 85 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"; 71 96 private String username; 72 97 private String encryptedPassword; … … 76 101 private Integer submitPort; 77 102 private String submitUrl; 103 private String submitCachePath; 78 104 79 105 /** … … 94 120 private Logger logger; 95 121 96 public Scrobbler(String username, String encryptedPassword, String backupUrl) { 122 public Scrobbler(String username, String encryptedPassword, String backupUrl, 123 String submitCachePath) { 97 124 this.username = username; 98 125 this.encryptedPassword = encryptedPassword; 99 126 this.backupUrl = backupUrl; 127 this.submitCachePath = submitCachePath; 100 128 logger = Logger.getLogger(getClass().getPackage().getName()); 101 129 } … … 336 364 } 337 365 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 338 450 /** 339 451 * Last.fm will informs this client if it needs to pause. This occurs when trunk/src/main/org/lastpod/action/SubmitTracks.java
r77 r104 139 139 busyIconTimer.start(); 140 140 141 return model.submitTracks(userInterface); 141 // TODO 142 return model.submitTracks(userInterface, false); 142 143 } 143 144
