@Override protected Photo doInBackground(Long... photo_ids) { long photo_id = photo_ids[0]; Flickr flickr = null; try { flickr = new Flickr( ApiKeys.FLICKR_API_KEY, // My API key ApiKeys.FLICKR_API_SECRET, // My API secret new REST()); } catch (ParserConfigurationException e) { e.printStackTrace(); } RequestContext requestContext = RequestContext.getRequestContext(); Auth auth = new Auth(); SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(context); String stored_auth_token = settings.getString(FlickrAuthRetrievalActivity.PREFKEY_FLICKR_AUTH_TOKEN, null); auth.setToken(stored_auth_token); auth.setPermission(Permission.READ); requestContext.setAuth(auth); Photo photo = null; try { photo = flickr.getPhotosInterface().getPhoto(Long.toString(photo_id)); } catch (IOException e1) { e1.printStackTrace(); } catch (FlickrException e1) { e1.printStackTrace(); } catch (SAXException e1) { e1.printStackTrace(); } return photo; }
public void connect(String[] keywords, boolean failedOnce, boolean failedTwice) { try { if (!sketch.flickrInit) { sketch.f = new Flickr(Colours.apiKeyFlickr, Colours.secretFlickr, new REST()); sketch.requestContext = RequestContext.getRequestContext(); Flickr.debugRequest = false; Flickr.debugStream = false; sketch.flickrInit = true; } if (picAct.canceled) { picAct.removeImgs(); return; } SearchParameters sParams = new SearchParameters(); sParams.setTags(keywords); sParams.setTagMode("any"); // sParams.setText(picAct.tagsStr); sParams.setSafeSearch("2"); sParams.setSort(SearchParameters.RELEVANCE); picAct.list = sketch.f.getPhotosInterface().search(sParams, picAct.MAX_IMAGES, picAct.pageOffset); if (picAct.list.isEmpty() && !failedOnce && !failedTwice) { picAct.pageOffset = 1; picAct.currentPicsIndex = 0; connect(picAct.tags, true, false); } else if (picAct.list.isEmpty() && failedOnce && !failedTwice) { picAct.pageOffset = 1; picAct.currentPicsIndex = 0; connect(picAct.topic, false, true); } else if (picAct.list.isEmpty() && failedTwice) { picAct.failed = true; picAct.loading.setActive(false); System.out.println("Error: Flickr result list is empty."); } else { for (int i = 0; i < picAct.NUM_IMAGES; i++) { if (!picAct.canceled) { PImage img = loadImage(); if (img != null) { picAct.setImgZone(picAct.imgs[i], img); } } } } } catch (IOException e) { picAct.failed = true; System.out.println("Error: Flickr IOException"); e.printStackTrace(); } catch (SAXException e) { picAct.failed = true; System.out.println("Error: Flickr SAXException"); e.printStackTrace(); } catch (FlickrException e) { picAct.failed = true; System.out.println("Error: Flickr FlickrException"); e.printStackTrace(); } catch (ParserConfigurationException e) { picAct.failed = true; System.out.println("Error: Flickr ParserConfigurationException"); e.printStackTrace(); } }
public void showActivityBB( String city, double v1x, double v1y, double v3x, double v3y, Calendar start, Calendar end, String output_file) throws IOException { PrintWriter out = new PrintWriter(new FileWriter(new File(output_file))); PhotosInterface pi = f.getPhotosInterface(); SearchParameters params = new SearchParameters(); params.setMinTakenDate(start.getTime()); params.setMaxTakenDate(end.getTime()); System.out.println(String.valueOf(v1x)); params.setBBox( String.valueOf(v1x), String.valueOf(v1y), String.valueOf(v3x), String.valueOf(v3y)); params.setHasGeo(true); int counter = 0; PhotoList list = null; try { list = pi.search(params, picXpage, 1); } catch (FlickrException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println( "\nRetrievind data around of " + city + "from " + "" + start.getTime() + " to " + end.getTime()); System.out.println(list.getTotal() + " pictures being retrieved..."); for (int k = 1; k <= list.getPages(); k++) { try { list = pi.search(params, picXpage, k); } catch (FlickrException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (SAXException e) { // TODO Auto-generated catch block e.printStackTrace(); } for (int i = 0; i < list.getPerPage(); i++) { Photo x = (Photo) list.get(i); String description, title = ""; try { x = pi.getInfo(x.getId(), x.getSecret()); } catch (SAXException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (FlickrException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } // String description=x.getDescription().replace('\n', ' '); // String title=x.getTitle().replace('\n', ' '); String date = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM, Locale.ITALY) .format(x.getDateTaken()); GeoData geo = x.getGeoData(); String username = x.getOwner().getUsername(); description = (x.getDescription()); if (description != null) description = description.replace('\n', ' '); title = (x.getTitle()); if (title != null) title = title.replace('\n', ' '); if (geo != null) { // out.println(username+"*"+geo.getLatitude()+"*"+geo.getLongitude()+"*"+geo.getAccuracy()+"*"+date); if (description != null) try { // Convert from Unicode to UTF-8 // String string = "abc\u5639\u563b"; byte[] utf8 = description.getBytes("UTF-8"); // Convert from UTF-8 to Unicode description = new String(utf8, "UTF-8"); System.out.println(description); } catch (UnsupportedEncodingException e) { System.out.println(e); } out.println( username + "\t" + title + "\t" + description + "\t" + geo.getLatitude() + "\t" + geo.getLongitude() + "\t" + geo.getAccuracy() + "\t" + date); counter++; } else { try { Iterator<Exif> iter = pi.getExif(x.getId(), x.getSecret()).iterator(); while (iter.hasNext()) { Exif exif = iter.next(); String raw = exif.getLabel() + " " + exif.getRaw(); if (raw.contains("atitude") || raw.contains("ongitude")) // avoid 1st letter for case sensitive System.out.println("!!! " + exif.getLabel() + " " + exif.getRaw()); } } catch (Exception e) { } System.out.println("broken picture @ page= " + k + " pic= " + i); } } System.out.println( (100 * k) / list.getPages() + "% Complete. Pictures " + counter + " on " + list.getTotal()); } out.close(); }