1 /*** 2 * Ambient - A music player for the Android platform 3 Copyright (C) 2007 Martin Vysny 4 5 This program is free software: you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation, either version 3 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 package sk.baka.ambient.lrc.lyrdb; 20 21 import java.io.IOException; 22 import java.io.InputStreamReader; 23 import java.io.LineNumberReader; 24 import java.net.MalformedURLException; 25 import java.net.URL; 26 import java.net.URLConnection; 27 import java.util.ArrayList; 28 import java.util.List; 29 30 import sk.baka.ambient.collection.TrackMetadataBean; 31 import sk.baka.ambient.commons.IOUtils; 32 import sk.baka.ambient.commons.MiscUtils; 33 34 /*** 35 * An immutable LyrDB track as returned by the LyrDB search. 36 * 37 * @author Martin Vysny 38 */ 39 public final class LyrdbTrack { 40 /*** 41 * the LyrDB id. 42 */ 43 public final String id; 44 /*** 45 * The track name. 46 */ 47 public final String trackName; 48 /*** 49 * artist 50 */ 51 public final String artist; 52 /*** 53 * A displayable string 54 */ 55 public final String displayableString; 56 57 /*** 58 * Creates new track. 59 * 60 * @param id 61 * the LyrDB id. 62 * @param trackName 63 * the track name. 64 * @param artist 65 * artist 66 */ 67 public LyrdbTrack(final String id, final String trackName, 68 final String artist) { 69 this.id = id; 70 this.trackName = trackName; 71 this.artist = artist; 72 displayableString = trackName + " - " + artist; 73 } 74 75 /*** 76 * Parses the result line and returns the track. 77 * 78 * @param line 79 * the line to parse 80 * @return non-<code>null</code> track instance or <code>null</code> if the 81 * line was not in a correct format. 82 */ 83 public static LyrdbTrack fromSearchLine(final String line) { 84 final String[] splitLine = line.split("////"); 85 if (splitLine.length < 3) 86 return null; 87 return new LyrdbTrack(splitLine[0], splitLine[1], splitLine[2]); 88 } 89 90 /*** 91 * Returns URL of the karaoke file for this track. 92 * 93 * @return the karaoke URL. 94 * @throws MalformedURLException 95 */ 96 public URL getKaraokeURL() throws MalformedURLException { 97 return new URL("http://webservices.lyrdb.com/showkar.php?q=" + id 98 + "&useid=1"); 99 } 100 101 /*** 102 * Search for a karaoke files for given track. Fetches results synchronously 103 * from the Internet. 104 * 105 * @param track 106 * search for lyrics for this track. 107 * @param relevantOnly 108 * if <code>true</code> then only the most relevant results are 109 * returned. 110 * @return list of tracks, never <code>null</code>. 111 * @throws IOException 112 * if i/o error occurs. 113 */ 114 public static List<LyrdbTrack> search(final TrackMetadataBean track, 115 final boolean relevantOnly) throws IOException { 116 final String query = IOUtils.encodeURL(track.getArtist() + " " 117 + track.getTitle()); 118 final URL url = new URL("http://webservices.lyrdb.com/karlookup.php?q=" 119 + query + (relevantOnly ? "mode=s" : "")); 120 final URLConnection conn = url.openConnection(); 121 conn.addRequestProperty("User-Agent", "Ambient"); 122 final List<LyrdbTrack> result = new ArrayList<LyrdbTrack>(); 123 final LineNumberReader reader = new LineNumberReader( 124 new InputStreamReader(url.openStream())); 125 try { 126 while (true) { 127 final String line = reader.readLine(); 128 if (line == null) { 129 break; 130 } 131 final LyrdbTrack lt = LyrdbTrack.fromSearchLine(line); 132 if (lt != null) { 133 result.add(lt); 134 } 135 } 136 } finally { 137 MiscUtils.closeQuietly(reader); 138 } 139 return result; 140 } 141 142 @Override 143 public String toString() { 144 return "LyrdbTrack: " + displayableString; 145 } 146 }