示例#1
0
    def _get_files_in_folder(self, folder_id):
        """ 
        Gets the metadata for all the children inside a Google Drive folder
        
        :param: folder_id = the Google Drive id of a folder

        :return: list of dicts that have all the file metadata
        """
        children_ids = gdrive.get_children(self.get_service(), folder_id)
        total = 0
        files_list = []
        for child_id in children_ids:
            metadata = gdrive.get_file_metadata(self.get_service(), child_id)
            if metadata["labels"]["trashed"]:
                continue  # the file was marked as trash, don't download

            # add file's info as tuple to the returned list
            title = metadata["title"]
            mime = metadata["mimeType"]
            if len(metadata["parents"]) != 0:
                # parent id is in a dict inside a list
                parents = metadata["parents"][0]
                if parents["isRoot"]:
                    parent_id = "root"
                else:
                    parent_id = parents["id"]
            else:
                # some files have an empty parents list
                parent_id = None

            files_list.append(
                {"id": child_id, "mime": mime, "title": title, "parent_id": parent_id, "metadata": metadata}
            )

            # logging to stdout
            total += 1
            print("Retrieved file " + str(total) + ". " + title + " (" + mime + ")")

        return files_list
 def test_get_file_metadata_on_nested_file(self):
     """ test a file that is further nested, make sure it's parent is not root"""
     data = gdrive.get_file_metadata(self.service, self.nested_file_id)
     parent_is_root = data['parents'][0]['isRoot']
     self.assertFalse(parent_is_root)
 def test_nested_file_title_is_correct(self):
     """ make sure I got the nsted file id and title correct """
     data = gdrive.get_file_metadata(self.service, self.nested_file_id)
     self.assertEqual(data['title'], self.nested_file_title)
 def test_get_file_metadata_on_root_file(self):
     """ test getting data for a file that belongs directly in root folder"""
     data = gdrive.get_file_metadata(self.service, self.root_file_id)
     parent_is_root = data['parents'][0]['isRoot']
     self.assertTrue(parent_is_root)