Adjust `.../email' and `.../link' uploads _prepare_{email,link}_upload() - transferwee - Download/upload file via wetransfer.com
 (HTM) git clone https://github.com/iamleot/transferwee
 (DIR) Log
 (DIR) Files
 (DIR) Refs
 (DIR) README
       ---
 (DIR) commit 2f96ab33a51812a830e4d36a8d60982b3cdd73f9
 (DIR) parent 15e83d26b305044188428e9d17977d99d0d6b513
 (HTM) Author: Leonardo Taccari <iamleot@gmail.com>
       Date:   Tue, 26 Mar 2019 19:24:19 +0100
       
       Adjust `.../email' and `.../link' uploads _prepare_{email,link}_upload()
       
       The API for `.../email' and `.../link' no longer expects just
       filenames but a list of files where the files contain their basename
       and their size.
       
       Introduce a _file_name_and_size() method to return such dict and use it in
       _prepare_{email,link}_upload() and _prepare_file_upload().
       
       Diffstat:
         M transferwee.py                      |      29 ++++++++++++++++++-----------
       
       1 file changed, 18 insertions(+), 11 deletions(-)
       ---
 (DIR) diff --git a/transferwee.py b/transferwee.py
       @@ -1,7 +1,7 @@
        #!/usr/bin/env python3.7
        
        #
       -# Copyright (c) 2018 Leonardo Taccari
       +# Copyright (c) 2018-2019 Leonardo Taccari
        # All rights reserved.
        # 
        # Redistribution and use in source and binary forms, with or without
       @@ -124,6 +124,20 @@ def download(url: str) -> None:
                    f.write(chunk)
        
        
       +def _file_name_and_size(file: str) -> dict:
       +    """Given a file, prepare the "name" and "size" dictionary.
       +
       +    Return a dictionary with "name" and "size" keys.
       +    """
       +    filename = os.path.basename(file)
       +    filesize = os.path.getsize(file)
       +
       +    return {
       +        "name": filename,
       +        "size": filesize
       +    }
       +
       +
        def _prepare_email_upload(filenames: List[str], message: str,
                                  sender: str, recipients: List[str]) -> str:
            """Given a list of filenames, message a sender and recipients prepare for
       @@ -132,7 +146,7 @@ def _prepare_email_upload(filenames: List[str], message: str,
            Return the parsed JSON response.
            """
            j = {
       -        "filenames": filenames,
       +        "files": [_file_name_and_size(f) for f in filenames],
                "from": sender,
                "message": message,
                "recipients": recipients,
       @@ -149,7 +163,7 @@ def _prepare_link_upload(filenames: List[str], message: str) -> str:
            Return the parsed JSON response.
            """
            j = {
       -        "filenames": filenames,
       +        "files": [_file_name_and_size(f) for f in filenames],
                "message": message,
                "ui_language": "en",
            }
       @@ -163,14 +177,7 @@ def _prepare_file_upload(transfer_id: str, file: str) -> str:
        
            Return the parsed JSON response.
            """
       -    filename = os.path.basename(file)
       -    filesize = os.path.getsize(file)
       -
       -    j = {
       -        "name": filename,
       -        "size": filesize
       -    }
       -
       +    j = _file_name_and_size(file)
            r = requests.post(WETRANSFER_FILES_URL.format(transfer_id=transfer_id),
                              json=j)
            return r.json()