Esempio n. 1
0
  public void testGetPhotosetsViaMockService() throws Exception {

    Photo photo1 = (Photo) EasyMock.createStrictMock(Photo.class);
    EasyMock.expect(photo1.getId()).andReturn("dummyId1");
    Photo photo2 = (Photo) EasyMock.createStrictMock(Photo.class);
    EasyMock.expect(photo2.getId()).andReturn("dummyId2");

    List photosetsPhotos1 = new ArrayList();
    photosetsPhotos1.add(photo1);
    List photosetsPhotos2 = new ArrayList();
    photosetsPhotos2.add(photo2);

    PhotosetsInterface photosetsInterface =
        (PhotosetsInterface) EasyMock.createStrictMock(PhotosetsInterface.class);
    EasyMock.expect(photosetsInterface.getPhotos("711101")).andReturn(photosetsPhotos1);
    EasyMock.expect(photosetsInterface.getPhotos("711155")).andReturn(photosetsPhotos2);

    PhotosInterface photosInterface =
        (PhotosInterface) EasyMock.createStrictMock(PhotosInterface.class);
    EasyMock.expect(photosInterface.getSizes("dummyId1")).andReturn(new ArrayList());
    EasyMock.expect(photosInterface.getSizes("dummyId2")).andReturn(new ArrayList());

    Flickr flickr = (Flickr) EasyMock.createStrictMock(Flickr.class);
    EasyMock.expect(flickr.getPhotosetsInterface()).andReturn(photosetsInterface);
    EasyMock.expect(flickr.getPhotosInterface()).andReturn(photosInterface);

    EasyMock.replay(new Object[] {flickr, photosetsInterface, photosInterface, photo1, photo2});

    FlickrFacade flickrFacade = new FlickrFacade(flickr);
    List photos = flickrFacade.getPhotosFromPhotosets(PHOTOSET_IDS_CSV);
    assertNotNull(photos);
    assertTrue(photos.size() == 2);

    EasyMock.verify(new Object[] {flickr, photosetsInterface, photosInterface, photo1, photo2});
  }
Esempio n. 2
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();
      }
    }
  }