extract functions to read/save seen

master
neingeist 6 years ago
parent 1c55509b0c
commit 8a2203ade7

@ -15,16 +15,19 @@ from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive from pydrive.drive import GoogleDrive
def read_seen():
global seen
try: try:
with open(os.path.expanduser('~/.local/share/drive-download/seen.pickle'), 'rb') as f: with open(os.path.expanduser('~/.local/share/drive-download/seen.pickle'), 'rb') as f:
seen = pickle.load(f) seen = pickle.load(f)
except FileNotFoundError: except FileNotFoundError:
seen = set() seen = set()
gauth = GoogleAuth(settings_file=os.path.expanduser('~/.config/drive-download/settings.yaml'))
gauth.CommandLineAuth()
drive = GoogleDrive(gauth) def save_seen():
global seen
with open(os.path.expanduser('~/.local/share/drive-download/seen.pickle'), 'wb') as f:
pickle.dump(seen, f)
def folder_id(folder_name): def folder_id(folder_name):
@ -34,6 +37,12 @@ def folder_id(folder_name):
return file1['id'] 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_query = "'%s' in parents and trashed=false" % folder_id(FOLDER)
file_list = drive.ListFile({'q': file_query}).GetList() file_list = drive.ListFile({'q': file_query}).GetList()
@ -56,8 +65,7 @@ for file1 in file_list:
if not os.path.exists(local_filename): if not os.path.exists(local_filename):
file1.GetContentFile(local_filename) file1.GetContentFile(local_filename)
seen.add(file1['id']) seen.add(file1['id'])
with open(os.path.expanduser('~/.local/share/drive-download/seen.pickle'), 'wb') as f: save_seen()
pickle.dump(seen, f)
else: else:
print("{} exists locally!".format(shlex.quote(local_filename))) print("{} exists locally!".format(shlex.quote(local_filename)))
sys.exit(2) sys.exit(2)

Loading…
Cancel
Save