Ejemplo n.º 1
0
def sync(service, drive_md, local_file):
    logger.info('Sync ' + local_file)
    exists = os.path.isfile(local_file)
    try:
        local_tf = int(os.path.getmtime(local_file))
    except OSError:
        local_tf = None

    if drive_md is not None:
        drive_tf = int(rfc3339.tf_from_timestamp(drive_md['modifiedDate']))
        if local_tf == drive_tf:
            logger.info('Up-to-date')
            return
        if local_tf is None or drive_tf > local_tf:
            body = service.files().get_media(
                    fileId=drive_md['id']).execute()
            with open(local_file, 'wb') as f:
                f.write(body)
            logger.info('Download ok')
            return

    if local_tf is None:
        return
    upload_md = {
        'mimeType': MIME_TYPE,
        'modifiedDate': rfc3339.timestamp_from_tf(local_tf),
    }
    f = MediaFileUpload(local_file, mimetype=MIME_TYPE)
    if drive_md is None:
        ids = service.files().generateIds(space='drive', maxResults=1).execute()
        upload_md['id'] = ids['ids'][0]
        upload_md['ownedByMe'] = True
        upload_md['originalFilename'] = local_file
        upload_md['title'] = local_file
        r = service.files().insert(body=upload_md, media_body=f)
        logger.info('Insert ok')
    else:
        upload_md['id'] = drive_md['id']
        r = service.files().update(
            fileId=drive_md['id'],
            body=upload_md,
            media_body=f,
            modifiedDateBehavior='fromBody')
        logger.info('Update ok')
    resp = r.execute()
Ejemplo n.º 2
0
 def _datetime_from_rfc3339(rfc3339):
     tf = tf_from_timestamp(rfc3339)
     return datetime.fromtimestamp(tf)