aboutsummaryrefslogtreecommitdiff
path: root/internal/drive
diff options
context:
space:
mode:
authorOnur Gündüz <ogunduz@gmail.com>2026-06-28 06:16:07 +0200
committerOnur Gündüz <ogunduz@gmail.com>2026-06-28 06:16:07 +0200
commit9eacbb42f9a5931bf33d968f775fc806c46b11b0 (patch)
treebf9a34182b2db560023ba0deb060146bdbcc0d34 /internal/drive
parent4137fae6df4e77198b8ddd9d9647a1ee0c7c49d3 (diff)
downloadcolabctl-9eacbb42f9a5931bf33d968f775fc806c46b11b0.tar.gz
colabctl-9eacbb42f9a5931bf33d968f775fc806c46b11b0.zip
Init
Diffstat (limited to 'internal/drive')
-rw-r--r--internal/drive/drive.go116
1 files changed, 116 insertions, 0 deletions
diff --git a/internal/drive/drive.go b/internal/drive/drive.go
new file mode 100644
index 0000000..677880b
--- /dev/null
+++ b/internal/drive/drive.go
@@ -0,0 +1,116 @@
+// Package drive provides a thin wrapper around the Google Drive API v3 for
+// operations relevant to Colab notebooks.
+package drive
+
+import (
+ "context"
+ "fmt"
+ "net/http"
+
+ gdrive "google.golang.org/api/drive/v3"
+ "google.golang.org/api/googleapi"
+ "google.golang.org/api/option"
+)
+
+const colabMIMEType = "application/vnd.google.colaboratory"
+
+// Notebook is a minimal representation of a Colab notebook file in Drive.
+type Notebook struct {
+ ID string
+ Name string
+ Modified string // RFC 3339 string
+ URL string
+}
+
+// Client wraps the Drive API service.
+type Client struct {
+ svc *gdrive.Service
+}
+
+// New creates a Drive client using the provided authenticated HTTP client.
+func New(ctx context.Context, httpClient *http.Client) (*Client, error) {
+ svc, err := gdrive.NewService(ctx, option.WithHTTPClient(httpClient))
+ if err != nil {
+ return nil, fmt.Errorf("create drive service: %w", err)
+ }
+ return &Client{svc: svc}, nil
+}
+
+// ListNotebooks returns all Colab notebooks (.ipynb files with the Colab MIME
+// type) that are owned by or shared with the authenticated user.
+func (c *Client) ListNotebooks(ctx context.Context) ([]*Notebook, error) {
+ var notebooks []*Notebook
+ pageToken := ""
+ fields := googleapi.Field("nextPageToken, files(id, name, modifiedTime, webViewLink)")
+ query := fmt.Sprintf("mimeType='%s' and trashed=false", colabMIMEType)
+
+ for {
+ call := c.svc.Files.List().
+ Q(query).
+ Fields(fields).
+ OrderBy("modifiedTime desc").
+ PageSize(100).
+ Context(ctx)
+ if pageToken != "" {
+ call = call.PageToken(pageToken)
+ }
+
+ resp, err := call.Do()
+ if err != nil {
+ return nil, fmt.Errorf("list drive files: %w", err)
+ }
+ for _, f := range resp.Files {
+ notebooks = append(notebooks, &Notebook{
+ ID: f.Id,
+ Name: f.Name,
+ Modified: f.ModifiedTime,
+ URL: f.WebViewLink,
+ })
+ }
+ if resp.NextPageToken == "" {
+ break
+ }
+ pageToken = resp.NextPageToken
+ }
+ return notebooks, nil
+}
+
+// GetNotebook fetches the raw JSON bytes of a notebook from Drive by its file
+// ID. The content is the raw .ipynb JSON.
+func (c *Client) GetNotebook(ctx context.Context, fileID string) ([]byte, error) {
+ resp, err := c.svc.Files.Export(fileID, "application/json").Context(ctx).Download()
+ if err != nil {
+ // Colab files are not Google Docs, so Export will not work.
+ // Use the plain media download instead.
+ resp2, err2 := c.svc.Files.Get(fileID).Context(ctx).Download()
+ if err2 != nil {
+ return nil, fmt.Errorf("download notebook (file %s): %w", fileID, err)
+ }
+ defer resp2.Body.Close()
+ buf := make([]byte, 0, 512*1024)
+ tmp := make([]byte, 32*1024)
+ for {
+ n, readErr := resp2.Body.Read(tmp)
+ if n > 0 {
+ buf = append(buf, tmp[:n]...)
+ }
+ if readErr != nil {
+ break
+ }
+ }
+ return buf, nil
+ }
+ defer resp.Body.Close()
+ buf := make([]byte, 0, 512*1024)
+ tmp := make([]byte, 32*1024)
+ for {
+ n, readErr := resp.Body.Read(tmp)
+ if n > 0 {
+ buf = append(buf, tmp[:n]...)
+ }
+ if readErr != nil {
+ break
+ }
+ }
+ return buf, nil
+}