警告
由于 Google 已弃用其使用的身份验证机制,此工具不再有效。请参阅issue #40。
创建一个包含 Google Drive 元数据的 SQLite 数据库
有关此项目的背景信息,请参阅我的博客上的Google Drive to SQLite。
如果您使用 Google Drive,特别是如果您与他人共享云端硬盘,很有可能您拥有数百甚至数千个您可能没有完全意识到的文件。
此工具可以下载这些文件的元数据 - 包括它们的名称、大小、文件夹、内容类型、权限、创建日期等 - 并将其存储在 SQLite 数据库中。
这使您可以使用 SQL 分析您的 Google Drive 内容,可以使用 Datasette 或 SQLite 命令行工具或任何其他 SQLite 数据库浏览软件。
使用 pip
安装此工具
pip install google-drive-to-sqlite
通过运行以下命令向 Google Drive 进行身份验证
google-drive-to-sqlite auth
现在使用以下命令创建一个 SQLite 数据库,其中包含所有您已加星标的文件的元数据
google-drive-to-sqlite files starred.db --starred
您可以使用 Datasette 浏览生成的数据库
$ pip install datasette
$ datasette starred.db
INFO: Started server process [24661]
INFO: Uvicorn running on http://127.0.0.1:8001
⚠️ 此应用尚未通过 Google 验证 - 在验证完成之前,您可能会发现无法进行身份验证。请参阅#10。您可以通过创建自己的 OAuth 客户端 ID 密钥,并使用
--google-client-id
和--google-client-secret
将其传递给auth
命令来解决此问题。
首先,使用 auth
命令向 Google Drive 进行身份验证
$ google-drive-to-sqlite auth
Visit the following URL to authenticate with Google Drive
https://#/o/oauth2/v2/auth?...
Then return here and paste in the resulting code:
Paste code here:
点击链接,使用 Google Drive 登录,然后将生成的代码复制并粘贴回工具中。
这会将身份验证令牌保存到当前目录中名为 auth.json
的文件中。
要为该文件指定不同的位置,请使用 --auth
选项
google-drive-to-sqlite auth --auth ~/google-drive-auth.json
auth
命令还提供了使用不同范围、Google 客户端 ID 和 Google 客户端密钥的选项。您可以使用这些选项创建自己的自定义身份验证令牌,以便与其他 Google API 一起使用,详情请参阅issue #5。
完整 --help
信息
Usage: google-drive-to-sqlite auth [OPTIONS]
Authenticate user and save credentials
Options:
-a, --auth FILE Path to save token, defaults to auth.json
--google-client-id TEXT Custom Google client ID
--google-client-secret TEXT Custom Google client secret
--scope TEXT Custom token scope
--help Show this message and exit.
要撤销存储在 auth.json
中的令牌,使其将来无法用于访问 Google Drive,请运行 revoke
命令
google-drive-to-sqlite revoke
或者如果您的令牌存储在其他位置
google-drive-to-sqlite revoke -a ~/google-drive-auth.json
您需要使用 auth
命令获取新的令牌才能继续使用此工具。
要检索 Google Drive 中的文件,或其中某个文件夹或搜索结果的元数据,请使用 google-drive-to-sqlite files
命令。
默认情况下,这将把您的 Google Drive 中每个文件的详细信息写入 SQLite 数据库
google-drive-to-sqlite files files.db
文件和文件夹将被写入数据库表,如果这些表尚不存在,将自动创建。数据库模式如下所示。
如果文件或文件夹已存在(基于匹配的 id
),则将被新数据替换。
除了写入 SQLite,您还可以使用 --json
输出为 JSON,或使用 --nl
输出为换行符分隔的 JSON
google-drive-to-sqlite files --nl
使用 --folder ID
检索指定文件夹及其子文件夹中的所有内容
google-drive-to-sqlite files files.db --folder 1E6Zg2X2bjjtPzVfX8YqdXZDCoB3AVA7i
使用 --q QUERY
使用自定义搜索查询
google-drive-to-sqlite files files.db -q "viewedByMeTime > '2022-01-01'"
以下快捷选项可帮助构建查询
--full-text TEXT
:搜索全文匹配搜索词的文件--starred
:您已加星标的文件和文件夹--trashed
:垃圾箱中的文件和文件夹--shared-with-me
:与您共享的文件和文件夹--apps
:Google Apps 文档、电子表格、演示文稿和绘图(相当于设置接下来的所有四个选项)--docs
:Google Apps 文档--sheets
:Google Apps 电子表格--presentations
:Google Apps 演示文稿--drawings
:Google Apps 绘图
您可以组合使用这些选项 - 例如,这将返回所有您已加星标且与您共享的文件
google-drive-to-sqlite files highlights.db \
--starred --shared-with-me
多个选项按 AND 逻辑处理,但 Google Apps 选项除外,它们按 OR 逻辑处理 - 因此以下命令将检索所有已加星标的电子表格和演示文稿
google-drive-to-sqlite files highlights.db \
--starred --sheets --presentations
您可以使用 --stop-after X
在检索 X 个文件后停止,这有助于快速尝试新的搜索模式并查看结果。
--import-json
和 --import-nl
选项主要用于测试和开发此工具。它们允许您重放之前使用 --json
或 --nl
获取的 JSON 或换行符分隔的 JSON,并使用它创建新的 SQLite 数据库,而无需进行任何出站 API 调用。
# Fetch all starred files from the API, write to starred.json
google-drive-to-sqlite files -q 'starred = true' --json > starred.json
# Now import that data into a new SQLite database file
google-drive-to-sqlite files starred.db --import-json starred.json
完整 --help
信息
Usage: google-drive-to-sqlite files [OPTIONS] [DATABASE]
Retrieve metadata for files in Google Drive, and write to a SQLite database or
output as JSON.
google-drive-to-sqlite files files.db
Use --json to output JSON, --nl for newline-delimited JSON:
google-drive-to-sqlite files files.db --json
Use a folder ID to recursively fetch every file in that folder and its sub-
folders:
google-drive-to-sqlite files files.db --folder
1E6Zg2X2bjjtPzVfX8YqdXZDCoB3AVA7i
Fetch files you have starred:
google-drive-to-sqlite files starred.db --starred
Options:
-a, --auth FILE Path to auth.json token file
--folder TEXT Files in this folder ID and its sub-folders
-q TEXT Files matching this query
--full-text TEXT Search for files with text match
--starred Files you have starred
--trashed Files in the trash
--shared-with-me Files that have been shared with you
--apps Google Apps docs, spreadsheets, presentations and
drawings
--docs Google Apps docs
--sheets Google Apps spreadsheets
--presentations Google Apps presentations
--drawings Google Apps drawings
--json Output JSON rather than write to DB
--nl Output newline-delimited JSON rather than write to DB
--stop-after INTEGER Stop paginating after X results
--import-json FILE Import from this JSON file instead of the API
--import-nl FILE Import from this newline-delimited JSON file
-v, --verbose Send verbose output to stderr
--help Show this message and exit.
download
命令可用于从 Google Drive 下载文件。
您需要一个或多个文件 ID,它们看起来像 0B32uDVNZfiEKLUtIT1gzYWN2NDI4SzVQYTFWWWxCWUtvVGNB
。
要下载文件,请运行此命令
google-drive-to-sqlite download 0B32uDVNZfiEKLUtIT1gzYWN2NDI4SzVQYTFWWWxCWUtvVGNB
这将检测文件的内容类型并将其用作扩展名 - 因此如果此文件是 JPEG,文件将下载为
0B32uDVNZfiEKLUtIT1gzYWN2NDI4SzVQYTFWWWxCWUtvVGNB.jpeg
您可以一次将多个文件 ID 传递给命令。
要隐藏进度条和文件名输出,请使用 -s
或 --silent
。
如果您正在下载单个文件,可以使用 -o
输出指定文件名和位置
google-drive-to-sqlite download 0B32uDVNZfiEKLUtIT1gzYWN2NDI4SzVQYTFWWWxCWUtvVGNB \
-o my-image.jpeg
使用 -o -
将文件内容写入标准输出
google-drive-to-sqlite download 0B32uDVNZfiEKLUtIT1gzYWN2NDI4SzVQYTFWWWxCWUtvVGNB \
-o - > my-image.jpeg
完整 --help
信息
Usage: google-drive-to-sqlite download [OPTIONS] FILE_IDS...
Download one or more files to disk, based on their file IDs.
The file content will be saved to a file with the name:
FILE_ID.ext
Where the extension is automatically picked based on the type of file.
If you are downloading a single file you can specify a filename with -o:
google-drive-to-sqlite download MY_FILE_ID -o myfile.txt
Options:
-a, --auth FILE Path to auth.json token file
-o, --output FILE File to write to, or - for standard output
-s, --silent Hide progress bar and filename
--help Show this message and exit.
export
命令可用于将 Google Docs 文档、电子表格和演示文稿导出为多种不同的格式。
您需要一个或多个文档 ID,它们看起来像 10BOHGDUYa7lBjUSo26YFCHTpgEmtXabdVFaopCTh1vU
。您可以通过查看 Google Docs 网站上您文档的 URL 找到这些 ID。
要将该文档导出为 PDF,请运行此命令
google-drive-to-sqlite export pdf 10BOHGDUYa7lBjUSo26YFCHTpgEmtXabdVFaopCTh1vU
文件将被导出为
10BOHGDUYa7lBjUSo26YFCHTpgEmtXabdVFaopCTh1vU-export.pdf
您可以一次将多个文件 ID 传递给命令。
对于 FORMAT
选项,您可以使用此页面上列出的任何 MIME 类型选项 - 例如,要导出为 Open Office 文档,您可以使用
google-drive-to-sqlite export \
application/vnd.oasis.opendocument.text \
10BOHGDUYa7lBjUSo26YFCHTpgEmtXabdVFaopCTh1vU
为方便起见,提供了以下常用文件格式的快捷方式
- Google Docs:
html
、txt
、rtf
、pdf
、doc
、zip
、epub
- Google Sheets:
xls
、pdf
、csv
、tsv
、zip
- 演示文稿:
ppt
、pdf
、txt
- 绘图:
jpeg
、png
、svg
zip
选项返回一个包含 HTML 的 zip 文件。txt
返回纯文本。其他选项应不言自明。
要隐藏文件名输出,请使用 -s
或 --silent
。
如果您正在导出单个文件,可以使用 -o
输出指定文件名和位置
google-drive-to-sqlite export pdf 10BOHGDUYa7lBjUSo26YFCHTpgEmtXabdVFaopCTh1vU \
-o my-document.pdf
使用 -o -
将文件内容写入标准输出
google-drive-to-sqlite export pdf 10BOHGDUYa7lBjUSo26YFCHTpgEmtXabdVFaopCTh1vU \
-o - > my-document.pdf
完整 --help
信息
Usage: google-drive-to-sqlite export [OPTIONS] FORMAT FILE_IDS...
Export one or more files to the specified format.
Usage:
google-drive-to-sqlite export pdf FILE_ID_1 FILE_ID_2
The file content will be saved to a file with the name:
FILE_ID-export.ext
Where the extension is based on the format you specified.
Available export formats can be seen here:
https://developers.google.com/drive/api/v3/ref-export-formats
Or you can use one of the following shortcuts:
- Google Docs: html, txt, rtf, pdf, doc, zip, epub
- Google Sheets: xls, pdf, csv, tsv, zip
- Presentations: ppt, pdf, txt
- Drawings: jpeg, png, svg
"zip" returns a zip file of HTML.
If you are exporting a single file you can specify a filename with -o:
google-drive-to-sqlite export zip MY_FILE_ID -o myfile.zip
Options:
-a, --auth FILE Path to auth.json token file
-o, --output FILE File to write to, or - for standard output
-s, --silent Hide progress bar and filename
--help Show this message and exit.
get
命令使用从 auth.json
文件派生的凭据向指定的 URL 发出已认证的请求。
例如
$ google-drive-to-sqlite get 'https://www.googleapis.com/drive/v3/about?fields=*'
{
"kind": "drive#about",
"user": {
"kind": "drive#user",
"displayName": "Simon Willison",
# ...
如果您正在获取的资源支持分页,可以使用 --paginate key
对指定键中的所有行进行分页。例如,以下 API 具有一个 nextPageToken
键和一个 files
列表,表明它支持分页
$ google-drive-to-sqlite get https://www.googleapis.com/drive/v3/files
{
"kind": "drive#fileList",
"nextPageToken": "~!!~AI9...wogHHYlc=",
"incompleteSearch": false,
"files": [
{
"kind": "drive#file",
"id": "1YEsITp_X8PtDUJWHGM0osT-TXAU1nr0e7RSWRM2Jpyg",
"name": "Title of a spreadsheet",
"mimeType": "application/vnd.google-apps.spreadsheet"
},
要对 files
列表中的所有内容进行分页,您可以像这样使用 --paginate files
$ google-drive-to-sqlite get https://www.googleapis.com/drive/v3/files --paginate files
[
{
"kind": "drive#file",
"id": "1YEsITp_X8PtDUJWHGM0osT-TXAU1nr0e7RSWRM2Jpyg",
"name": "Title of a spreadsheet",
"mimeType": "application/vnd.google-apps.spreadsheet"
},
# ...
添加 --nl
以将分页数据流式传输为换行符分隔的 JSON
$ google-drive-to-sqlite get https://www.googleapis.com/drive/v3/files --paginate files --nl
{"kind": "drive#file", "id": "1YEsITp_X8PtDUJWHGM0osT-TXAU1nr0e7RSWRM2Jpyg", "name": "Title of a spreadsheet", "mimeType": "application/vnd.google-apps.spreadsheet"}
{"kind": "drive#file", "id": "1E6Zg2X2bjjtPzVfX8YqdXZDCoB3AVA7i", "name": "Subfolder", "mimeType": "application/vnd.google-apps.folder"}
添加 --stop-after 5
在获取 5 条记录后停止 - 这对于测试很有用。
完整 --help
信息
Usage: google-drive-to-sqlite get [OPTIONS] URL
Make an authenticated HTTP GET to the specified URL
Options:
-a, --auth FILE Path to auth.json token file
--paginate TEXT Paginate through all results in this key
--nl Output paginated data as newline-delimited JSON
--stop-after INTEGER Stop paginating after X results
-v, --verbose Send verbose output to stderr
--help Show this message and exit.
此工具创建的数据库具有以下模式
CREATE TABLE [drive_users] (
[permissionId] TEXT PRIMARY KEY,
[kind] TEXT,
[displayName] TEXT,
[photoLink] TEXT,
[me] INTEGER,
[emailAddress] TEXT
);
CREATE TABLE [drive_folders] (
[id] TEXT PRIMARY KEY,
[_parent] TEXT,
[_owner] TEXT,
[lastModifyingUser] TEXT,
[kind] TEXT,
[name] TEXT,
[mimeType] TEXT,
[starred] INTEGER,
[trashed] INTEGER,
[explicitlyTrashed] INTEGER,
[parents] TEXT,
[spaces] TEXT,
[version] TEXT,
[webViewLink] TEXT,
[iconLink] TEXT,
[hasThumbnail] INTEGER,
[thumbnailVersion] TEXT,
[viewedByMe] INTEGER,
[createdTime] TEXT,
[modifiedTime] TEXT,
[modifiedByMe] INTEGER,
[shared] INTEGER,
[ownedByMe] INTEGER,
[viewersCanCopyContent] INTEGER,
[copyRequiresWriterPermission] INTEGER,
[writersCanShare] INTEGER,
[folderColorRgb] TEXT,
[quotaBytesUsed] TEXT,
[isAppAuthorized] INTEGER,
[linkShareMetadata] TEXT,
FOREIGN KEY([_parent]) REFERENCES [drive_folders]([id]),
FOREIGN KEY([_owner]) REFERENCES [drive_users]([permissionId]),
FOREIGN KEY([lastModifyingUser]) REFERENCES [drive_users]([permissionId])
);
CREATE TABLE [drive_files] (
[id] TEXT PRIMARY KEY,
[_parent] TEXT,
[_owner] TEXT,
[lastModifyingUser] TEXT,
[kind] TEXT,
[name] TEXT,
[mimeType] TEXT,
[starred] INTEGER,
[trashed] INTEGER,
[explicitlyTrashed] INTEGER,
[parents] TEXT,
[spaces] TEXT,
[version] TEXT,
[webViewLink] TEXT,
[iconLink] TEXT,
[hasThumbnail] INTEGER,
[thumbnailVersion] TEXT,
[viewedByMe] INTEGER,
[createdTime] TEXT,
[modifiedTime] TEXT,
[modifiedByMe] INTEGER,
[shared] INTEGER,
[ownedByMe] INTEGER,
[viewersCanCopyContent] INTEGER,
[copyRequiresWriterPermission] INTEGER,
[writersCanShare] INTEGER,
[quotaBytesUsed] TEXT,
[isAppAuthorized] INTEGER,
[linkShareMetadata] TEXT,
FOREIGN KEY([_parent]) REFERENCES [drive_folders]([id]),
FOREIGN KEY([_owner]) REFERENCES [drive_users]([permissionId]),
FOREIGN KEY([lastModifyingUser]) REFERENCES [drive_users]([permissionId])
);
您可以使用以下 URL 为已知文件 ID 构建缩略图
https://drive.google.com/thumbnail?sz=w800-h800&id=FILE_ID
已登录 Google Drive 且具有查看文件权限的用户将被重定向到该文件的缩略图版本。您可以调整 w800
和 h800
参数以请求不同的缩略图尺寸。
此工具请求访问您的 Google Drive 账户,以便检索您在那里的文件的元数据。它还提供了一个可以下载这些文件内容的功能。
用于访问您账户的凭据存储在您计算机上的 auth.json
文件中。从 Google Drive 检索到的元数据和内容也仅存储在您自己的个人计算机上。
此工具的开发者在任何时候都不会获取您的任何数据。
要对此工具做出贡献,请先检出代码。然后创建一个新的虚拟环境
cd google-drive-to-sqlite
python -m venv venv
source venv/bin/activate
或者如果您正在使用 pipenv
pipenv shell
现在安装依赖项和测试依赖项
pip install -e '.[test]'
要运行测试
pytest