// 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");
  }
  @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;
  }
Exemple #3
0
  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();
      }
    }
  }