protected void prepareFlickr(FeedService feedService, FlickrCredentials credentials) { List<Feed> feeds = feedService.getFeedsToUpdate(Type.FLICKR); REST rest = null; try { // This flickrj library is so poorly designed I want to puke all over myself. rest = new REST(); } catch (ParserConfigurationException e) { logger.warn(ErrorUtil.getErrorDetail("POS Flickr Library sucks ass: ", e)); } Flickr flickr = new Flickr(credentials.getKey(), credentials.getSecret(), rest); Flickr.debugStream = true; Flickr.debugRequest = true; // again, I am amazed at how much better the API is for twitter than flickr // a thread local for subsequent calls?? Is this 1999? RequestContext requestContext = RequestContext.getRequestContext(); for (int counter = 0; counter < feeds.size(); counter++) { if (counter >= maxPerRun) { logger.warn( "Hit processing limit of: " + maxPerRun + ", There are " + (feeds.size() - maxPerRun) + " left to process."); break; } Feed f = feeds.get(counter); feedService.loadFlickrFeed(f, flickr); } }
@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; }
private void authorize() throws IOException, SAXException, FlickrException { String frob = this.flickr.getAuthInterface().getFrob(); URL authUrl = this.flickr.getAuthInterface().buildAuthenticationUrl(Permission.READ, frob); System.out.println("Please visit: " + authUrl.toExternalForm() + " then, hit enter."); System.in.read(); Auth token = this.flickr.getAuthInterface().getToken(frob); RequestContext.getRequestContext().setAuth(token); this.authStore.store(token); System.out.println( "Thanks. You probably will not have to do this every time. Now starting backup."); }
// FLICKR AUTHENTICATION HELPER FUNCTIONS // Attempts to authneticate with a given token public void authenticateWithToken(String _token) { AuthInterface authInterface = flickr.getAuthInterface(); // make sure the token is legit try { authInterface.checkToken(_token); } catch (Exception e) { println("Token is bad, getting a new one"); getAuthentication(); return; } auth = new Auth(); RequestContext requestContext = RequestContext.getRequestContext(); requestContext.setSharedSecret(secretKey); requestContext.setAuth(auth); auth.setToken(_token); auth.setPermission(Permission.WRITE); flickr.setAuth(auth); println("Authentication success"); }
public void doBackup(File directory) throws Exception { if (!directory.exists()) directory.mkdir(); RequestContext rc = RequestContext.getRequestContext(); if (this.authStore != null) { Auth auth = this.authStore.retrieve(this.nsid); if (auth == null) this.authorize(); else rc.setAuth(auth); } PhotosetsInterface pi = flickr.getPhotosetsInterface(); PhotosInterface photoInt = flickr.getPhotosInterface(); Map<String, Collection> allPhotos = new HashMap<String, Collection>(); Iterator sets = pi.getList(this.nsid).getPhotosets().iterator(); while (sets.hasNext()) { Photoset set = (Photoset) sets.next(); PhotoList photos = pi.getPhotos(set.getId(), 500, 1); allPhotos.put(set.getTitle(), photos); } int notInSetPage = 1; Collection notInASet = new ArrayList(); while (true) { Collection nis = photoInt.getNotInSet(50, notInSetPage); notInASet.addAll(nis); if (nis.size() < 50) break; notInSetPage++; } allPhotos.put("NotInASet", notInASet); Iterator<String> allIter = allPhotos.keySet().iterator(); while (allIter.hasNext()) { String setTitle = allIter.next(); String setDirectoryName = makeSafeFilename(setTitle); Collection currentSet = allPhotos.get(setTitle); Iterator setIterator = currentSet.iterator(); File setDirectory = new File(directory, setDirectoryName); setDirectory.mkdir(); while (setIterator.hasNext()) { Photo p = (Photo) setIterator.next(); String url = p.getLargeUrl(); URL u = new URL(url); String filename = u.getFile(); filename = filename.substring(filename.lastIndexOf("/") + 1, filename.length()); System.out.println("Now writing " + filename + " to " + setDirectory.getCanonicalPath()); BufferedInputStream inStream = new BufferedInputStream(photoInt.getImageAsStream(p, Size.LARGE)); File newFile = new File(setDirectory, filename); FileOutputStream fos = new FileOutputStream(newFile); int read; while ((read = inStream.read()) != -1) { fos.write(read); } fos.flush(); fos.close(); inStream.close(); } } }
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 TestMyFlickrAppBB12() throws ParserConfigurationException, IOException { f = new Flickr(apiKey, sharedSecret, new REST()); requestContext = RequestContext.getRequestContext(); }