KingZack commited on
Commit
e9e3bda
·
1 Parent(s): 40cdcb5

adding google drive service readme

Browse files
src/ctp_slack_bot/services/GOOGLE_DRIVE_README.md ADDED
@@ -0,0 +1,228 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Google Drive Access Module
2
+
3
+ This Python module provides a simplified way to interact with Google Drive, focusing on easy access to files in nested folders using path-like syntax. It handles various Google file formats and provides comprehensive metadata for files and folders.
4
+
5
+ ## Features
6
+
7
+ - **Path-based folder access**: Access files using simple paths like `folder1/folder2/folder3`
8
+ - **Efficient caching**: Folder IDs are cached to improve performance
9
+ - **Comprehensive metadata**: Get detailed information about files and folders
10
+ - **Read various file types**:
11
+ - Text files
12
+ - Google Docs
13
+ - VTT files
14
+ - **Robust folder finding**: Works with exact and partial name matching
15
+ - **Simple API**: Designed for ease of use with minimal code
16
+
17
+ ## Setup Instructions
18
+
19
+ ### 1. Create a Google Cloud Project
20
+
21
+ 1. Go to the [Google Cloud Console](https://console.cloud.google.com/)
22
+ 2. Click on the project dropdown at the top of the page and select "New Project"
23
+ 3. Enter a project name and click "Create"
24
+ 4. Once created, make sure your new project is selected in the dropdown
25
+
26
+ ### 2. Enable the Google Drive API
27
+
28
+ 1. In the Google Cloud Console, navigate to "APIs & Services" > "Library" in the left sidebar
29
+ 2. Search for "Google Drive API" in the search bar
30
+ 3. Click on "Google Drive API" in the results
31
+ 4. Click the "Enable" button
32
+
33
+ ### 3. Create OAuth Credentials
34
+
35
+ 1. In the Google Cloud Console, go to "APIs & Services" > "Credentials" in the left sidebar
36
+ 2. Click "Create Credentials" at the top and select "OAuth client ID"
37
+ 3. If prompted to configure the OAuth consent screen:
38
+ - Choose "External" user type (or "Internal" if you're in a Google Workspace organization)
39
+ - Fill in the required information (App name, User support email, Developer contact email)
40
+ - Click "Save and Continue"
41
+ - Add the following scopes:
42
+ - `.../auth/drive` (Full access to Google Drive)
43
+ - Click "Save and Continue" and complete the registration
44
+ 4. Return to the "Create OAuth client ID" screen
45
+ 5. Select "Desktop application" as the Application type
46
+ 6. Enter a name for your OAuth client (e.g., "Google Drive Access Desktop")
47
+ 7. Click "Create"
48
+ 8. Download the JSON file (this is your `client_secret.json`)
49
+
50
+ ### 4. Project Setup
51
+
52
+ 1. Setup a virtual environment and install dependencies:
53
+ ```bash
54
+ python -m venv venv
55
+ source venv/bin/activate # On Windows: venv\Scripts\activate
56
+ pip install -r requirements.txt
57
+ ```
58
+
59
+ 2. Place your credentials:
60
+ - Create a `credentials` directory in your project root
61
+ - Move the downloaded OAuth client JSON file to the `credentials` directory
62
+ - Rename it to `client_secret.json`
63
+
64
+ ### 5. Authentication Process
65
+
66
+ When you run the application for the first time:
67
+ 1. A browser window will open automatically
68
+ 2. You'll be asked to sign in to your Google account
69
+ 3. You'll see a consent screen asking for permission to access your Google Drive
70
+ 4. After granting permission, the browser will display a success message
71
+ 5. The application will save a token file (`token.pickle`) in the credentials directory for future use
72
+
73
+ ## Usage Guide
74
+
75
+ The `EasyGoogleDrive` class provides several methods to interact with Google Drive. Here's how to use the core functionality:
76
+
77
+ ### Basic Usage
78
+
79
+ ```python
80
+ from google_drive_access import EasyGoogleDrive
81
+
82
+ # Initialize the Google Drive client
83
+ drive = EasyGoogleDrive()
84
+
85
+ # Example folder path - replace with your actual folder path
86
+ folder_path = "Spring-2025-BAI"
87
+ subfolder_path = "Spring-2025-BAI/transcripts"
88
+ ```
89
+
90
+ ### Listing Folders
91
+
92
+ ```python
93
+ # List folders in a directory
94
+ folders = drive.get_folders_in_folder(folder_path)
95
+
96
+ # Access folder properties
97
+ for folder in folders:
98
+ print(f"Folder: {folder['name']}")
99
+ print(f" Created: {folder.get('createdTimeFormatted', 'Unknown')}")
100
+ print(f" Modified: {folder.get('modifiedTimeFormatted', 'Unknown')}")
101
+ ```
102
+
103
+ ### Listing Files
104
+
105
+ ```python
106
+ # List files in a directory
107
+ files = drive.get_files_in_folder(subfolder_path)
108
+
109
+ # Access file properties
110
+ for file in files:
111
+ print(f"File: {file['name']}")
112
+ print(f" Type: {file.get('fileType', 'Unknown')}")
113
+ print(f" Created: {file.get('createdTimeFormatted', 'Unknown')}")
114
+ print(f" Modified: {file.get('modifiedTimeFormatted', 'Unknown')}")
115
+ print(f" Size: {file.get('sizeFormatted', 'Unknown')}")
116
+ ```
117
+
118
+ ### Getting a Specific File
119
+
120
+ ```python
121
+ # Get a specific file with metadata
122
+ file = drive.get_file("example.txt", subfolder_path, include_metadata=True)
123
+
124
+ if file:
125
+ print(f"File: {file['name']}")
126
+ print(f" Type: {file.get('fileType', 'Unknown')}")
127
+ print(f" Created: {file.get('createdTimeFormatted', 'Unknown')}")
128
+ print(f" Modified: {file.get('modifiedTimeFormatted', 'Unknown')}")
129
+ print(f" Size: {file.get('sizeFormatted', 'Unknown')}")
130
+ ```
131
+
132
+ ### Getting All Items in a Folder
133
+
134
+ ```python
135
+ # Get all items (files and folders) in a folder
136
+ all_items = drive.get_all_files_in_folder(folder_path)
137
+
138
+ # Access item properties
139
+ for item in all_items:
140
+ item_type = "Folder" if item.get('mimeType') == drive.MIME_TYPES['folder'] else item.get('fileType', 'Unknown')
141
+ print(f"Item: {item['name']} ({item_type})")
142
+ ```
143
+
144
+ ### Checking if a File Exists
145
+
146
+ ```python
147
+ # Check if a file exists
148
+ exists = drive.file_exists("example.txt", subfolder_path)
149
+ print(f"File exists: {exists}")
150
+ ```
151
+
152
+ ### Getting File Modified Time
153
+
154
+ ```python
155
+ # Get file modified time
156
+ modified_time = drive.get_file_modified_time("example.txt", subfolder_path)
157
+ if modified_time:
158
+ print(f"Last modified: {modified_time}")
159
+ ```
160
+
161
+ ### Reading File Content
162
+
163
+ ```python
164
+ # Get file with content
165
+ file_with_content = drive.get_file("example.txt", subfolder_path, include_content=True)
166
+
167
+ if file_with_content and 'file_content' in file_with_content:
168
+ content = file_with_content['file_content']
169
+ if content:
170
+ print(f"Content: {content[:100]}...") # Print first 100 characters
171
+ ```
172
+
173
+ ## Complete Example
174
+
175
+ For a complete example of how to use the `EasyGoogleDrive` class, see the `basic_usage.py` file included in this package. This file demonstrates all the core functionality with practical examples.
176
+
177
+ ## Key Concepts
178
+
179
+ ### Path-based Folder Access
180
+
181
+ The module uses a simple path-like syntax to access folders:
182
+
183
+ ```python
184
+ # Access a deeply nested folder
185
+ folder_path = "folder1/folder2/folder3"
186
+ files = drive.get_files_in_folder(folder_path)
187
+ ```
188
+
189
+ This makes it much easier to work with nested folder structures compared to using folder IDs.
190
+
191
+ ### Metadata Fields
192
+
193
+ The module provides comprehensive metadata for files and folders, including:
194
+
195
+ - **Creation and modification dates**: Both as datetime objects and formatted strings
196
+ - **File size**: Both in bytes and human-readable format (KB, MB, GB)
197
+ - **File type**: Simplified type based on MIME type
198
+ - **Owner information**: Names and email addresses of file owners
199
+ - **Sharing status**: Whether the file is shared
200
+ - **Web links**: Direct links to view the file in a browser
201
+
202
+ ## Error Handling
203
+
204
+ The module includes comprehensive error handling:
205
+
206
+ - **Authentication errors**: Clear messages when credentials are missing or invalid
207
+ - **Folder not found**: Helpful messages when a folder in the path cannot be found
208
+ - **File not found**: Attempts partial name matching before giving up
209
+ - **Decoding errors**: Handles issues with file content encoding
210
+
211
+ ## Dependencies
212
+
213
+ - **Required**:
214
+ - google-auth-oauthlib
215
+ - google-auth-httplib2
216
+ - google-api-python-client
217
+ - python-dateutil
218
+
219
+ ## Security Notes
220
+
221
+ - Never commit your `client_secret.json` or token files to version control
222
+ - Add `credentials/` to your `.gitignore` file
223
+ - Keep your credentials secure and don't share them
224
+ - For production applications, consider using service accounts with the minimum required permissions
225
+
226
+ ## Contributing
227
+
228
+ Feel free to contribute to this project by submitting issues or pull requests.