Initial commit

This commit is contained in:
Zhongwei Li
2025-11-30 09:05:19 +08:00
commit 09fec2555b
96 changed files with 24269 additions and 0 deletions

View File

@@ -0,0 +1,264 @@
# PyDrive2 API Quick Reference
## Core Methods
### Create File Object
```python
# New file
file = drive.CreateFile({'title': 'filename.txt'})
# Existing file by ID
file = drive.CreateFile({'id': 'FILE_ID_HERE'})
# File in specific folder
file = drive.CreateFile({
'title': 'filename.txt',
'parents': [{'id': 'FOLDER_ID'}]
})
```
### Upload Operations
```python
# Upload from local file
file.SetContentFile('/path/to/local/file.txt')
file.Upload()
# Upload from string
file.SetContentString('Hello, World!')
file.Upload()
# Upload from string with markdown MIME type
file = drive.CreateFile({
'title': 'document.md',
'mimeType': 'text/markdown'
})
file.SetContentString('# My Document\n\nMarkdown content here')
file.Upload()
# Upload with metadata update
file['title'] = 'New Title'
file.Upload()
```
### Download Operations
```python
# Download to local file
file.GetContentFile('/path/to/download/location.txt')
# Get content as string
content = file.GetContentString()
# Get content as file object
file_obj = file.GetContentFile() # Returns file object
```
### Metadata Operations
```python
# Fetch metadata
file.FetchMetadata()
# Access metadata fields
title = file['title']
mime_type = file['mimeType']
file_size = file['fileSize']
created = file['createdDate']
modified = file['modifiedDate']
web_link = file['alternateLink']
# Update metadata
file['title'] = 'New Title'
file['description'] = 'Updated description'
file.Upload() # Save changes
```
### List/Search Operations
```python
# List all files
file_list = drive.ListFile().GetList()
# List with query
file_list = drive.ListFile({'q': "title contains 'report'"}).GetList()
# List with pagination
file_list = drive.ListFile({
'q': "trashed = false",
'maxResults': 10
}).GetList()
# Iterate through results
for file in file_list:
print(f"{file['title']} - {file['id']}")
```
### Delete Operations
```python
# Move to trash
file.Trash()
# Permanently delete
file.Delete()
# Restore from trash
file.UnTrash()
```
### Permission Operations
```python
# Get permissions
permissions = file.GetPermissions()
# Share with specific user
permission = file.InsertPermission({
'type': 'user',
'value': 'user@example.com',
'role': 'reader'
})
# Share with anyone (public)
permission = file.InsertPermission({
'type': 'anyone',
'role': 'reader'
})
# Remove permission
file.DeletePermission(permission_id)
```
## Metadata Fields Reference
### Common File Fields
```python
file['id'] # File ID
file['title'] # File name
file['mimeType'] # MIME type
file['description'] # Description
file['createdDate'] # Creation timestamp
file['modifiedDate'] # Last modified timestamp
file['fileSize'] # Size in bytes
file['parents'] # Parent folder IDs
file['owners'] # Owner information
file['alternateLink'] # Web view link
file['downloadUrl'] # Direct download URL
file['thumbnailLink'] # Thumbnail URL
file['shared'] # Shared status (boolean)
file['trashed'] # Trash status (boolean)
```
## Export Google Docs
```python
# Export Google Doc as PDF
file = drive.CreateFile({'id': 'DOC_ID'})
file.GetContentFile('output.pdf', mimetype='application/pdf')
# Export Google Sheet as Excel
file.GetContentFile('output.xlsx',
mimetype='application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
# Export Google Slides as PowerPoint
file.GetContentFile('output.pptx',
mimetype='application/vnd.openxmlformats-officedocument.presentationml.presentation')
```
## Folder Operations
```python
# Create folder
folder = drive.CreateFile({
'title': 'My Folder',
'mimeType': 'application/vnd.google-apps.folder'
})
folder.Upload()
# List files in folder
file_list = drive.ListFile({
'q': f"'{folder['id']}' in parents and trashed = false"
}).GetList()
# Upload file to folder
file = drive.CreateFile({
'title': 'file.txt',
'parents': [{'id': folder['id']}]
})
file.SetContentString('Content')
file.Upload()
```
## Error Handling
```python
from pydrive2.files import ApiRequestError
try:
file = drive.CreateFile({'id': 'FILE_ID'})
file.FetchMetadata()
except ApiRequestError as e:
if e.error['code'] == 404:
print("File not found")
else:
print(f"Error: {e}")
```
## Batch Operations
```python
# Upload multiple files
files_to_upload = [
('file1.txt', '/path/to/file1.txt'),
('file2.txt', '/path/to/file2.txt'),
]
for title, path in files_to_upload:
file = drive.CreateFile({'title': title})
file.SetContentFile(path)
file.Upload()
print(f"Uploaded {title}: {file['id']}")
```
## Advanced Features
### Resumable Upload (for large files)
```python
# PyDrive2 handles resumable uploads automatically
# Just use normal upload for large files
file = drive.CreateFile({'title': 'large_file.zip'})
file.SetContentFile('/path/to/large_file.zip')
file.Upload() # Automatically uses resumable upload
```
### File Revisions
```python
# List revisions
revisions = file.GetRevisions()
for rev in revisions:
print(f"Revision ID: {rev['id']}")
print(f"Modified: {rev['modifiedDate']}")
```
### Copy File
```python
# Copy file
original = drive.CreateFile({'id': 'ORIGINAL_FILE_ID'})
copied = original.Copy()
copied['title'] = 'Copy of ' + original['title']
copied.Upload()
```
## Performance Tips
1. **Fetch only needed fields**: Use `fields` parameter
2. **Batch operations**: Group multiple API calls when possible
3. **Cache metadata**: Store frequently accessed metadata locally
4. **Use file IDs**: Faster than searching by title

