1 package org.varienaja.util.coverart;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.URLConnection;
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.Set;
9 import java.util.TreeSet;
10
11 import org.apache.log4j.Logger;
12
13
14
15
16
17
18 public class CoverArtProcessor implements ICoverArtFinder {
19
20
21
22
23
24
25
26 static class ThreadedCoverFinder extends Thread {
27 private ICoverArtFinder _finder;
28 private String _band;
29 private String _album;
30 private Collection<CoverArtSearchResult> _results;
31
32
33
34
35
36
37
38
39 public ThreadedCoverFinder(ICoverArtFinder finder, String band, String album) {
40 super();
41
42 _finder = finder;
43 _band = band;
44 _album = album;
45 _results = new ArrayList<CoverArtSearchResult>();
46 }
47
48
49
50
51
52 public void run() {
53 _results = _finder.findCoverURLs(_band,_album);
54 }
55
56
57
58
59
60 public Collection<CoverArtSearchResult> getResults() {
61 return _results;
62 }
63
64 }
65
66 private static final Logger log = Logger.getLogger(CoverArtProcessor.class);
67
68 public Collection<CoverArtSearchResult> findCoverURLs(String band, String album) {
69 Set<CoverArtSearchResult> results = new TreeSet<CoverArtSearchResult>();
70
71
72 ThreadedCoverFinder[] threads = new ThreadedCoverFinder[3];
73 threads[0] = new ThreadedCoverFinder(new GoogleCoverArtFinder(),band,album);
74 threads[1] = new ThreadedCoverFinder(new AmazonCoverArtFinder(),band,album);
75 threads[2] = new ThreadedCoverFinder(new YahooCoverArtFinder(),band,album);
76
77 try {
78 log.debug("Starting coversearch-threads");
79 for (int i=0;i<threads.length;i++) {
80 threads[i].start();
81 }
82
83
84 for (int i=0;i<threads.length;i++) {
85 threads[i].join();
86 results.addAll(threads[i].getResults());
87 }
88 } catch (InterruptedException e) {
89 log.error("Error while searching coverart: "+e);
90 }
91
92 return results;
93 }
94
95 public InputStream getCover(CoverArtSearchResult casr,long id) throws IOException {
96 try {
97 log.debug("Getting coverart "+casr.getURI());
98 URLConnection urlc = casr.getURI().toURL().openConnection();
99 urlc.setRequestProperty("user-agent","Mozilla/5.0");
100 return urlc.getInputStream();
101 } catch (IOException e) {
102 log.error("Error saving coverart: "+e);
103 throw e;
104 }
105 }
106
107 }