You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.4 KiB
Python

#!/usr/bin/env python3
# Download unseen files from a folder in Google Drive
import sys
if sys.version_info < (3, 3):
sys.stderr.write("Sorry, requires at least Python 3.3\n")
sys.exit(1)
import argparse
import collections
6 years ago
import os
import pickle
import shlex
6 years ago
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
def read_seen():
global seen
try:
with open(os.path.expanduser('~/.local/share/drive-download/seen.pickle'), 'rb') as f:
seen = pickle.load(f)
except FileNotFoundError:
seen = set()
6 years ago
def save_seen():
global seen
with open(os.path.expanduser('~/.local/share/drive-download/seen.pickle'), 'wb') as f:
pickle.dump(seen, f)
6 years ago
def folder_id(folder_name):
query = "title = '%s'" % folder_name
file1 = drive.ListFile({'q': query}).GetList()[0]
assert file1['mimeType'] == 'application/vnd.google-apps.folder'
return file1['id']
def main():
global seen, drive
parser = argparse.ArgumentParser(
description='Download unseen files from a folder in Google Drive')
parser.add_argument('folder', type=str, help='folder to download from')
args = parser.parse_args()
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(args.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: