Changeset 40

Show
Ignore:
Timestamp:
04/23/07 00:15:19 (2 years ago)
Author:
chris
Message:

r7629@flan: chris | 2007-04-23 00:15:12 -0700
preferences now have a backup URL available this will allow LastPod to submit
to another URL in addition to last.fm

Files:

Legend:

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

    r38 r40  
    116116        } 
    117117 
     118        String backupUrl = fPrefs.get("backupUrl", ""); 
     119 
    118120        try { 
    119             LastPod.scrobbler = new Scrobbler(username, encryptedPassword); 
     121            LastPod.scrobbler = new Scrobbler(username, encryptedPassword, backupUrl); 
    120122 
    121123            List activeRecentPlayed = onlyActiveTrackItems(recentplayed); 
  • trunk/src/org/lastpod/PreferencesEditor.java

    r39 r40  
    6060    private JPasswordField passfield; 
    6161    private JTextField dbfield; 
     62    private JTextField backupUrlField; 
    6263    private JTextField iTunesfield; 
    6364    private JCheckBox iTCheck; 
     
    141142        JPanel p4 = new JPanel(); 
    142143        p4.setLayout(new SpringLayout()); 
     144        p.add(new JLabel()); 
    143145 
    144146        TitledBorder b4 = BorderFactory.createTitledBorder("iTunes:"); 
     
    218220        p3.setToolTipText(toolTip); 
    219221 
    220         JLabel j3 = new JLabel("Backup URL: "); 
    221         p3.add(j3); 
    222         p3.add(new JTextField()); 
     222        JLabel backupUrlLabel = new JLabel("Backup URL: "); 
     223        p3.add(backupUrlLabel); 
     224        backupUrlField = new JTextField(); 
     225        p3.add(backupUrlField); 
    223226 
    224227        SpringUtilities.makeCompactGrid(p3, 1, 2, 5, 2, 3, 4); 
     
    308311        this.userfield.setText(fPrefs.get("Username", "<Username>")); 
    309312        this.dbfield.setText(fPrefs.get("iTunes Path", "<iPod iTunes Database Location>")); 
    310         this.iTunesfield.setText(fPrefs.get("iT Path", "   < Enter iTunes path >   ")); 
     313        this.backupUrlField.setText(fPrefs.get("backupUrl", "")); 
     314        this.iTunesfield.setText(fPrefs.get("iT Path", "")); 
    311315 
    312316        if (fPrefs.get("iTunes Status", "Enabled").equals("Enabled")) { 
     
    334338 
    335339        fPrefs.put("iTunes Path", this.dbfield.getText()); 
     340        fPrefs.put("backupUrl", this.backupUrlField.getText()); 
    336341        fPrefs.put("iT Path", this.iTunesfield.getText()); 
    337342        fPrefs.put("iTunes Status", this.iTunesStatus.getText()); 
  • trunk/src/org/lastpod/Scrobbler.java

    r38 r40  
    2222import java.io.BufferedReader; 
    2323import java.io.IOException; 
     24import java.io.InputStream; 
    2425import java.io.InputStreamReader; 
     26import java.io.OutputStream; 
    2527import java.io.OutputStreamWriter; 
     28import java.io.Reader; 
    2629import java.io.UnsupportedEncodingException; 
    2730 
    2831import java.net.HttpURLConnection; 
    2932import java.net.MalformedURLException; 
     33import java.net.ProtocolException; 
    3034import java.net.URL; 
    3135import java.net.URLEncoder; 
     
    5357    private String username; 
    5458    private String encryptedPassword; 
     59    private String backupUrl; 
    5560    private String challenge; 
    5661    private String submithost; 
     
    5964    private Logger logger; 
    6065 
    61     public Scrobbler(String username, String encryptedPassword) { 
     66    public Scrobbler(String username, String encryptedPassword, String backupUrl) { 
    6267        this.username = username; 
    6368        this.encryptedPassword = encryptedPassword; 
     69        this.backupUrl = backupUrl; 
    6470        this.logger = Logger.getLogger(this.getClass().getPackage().getName()); 
    6571    } 
     
    189195        querystring = querystring.substring(0, querystring.length() - 1); //trim last & 
    190196 
    191         URL url = new URL("http://" + this.submithost + ":" + this.submitport + this.submiturl); 
     197        String content = null; 
     198 
     199        /* If a backup URL is specified then two submits will take place.  A 
     200         * backup URL can be used to send your information to another server. 
     201         */ 
     202        if ((backupUrl != null) && !backupUrl.equals("")) { 
     203            content = fetchContent(backupUrl, querystring); 
     204            logger.log(Level.FINE, "Received from server:\n" + content); 
     205        } 
     206 
     207        String urlString = "http://" + submithost + ":" + submitport + submiturl; 
     208        content = fetchContent(urlString, querystring); 
     209        logger.log(Level.FINE, "Received from server:\n" + content); 
     210 
     211        if ((content == null) || (content.length() == 0)) { 
     212            throw new RuntimeException("Invalid response received from AudioScrobbler"); 
     213        } 
     214 
     215        String[] lines = content.split("\n"); 
     216 
     217        if ((lines[0].length() >= 6) && lines[0].substring(0, 6).equals("FAILED")) { 
     218            throw new RuntimeException(lines[0].substring(7)); 
     219        } 
     220 
     221        if ((lines[0].length() >= 7) && lines[0].substring(0, 7).equals("BADAUTH")) { 
     222            throw new FailedLoginException("Invalid username/password"); 
     223        } 
     224 
     225        if ((lines[0].length() >= 2) && !lines[0].substring(0, 2).equals("OK")) { 
     226            throw new RuntimeException("Unknown error submitting tracks"); 
     227        } 
     228 
     229        this.logger.log(Level.INFO, "Tracks submitted"); 
     230        this.logger.log(Level.INFO, 
     231            "You must now sync your iPod with your music management software " 
     232            + "or delete 'Play Counts' from the iTunes folder!"); 
     233    } 
     234 
     235    /** 
     236     * Creates the histories and writes them to a file. 
     237     * @param activeRecentPlayed  The list of active recently played tracks. 
     238     * @param inactiveRecentPlayed  The list of inactive recently played tracks. 
     239     */ 
     240    public void addHistories(List activeRecentPlayed, List inactiveRecentPlayed) { 
     241        for (int i = 0; i < activeRecentPlayed.size(); i++) { 
     242            TrackItem track = (TrackItem) activeRecentPlayed.get(i); 
     243            History.getInstance().addhistory(track.getLastplayed()); 
     244        } 
     245 
     246        for (int i = 0; i < inactiveRecentPlayed.size(); i++) { 
     247            TrackItem track = (TrackItem) inactiveRecentPlayed.get(i); 
     248 
     249            if (History.getInstance().isInHistory(track.getLastplayed())) { 
     250                History.getInstance().addhistory(track.getLastplayed()); 
     251            } 
     252        } 
     253 
     254        History.getInstance().write(); 
     255    } 
     256 
     257    /** 
     258     * Fetches the HTTP content given a URL String and a query String. 
     259     * @param urlString  The URL to fetch from. 
     260     * @param querystring  The query String to submit. 
     261     * @return  The content returned from the request. 
     262     * @throws MalformedURLException  Thrown if exceptions occur. 
     263     * @throws IOException  Thrown if exceptions occur. 
     264     * @throws ProtocolException  Thrown if exceptions occur. 
     265     */ 
     266    private String fetchContent(String urlString, String querystring) 
     267            throws MalformedURLException, IOException, ProtocolException { 
     268        String content = null; 
     269        URL url = new URL(urlString); 
    192270        this.logger.log(Level.FINE, "Submitting tracks to URL: " + url.toString()); 
    193271 
     
    204282        this.logger.log(Level.FINE, "POST query string:\n" + querystring); 
    205283 
    206         OutputStreamWriter wr = new OutputStreamWriter(c.getOutputStream()); 
    207         wr.write(querystring); 
    208         wr.flush(); 
    209         wr.close(); 
    210  
    211         if (c.getResponseCode() != 200) { 
    212             throw new RuntimeException("Invalid HTTP return code"); 
    213         } 
    214  
    215         BufferedReader breader = new BufferedReader(new InputStreamReader(c.getInputStream())); 
    216  
    217         String content = null; 
    218         String buffer = null; 
    219  
    220         while ((buffer = breader.readLine()) != null) { 
    221             if (content != null) { 
    222                 content += (buffer + "\n"); 
    223             } else { 
    224                 content = buffer + "\n"; 
    225             } 
    226         } 
    227  
    228         this.logger.log(Level.FINE, "Received from server:\n" + content); 
    229  
    230         if ((content == null) || (content.length() == 0)) { 
    231             throw new RuntimeException("Invalid response received from AudioScrobbler"); 
    232         } 
    233  
    234         String[] lines = content.split("\n"); 
    235  
    236         if ((lines[0].length() >= 6) && lines[0].substring(0, 6).equals("FAILED")) { 
    237             throw new RuntimeException(lines[0].substring(7)); 
    238         } 
    239  
    240         if ((lines[0].length() >= 7) && lines[0].substring(0, 7).equals("BADAUTH")) { 
    241             throw new FailedLoginException("Invalid username/password"); 
    242         } 
    243  
    244         if ((lines[0].length() >= 2) && !lines[0].substring(0, 2).equals("OK")) { 
    245             throw new RuntimeException("Unknown error submitting tracks"); 
    246         } 
    247  
    248         this.logger.log(Level.INFO, "Tracks submitted"); 
    249         this.logger.log(Level.INFO, 
    250             "You must now sync your iPod with your music management software " 
    251             + "or delete 'Play Counts' from the iTunes folder!"); 
    252     } 
    253  
    254     /** 
    255      * Creates the histories and writes them to a file. 
    256      * @param activeRecentPlayed  The list of active recently played tracks. 
    257      * @param inactiveRecentPlayed  The list of inactive recently played tracks. 
    258      */ 
    259     public void addHistories(List activeRecentPlayed, List inactiveRecentPlayed) { 
    260         for (int i = 0; i < activeRecentPlayed.size(); i++) { 
    261             TrackItem track = (TrackItem) activeRecentPlayed.get(i); 
    262             History.getInstance().addhistory(track.getLastplayed()); 
    263         } 
    264  
    265         for (int i = 0; i < inactiveRecentPlayed.size(); i++) { 
    266             TrackItem track = (TrackItem) inactiveRecentPlayed.get(i); 
    267  
    268             if (History.getInstance().isInHistory(track.getLastplayed())) { 
    269                 History.getInstance().addhistory(track.getLastplayed()); 
    270             } 
    271         } 
    272  
    273         History.getInstance().write(); 
     284        OutputStream out = null; 
     285        OutputStreamWriter writer = null; 
     286        InputStream in = null; 
     287        Reader reader = null; 
     288        BufferedReader bufferedReader = null; 
     289 
     290        try { 
     291            out = c.getOutputStream(); 
     292            writer = new OutputStreamWriter(out); 
     293            writer.write(querystring); 
     294            writer.flush(); 
     295 
     296            IoUtils.cleanup(null, writer); 
     297            IoUtils.cleanup(null, out); 
     298 
     299            if (c.getResponseCode() != 200) { 
     300                throw new RuntimeException("Invalid HTTP return code"); 
     301            } 
     302 
     303            in = c.getInputStream(); 
     304            reader = new InputStreamReader(in); 
     305            bufferedReader = new BufferedReader(reader); 
     306 
     307            String buffer = null; 
     308 
     309            while ((buffer = bufferedReader.readLine()) != null) { 
     310                if (content != null) { 
     311                    content += (buffer + "\n"); 
     312                } else { 
     313                    content = buffer + "\n"; 
     314                } 
     315            } 
     316        } finally { 
     317            IoUtils.cleanup(null, writer); 
     318            IoUtils.cleanup(null, out); 
     319            IoUtils.cleanup(bufferedReader, null); 
     320            IoUtils.cleanup(reader, null); 
     321            IoUtils.cleanup(in, null); 
     322        } 
     323 
     324        return content; 
    274325    } 
    275326}