Package com.volongoto.quickbp

Source Code of com.volongoto.quickbp.QuickPB

/**
* Quick Photo Blogger
* Copyright (C) 2009 Eren Inan Canpolat
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
*
* @author Eren Inan Canpolat
*/

package com.volongoto.quickbp;

import java.util.*;
import java.io.File;
import com.google.gdata.client.GoogleService.InvalidCredentialsException;
import com.google.gdata.data.*;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.data.photos.UserFeed;

public class QuickPB {

  private static BloggerServiceHandler bloggerServiceHandler;
  private static PicasawebServiceHandler picasaServiceHandler;
  private static FileHandler fileHandler;
  private static UserPreferences prefs;
  private static AlbumEntry album;
 
  /**
   * @param args array of string arguments
   */
  public static void main(String[] args) {
    QuickPB.printCopyrightNotice();

    fileHandler = new FileHandler();
    prefs = new UserPreferences();
    boolean useSavedPreferences = prefs.confirm();
   
    if(!useSavedPreferences) {
      QuickPB.changePreferences();
    }
   
    QuickPB.initServices(prefs.getUsername());
    QuickPB.authenticateServices();
   
    Feed bloggerBlogList = bloggerServiceHandler.getMetaFeed();
    UserFeed picasaAlbumList = picasaServiceHandler.getMetaFeed();
    if(!useSavedPreferences) {
      QuickPB.getBlogInfoFromUser(bloggerBlogList);
    }
    album = picasaServiceHandler.findCorrespondingPicasaAlbum(prefs.getBlogName(), picasaAlbumList);
    if(album == null)
    {
      picasaServiceHandler.createAlbum(prefs.getBlogName());
    }
    QuickPB.sendAllFiles();
  }

  private static void printCopyrightNotice() {
    System.out.println("Quick Photo Blogger  Copyright (C) 2009  Eren Inan Canpolat");
    System.out.println("This program comes with ABSOLUTELY NO WARRANTY.");
    System.out.println("This is free software, and you are welcome to redistribute it");
    System.out.println("under certain conditions.");
    System.out.println("Please refer to <http://www.gnu.org/licenses/> for details.");
    System.out.println("");
  }

  /**
   * Authenticates the two services used by the application:
   * BloggerService and PicasawebService
   * @return No return value
   */
  private static void authenticateServices() {
    boolean picasaAuthenticated = false;
    boolean bloggerAuthenticated = false;
    boolean terminate = false;
   
    System.out.println("Picasa is authenticating");
    while((!picasaAuthenticated) && (!terminate)) {
      try {
        QuickPB.picasaServiceHandler.authenticate(prefs.getUsername(), prefs.getPassword());
        picasaAuthenticated = true;
        System.out.println("Authentication successful.");
      } catch (InvalidCredentialsException e) {
        QuickPB.getUserCredentials();
        if(prefs.getUsername() == "0")
        {
          terminate = true;
        }
      }
    }
   
    System.out.println("Blogger is authenticating");
    while((!bloggerAuthenticated) && (!terminate)) {
      try {
        QuickPB.bloggerServiceHandler.authenticate(prefs.getUsername(), prefs.getPassword());
        bloggerAuthenticated = true;
        System.out.println("Authentication successful.");
      } catch (InvalidCredentialsException e) {
        QuickPB.getUserCredentials();
        if(prefs.getUsername() == "0")
        {
          terminate = true;
        }
      }
    }
  }

  /**
   * Sends all files in the folder to Picasa Web Album.
   * Then uses those images in the web album to create blog posts.
   */
  private static void sendAllFiles() {
    boolean allFilesSent = false;
    String photoName = null;
    while(!allFilesSent) {
      photoName = fileHandler.getFirstUnsentFile(prefs.getFolderPath());
      if(photoName == null) {
        allFilesSent = true;
        continue;
      }
      File photo = new File(prefs.getFolderPath()+"\\"+photoName);
      String mediaType = new String("image/jpeg");
      PhotoEntry addedPhoto = picasaServiceHandler.uploadPhoto(album, photo, mediaType);
     
      String photoUrl = addedPhoto.getMediaThumbnails().get(0).getUrl();
      photoUrl = photoUrl.replaceAll("s72", "s640");

      String content = QuickPB.generateContent(photoUrl);
      String title = photoName.split(".jpg")[0];
      bloggerServiceHandler.createPost(prefs.getBlogId(), title, content);
     
      // Delete file
      photo.delete();
    }
  }

  private static void changePreferences() {
    QuickPB.getUserCredentials();
    QuickPB.getPhotoFolderFromUser();
    prefs.put();
  }

  private static String generateContent(String photoUrl) {
    String content = new String();
    content = "<center>";
    content += "<img src=\"" + photoUrl + "\">";
    content += "</center>";
    return content;
  }
 
  private static void initServices(String username) {
    bloggerServiceHandler = new BloggerServiceHandler();
    picasaServiceHandler = new PicasawebServiceHandler(prefs.getUsername());
  }
 
  private static void getPhotoFolderFromUser() {
    String path = null;
    Scanner in = new Scanner(System.in);
    System.out.println("Enter the photo folder path: ");
    path = in.nextLine();
    prefs.setFolderPath(path);
  }

  private static void getUserCredentials() {
    System.out.println("Getting user credentials.");
    System.out.println("Enter 0 for user to terminate.");
    getUsername();
    getUserPassword();
  }

  private static void getUserPassword() {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter password for " + prefs.getUsername() + ": ");
    String password = in.nextLine();
    prefs.setPassword(password);
  }

  private static void getUsername() {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter username: ");
    String username = in.nextLine();
    prefs.setUsername(username);
  }
 

  private static void getBlogInfoFromUser(Feed bloggerBlogList)
  {
    // Print the results
    System.out.println(bloggerBlogList.getTitle().getPlainText());
   
    for (int i = 0; i < bloggerBlogList.getEntries().size(); i++) {
      Entry entry = bloggerBlogList.getEntries().get(i);
      System.out.print(i + " - ");
      System.out.println(entry.getTitle().getPlainText());
      //System.out.println("\tID: " + entry.getId());
    }
    Scanner in = new Scanner(System.in);
    System.out.println("Select blog (enter the number): ");
    int blogIndex = in.nextInt();
    String blogName = bloggerBlogList.getEntries().get(blogIndex).getTitle().getPlainText();
    prefs.setBlogName(blogName);
    String bId = bloggerBlogList.getEntries().get(blogIndex).getId();
    bId = bId.split("blog-")[1];
    prefs.setBlogId(bId);
    // Save preferences
    prefs.put();
  }


}
TOP

Related Classes of com.volongoto.quickbp.QuickPB

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.