Esempio n. 1
0
def download_from_sfs(target_dir, files):
    """
    Downloads snaps given a list of Snap objects, writing them to `target_dir`

    @target_dir Where to download the files to.
    @files The list of (filename, content_hash, recv_id, snap) tuples to download
    """

    # download each file in sequence; if we find two files with the same
    # name, we give the file a name that includes a hash of the contents
    filenames_downloaded = set()
    for filename, content_hash, received_id, snap in files:
        try:
            data = snap.download()
            if filename not in filenames_downloaded:
                print(util.green("Downloading snap ") + filename)
                path = os.path.join(target_dir, filename)
            else:
                print(util.green("Downloading snap ") + filename +
                      (util.red("but filename is not unique; ") +
                       ("downloading as: %s" %
                        (filename + "-" + content_hash))))
                path = os.path.join(target_dir
                                    , filename + "-" + content_hash)

            filenames_downloaded.add(filename)
            with open(os.path.join(target_dir, filename+content_hash)
                      , 'w') as w:
                w.write(data)

        except Exception as e:
            print("Failed to download %s: %s" % (filename, e))
            raise
Esempio n. 2
0
def download_from_sfs(target_dir, files):
    """
    Downloads snaps given a list of Snap objects, writing them to `target_dir`

    @target_dir Where to download the files to.
    @files The list of (filename, content_hash, recv_id, snap) tuples to download
    """

    # download each file in sequence; if we find two files with the same
    # name, we give the file a name that includes a hash of the contents
    filenames_downloaded = set()
    for filename, content_hash, received_id, snap in files:
        try:
            data = snap.download()
            if filename not in filenames_downloaded:
                print(util.green("Downloading snap ") + filename)
                path = os.path.join(target_dir, filename)
            else:
                print(
                    util.green("Downloading snap ") + filename +
                    (util.red("but filename is not unique; ") +
                     ("downloading as: %s" % (filename + "-" + content_hash))))
                path = os.path.join(target_dir, filename + "-" + content_hash)

            filenames_downloaded.add(filename)
            with open(os.path.join(target_dir, filename + content_hash),
                      'w') as w:
                w.write(data)

        except Exception as e:
            print("Failed to download %s: %s" % (filename, e))
            raise
Esempio n. 3
0
def upload_sfs_file(session, filename):
    """
    Uploads a file to Snapchat FS.

    @session An SfsSession object that has been logged in.
    @filename Path of the file to upload.
    """
    with open(filename) as f:
        data = f.read()

    basename = os.path.basename(filename)
    print util.green('Uploading file ') + (basename)
    sfs_id = session.generate_sfs_id(basename, data)
    session.upload_image(data, sfs_id)
    session.send_image_to(session.username, sfs_id)
Esempio n. 4
0
def upload_sfs_file(session, filename):
    """
    Uploads a file to Snapchat FS.

    @session An SfsSession object that has been logged in.
    @filename Path of the file to upload.
    """
    with open(filename) as f:
        data = f.read()

    basename = os.path.basename(filename)
    print util.green('Uploading file ') + (basename)
    sfs_id = session.generate_sfs_id(basename, data)
    session.upload_image(data, sfs_id)
    session.send_image_to(session.username, sfs_id)