I combined dropbox with celery in my application, and with this I allow users to have their own photos if they have Dropbox associated.
I wrote a piece of code, but I'm worried that this could lead to an endless loop that will kill the system.
The API I click on only allows 60 photos at a time when it provides you with pagination.
Here is a copy of my tasks.py file - it really works fine, but I want to check that I am doing the right thing and not affecting the system too much.
class DropboxUsers(PeriodicTask):
run_every = timedelta(hours=4)
def run(self, **kwargs):
logger = self.get_logger(**kwargs)
logger.info("Collecting Dropbox users")
dropbox_users = UserSocialAuth.objects.filter(provider='dropbox')
for db in dropbox_users:
...
...
...
sync_images.delay(first, second, third_argument)
return True
@task(ignore_result=True)
def sync_images(token, secret, username):
"""docstring for sync_images"""
logger = sync_images.get_logger()
logger.info("Syncing images for %s" % username)
...
...
...
...
feed = api.user_recent_media(user_id='self', count=60)
images = feed[0]
pagination = feed[1]
for obj in images:
...
...
...
response = dropbox.put_file(f, my_picture, overwrite=True)
sess.unlink()
if pagination:
store_images.delay(first, second, third, fourth_argument)
@task(ignore_result=True)
def store_images(token, secret, username, max_id):
"""docstring for sync_images"""
logger = store_images.get_logger()
logger.info("Storing images for %s" % username)
...
...
...
...
feed = api.user_recent_media(user_id='self', count=60, max_id=max_id)
images = feed[0]
try:
pagination = feed[1]
except:
pagination = None
for obj in images:
...
...
...
response = dropbox.put_file(f, my_picture, overwrite=True)
sess.unlink()
if pagination:
store_images.delay(first, second, third, fourth_argument)
return True
Your experience is much appreciated.
ApPeL source
share