Pages

Tags

Validation (9) XML (9) Geeky (3) Java (3) Android (2) Business IT (1) Chromecast (1) Devfest (1) Web (1)

Monday, September 19, 2011

Synchronize your IDE with Google Docs for you Google Phone interview

I had a phone interview with Google recently. When I prepared for the interview I tried to write code in Google Docs and was very much missing all the features of my IDE.

Maybe someone else may find my simple code snippet below useful. It does auto-synchronize you local file with the Google Document shared for you interview. So whenever you save the file in the IDE it gets uploaded to Docs so that you interview can see what you wrote. At the same time you have full code-completion/generation and the code does also syntax highlighting through JHighlight.

Here is the result:


In order to make it working you need to put your self to the sharing list (which you can do), otherwise there would be a permission error. If anyone would like to improve the code, back-synchronization of what the interviewer writes would be great.


package com.urbandroid.gdata;

import com.google.gdata.client.docs.DocsService;
import com.google.gdata.data.MediaContent;
import com.google.gdata.data.docs.DocumentListEntry;
import com.google.gdata.data.docs.DocumentListFeed;
import com.google.gdata.data.media.MediaByteArraySource;
import com.google.gdata.data.media.MediaSource;

import com.uwyn.jhighlight.renderer.JavaXhtmlRenderer;

import java.io.*;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

public class UpdateDocument {


    public static void main(String[] args) throws Exception {
        UpdateDocument updater = new UpdateDocument();
        File file = new File(args.length == 1 ? args[0] : "/home/petr/project/google/src/Interview.java");

        JavaXhtmlRenderer renderer = new JavaXhtmlRenderer();
        SimpleDateFormat dateFormat = new SimpleDateFormat("hh:mm:ss");

        long modified = -1;
        while (true) {
            if (modified != file.lastModified()) {
                modified = file.lastModified();
                System.err.println("File modified...");
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                renderer.highlight("GPhoneInterview by Petr Nalevka, last update: " + dateFormat.format(new Date()), new FileInputStream(file), out, "UTF-8", false);
                updater.update(new ByteArrayInputStream(out.toByteArray()), "Phone Interview");
            }
            Thread.sleep(1500);
        }
    }


    public void update(InputStream in, String titleSubstring) throws Exception {
        DocsService client = new DocsService("PetrNalevka-DocSync-v1");
        client.setUserCredentials("petr.nalevka@gmail.com", PassProvider.PASSWORD);
        URL feedUri = new URL("http://docs.google.com/feeds/default/private/full/");
        DocumentListFeed feed = client.getFeed(feedUri, DocumentListFeed.class);
        DocumentListEntry found = null;
        for (DocumentListEntry entry : feed.getEntries()) {
            System.err.println("Entry: "+entry.getTitle().getPlainText());
            if (entry.getTitle().getPlainText().contains(titleSubstring)) {
                found = entry;
                break;
            }
        }
        if (found == null) {
            System.err.println("Entry not found, exiting.");
            return;
        }

        found.setCanEdit(true);

        MediaContent mediaContent = (MediaContent) found.getContent();
        MediaSource mediaSource = client.getMedia(mediaContent);

        found.setMediaSource(mediaSource);

        String page = InputStreamUtil.read(in);
        mediaContent.setMediaSource(new MediaByteArraySource(page.getBytes(), DocumentListEntry.MediaType.HTM.getMimeType()));

        // shared documents do not provide edit link?
        URL url;
        if (found.getEditLink() != null) {
            url = new URL(found.getEditLink().getHref());
        } else {
            url = new URL("http://docs.google.com/feeds/default/private/full/document%3A"+found.getDocId());
        }
        System.err.println("Updating URL: " + url);
        try {
            client.updateMedia(url, found);
        } catch (Exception e) {
            // ignore, hack because GData returns error on shared documents, but update works
        }
    }

}







2 comments:

  1. Has anyone tried Bing's airport map feature? It's awesome!

    ReplyDelete
  2. Hi,

    Would you care to upload the source code for this on github or the like? I too have an interview with Google coming up and would like to update this with the latest GDrive api's.

    Thanks

    ReplyDelete