|
|
|
@ -1,5 +1,16 @@ |
|
|
|
from gapi.api import API |
|
|
|
import re |
|
|
|
APPLICATION_NAME = "Google Calendar API Python" |
|
|
|
def is_folder(element): |
|
|
|
return element['mimeType'] == 'application/vnd.google-apps.folder' |
|
|
|
def split_path(path,rev=False): |
|
|
|
|
|
|
|
s = re.findall(r'(?:^/|[^/\x00]+)',path) |
|
|
|
if rev: |
|
|
|
return s[::-1] |
|
|
|
else: |
|
|
|
return s |
|
|
|
|
|
|
|
class drive_api(API): |
|
|
|
def __init__( |
|
|
|
self, |
|
|
|
@ -10,8 +21,58 @@ class drive_api(API): |
|
|
|
version = 'v3', |
|
|
|
): |
|
|
|
super().__init__('drive',scopes,app_name,client_secret_file,credentials_dir,version) |
|
|
|
def get_files(self): |
|
|
|
pass |
|
|
|
self.filesystem = {'/':{}} |
|
|
|
root_meta = self.service.files().get(fileId='root').execute() |
|
|
|
del root_meta['kind'] |
|
|
|
self.filesystem['/'][0] = root_meta |
|
|
|
|
|
|
|
def __inner_path_func__(self,path,parent): |
|
|
|
print(path) |
|
|
|
if len(path) > 1: |
|
|
|
n = path.pop() |
|
|
|
try: |
|
|
|
new_parent = parent[n] |
|
|
|
except KeyError: |
|
|
|
self.fill_in(parent) |
|
|
|
new_parent = parent[n] |
|
|
|
return self.__inner_path_func__(path,new_parent) |
|
|
|
else: |
|
|
|
try: |
|
|
|
return parent[path[0]] |
|
|
|
except KeyError: |
|
|
|
self.fill_in(parent) |
|
|
|
try: |
|
|
|
return parent[path[0]] |
|
|
|
except KeyError: |
|
|
|
raise FileNotFoundError() |
|
|
|
|
|
|
|
def get_file_by_path(self,path): |
|
|
|
path = split_path(path,True) |
|
|
|
return self.__inner_path_func__(path,self.filesystem) |
|
|
|
|
|
|
|
# finds all files and folders from a parent |
|
|
|
def fill_in(self,parent): |
|
|
|
parent_id = parent[0]['id'] |
|
|
|
child_list = self.list_children(parent_id) |
|
|
|
for child in child_list: |
|
|
|
del child['kind'] |
|
|
|
name = child.pop('name') |
|
|
|
if is_folder(child): |
|
|
|
parent[name] = {} |
|
|
|
parent[name][0] = child |
|
|
|
else: |
|
|
|
parent[name] = child |
|
|
|
|
|
|
|
def list_children(self,parent_id): |
|
|
|
page_token = None |
|
|
|
first = True |
|
|
|
while page_token or first: |
|
|
|
first = False |
|
|
|
response = self.service.files().list(q = "'{}' in parents".format(parent_id),pageToken=page_token).execute() |
|
|
|
page_token = response.get('nextPageToken',None) |
|
|
|
for file in response['files']: |
|
|
|
yield file |
|
|
|
|
|
|
|
if __name__ == "__main__": |
|
|
|
my_api = drive_api(APPLICATION_NAME,r'..\test\drive\client_secret.json',r'..\test\drive') |
|
|
|
service = my_api.service |
|
|
|
service = my_api.service |