def getall(): #login username = request.args.get('username', '') password = request.args.get('password', '') s = Snapchat() s.login(username, password) #get all snaps for the user snaps = s.get_snaps() if snaps == False: snaps = [] allsnaps = [] for snap in snaps: if snap['id'][-1] != 's' and snap['status'] != 2: # Download a snap media = s.get_media(snap['id']) if media == False: s.login(username, password) media = s.get_media(snap['id']) if media == False: next reportedMediaType = snap['media_type'] fileType = { None: 'image', 1: 'video', 2: 'video', 3: None, 4: 'image', 5: 'video', 6: 'video' }[reportedMediaType] ext = '.mp4' if fileType == 'image': ext = ".jpeg" filepath = '/var/www/static/' + username + '_' + snap['id'] + ext os.system('rm -rf ' + filepath) newFile = open(filepath, "wb") if fileType == 'image' or fileType == 'video': newFileByteArray = bytearray(media) if len(newFileByteArray) > 0: newFile.write(newFileByteArray) # Is this one of ours? connection = Connections.find_one({ "id": snap['id'] }) is_reaction = False if connection: is_reaction = True allsnaps.append({ 'file':username + '_' + snap['id'] + ext, 'senderName':snap['sender'], 'fileType': fileType, 'time': snap['time'], 'isReaction': is_reaction}) return Response(json.dumps(allsnaps), mimetype='text/javascript')
media_path = 'cache/' + snap['id'] if os.path.isfile(media_path): log ('Already downloaded ' + snap['id']) continue log ('Writing history') try: with open('history.json'.format(datetime.now()), 'w') as f: f.write(json.dumps(snaps, default=date_serializer, indent=2)) except TypeError as e: log ('Error saving history: {0}'.format(e)) log ('Downloading ' + snap['id']) media = s.get_media(snap['id']) # log ('Marking {0} as seen'.format(snap['id'])) # s.mark_seen(snap['id']) if media == None: log ('Cannot download ' + snap['id']) continue log ('Successfully downloaded ' + snap['id']) try: with open(media_path, 'w') as f: log ('Writing to cache from ' + snap['id']) f.write(media) except TypeError as e: