예제 #1
0
def upload_file(parent_id, file_path, title, description, mimetype):
    """Upload a file to a google drive folder.

    Args:
        parent_id (str):
            Identifier for the drive folder to upload to.
        file_path (str):
            Local file path to the file to upload.
        title (str):
            Title for the uploaded document.
        description (str):
            A description of the file to upload.
        mimetype (str):
            Mimetype of the uploaded content.

    Returns:
        file_metadata (dict):
            A Google Apps File resource with metadata about the uploaded file.
    """

    service = Service(settings.GOOGLE_P12_PATH,
                      settings.GOOGLE_SERVICE_EMAIL)
    file_metadata = service.insert_file(
        title,
        description,
        parent_id,
        mimetype,
        mimetype,
        file_path
    )
    return file_metadata
예제 #2
0
def upload_file(parent_id, file_path, title, description, mimetype):
    """Upload a file to a google drive folder.

    Args:
        parent_id (str):
            Identifier for the drive folder to upload to.
        file_path (str):
            Local file path to the file to upload.
        title (str):
            Title for the uploaded document.
        description (str):
            A description of the file to upload.
        mimetype (str):
            Mimetype of the uploaded content.

    Returns:
        file_metadata (dict):
            A Google Apps File resource with metadata about the uploaded file.
    """

    service = Service(settings.GOOGLE_P12_PATH,
                      settings.GOOGLE_SERVICE_EMAIL)
    file_metadata = service.insert_file(
        title,
        description,
        parent_id,
        mimetype,
        file_path
    )
    return file_metadata
예제 #3
0
def autoadjust_photos(project_data, prerequisites):
    """Resize all images in a google drive directory."""
    task_data = {}
    parent_folder_id = project_data['project_folder_id']

    # Create a directory to output the photos
    output_folder = create_folder_with_permissions(
        parent_folder_id,
        'Processed Photos',
        permissions=[write_with_link_permission],
    )
    task_data['processed_photo_folder'] = output_folder['id']

    # List the existing photos
    raw_photo_folder_id = (
        prerequisites.get('document_creation').get('raw_photo_folder'))
    service = Service(settings.GOOGLE_P12_PATH, settings.GOOGLE_SERVICE_EMAIL)
    photos_metadata = service.list_folder(raw_photo_folder_id)

    # Iterate over the input photos and process them.
    task_data['photos_for_caption'] = []
    for photo_metadata in photos_metadata:
        photo, title, mimetype = download_file(photo_metadata)
        adjusted_photo_tmpfile = adjust_photo(photo)
        upload = upload_file(task_data['processed_photo_folder'],
                             adjusted_photo_tmpfile.name, title, 'image',
                             mimetype)
        os.unlink(adjusted_photo_tmpfile.name)
        embed_link = upload['webContentLink'].replace('&export=download', '')
        task_data['photos_for_caption'].append(embed_link)
    return task_data
예제 #4
0
def _upload_csv_to_google(spreadsheet_name, file):
    service = Service(settings.GOOGLE_P12_PATH, settings.GOOGLE_SERVICE_EMAIL)
    sheet = service.insert_file(
        spreadsheet_name, '',
        settings.ORCHESTRA_TODO_LIST_TEMPLATE_EXPORT_GDRIVE_FOLDER, 'text/csv',
        'application/vnd.google-apps.spreadsheet', file.name)
    service.add_permission(sheet['id'], write_with_link_permission)
    return sheet['alternateLink']
예제 #5
0
def create_folder_with_permissions(parent_id, folder_name, permissions=None):
    """
    Create drive folder in the specified location with given permissions.
    """
    service = Service(settings.GOOGLE_P12_PATH, settings.GOOGLE_SERVICE_EMAIL)
    folder = service.insert_folder(folder_name, parent_id)
    if folder is None:
        raise GoogleDriveError('Could not create a folder')

    permissions = permissions or []
    for permission in permissions:
        service.add_permission(folder.get('id'), permission)

    return folder
예제 #6
0
def create_folder_with_permissions(parent_id, folder_name, permissions=None):
    """
    Create drive folder in the specified location with given permissions.
    """
    service = Service(settings.GOOGLE_P12_PATH, settings.GOOGLE_SERVICE_EMAIL)
    folder = service.insert_folder(folder_name, parent_id)
    if folder is None:
        raise GoogleDriveError("Could not create a folder")

    permissions = permissions or []
    for permission in permissions:
        service.add_permission(folder.get("id"), permission)

    return folder
예제 #7
0
def create_document_from_template(template_id, name, parent_ids=None, permissions=None):
    service = Service(settings.GOOGLE_P12_PATH, settings.GOOGLE_SERVICE_EMAIL)
    upload_info = service.copy_file(template_id, name, parent_ids=parent_ids)
    if upload_info is None:
        raise GoogleDriveError("Could not create document {}".format(name))

    logger.info(upload_info)
    document_id = upload_info.get("id")

    permissions = permissions or []
    for permission in permissions:
        service.add_permission(document_id, permission)

    upload_info["status"] = "success"
    upload_info["id"] = document_id
    return upload_info
예제 #8
0
def create_document_from_template(template_id,
                                  name,
                                  parent_ids=None,
                                  permissions=None):
    service = Service(settings.GOOGLE_P12_PATH, settings.GOOGLE_SERVICE_EMAIL)
    upload_info = service.copy_file(template_id, name, parent_ids=parent_ids)
    if upload_info is None:
        raise GoogleDriveError('Could not create document {}'.format(name))

    logger.info(upload_info)
    document_id = upload_info.get('id')

    permissions = permissions or []
    for permission in permissions:
        service.add_permission(document_id, permission)

    upload_info['status'] = 'success'
    upload_info['id'] = document_id
    return upload_info
예제 #9
0
def autoadjust_photos(project_data, prerequisites):
    """Resize all images in a google drive directory."""
    task_data = {}
    parent_folder_id = project_data['project_folder_id']

    # Create a directory to output the photos
    output_folder = create_folder_with_permissions(
        parent_folder_id,
        'Processed Photos',
        permissions=[write_with_link_permission],
    )
    task_data['processed_photo_folder'] = output_folder['id']

    # List the existing photos
    raw_photo_folder_id = (prerequisites
                           .get('photography')
                           .get('prerequisites')
                           .get('article_planning')
                           .get('prerequisites')
                           .get('document_creation')
                           .get('task')
                           .get('data')
                           .get('raw_photo_folder'))
    service = Service(settings.GOOGLE_P12_PATH,
                      settings.GOOGLE_SERVICE_EMAIL)
    photos_metadata = service.list_folder(raw_photo_folder_id)

    # Iterate over the input photos and process them.
    task_data['photos_for_caption'] = []
    for photo_metadata in photos_metadata:
        photo, title, mimetype = download_file(photo_metadata)
        adjusted_photo_tmpfile = adjust_photo(photo)
        upload = upload_file(
            task_data['processed_photo_folder'],
            adjusted_photo_tmpfile.name,
            title,
            'image',
            mimetype
        )
        os.unlink(adjusted_photo_tmpfile.name)
        embed_link = upload['webContentLink'].replace('&export=download', '')
        task_data['photos_for_caption'].append(embed_link)
    return task_data
예제 #10
0
def download_file(file_metadata):
    """Download a file from a google drive folder.

    Args:
        file_metadata (dict):
            A Google Apps API file resource.

    Returns:
        file_contents (str):
            A buffer containing the raw binary contents of the file.
        title (str):
            The title of the file.
        mimetype(str):
            The mimetype of the file.
    """

    service = Service(settings.GOOGLE_P12_PATH, settings.GOOGLE_SERVICE_EMAIL)
    mimetype = file_metadata['mimeType']
    title = file_metadata['title']
    return service.get_file_content(file_metadata['id']), title, mimetype
예제 #11
0
def download_file(file_metadata):
    """Download a file from a google drive folder.

    Args:
        file_metadata (dict):
            A Google Apps API file resource.

    Returns:
        file_contents (str):
            A buffer containing the raw binary contents of the file.
        title (str):
            The title of the file.
        mimetype(str):
            The mimetype of the file.
    """

    service = Service(settings.GOOGLE_P12_PATH, settings.GOOGLE_SERVICE_EMAIL)
    mimetype = file_metadata["mimeType"]
    title = file_metadata["title"]
    return service.get_file_content(file_metadata["id"]), title, mimetype
예제 #12
0
def create_media_folder_with_images(parent_id, image_links, folder_name):
    """
    Given a folder name and a list of image links create a new
    google drive folder with images in it.
    """
    service = Service(settings.GOOGLE_P12_PATH, settings.GOOGLE_SERVICE_EMAIL)
    folder = create_folder_with_permissions(parent_id, folder_name,
                                            [read_with_link_permission])
    folder_id = folder['id']
    counter = Counter()
    for image_link in image_links:
        try:
            image = add_image(service, folder_id, image_link)
            counter['uploaded_images'] += 1
            logger.info('Image has been uploaded %s', image)
        except (InvalidUrlError, FailedRequest):
            counter['not_uploaded_images'] += 1
            logger.exception('Failed to retrieve image from %s', image_link)
    return {'folder': folder, 'image_counter': counter}