Skip to content

Instantly share code, notes, and snippets.

@cmcewen
Created January 31, 2015 20:57

Revisions

  1. cmcewen created this gist Jan 31, 2015.
    61 changes: 61 additions & 0 deletions group_stories.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,61 @@
    #!/usr/bin/env python

    #using the awesome pysnap library: https://github.com/martinp/pysnap

    from __future__ import print_function

    import os.path
    import sys
    from getpass import getpass
    from zipfile import is_zipfile, ZipFile

    from pysnap import get_file_extension, Snapchat

    def process_snap(s, snap, path):
    filename = '{0}_{1}.{2}'.format(snap['sender'], snap['id'],
    get_file_extension(snap['media_type']))
    abspath = os.path.abspath(os.path.join(path, filename))
    if os.path.isfile(abspath):
    return
    data = s.get_blob(snap['id'])
    if data is None:
    return
    with open(abspath, 'wb') as f:
    f.write(data)
    print('Saved: {0}'.format(abspath))
    return abspath

    if is_zipfile(abspath):
    zipped_snap = ZipFile(abspath)
    unzip_dir = os.path.join(path, '{0}_{1}'.format(snap['sender'],
    snap['id']))
    zipped_snap.extractall(unzip_dir)
    print('Unzipped {0} to {1}'.format(filename, unzip_dir))
    return abspath

    def upload_story(s, snap_path):
    media_id = s.upload(snap_path)
    if media_id:
    s.send_to_story(media_id, time=5)

    def main():
    username = # Your username goes here
    password = # Your password goes here
    path = 'Documents/snaps' #where you download the snaps to

    s = Snapchat()
    if not s.login(username, password).get('logged'):
    print('Invalid username or password')
    sys.exit(1)

    new_snaps = []
    for snap in s.get_snaps():
    result = process_snap(s, snap, path)
    if result:
    new_snaps.append(result)
    for snap_path in new_snaps:
    upload_story(s, snap_path)


    if __name__ == '__main__':
    main()