Skip to content

Commit

Permalink
feat(Google Drive): Add List folder
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorentLvr committed Sep 27, 2023
1 parent 3fe2928 commit cdd8ebb
Showing 1 changed file with 263 additions and 0 deletions.
263 changes: 263 additions & 0 deletions Google Drive/Google_Drive_List_folder.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "5f52edaa-24b0-47b1-ad38-2102da01e7ea",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"<img width=\"10%\" alt=\"Naas\" src=\"https://landen.imgix.net/jtci2pxwjczr/assets/5ice39g4.png?w=160\"/>"
]
},
{
"cell_type": "markdown",
"id": "210901d4-efad-467e-9c1b-7133f99edc51",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"# Google Drive - List folder"
]
},
{
"cell_type": "markdown",
"id": "c8c92f63-7e98-4c5f-94b3-6d6329887bf5",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Tags:** #google #drive #list #folder #python #api"
]
},
{
"cell_type": "markdown",
"id": "39db9889-b178-401f-8fb7-2e4fcd916542",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Author:** [Florent Ravenel](https://www.linkedin.com/in/florent-ravenel)"
]
},
{
"cell_type": "markdown",
"id": "f5a97053-b098-4ef4-a6ab-d098727800b3",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Last update:** 2023-09-27 (Created: 2023-09-27)"
]
},
{
"cell_type": "markdown",
"id": "b97391b8-40c8-42f0-878a-43e1a2150cf6",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**Description:** This notebook will list all the folders in a Google Drive account and how it is useful for organization."
]
},
{
"cell_type": "markdown",
"id": "a830f22b-75c1-4a80-9f96-da44a0aba953",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"**References:**\n- [Google Drive API Documentation](https://developers.google.com/drive/api/v3/reference)\n- [Google Drive Python Quickstart](https://developers.google.com/drive/api/v3/quickstart/python)"
]
},
{
"cell_type": "markdown",
"id": "ec9d0770-1299-450b-8ae9-d09d19696af4",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Input"
]
},
{
"cell_type": "markdown",
"id": "7543855b-40fc-4b9e-98ff-d65eae66422f",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Import libraries"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0fdd3dbb-6868-4921-be2c-793d7f4465e9",
"metadata": {
"papermill": {},
"tags": []
},
"source": "import os\nfrom googleapiclient.discovery import build\nfrom google_auth_oauthlib.flow import InstalledAppFlow\nfrom google.auth.transport.requests import Request",
"outputs": []
},
{
"cell_type": "markdown",
"id": "43d5e0c3-d916-4e80-a737-8c15dd67647b",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Setup variables\n- **SCOPES**: `https://www.googleapis.com/auth/drive.readonly`\n- **CREDENTIALS_FILE**: `credentials.json`"
]
},
{
"cell_type": "markdown",
"id": "7205197d-b2ca-4103-a931-721e2ce5e33e",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"For more information on how to setup the credentials, please refer to [Google Drive Python Quickstart](https://developers.google.com/drive/api/v3/quickstart/python)."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "91a1b3fd-9bd9-4fcb-81fe-85bced49782e",
"metadata": {
"papermill": {},
"tags": []
},
"source": "SCOPES = [\"https://www.googleapis.com/auth/drive.readonly\"]\nCREDENTIALS_FILE = \"credentials.json\"",
"outputs": []
},
{
"cell_type": "markdown",
"id": "f7e932bf-1b45-4940-8cb4-c69d32826a13",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Model"
]
},
{
"cell_type": "markdown",
"id": "c149081a-7527-40d9-be9e-aea27274864b",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### List folders"
]
},
{
"cell_type": "markdown",
"id": "2428da37-4821-4f50-be7f-7137a123a2b6",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"This function will list all the folders in a Google Drive account."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "6eb6e3b5-b4ca-4349-84b1-6205ecd4d11d",
"metadata": {
"papermill": {},
"tags": []
},
"source": "def list_folders(credentials_file):\n # Authenticate and create the Google Drive service\n creds = None\n if os.path.exists(\"token.pickle\"):\n with open(\"token.pickle\", \"rb\") as token:\n creds = pickle.load(token)\n if not creds or not creds.valid:\n if creds and creds.expired and creds.refresh_token:\n creds.refresh(Request())\n else:\n flow = InstalledAppFlow.from_client_secrets_file(credentials_file, SCOPES)\n creds = flow.run_local_server(port=0)\n with open(\"token.pickle\", \"wb\") as token:\n pickle.dump(creds, token)\n service = build(\"drive\", \"v3\", credentials=creds)\n # List folders\n results = (\n service.files()\n .list(\n q=\"mimeType='application/vnd.google-apps.folder'\", fields=\"files(id, name)\"\n )\n .execute()\n )\n items = results.get(\"files\", [])\n if not items:\n print(\"No folders found.\")\n else:\n print(\"Folders:\")\n for item in items:\n print(\"{0} ({1})\".format(item[\"name\"], item[\"id\"]))",
"outputs": []
},
{
"cell_type": "markdown",
"id": "11616427-0b9d-49bb-86a1-8478bae6b703",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"## Output"
]
},
{
"cell_type": "markdown",
"id": "7d948195-81ae-4796-a0a9-e782b44e4c0e",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
"### Display result"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5c42e249-1bbd-4b53-a2ee-d554f015b2f2",
"metadata": {
"papermill": {},
"tags": []
},
"source": "list_folders(CREDENTIALS_FILE)",
"outputs": []
},
{
"cell_type": "markdown",
"id": "81063e3e-0fec-4148-b2ff-5d8b800140ae",
"metadata": {
"papermill": {},
"tags": []
},
"source": [
" "
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
},
"widgets": {
"application/vnd.jupyter.widget-state+json": {
"state": {},
"version_major": 2,
"version_minor": 0
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}

0 comments on commit cdd8ebb

Please sign in to comment.