August 27, 2013

How to get all your Google Drive folders and URLs with GAS JSONP servlet




Using all the folders of a Google Drive account as a collaboration point over an external web app sounds pretty convenient under certain scenarios, especially for web-based Centralized Business Management.

Apps Script makes this easy with Drive Services and with very little effort you can make a GAS JSONP servlet that query's and packs all the folder names and URLs in a single JSON string, this is specially useful if you are trying to merge different accounts over a single interface. You can get pretty creative about how to implement this.

To make your own GAS JSONP servlet to handle your G.Drive file names & URLs, create a new Google Apps Script project and copy the following code:


Depending on what you need, you might add more data about the folders, this are the most relevant methods:

1.- getAccess(email): Check the users rights on this folder:

2.- getDateCreated(): To know the date when the folder was created.

3.- getDescription(): To get the folders description.

4.- getId(): This one it's particularly useful when working with JSONP, you can use the padding to tell the servlet to do something to a specific folder using the getFolderById(id) method.

5.- getFiles(): It's possible to add all files inside each folder in our JSON package, just add another While cycle but inside the folder's while cycle, this way you get all the folders name, URLs and with all it's files names and URLs, always use parallel loading when using this, in most cases the servlet will take some time to parse all this data.

6.- getSize(): Get's the number of bytes used to store this folder in Drive, in case you need to calculate loads.

Here is a very basic script to call the servlet from your HTML, for testing purposes the prefix it's set to "alert" so the data will be displayed on a pop-up as soon as it loads. For real-life applications change the "alert" prefix to the name of the Javascript function responsible for parsing the JSON, I also recommend
using AngularJS to handle parallel data loading when calling different servlets.


The HTML already includes the URL of a fully working servlet, feel free to use it for testing, but before you can use it, you need to grant access to the servlet, this is a 1 time requirement and it's done by manually calling the servlet's URL:

https://script.google.com/macros/s/AKfycbyixGlm9VJb9RuK_1ZYbsLrduuee4RX2v27mzKvtiFiTm5WaKRK/exec

Stay tuned for articles with more advanced uses of this approach, feel free to post your ideas, comments or suggestions.

Happy coding!

August 23, 2013

How to backup your FirebaseDB on Google Drive using Apps Script!




I've recently started using Firebase, and I've got to say that it's pretty awesome! Especially if you use AngularJS as your front-end you can start making live Apps in minutes, and thanks to Google Drive HTML hosting you don't even need to waste time looking for a place to host everything.

I really recommend trying the example "Wire up a Backend" found on the home page of the AngularJS project.You won't believe at first that "that was it", it's so fast and easy that I'm sure you will think that there is something else to do before it starts actually working.

Firebase can do a great job as a Live Data back-end, but it's not very appropriate for historical records for a few reasons:

* Storing static backups takes too much space, and it should be used wisely (the free 100 Mb quota can run out quickly if you are not careful).

* Downloading all the data every time you want to make a report consumes a lot of bandwidth quota, especially when you call many backups to make a time graph with Google Charts.

* It's best to save those precious connections, since those comes with a price too if you exceed the free 50 concurrent connections quota.

So, instead, it's best to save a backup of your precious Firebase Data on a safe place, Firebase already  has a button that let's you save a backup of the interface anytime you want... and that's good! but let's make it great by storing it automatically on a specific folder using the date as the file name. Google Drive's free quota it's more than large enough for millions of backups on most applications, no bandwidth limits and it can hold up to 10 requests per second/user, and they give 10,000,000 free requests a day... So feel free to let users query and make those cool graphs all they want from Drive.This way you use only 1 connection, for a very short time and you only need 1 download (usually once a day) to get a copy of your data.

So, how do you communicate both services? Well, using Firebase with Google Apps Script it's a piece of cake thanks to Firebase's REST API, Apps Script URL Fetch Service and Apps Script DocsList Service, let's see how simple it is:


Since Firebase's REST API allows us to get a copy of the DB with a simple URL, we can use it to get all the information directly via URLFetch, in this example we simply store the text inside a specific Google Drive folder, but its also possible to parse and pass the data to ScriptDB, Spreadsheets, Charts, Gmail, etc.

So give it a try, you don't even need to make the example to try this, you can register to Firebase and manually create a DB and some dummy data to work with, Firebase Web interface it's extremely easy to use so you won't have any problems with this.

Promising huh? well, if you want to do some serious data processing, you can use Bigquery or Fusion tables to put all that data together and watch how it evolves through time, also, if anything ever goes wrong, simply upload your most recent backup from drive, Firebase makes this very easy giving an "Import JSON" option.

Looking for a way to communicate your brand-new and super-cool Firebase live DB with your client's "traditional" web server?, you can use Apps Script XML Service, Apps Script JDBC Service, or pass them to ScriptDB and to have a frozen copy that responds to GET or POST requests via Apps Script Content Service, this one it's very usefull to make Charts on web pages using Google Charts Javascript API and some JSONP.

Just keep in mind when you are working with Firebase to be very carefull with your quotas (they are meant to last a month), and use whatever technique you find appropriate to reduce data consumption.

Feel free to let us know your ideas and opinions about this platforms at the bottom of this article and stay tuned for more articles about this great technology combo.

Happy coding!

August 7, 2013

Ways to share anything with anyone with Gdrive public HTML hosting!


Looking for sharing your files with the world, but just sharing Gdocs isn't good enough for you? Don't worry! Google Drive has a special service called public HTML hosting, which allows you to transform any Gdrive folder into a virtual "Web Server"! When others access the page, they see a list with all the folders public content (by default everything), if they click on the file, if it's not something Gdrive can open directly (like images or docs) the users browser will simply download the file.

Let's see an example, this Gdrive Folder contains a .rar file, since Gdrive cannot open this kind of files directly, when you click on it, your browser will start downloading it:

https://googledrive.com/host/0B_RClkFMLkcpdXlOZWdwM2JuWUk/

You can even put it inside an iframe to show its contents directly on a website!



Sweet huh? The best is that the content keeps in sync with your Gdrive, so if you change your content, the public version will be instantly updated! You can even host webpages! just throw some index.html file on that folder and when the users open the folders URL, they will receive the content of index.html instead of the default folder view.

But that's not all you can do... if instead of using the folder's URL you point to a specific file, you can use the URL to share that specific file with someone, and when they open it, the file will start downloading automatically (except for docs, html and images)... but hey, that's not the cool part! you can use this specific URLs to stream multimedia content for you webpages! You don't even need to worry about a player, you can use HTML5 video or audio tags and show your content straight away!

This works even for large videos, here is a video of one of my cats (he's name is Volt) playing zelda:


Right on huh? But no, you cannot have my cat... but you can host your pet's video and share it with us in the comments section at the bottom of this article!

To make things easier, I also added a copy of a very nice HTML5 video converter for windows (there is a version for Mac OS users on the authors site), it does a great job getting your video ready for some HTML5 action.

Just remember, you are responsible of what you share on the internet, so be careful with how you use this tool, never distribute proprietary material like music, movies or videoclips.

Happy coding!