View File

@@ -0,0 +1,84 @@
# Google Drive API Authentication Setup
## Quick Setup (Already Configured)
Authentication is already configured at:
- **Credentials**: `~/.gdrivelm/credentials.json`
- **Settings**: `~/.gdrivelm/settings.yaml`
- **Token**: `~/.gdrivelm/token.json` (auto-generated)
- **Virtual Env**: `~/Desktop/zPersonalProjects/gdrivelm/venv/` (update to your path)
## Authentication Code Pattern
```python
from pydrive2.auth import GoogleAuth
from pydrive2.drive import GoogleDrive
import os
def authenticate():
"""Authenticate with Google Drive API"""
settings_path = os.path.expanduser('~/.gdrivelm/settings.yaml')
token_path = os.path.expanduser('~/.gdrivelm/token.json')
gauth = GoogleAuth(settings_file=settings_path)
gauth.LoadCredentialsFile(token_path)
if gauth.credentials is None:
gauth.LocalWebserverAuth()
elif gauth.access_token_expired:
gauth.Refresh()
else:
gauth.Authorize()
gauth.SaveCredentialsFile(token_path)
return GoogleDrive(gauth)
```
## Settings Configuration
Located at `~/.gdrivelm/settings.yaml`:
```yaml
client_config_backend: file
client_config_file: /Users/wz/.gdrivelm/credentials.json
save_credentials: True
save_credentials_backend: file
save_credentials_file: /Users/wz/.gdrivelm/token.json
get_refresh_token: True
oauth_scope:
- https://www.googleapis.com/auth/drive
- https://www.googleapis.com/auth/drive.file
- https://www.googleapis.com/auth/drive.metadata.readonly
```
## OAuth Scopes
- `https://www.googleapis.com/auth/drive` - Full Drive access
- `https://www.googleapis.com/auth/drive.file` - Per-file access
- `https://www.googleapis.com/auth/drive.metadata.readonly` - Metadata reading
## First Run
On first use, the authentication will:
1. Open browser for OAuth consent
2. Save token to `~/.gdrivelm/token.json`
3. Auto-refresh on subsequent uses
## Python Environment
Always activate the virtual environment first:
```bash
cd ~/Desktop/zPersonalProjects/gdrivelm
source venv/bin/activate
```
## Installed Packages
- PyDrive2 v1.21.3
- google-api-python-client v2.187.0
- google-auth-oauthlib v1.2.3
- google-auth-httplib2 v0.2.1

