001package ca.bc.webarts.widgets; 002 003import java.util.Calendar; 004import java.util.Date; 005import java.io.IOException; 006import java.text.DateFormat; 007 008 009import de.umass.lastfm.Event; 010import de.umass.lastfm.Geo; 011import de.umass.lastfm.scrobble.ResponseStatus; 012import de.umass.lastfm.scrobble.Scrobbler; 013import de.umass.lastfm.scrobble.Source; 014import de.umass.lastfm.PaginatedResult; 015 016/** 017 * @author Tom Gutwin 018 */ 019public class Scrobble 020{ 021 String key = "yourOwnKey"; 022 String userId = "userName"; 023 String userPass = "password"; 024 String clientId = "tst"; //"jOggPlayer"; 025 String clientVersion = "1.0"; //"1.1.6"; 026 String currArtist = "Bryan Adams"; 027 String currTitle = "Run To You"; 028 String currAlbum = "Reckless"; 029 public Date startTime = null; 030 public static ResponseStatus resp = null ; 031 Scrobbler sc = null; 032 033 /** 034 * Scrobble Class to enable scrobbling of tunes to Last.fm. 035 * It uses the Roar Software Java Wrapper of the last.fm web services api. 036 * See http://www.u-mass.de/lastfm 037 **/ 038 public Scrobble() 039 { 040 /* Get a Scrobbler Oject */ 041 sc = de.umass.lastfm.scrobble.Scrobbler.newScrobbler(clientId, clientVersion, userId) ; 042 043 /* handshake with Last.fm */ 044 //handshake(); 045 } 046 047 048 /** Performs the handshake and returns success or failure **/ 049 public boolean handshake() 050 { 051 boolean retVal = true; 052 try 053 { 054 resp = sc.handshake(userPass); 055 if (!resp.ok()) retVal = false; 056 } 057 catch (IOException ioEx) 058 { 059 retVal = false; 060 //ioEx.printStackTrace(); 061 } 062 return retVal; 063 } 064 065 066 public static void main(String[] args) 067 { 068 Scrobble instance = new Scrobble(); 069 /* handshake with last.fm */ 070 try 071 { 072 //instance.handshake(); 073 if (instance.handshake()) 074 { 075 System.out.println("Handshake Succeeded for user: "+instance.userId); 076 077 // Simulate 5 song to be played 078 for (int i = 1; i < 5;i++) 079 { 080 /* Tell last.fm Whats currently Playing */ 081 instance.resp = instance.sc.nowPlaying(instance.currArtist, instance.currTitle, instance.currAlbum ,0,-1 ); 082 if (instance.resp.ok()) 083 { 084 System.out.println("Now Playing: "+ instance.currArtist+" "+instance.currTitle+" "+instance.currAlbum); 085 try 086 { 087 instance.startTime = new Date(); 088 Thread.sleep(25000); 089 } 090 catch (InterruptedException iex) 091 { 092 // don't need to do anything 093 } 094 095 /* Sumit the song as played to last.fm */ 096 System.out.println("Submitting: "+ instance.currArtist+" "+instance.currTitle+" "+instance.currAlbum+ " at " + instance.startTime.getTime()); 097 instance.resp = instance.sc.submit(instance.currArtist, instance.currTitle, instance.currAlbum , 098 180,5, 099 de.umass.lastfm.scrobble.Source.USER,instance.startTime.getTime()/1000); 100 if (instance.resp.ok()) 101 { 102 System.out.println("Song Sumbitted: "+ instance.currArtist+" "+instance.currTitle+" "+instance.currAlbum); 103 } 104 else 105 { 106 if (instance.resp != null) 107 { 108 System.out.println("Sumbmit Failed: "+ instance.currArtist+" "+instance.currTitle+" "+instance.currAlbum + " --> ("+getErrorType(resp.getStatus()) +") "+instance.resp.getMessage()); 109 } 110 } 111 } 112 else 113 { 114 if (instance.resp != null) 115 { 116 System.out.println("Now Playing Failed for user: "+instance.userId + " - ("+getErrorType(resp.getStatus()) +") "+instance.resp.getMessage()); 117 } 118 } 119 120 } 121 } 122 else 123 { 124 if (resp != null) 125 { 126 System.out.println("Handshake Failed for user: "+instance.userId + " - ("+getErrorType(resp.getStatus()) +") "+instance.resp.getMessage()); 127 } 128 } 129 } 130 catch (IOException ioEx) 131 { 132 ioEx.printStackTrace(); 133 } 134 135 } 136 137 138 /** A small Lookup method to translate the ResponseStatus code into a String 139 * @return string describing the responseCode 140 **/ 141 public static String getErrorType(int err) 142 { 143 String retVal = ""; 144 switch (err) 145 { 146 case ResponseStatus.BADAUTH: 147 retVal = "BADAUTH"; 148 break; 149 case ResponseStatus.BADSESSION: 150 retVal = "BADSESSION"; 151 break; 152 case ResponseStatus.BADTIME: 153 retVal = "BADTIME"; 154 break; 155 case ResponseStatus.BANNED: 156 retVal = "BANNED"; 157 break; 158 case ResponseStatus.FAILED: 159 retVal = "FAILED"; 160 break; 161 case ResponseStatus.OK: 162 retVal = "OK"; 163 } 164 return retVal; 165 } 166}