import java.io.IOException;
import java.net.URL;
import com.google.gdata.client.GoogleService;
import com.google.gdata.client.Query;
import com.google.gdata.client.calendar.CalendarService;
import com.google.gdata.data.DateTime;
import com.google.gdata.data.Entry;
import com.google.gdata.data.Feed;
import com.google.gdata.data.HtmlTextConstruct;
import com.google.gdata.data.PlainTextConstruct;
import com.google.gdata.data.calendar.CalendarEventEntry;
import com.google.gdata.data.calendar.CalendarEventFeed;
import com.google.gdata.data.extensions.When;
import com.google.gdata.data.extensions.Where;
import com.google.gdata.util.ServiceException;
public class BloggerPostRetriever {
// Google Calendar Feed Url
private static String feedUrlString = "http://www.google.com/calendar/feeds/FEED_ID/private/full";
// Blooger Blog ID
private static String blogId = "feedId";
// Google Calendar user
private static String user = "user@gmail.com";
// Google Calendar password
private static String password = "verySecretPassword";
private static GoogleService myService = new GoogleService("blogger","rmk-Blogger2GCal-1");
private static CalendarService myService2 = new CalendarService("rmk-Blogger2GCal-1");
public boolean checkIfEventExists(CalendarService myService,String title)
throws Exception {
URL feedUrl =
new URL(feedUrlString);
Query myQuery = new Query(feedUrl);
myQuery.setFullTextQuery(title);
CalendarEventFeed myResultsFeed = myService.query(myQuery,
CalendarEventFeed.class);
if (myResultsFeed.getEntries().size() > 0) return true; else
return false;
}
public void createEvent(CalendarService myService,String title,String content,DateTime dateTime)
throws Exception
{
URL postUrl =
new URL(feedUrlString);
CalendarEventEntry myEntry = new CalendarEventEntry();
myEntry.setTitle(new PlainTextConstruct(title));
myEntry.setSummary(new PlainTextConstruct(content));
Where where = new Where();
myEntry.addHtmlLink(content, "pl", title);
myEntry.addLocation(where);
myEntry.setContent(new HtmlTextConstruct("<a href='"+content+"'>"+title+"</a> - "+content));
DateTime startTime = dateTime;
DateTime endTime = dateTime;
When eventTimes = new When();
eventTimes.setStartTime(startTime);
eventTimes.setEndTime(endTime);
myEntry.addTime(eventTimes);
if (!checkIfEventExists(myService, title)) {
CalendarEventEntry insertedEntry = myService.insert(postUrl, myEntry);
System.out.println("plum :-)");
}
}
public void printAllPosts(GoogleService myService, String blogId)
throws ServiceException, IOException,Exception {
String blogID = blogId;
URL feedUrl = new URL("http://www.blogger.com/feeds/" + blogID + "/posts/default");
Feed resultFeed = myService.getFeed(feedUrl, Feed.class);
System.out.println(resultFeed.getTitle().getPlainText());
for (int i = 0; i < resultFeed.getEntries().size(); i++) {
Entry entry = resultFeed.getEntries().get(i);
System.out.println("\t" + entry.getTitle().getPlainText()+" "+entry.getPublished());
String title = entry.getTitle().getPlainText();
String content = entry.getHtmlLink().getHref();
DateTime dateTime = entry.getPublished();
createEvent(myService2, title, content, dateTime);
}
}
public static void main(String[] args) throws Exception{
BloggerPostRetriever bloggerPostRetriever = new BloggerPostRetriever();
myService2.setUserCredentials(user,password);
bloggerPostRetriever.printAllPosts(myService, blogId);
}
}
|