/examples/advanced/flickrMT/FlickrLoader.java

http://mt4j.googlecode.com/ · Java · 591 lines · 242 code · 99 blank · 250 comment · 40 complexity · 062f40578fd74a762ea20b853fd46c1c MD5 · raw file

  1. /***********************************************************************
  2. * mt4j Copyright (c) 2008 - 2009 C.Ruff, Fraunhofer-Gesellschaft All rights reserved.
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. ***********************************************************************/
  18. package advanced.flickrMT;
  19. import java.io.IOException;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import javax.xml.parsers.ParserConfigurationException;
  23. import org.mt4j.components.visibleComponents.widgets.progressBar.AbstractProgressThread;
  24. import org.xml.sax.SAXException;
  25. import processing.core.PApplet;
  26. import processing.core.PImage;
  27. import com.aetrion.flickr.Flickr;
  28. import com.aetrion.flickr.FlickrException;
  29. import com.aetrion.flickr.REST;
  30. import com.aetrion.flickr.RequestContext;
  31. import com.aetrion.flickr.auth.AuthInterface;
  32. import com.aetrion.flickr.photos.GeoData;
  33. import com.aetrion.flickr.photos.Photo;
  34. import com.aetrion.flickr.photos.PhotoList;
  35. import com.aetrion.flickr.photos.PhotosInterface;
  36. import com.aetrion.flickr.photos.SearchParameters;
  37. import com.aetrion.flickr.photos.geo.GeoInterface;
  38. import com.aetrion.flickr.places.Place;
  39. import com.aetrion.flickr.places.PlacesInterface;
  40. import com.aetrion.flickr.places.PlacesList;
  41. /**
  42. * The Class FlickrLoader.
  43. */
  44. public class FlickrLoader extends AbstractProgressThread {
  45. /** The images. */
  46. private PImage[] images;
  47. /** The pa. */
  48. private PApplet pa;
  49. /** The use places for geo search. */
  50. private boolean usePlacesForGeoSearch;
  51. // private ImageCard mtFotos[];
  52. // private FlickrHelper flickHelper;
  53. /** The search parameters. */
  54. private SearchParameters searchParameters;
  55. /** The foto count. */
  56. private int fotoCount;
  57. /** The search page offset. */
  58. private int searchPageOffset;
  59. /** The flickr key. */
  60. private String flickrKey;
  61. /** The flickr secret. */
  62. private String flickrSecret;
  63. // A Flickr object for making requests
  64. /** The f. */
  65. private Flickr f;
  66. /** The auth interface. */
  67. private AuthInterface authInterface;
  68. /** The request context. */
  69. private RequestContext requestContext;
  70. /** The photos. */
  71. private List<Photo> photos;
  72. /**
  73. * The Constructor.
  74. *
  75. * @param pa the pa
  76. * @param flickrKey the flickr key
  77. * @param flickrSecret the flickr secret
  78. * @param sleepTime the sleep time
  79. */
  80. public FlickrLoader(PApplet pa, String flickrKey, String flickrSecret, long sleepTime) {
  81. this(pa, flickrKey, flickrSecret, new String[]{}, sleepTime);
  82. }
  83. /**
  84. * The Constructor.
  85. *
  86. * @param pa the pa
  87. * @param flickrKey the flickr key
  88. * @param flickrSecret the flickr secret
  89. * @param photoSearchString the photo search string
  90. * @param sleepTime the sleep time
  91. */
  92. public FlickrLoader(PApplet pa, String flickrKey, String flickrSecret, String photoSearchString, long sleepTime) {
  93. this(pa, flickrKey, flickrSecret, new String[]{photoSearchString}, sleepTime);
  94. }
  95. /**
  96. * The Constructor.
  97. *
  98. * @param pa the pa
  99. * @param flickrKey the flickr key
  100. * @param flickrSecret the flickr secret
  101. * @param photoSearchString the photo search string
  102. * @param sleepTime the sleep time
  103. */
  104. public FlickrLoader(PApplet pa, String flickrKey, String flickrSecret, String[] photoSearchString, long sleepTime) {
  105. this(pa, flickrKey, flickrSecret, new SearchParameters(), sleepTime);
  106. SearchParameters sp = new SearchParameters();
  107. // Simple example, just looking for a single tag
  108. sp.setTags(photoSearchString);
  109. this.setSearchParameters(sp);
  110. }
  111. /**
  112. * The Constructor.
  113. *
  114. * @param pa the pa
  115. * @param flickrKey the flickr key
  116. * @param flickrSecret the flickr secret
  117. * @param sleepTime the sleep time
  118. * @param sp the sp
  119. */
  120. public FlickrLoader(PApplet pa, String flickrKey, String flickrSecret, SearchParameters sp, long sleepTime) {
  121. super(sleepTime);
  122. this.pa = pa;
  123. try {
  124. f = new Flickr(flickrKey, flickrSecret, new REST());
  125. } catch (ParserConfigurationException e) {
  126. e.printStackTrace();
  127. }
  128. authInterface = f.getAuthInterface();
  129. requestContext = RequestContext.getRequestContext();
  130. this.searchParameters = sp;
  131. this.fotoCount = 5;
  132. this.searchPageOffset = 0;
  133. this.photos = new ArrayList<Photo>();
  134. this.usePlacesForGeoSearch = true;
  135. }
  136. /* (non-Javadoc)
  137. * @see com.jMT.components.visibleComponents.progressBar.AbstractProgressThread#run()
  138. */
  139. @Override
  140. public void run() {
  141. if (this.getSearchParameters() != null){
  142. /*
  143. //Print search tags
  144. System.out.print("\nSearching for flickr tags: " );
  145. String[] tags = this.getSearchParameters().getTags();
  146. if (tags != null){
  147. for (int i = 0; i < tags.length; i++) {
  148. String string = tags[i];
  149. System.out.print(string + ", ");
  150. }
  151. System.out.println();
  152. }
  153. */
  154. boolean isGeoSearch = this.isGeoSearch(this.getSearchParameters());
  155. PhotoList fotoList = this.getSearchedFotoList(this.getSearchParameters(), this.getFotoLoadCount(), this.getSearchPageOffset(), isGeoSearch);
  156. if (fotoList != null && fotoList.size() > 0){
  157. // System.out.println("Found " + fotoList.size() + " fotos.");
  158. this.setTarget(fotoList.size());
  159. // mtFotos = new ImageCard[fotoList.size()];
  160. //Go through all found fotos
  161. for (int i = 0; i < fotoList.size(); i++) {
  162. try {
  163. Thread.sleep(this.getSleepTime());
  164. } catch (InterruptedException e) {
  165. e.printStackTrace();
  166. //pa.unregisterPre(this);
  167. this.setFinished(true);
  168. break;
  169. }
  170. Photo foto = (Photo) fotoList.get(i);
  171. String fotoName = foto.getTitle();
  172. /*
  173. if (foto.hasGeoData()){
  174. System.out.println("Foto: " + fotoName + " has geodata.");
  175. System.out.println(fotoName + "-> Lat:" + foto.getGeoData().getLatitude() + " Lon:" + foto.getGeoData().getLongitude() + " PlaceID: " + foto.getPlaceId());
  176. }
  177. */
  178. this.setCurrentAction("Loading: " + fotoName);
  179. this.processFoto(foto);
  180. this.setCurrent(i+1);
  181. }
  182. }else{
  183. System.err.println("Foto list returned null or list is empty!");
  184. //Fire event that all loaded
  185. this.setFinished(true);
  186. }
  187. }//if sp != null
  188. else{
  189. System.err.println("No search parameters for flickr search specified!");
  190. //Fire event that all loaded
  191. this.setFinished(true);
  192. }
  193. // this.setFinished(true);
  194. }
  195. /**
  196. * This method gets called in the loading thread of the fotos. This
  197. * method can be overridden and used to process the newly loaded foto in different ways.
  198. *
  199. * @param foto the foto
  200. */
  201. protected void processFoto(Photo foto){
  202. }
  203. /**
  204. * Searches fotos on flickr with the specified searchparameters.
  205. *
  206. * @param sp the sp
  207. * @param n number of fotos to search for
  208. * @param startPage offset of the result pages to start from
  209. * @param isGeoSearch the is geo search
  210. *
  211. * @return the searched foto list
  212. *
  213. * the list of found fotos
  214. */
  215. public PhotoList getSearchedFotoList(SearchParameters sp, int n, int startPage, boolean isGeoSearch) {
  216. PhotoList photoList = null;
  217. //Interface with Flickr photos
  218. PhotosInterface photoInterface = f.getPhotosInterface();
  219. try {
  220. //TODO was kann man damit machen?
  221. //GeoInterface g = photos.getGeoInterface();
  222. //search results for exact location, display on map
  223. /*
  224. //Search in a location bounding box
  225. sp.setBBox(
  226. "16.3680536232", "48.2057162608",
  227. "16.3760536232", "48.2111162608");
  228. sp.setAccuracy(1);
  229. sp.setHasGeo(true);
  230. */
  231. //Search in a location with a radius
  232. /*
  233. sp.setLatitude("48.7771056");
  234. sp.setLongitude("9.1807688");
  235. sp.setRadius(5);
  236. sp.setRadiusUnits("km");
  237. sp.setHasGeo(true);
  238. sp.setAccuracy(Flickr.ACCURACY_REGION);
  239. */
  240. // /*
  241. //Instead of taking the location, search for a nearby flickr "place"
  242. if (isGeoSearch && sp.getLongitude() != null && sp.getLatitude() != null
  243. && usePlacesForGeoSearch
  244. ){
  245. //TODO TEST DISABLE FIXME
  246. //Search for places at a location
  247. PlacesInterface p = f.getPlacesInterface();
  248. PlacesList placesList = null;
  249. // placesList = p.findByLatLon(48.7771056f, 9.1807688f, 14);
  250. // placesList = p.findByLatLon(40.689, -74.044, 12); //liberty islang
  251. placesList = p.findByLatLon(Double.parseDouble(sp.getLatitude()), Double.parseDouble(sp.getLongitude()), sp.getAccuracy());
  252. sp.setLatitude(null);
  253. sp.setLongitude(null);
  254. System.out.println("Places found: " + placesList.size());
  255. for (Object aPlacesList : placesList) {
  256. Place place = (Place) aPlacesList;
  257. String placeID = place.getPlaceId();
  258. //System.out.println("Place ID: " + placeID);
  259. sp.setPlaceId(placeID);
  260. sp.setWoeId(place.getWoeId());
  261. // Location placeResolved = p.resolvePlaceURL(place.getPlaceUrl());
  262. // System.out.println("Place: " + placeResolved);
  263. // sp.setBBox(
  264. // 48.5129f, 2.1740f,
  265. // 48.5130f, 2.1741f);
  266. }
  267. }
  268. // else if (isGeoSearch && sp.getLongitude() != null && sp.getLatitude() != null
  269. // && !usePlacesForGeoSearch){
  270. //
  271. // }
  272. // */
  273. //We're looking for n images, starting at "page" startPage
  274. //PhotoList
  275. photoList = photoInterface.search(sp, n, startPage);
  276. //Check if no fotos were found and search again with reduced accurracy
  277. if (isGeoSearch
  278. && photoList != null
  279. ){
  280. System.out.println("Found " + photoList.size() + " fotos.");
  281. if (photoList.size() <= 0){
  282. System.out.println("Found no fotos, reducing accuracy and trying again.");
  283. int a = sp.getAccuracy();
  284. for (int i = 1; i < 6; i++) {
  285. a--;
  286. if (a < 1){
  287. a++;
  288. break;
  289. }
  290. }
  291. System.out.println("Using new accuracy: " + a);
  292. sp.setAccuracy(a);
  293. photoList = photoInterface.search(sp, n, startPage);
  294. if (photoList != null)
  295. System.out.println("Found " + photoList.size() + " fotos.");
  296. }
  297. }
  298. if (isGeoSearch){
  299. //System.out.println("Trying to attach geo information:");
  300. if (photoList != null){
  301. GeoInterface g = photoInterface.getGeoInterface();
  302. //Nullpointer error - bug in flickj library,
  303. //photosForLocation return aber evtl nur eigene fotos!
  304. // PhotoList fot = g.photosForLocation(new GeoData( new Float(48.7771056f).toString(), new Float(9.1807688f).toString(), new Integer(1).toString()), null, 5, 0);
  305. // System.out.println("photosForLocation (location = stutgart mitte) returned fotos: " + fot.size());
  306. //Go through all found fotos
  307. for (Object aPhotoList : photoList) {
  308. Photo foto = (Photo) aPhotoList;
  309. //Add to result list
  310. photos.add(foto);
  311. String id = foto.getId();
  312. try {
  313. GeoData loc = g.getLocation(id);
  314. if (loc != null) {
  315. foto.setGeoData(loc);
  316. }
  317. } catch (Exception e) {
  318. System.err.println("Error fetching geodata for foto");
  319. e.printStackTrace();
  320. }
  321. }
  322. }
  323. }else{
  324. //Add to result list
  325. if (photoList != null){
  326. for (Object aPhotoList : photoList) {
  327. photos.add((Photo) aPhotoList);
  328. }
  329. }
  330. }
  331. //Geht nur mit eigenen fotos!?
  332. // photos.getWithGeoData(arg0, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
  333. return photoList;
  334. } catch (IOException e) {
  335. e.printStackTrace();
  336. } catch (SAXException e) {
  337. e.printStackTrace();
  338. } catch (FlickrException e) {
  339. e.printStackTrace();
  340. }
  341. return photoList;
  342. }
  343. /**
  344. * Checks if is geo search.
  345. *
  346. * @param sp the sp
  347. *
  348. * @return true, if is geo search
  349. */
  350. private boolean isGeoSearch(SearchParameters sp){
  351. return
  352. (
  353. (sp.getLatitude() != null //TODO is it null when not set?
  354. && !sp.getLatitude().equalsIgnoreCase(""))
  355. ||
  356. (sp.getLongitude() != null
  357. && !sp.getLongitude().equalsIgnoreCase(""))
  358. ||
  359. (sp.getBBox() != null
  360. && sp.getBBox().length > 0)
  361. ||
  362. (sp.getWoeId() != null
  363. && !sp.getWoeId().equalsIgnoreCase(""))
  364. ||
  365. (sp.getPlaceId() != null
  366. && !sp.getPlaceId().equalsIgnoreCase(""))
  367. || sp.getRadius() != -1
  368. );
  369. }
  370. /**
  371. * Gets the foto urls.
  372. *
  373. * @param list the list
  374. *
  375. * @return the foto urls
  376. */
  377. private String[] getFotoUrls(PhotoList list){
  378. //Grab all the image paths and store in String array
  379. String[] smallURLs = new String[list.size()];
  380. for (int i = 0; i < list.size(); i++) {
  381. Photo p = (Photo) list.get(i);
  382. // smallURLs[i] = p.getSmallUrl();
  383. // smallURLs[i] = p.getUrl();
  384. smallURLs[i] = p.getMediumUrl();
  385. }
  386. return smallURLs;
  387. }
  388. /**
  389. * Gets the foto load count.
  390. *
  391. * @return the foto load count
  392. */
  393. public int getFotoLoadCount() {
  394. return fotoCount;
  395. }
  396. /**
  397. * Sets the foto load count.
  398. *
  399. * @param fotosCount the new foto load count
  400. */
  401. public void setFotoLoadCount(int fotosCount) {
  402. this.fotoCount = fotosCount;
  403. }
  404. /**
  405. * Gets the search parameters.
  406. *
  407. * @return the search parameters
  408. */
  409. public SearchParameters getSearchParameters() {
  410. return searchParameters;
  411. }
  412. /**
  413. * Sets the search parameters.
  414. *
  415. * @param searchParameters the new search parameters
  416. */
  417. public void setSearchParameters(SearchParameters searchParameters) {
  418. this.searchParameters = searchParameters;
  419. }
  420. /**
  421. * Gets the search page offset.
  422. *
  423. * @return the search page offset
  424. */
  425. private int getSearchPageOffset() {
  426. return this.searchPageOffset;
  427. }
  428. /**
  429. * Sets the search page offset.
  430. *
  431. * @param searchPageOffset the new search page offset
  432. */
  433. public void setSearchPageOffset(int searchPageOffset) {
  434. this.searchPageOffset = searchPageOffset;
  435. }
  436. /**
  437. * Gets the flickr key.
  438. *
  439. * @return the flickr key
  440. */
  441. public String getFlickrKey() {
  442. return flickrKey;
  443. }
  444. /**
  445. * Sets the flickr key.
  446. *
  447. * @param flickrKey the new flickr key
  448. */
  449. public void setFlickrKey(String flickrKey) {
  450. this.flickrKey = flickrKey;
  451. }
  452. /**
  453. * Gets the flickr secret.
  454. *
  455. * @return the flickr secret
  456. */
  457. public String getFlickrSecret() {
  458. return flickrSecret;
  459. }
  460. /**
  461. * Sets the flickr secret.
  462. *
  463. * @param flickrSecret the new flickr secret
  464. */
  465. public void setFlickrSecret(String flickrSecret) {
  466. this.flickrSecret = flickrSecret;
  467. }
  468. /**
  469. * Gets the photos.
  470. *
  471. * @return the photos
  472. */
  473. public Photo[] getPhotos() {
  474. return this.photos.toArray(new Photo[this.photos.size()]);
  475. }
  476. /**
  477. * Checks if is use places for geo search.
  478. *
  479. * @return true, if is use places for geo search
  480. */
  481. public boolean isUsePlacesForGeoSearch() {
  482. return usePlacesForGeoSearch;
  483. }
  484. /**
  485. * Uses flickr "places" for the geo search
  486. *
  487. * @param usePlacesForGeoSearch the new use places for geo search
  488. */
  489. public void setUsePlacesForGeoSearch(boolean usePlacesForGeoSearch) {
  490. this.usePlacesForGeoSearch = usePlacesForGeoSearch;
  491. }
  492. }