View File

@@ -0,0 +1,135 @@
# Google Drive Search Query Syntax
## Query Operators
| Query | Description |
|-------|-------------|
| `title contains 'text'` | Files with title containing text |
| `fullText contains 'text'` | Search file content |
| `mimeType = 'type'` | Files of specific MIME type |
| `'parent_id' in parents` | Files in specific folder |
| `'root' in parents` | Files in root directory |
| `trashed = false` | Not in trash |
| `trashed = true` | In trash |
| `'me' in owners` | Files you own |
| `starred = true` | Starred files |
| `modifiedDate > 'date'` | Modified after date |
| `modifiedDate < 'date'` | Modified before date |
| `createdDate > 'date'` | Created after date |
## Common MIME Types
| Type | MIME Type |
|------|-----------|
| PDF | `application/pdf` |
| Text | `text/plain` |
| Word | `application/vnd.openxmlformats-officedocument.wordprocessingml.document` |
| Excel | `application/vnd.openxmlformats-officedocument.spreadsheetml.sheet` |
| PowerPoint | `application/vnd.openxmlformats-officedocument.presentationml.presentation` |
| Google Doc | `application/vnd.google-apps.document` |
| Google Sheet | `application/vnd.google-apps.spreadsheet` |
| Google Slides | `application/vnd.google-apps.presentation` |
| Folder | `application/vnd.google-apps.folder` |
| Image | `image/jpeg`, `image/png`, `image/gif` |
## Search Examples
### Basic Searches
```python
# Files containing "report" in title
file_list = drive.ListFile({'q': "title contains 'report'"}).GetList()
# PDF files only
file_list = drive.ListFile({'q': "mimeType = 'application/pdf'"}).GetList()
# Files in root, not trashed
file_list = drive.ListFile({'q': "'root' in parents and trashed = false"}).GetList()
```
### Complex Queries
```python
# Multiple conditions with AND
query = (
"title contains 'invoice' and "
"mimeType = 'application/pdf' and "
"trashed = false"
)
file_list = drive.ListFile({'q': query}).GetList()
# Files you own
file_list = drive.ListFile({'q': "'me' in owners"}).GetList()
# Modified after specific date
file_list = drive.ListFile({'q': "modifiedDate > '2024-01-01'"}).GetList()
# Folders only
file_list = drive.ListFile({'q': "mimeType = 'application/vnd.google-apps.folder'"}).GetList()
# Starred PDFs
query = "starred = true and mimeType = 'application/pdf'"
file_list = drive.ListFile({'q': query}).GetList()
```
### Content Search
```python
# Search file content (not just title)
file_list = drive.ListFile({'q': "fullText contains 'keyword'"}).GetList()
```
## Date Format
Use ISO 8601 format: `YYYY-MM-DD` or `YYYY-MM-DDTHH:MM:SS`
Examples:
- `'2024-01-01'`
- `'2024-12-31T23:59:59'`
## Combining Conditions
Use `and` and `or` operators:
```python
# Either condition
query = "title contains 'report' or title contains 'summary'"
# Both conditions
query = "title contains 'report' and mimeType = 'application/pdf'"
# Complex combination
query = (
"(title contains 'invoice' or title contains 'receipt') and "
"mimeType = 'application/pdf' and "
"modifiedDate > '2024-01-01'"
)
```
## Special Characters
Escape single quotes in search terms:
```python
# Searching for "O'Brien"
query = "title contains 'O\\'Brien'"
```
## Performance Tips
1. **Be specific**: More specific queries return faster
2. **Limit fields**: Use `fields` parameter to request only needed data
3. **Use pagination**: For large result sets, use `pageToken`
4. **Avoid fullText searches**: They are slower than title searches
## Iterating Results
```python
# Process all results
file_list = drive.ListFile({'q': "title contains 'report'"}).GetList()
for file in file_list:
print(f"{file['title']} (ID: {file['id']})")
print(f" Modified: {file['modifiedDate']}")
print(f" Size: {file.get('fileSize', 'N/A')} bytes")
```