Package: getfilelistpy
getfilelistpyOverviewThis is a python library to retrieve the file list with the folder tree from the specific folder of own Google Drive and shared Drives.DescriptionWhen I create applications for using Google Drive, I often retrieve a file list from a folder in the application. So far, I had created the script for retrieving a file list from a folder for each application. Recently, I thought that if there is the script for retrieving the file list with the folder tree from the folder of Google Drive as a library, it will be useful for me and other users. So I created this.FeaturesThis library retrieves all files from a folder in Google Drive.All files include the folder structure in Google Drive.Only folder tree can be also retrieved.Install$ pip install getfilelistpyYou can also check this library athttps://pypi.org/project/getfilelistpy/.MethodMethodExplanationGetFolderTree(object)Retrieve only folder structure from a folderGetFileList(object)Retrieve file list with folder structure from a folderUsageThere are 3 patterns for using this library.1. Use API keyThis is a sample script using API key. When you want to retrieve the API key, please do the following flow.Login to Google.Access tohttps://console.cloud.google.com/?hl=en.Click select project at the right side of "Google Cloud Platform" of upper left of window.Click "NEW PROJECT"Input "Project Name".Click "CREATE".Open the created project.Click "Enable APIs and get credentials like keys".Click "Library" at left side.Input "Drive API" in "Search for APIs & Services".Click "Google Drive API".Click "ENABLE".Back tohttps://console.cloud.google.com/?hl=en.Click "Enable APIs and get credentials like keys".Click "Credentials" at left side.Click "Create credentials" and select API key.Copy the API key. You can use this API key.Sample scriptfromgetfilelistpyimportgetfilelistresource={"api_key":"#####","id":"### Folder ID ###","fields":"files(name,id)",}res=getfilelist.GetFileList(resource)# or r = getfilelist.GetFolderTree(resource)print(res)NoteWhen you want to retrieve the file list from the folder using API key, the folder is required to be shared.You can modify the property offields. When this is not used, the default fields are used.2. Use OAuth2Document of OAuth2 ishere.Sample script 1In this sample script foroauth2client.fromhttplib2importHttpfromoauth2clientimportfile,client,toolsfromgetfilelistpyimportgetfilelistSCOPES='https://www.googleapis.com/auth/drive.metadata.readonly'store=file.Storage('token.json')creds=store.get()ifnotcredsorcreds.invalid:flow=client.flow_from_clientsecrets('client_secret.json',SCOPES)creds=tools.run_flow(flow,store)resource={"oauth2":creds.authorize(Http()),"id":"### Folder ID ###","fields":"files(name,id)",}res=getfilelist.GetFileList(resource)# or r = getfilelist.GetFolderTree(resource)print(res)Sample script 2In this sample script forgoogle_auth_oauthlib, the process of OAuth2 usesthe quickstart of Google. Please check this.importpickleimportos.pathfromgoogle_auth_oauthlib.flowimportInstalledAppFlowfromgoogle.auth.transport.requestsimportRequestfromgetfilelistpyimportgetfilelistSCOPES='https://www.googleapis.com/auth/drive.metadata.readonly'creds=NonecreFile='token.pickle'ifos.path.exists(creFile):withopen(creFile,'rb')astoken:creds=pickle.load(token)ifnotcredsornotcreds.valid:ifcredsandcreds.expiredandcreds.refresh_token:creds.refresh(Request())else:flow=InstalledAppFlow.from_client_secrets_file('client_secret.json',SCOPES)creds=flow.run_local_server()withopen(creFile,'wb')astoken:pickle.dump(creds,token)resource={"oauth2":creds,"id":"### Folder ID ###","fields":"files(name,id)",}res=getfilelist.GetFileList(resource)# or r = getfilelist.GetFolderTree(resource)print(res)NoteHere, as a sample, the script of the authorization uses the script ofquickstart.You can modify the property offields. When this is not used, the default fields are used.3. Use Service accountDocument of Service account ishere.Sample scriptfromgoogle.oauth2importservice_accountfromgetfilelistpyimportgetfilelistSCOPES=['https://www.googleapis.com/auth/drive']SERVICE_ACCOUNT_FILE='service-account-credentials.json'credentials=service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE,scopes=SCOPES)resource={"service_account":credentials,"id":"### Folder ID ###","fields":"files(name,id)",}res=getfilelist.GetFileList(resource)# or r = getfilelist.GetFolderTree(resource)print(res)NoteYou can modify the property offields. When this is not used, the default fields are used.ValuesAs a sample, when the values are retrieved from above structure, the results ofGetFolderTree()becomes as follows.Values retrieved by GetFolderTree()res=getfilelist.GetFolderTree(resource)print(res){"id":[["folderIdOfsampleFolder1"],["folderIdOfsampleFolder1","folderIdOfsampleFolder_2a"],["folderIdOfsampleFolder1","folderIdOfsampleFolder_2b"],["folderIdOfsampleFolder1","folderIdOfsampleFolder_2a","folderIdOfsampleFolder_2a_3a"],["folderIdOfsampleFolder1","folderIdOfsampleFolder_2b","folderIdOfsampleFolder_2b_3a"],["folderIdOfsampleFolder1","folderIdOfsampleFolder_2b","folderIdOfsampleFolder_2b_3b"],["folderIdOfsampleFolder1","folderIdOfsampleFolder_2b","folderIdOfsampleFolder_2b_3b","folderIdOfsampleFolder_2b_3b_4a"]],"names":["sampleFolder1","sampleFolder_2a","sampleFolder_2b","sampleFolder_2a_3a","sampleFolder_2b_3a","sampleFolder_2b_3b","sampleFolder_2b_3b_4a"],"folders":["folderIdOfsampleFolder1","folderIdOfsampleFolder_2a","folderIdOfsampleFolder_2b","folderIdOfsampleFolder_2a_3a","folderIdOfsampleFolder_2b_3a","folderIdOfsampleFolder_2b_3b","folderIdOfsampleFolder_2b_3b_4a"]}Values retrieved by Do()res=getfilelist.GetFileList(resource)print(res){"searchedFolder":{"id":"###","name":"sampleFolder1","mimeType":"application/vnd.google-apps.folder","parents":["###"],"createdTime":"2000-01-01T01:23:45.000Z","modifiedTime":"2000-01-01T01:23:45.000Z","webViewLink":"https://drive.google.com/drive/folders/###","owners":[{"displayName":"###","permissionId":"###","emailAddress":"###"}],"shared":true},"folderTree":{"id":[["folderIdOfsampleFolder1"],["folderIdOfsampleFolder1","folderIdOfsampleFolder_2a"],["folderIdOfsampleFolder1","folderIdOfsampleFolder_2b"],["folderIdOfsampleFolder1","folderIdOfsampleFolder_2a","folderIdOfsampleFolder_2a_3a"],["folderIdOfsampleFolder1","folderIdOfsampleFolder_2b","folderIdOfsampleFolder_2b_3a"],["folderIdOfsampleFolder1","folderIdOfsampleFolder_2b","folderIdOfsampleFolder_2b_3b"],["folderIdOfsampleFolder1","folderIdOfsampleFolder_2b","folderIdOfsampleFolder_2b_3b","folderIdOfsampleFolder_2b_3b_4a"]],"names":["sampleFolder1","sampleFolder_2a","sampleFolder_2b","sampleFolder_2a_3a","sampleFolder_2b_3a","sampleFolder_2b_3b","sampleFolder_2b_3b_4a"],"folders":["folderIdOfsampleFolder1","folderIdOfsampleFolder_2a","folderIdOfsampleFolder_2b","folderIdOfsampleFolder_2a_3a","folderIdOfsampleFolder_2b_3a","folderIdOfsampleFolder_2b_3b","folderIdOfsampleFolder_2b_3b_4a"]},"fileList":[{"folderTree":["folderIdOfsampleFolder1"],"files":[{"name":"Spreadsheet1","mimeType":"application/vnd.google-apps.spreadsheet"}]},{"folderTree":["folderIdOfsampleFolder1","folderIdOfsampleFolder_2a"],"files":[{"name":"Spreadsheet2","mimeType":"application/vnd.google-apps.spreadsheet"}]},{"folderTree":["folderIdOfsampleFolder1","folderIdOfsampleFolder_2b"],"files":[{"name":"Spreadsheet4","mimeType":"application/vnd.google-apps.spreadsheet"}]},{"folderTree":["folderIdOfsampleFolder1","folderIdOfsampleFolder_2a","folderIdOfsampleFolder_2a_3a"],"files":null},{"folderTree":["folderIdOfsampleFolder1","folderIdOfsampleFolder_2b","folderIdOfsampleFolder_2b_3a"],"files":[{"name":"Spreadsheet3","mimeType":"application/vnd.google-apps.spreadsheet"}]},{"folderTree":["folderIdOfsampleFolder1","folderIdOfsampleFolder_2b","folderIdOfsampleFolder_2b_3b"],"files":null},{"folderTree":["folderIdOfsampleFolder1","folderIdOfsampleFolder_2b","folderIdOfsampleFolder_2b_3b","folderIdOfsampleFolder_2b_3b_4a"],"files":[{"name":"Document1","mimeType":"application/vnd.google-apps.document"},{"name":"image1.png","mimeType":"image/png"},{"name":"Slides1","mimeType":"application/vnd.google-apps.presentation"},{"name":"Spreadsheet5","mimeType":"application/vnd.google-apps.spreadsheet"},{"name":"StandaloneProject1","mimeType":"application/vnd.google-apps.script"},{"name":"Test1.txt","mimeType":"text/plain"}]}],"totalNumberOfFiles":10,"totalNumberOfFolders":7}For other languagesAs the libraries "GetFileList" for other languages, there are following libraries.Golang:https://github.com/tanaikech/go-getfilelistGoogle Apps Script:https://github.com/tanaikech/FilesAppJavascript:https://github.com/tanaikech/GetFileList_jsNode.js:https://github.com/tanaikech/node-getfilelistPython:https://github.com/tanaikech/getfilelistpyLicenceMITAuthorTanaikeIf you have any questions and commissions for me, feel free to tell me.Update Historyv1.0.0 (November 17, 2018)Initial release.v1.0.3 (July 16, 2019)Markdown format was used to the readme file athttps://pypi.org/project/getfilelistpy/.v1.0.4 (August 23, 2019)For OAuth2,oauth2clientandgoogle_auth_oauthlibgot to be able to be used. About the sample script forgoogle_auth_oauthlib, please seethis.v1.0.5 (May 15, 2020)Shared drive got to be able to be used. The file list can be retrieved from both your Google Drive and the shared drive.For example, when the folder ID in the shared Drive is usedidofresource, you can retrieve the file list from the folder in the shared Drive.v1.0.6 (June 1, 2020)When the file is retrieved from the shared drive, the parameter was not completed. This bug was removed.TOP