Example #1
0
    def _rec_download_files(self, node):
        """Helper function to download_files()"""
        for child in node.get_children():
            if child.get_mime() == "application/vnd.google-apps.folder":
                try:
                    os.mkdir(child.get_title())
                except OSError as e:
                    print("Error:", e)
                    print("Tried to create '%s'" % child.get_title())
                try:
                    os.chdir(child.get_title())
                except OSError as e:
                    print("Error:", e)
                    print("Couldn't chdir to '%s'. Skipping download of this directory." % child.get_title())
                    continue

                self._rec_download_files(child)
            elif "vnd.google-apps" in child.get_mime():
                # file is a "google doc" file and can't be downloaded. Http error 400
                continue
            else:
                try:
                    gdrive.download_file(self.get_service(), child.get_id(), child.get_title())
                except OSError as e:
                    print("Error:", e)
                    print("Failed downloading '%s'" % child.get_title())
        os.chdir("../")
 def test_download_file_using_pickled_file(self):
     """ make sure downloading files is working using the pickled tree """
     #os.chdir('../')
     with open('dir_tree.pickle', 'rb') as f:
         print("loading pickled tree...")
         t = pickle.load(f)
     #os.chdir('tests')
     nodes = t.get_root().get_children()
     node = next(iter(nodes)) # nodes are a Set(), so have to do this weird thing
     file_name = node.get_title()
     file_id = node.get_id()
     gdrive.download_file(self.service, file_id, file_name)
     self.assertTrue(file_name in os.listdir("."))