1 /* 2 * aTunes 3 * Copyright (C) Alex Aranda, Sylvain Gaudard and contributors 4 * 5 * See http://www.atunes.org/wiki/index.php?title=Contributing for information about contributors 6 * 7 * http://www.atunes.org 8 * http://sourceforge.net/projects/atunes 9 * 10 * This program is free software; you can redistribute it and/or 11 * modify it under the terms of the GNU General Public License 12 * as published by the Free Software Foundation; either version 2 13 * of the License, or (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 */ 20 21 package net.sourceforge.atunes.kernel.modules.webservices.lyrics; 22 23 import java.io.BufferedReader; 24 import java.io.IOException; 25 import java.io.StringReader; 26 27 import net.sourceforge.atunes.Constants; 28 import net.sourceforge.atunes.model.ILyrics; 29 import net.sourceforge.atunes.model.ILyricsRetrieveOperation; 30 import net.sourceforge.atunes.utils.Logger; 31 import net.sourceforge.atunes.utils.StringUtils; 32 33 /** 34 * Engine to retrieve lyrics from LyrDB: http://www.lyrdb.com/ by Flavio 35 * González Vázquez 36 * 37 * @author fleax 38 * 39 */ 40 public class LyrDBEngine extends AbstractLyricsEngine { 41 42 private static final String QUERY_WILDCARD = "(%QUERY%)"; 43 private static final String LYRICS_ID_WILDCARD = "(%ID%)"; 44 private static final String AGENT = Constants.APP_NAME; 45 46 private static final String FOOTER = "\nLyrics provided by LyrDB (www.lyrdb.com) and Flavio González Vázquez"; 47 48 /** The search url. */ 49 private static final String SEARCH_URL = StringUtils.getString( 50 "http://www.lyrdb.com/lookup.php?q=", QUERY_WILDCARD, 51 "&for=match&agent=", AGENT); 52 53 /** The url to retrieve a lyric */ 54 private static final String LYRIC_URL = StringUtils.getString( 55 "http://www.lyrdb.com/getlyr.php?q=", LYRICS_ID_WILDCARD); 56 57 @Override getLyricsFor(String artist, String title, ILyricsRetrieveOperation operation)58 public ILyrics getLyricsFor(String artist, String title, 59 ILyricsRetrieveOperation operation) { 60 ILyrics lyrics = null; 61 try { 62 // Build url and search 63 String urlString = SEARCH_URL.replace(QUERY_WILDCARD, StringUtils 64 .getString(encodeString(artist), "|", encodeString(title))); // "|" 65 // can't 66 // be 67 // encoded 68 String searchResult = readURL(getConnection(urlString, operation), 69 "UTF-8"); 70 71 // Parse result 72 BufferedReader br = new BufferedReader(new StringReader( 73 searchResult)); 74 String line = br.readLine(); 75 while (line != null && lyrics == null) { 76 lyrics = retrieveSearchResult(line, operation); 77 line = br.readLine(); 78 } 79 br.close(); 80 81 } catch (IOException e) { 82 Logger.error(StringUtils.getString(e.getClass().getCanonicalName(), 83 " (", e.getMessage(), ")")); 84 } 85 return lyrics; 86 } 87 88 /** 89 * Retrieves lyric for a search result 90 * 91 * @param searchResult 92 * @param operation 93 * @return 94 * @throws IOException 95 */ retrieveSearchResult(String searchResult, ILyricsRetrieveOperation operation)96 private ILyrics retrieveSearchResult(String searchResult, 97 ILyricsRetrieveOperation operation) throws IOException { 98 int firstSlash = searchResult.indexOf('\\'); 99 if (firstSlash != -1) { 100 String id = searchResult.substring(0, firstSlash); 101 // Build url for ID 102 String urlString = LYRIC_URL.replace(LYRICS_ID_WILDCARD, id); 103 String lyric = readURL(getConnection(urlString, operation), "UTF-8"); 104 if (lyric != null) { 105 // Remove carriage return 106 lyric = lyric.replace("\r\r", ""); 107 lyric = appendFooter(lyric); 108 return new Lyrics(lyric, urlString); 109 } 110 } 111 return null; 112 } 113 appendFooter(String lyric)114 private String appendFooter(String lyric) { 115 return StringUtils.getString(lyric, FOOTER); 116 } 117 118 @Override getLyricsProviderName()119 public String getLyricsProviderName() { 120 return "LyrDB"; 121 } 122 123 @Override getUrlForAddingNewLyrics(String artist, String title)124 public String getUrlForAddingNewLyrics(String artist, String title) { 125 return "http://www.lyrdb.com/submit.php"; 126 } 127 } 128