From 69f8a6470b2e52bb78bc921af5ee3fc5a600771e Mon Sep 17 00:00:00 2001 From: neingeist Date: Sat, 21 Jul 2018 15:36:36 +0200 Subject: [PATCH] move main code to a function --- drive-download | 70 +++++++++++++++++++++++++++----------------------- 1 file changed, 38 insertions(+), 32 deletions(-) diff --git a/drive-download b/drive-download index e8726c8..6492ee7 100755 --- a/drive-download +++ b/drive-download @@ -37,37 +37,43 @@ def folder_id(folder_name): return file1['id'] -read_seen() - -gauth = GoogleAuth(settings_file=os.path.expanduser('~/.config/drive-download/settings.yaml')) -gauth.CommandLineAuth() -drive = GoogleDrive(gauth) - -file_query = "'%s' in parents and trashed=false" % folder_id(FOLDER) -file_list = drive.ListFile({'q': file_query}).GetList() - -file_names = [f['title'] for f in file_list] -file_names_dupes = [f for f, count in collections.Counter(file_names).items() if count > 1] -if len(file_names_dupes) > 0: - print('Duplicate filenames on Drive!') - for fn in sorted(file_names_dupes): - print(fn) - sys.exit(1) - -for file1 in file_list: - if file1['mimeType'] == 'application/vnd.google-apps.folder': - continue - if file1['id'] in seen: # XXX could update the file if it needs updating - continue - - local_filename = file1['title'] - print(shlex.quote(local_filename)) - if not os.path.exists(local_filename): - file1.GetContentFile(local_filename) - seen.add(file1['id']) - save_seen() - else: - print("{} exists locally!".format(shlex.quote(local_filename))) - sys.exit(2) +def main(): + global seen, drive + + read_seen() + + gauth = GoogleAuth(settings_file=os.path.expanduser('~/.config/drive-download/settings.yaml')) + gauth.CommandLineAuth() + drive = GoogleDrive(gauth) + + file_query = "'%s' in parents and trashed=false" % folder_id(FOLDER) + file_list = drive.ListFile({'q': file_query}).GetList() + + file_names = [f['title'] for f in file_list] + file_names_dupes = [f for f, count in collections.Counter(file_names).items() if count > 1] + if len(file_names_dupes) > 0: + print('Duplicate filenames on Drive!') + for fn in sorted(file_names_dupes): + print(shlex.quote(fn)) + sys.exit(1) + + for file1 in file_list: + if file1['mimeType'] == 'application/vnd.google-apps.folder': + continue + if file1['id'] in seen: # XXX could update the file if it needs updating + continue + + local_filename = file1['title'] + print(shlex.quote(local_filename)) + if not os.path.exists(local_filename): + file1.GetContentFile(local_filename) + seen.add(file1['id']) + save_seen() + else: + print("{} exists locally!".format(shlex.quote(local_filename))) + sys.exit(2) + + +main() # vim:tw=100: