View Javadoc

1   package com.es.skreemr;
2   
3   import hotsax.html.sax.SaxParser;
4   
5   import java.io.IOException;
6   import java.util.ArrayList;
7   import java.util.List;
8   
9   import org.xml.sax.Attributes;
10  import org.xml.sax.SAXException;
11  import org.xml.sax.XMLReader;
12  import org.xml.sax.helpers.DefaultHandler;
13  
14  import android.net.Uri;
15  
16  /***
17   * Service uses SkreemR.com to look for songs on the web and returns a list of
18   * songs that can be dealt with later
19   * 
20   * @author Keith Blackard
21   */
22  public class SkreemRSearch extends DefaultHandler {
23  	/*** An XMLReader used to parse HTML pages */
24  	private final XMLReader parser;
25  	/*** The last results returned */
26  	private List<SkreemRSong> results;
27  	/*** The last keyword(s) used */
28  	private String lastKeywords;
29  	/*** The last size of search (multiplied by 10) */
30  	private int lastSize;
31  	/*** The last start of search (Page number * 10) */
32  	private int lastStart;
33  
34  	/***
35  	 * Creates a new SkreemRSearch Object. There should at most be one instance.
36  	 */
37  	public SkreemRSearch() {
38  		super();
39  		parser =  new SaxParser();
40  		parser.setContentHandler(this);
41  	}
42  
43  	/***
44  	 * Make a new search.
45  	 * 
46  	 * @param keyWords
47  	 *            The search parameters.
48  	 * @param size
49  	 *            The number of pages of results to be returned.
50  	 * @param page
51  	 *            The first page to start at.
52  	 * @return a list of songs found in search. Maximum size of list equals size *
53  	 *         10
54  	 * @throws IOException
55  	 * @throws SAXException
56  	 */
57  	public List<SkreemRSong> search(String keyWords, int size, int page)
58  			throws IOException, SAXException {
59  		if (results != null)
60  			results.clear();
61  		else
62  			results = new ArrayList<SkreemRSong>(size);
63  		lastKeywords = keyWords;
64  		lastSize = size;
65  		lastStart = (page - 1) * 10;
66  		String fixedKeywords = getSearchString(keyWords);
67  		System.out.println(fixedKeywords);
68  		for (int start = lastStart + 1; start < lastStart + size * 10; start += 10) {
69  			parser.parse("http://www.skreemr.com/results.jsp?q="
70  					+ fixedKeywords + "&s=" + start);
71  		}
72  		return results;
73  	}
74  
75  	private static String getSearchString(final String query) {
76  		String result = query.replace("/", " ");
77  		// replace spaces with +
78  		result = result.replaceAll("//s+", "+");
79  		result = Uri.encode(result, "+");
80  		return result;
81  	}
82  
83  	/***
84  	 * Make a new search.
85  	 * 
86  	 * @param keyWords
87  	 *            The search parameters.
88  	 * @param page
89  	 *            The first page to start at.
90  	 * @return a list of songs found in search. Maximum of 10 results in list.
91  	 * @throws IOException
92  	 * @throws SAXException
93  	 */
94  	public List<SkreemRSong> search(String keyWords, int page)
95  			throws IOException, SAXException {
96  		return search(keyWords, 1, page);
97  	}
98  
99  	/***
100 	 * Continue search on next page, keeps same keywords and size.
101 	 * 
102 	 * @return a list of songs found in search. Maximum size is based on last
103 	 *         search size.
104 	 * @throws IOException
105 	 * @throws SAXException
106 	 */
107 	public List<SkreemRSong> nextPage() throws IOException, SAXException {
108 		return search(lastKeywords, lastSize, lastStart / 10);
109 	}
110 
111 	/***
112 	 * @return the results of the previous search
113 	 */
114 	public List<SkreemRSong> getResults() {
115 		return results;
116 	}
117 
118 	/***
119 	 * @return the last keyword(s) used
120 	 */
121 	public String getLastKeywords() {
122 		return lastKeywords;
123 	}
124 
125 	/***
126 	 * @return the last size of search
127 	 */
128 	public int getLastSize() {
129 		return lastSize;
130 	}
131 
132 	/***
133 	 * @return the last start page
134 	 */
135 	public int getLastPage() {
136 		return lastStart / 10;
137 	}
138 
139 	@Override
140 	public void startElement(String namespaceURI, String localName,
141 			String name, Attributes atts) {
142 		/*
143 		 * At the start of each tag: Check if tag is an 'a' tag Check that
144 		 * onmouseover attribute in not null and is a tool tip tag
145 		 */
146 		if (localName.equals("a") && atts.getValue("onmouseover") != null
147 				&& atts.getValue("onmouseover").startsWith("Tip('<table>")) {
148 			String onmouseover = atts.getValue("onmouseover");
149 			results.add(new SkreemRSong(atts.getValue("href"), onmouseover
150 					.substring(onmouseover.indexOf("<b>"), onmouseover
151 							.indexOf("</td></tr>"))));
152 		}
153 	}
154 }