Skip to content

Instantly share code, notes, and snippets.

@evanrinehart
Created October 15, 2014 15:53
Show Gist options
  • Save evanrinehart/a41180e897d42a1c6993 to your computer and use it in GitHub Desktop.
Save evanrinehart/a41180e897d42a1c6993 to your computer and use it in GitHub Desktop.
download a csv export of a google sheet
(ns atomic-playground.sheets
(:import [com.google.api.client.http GenericUrl]
[com.google.api.services.drive.model File]
[com.google.api.client.googleapis.auth.oauth2 GoogleCredential GoogleCredential$Builder]
[com.google.api.client.http HttpTransport]
[com.google.api.services.drive Drive Drive$Builder]
[com.google.api.services.drive DriveScopes]
(com.google.api.client.http.javanet NetHttpTransport)
(com.google.api.client.json.jackson2 JacksonFactory)
(java.io IOException)))
(def service-account-email "96051011742-t28asfs9kjegj1obau4vtdpg5spnk2bf@developer.gserviceaccount.com")
(def private-key-path "my.p12")
(def file-id "1ptY45ltvZYNNR8eMpW7MQSKRdoji8V27aZUjyXIkue0")
(defn get-drive-service []
(let [http-transport (NetHttpTransport.)
json-factory (JacksonFactory.)
credential (-> (GoogleCredential$Builder.)
(.setTransport http-transport)
(.setJsonFactory json-factory)
(.setServiceAccountId service-account-email)
(.setServiceAccountScopes [DriveScopes/DRIVE])
(.setServiceAccountPrivateKeyFromP12File (java.io.File. private-key-path))
(.build)
)]
(-> (Drive$Builder. http-transport json-factory nil)
(.setHttpRequestInitializer credential)
(.setApplicationName "SomeRandomString")
(.build))))
(defn get-file-object [service]
(-> service (.files) (.get file-id) (.execute)))
(defn download-file [service, file]
(when-let [{link "text/csv"} (.getExportLinks file)]
(when (not= link "")
(try (-> service
(.getRequestFactory)
(.buildGetRequest (GenericUrl. link))
(.execute)
(.getContent)
(line-seq)) (catch IOException e (System/exit -1))
))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment