def fetch_photos_from_msg(self, album, msg=None): u = album.user token = get_access_token(u) graph = facebook.GraphAPI(token) if msg.status == 'awaiting': parts = urlparse.urlparse(msg.next_page) qs = urlparse.parse_qs(parts.query) after = qs.get('after')[0] photos = graph.get_object(album.fb_album_id + "/photos", fields='id,source', limit=2, after=after) new_next_page = photos.get('paging').get('next') new_msg = Message.objects.create(next_page=new_next_page, user=u, status='awaiting') for photo in photos.get('data'): img_temp = NamedTemporaryFile(delete=True) img_temp.write(urlopen(photo.get('source')).read()) img_temp.flush() photo_object = Photo.objects.create(title=photo.get('id'), description=photo.get('created_time'), album=album, file=File(img_temp)) pprint(photo_object.filename) self.stdout.write('Successfully fetched photo for source "%s"\n' % photo.get('source')) msg.status = 'done' msg.save() self.stdout.write('Finished this queue "%s"\n' % new_msg.next_page)
def get_photos(self, album): u = album.user token = get_access_token(u) graph = facebook.GraphAPI(token) photos = graph.get_object(album.fb_album_id + "/photos", fields='id,source', limit=2) next_page = photos.get('paging').get('next') for photo in photos.get('data'): img_temp = NamedTemporaryFile(delete=True) img_temp.write(urlopen(photo.get('source')).read()) img_temp.flush() photo_object = Photo.objects.create(title=photo.get('id'), description=photo.get('created_time'), album=album, file=File(img_temp)) pprint(photo_object.filename) self.stdout.write('Successfully fetched photo for source "%s"\n' % photo.get('source')) msg = Message.objects.create(next_page=next_page, user=u, status='awaiting') self.stdout.write('New message queued with this page "%s"\n' % msg.next_page)