创建包含Google Drive元数据的SQLite数据库
项目描述
google-drive-to-sqlite
创建包含Google Drive元数据的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
认证
:warning: 此应用程序尚未被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一起使用,有关详细信息,请参阅问题#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-to-sqlite 文件
要检索您Google Drive中文件的元数据,或对其中的文件夹或搜索进行搜索,请使用google-drive-to-sqlite 文件
命令。
默认情况下,此命令会将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.
google-drive-to-sqlite download FILE_ID
“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.
google-drive-to-sqlite export FORMAT FILE_ID
可以使用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.
google-drive-to-sqlite get URL
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
项目详情
下载文件
下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。
源代码分发
构建分发
google-drive-to-sqlite-0.4.tar.gz的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 84b0c058f8bf78e82a45020478ecc807c694302dd72766e5d38b79490400248c |
|
MD5 | 8c688d8c7ba2cda2eae4a434dd0d7070 |
|
BLAKE2b-256 | 79d0361f9d6d5812d89fd141185ce38e9e56f6ab657b476f72293354cfaa8752 |
google_drive_to_sqlite-0.4-py3-none-any.whl的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | c0269efd18dd3653fbdd96b0ec631734d758e296119c3c0dd176c3eb8290d04f |
|
MD5 | 2441ae1d70a95aa62f2a6a97ff93a994 |
|
BLAKE2b-256 | e98ca6cc5372980efdb39da57f186e9424f316eaa1cb7fd176d252b52d3bfad6 |