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.

66 lines
1.9 KiB
Python

#!/usr/bin/env python3
FOLDER = 'scanbot' # XXX Should be arg
import sys
if sys.version_info < (3, 3):
sys.stderr.write("Sorry, requires at least Python 3.3\n")
sys.exit(1)
import collections
import os
import pickle
import shlex
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
try:
with open(os.path.expanduser('~/.local/share/drive-download/seen.pickle'), 'rb') as f:
seen = pickle.load(f)
except FileNotFoundError:
seen = set()
gauth = GoogleAuth(settings_file=os.path.expanduser('~/.config/drive-download/settings.yaml'))
gauth.CommandLineAuth()
drive = GoogleDrive(gauth)
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']
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'])
with open(os.path.expanduser('~/.local/share/drive-download/seen.pickle'), 'wb') as f:
pickle.dump(seen, f)
else:
print("{} exists locally!".format(shlex.quote(local_filename)))
sys.exit(2)
# vim:tw=100: