Last active
July 14, 2020 16:24
-
-
Save LouisdeBruijn/7788db35c36fb1613eebb6e24b79e36f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Create your tasks here | |
| from __future__ import absolute_import, unicode_literals | |
| from celery import shared_task | |
| from celery.utils.log import get_task_logger | |
| from .models import Tweet | |
| from .twitter import twitter_api, get_tweets | |
| logger = get_task_logger(__name__) | |
| @shared_task(bind=True, track_started=True) | |
| def c_get_tweets(self, uid, tweet_ids): | |
| """An asynchronous function to get tweets via Tweepy wrapper for Twitter API. | |
| :param self: instance of the class used to update the status | |
| :type self: @shared_task | |
| :param uid: user ID | |
| :type uid: int | |
| :param tweet_ids: tweet IDs to be scraped via Twitter API | |
| :type tweet_ids: list | |
| """ | |
| api = twitter_api() | |
| n = len(tweet_ids) | |
| for i, tweet_id in enumerate(tweet_ids): | |
| self.update_state(state='PROGRESS', meta={'done': i, 'total': n, 'uid': uid}) | |
| tweet, created = Tweet.objects.get_or_create(tweet_id=tweet_id) | |
| get_tweets(api, tweet_id, tweet) | |
| return "Celery has imported {0} tweets from Twitter.".format(n) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment