def handle(self, *args, **options): try: twc = TwitterConnection.objects.filter(deleted=False)[0] except IndexError: self.stdout.write(u'Error: there is no TwitterConnection') twc = None if twc: # api client auth = tweepy.OAuthHandler( settings.TW_CONSUMER_KEY, settings.TW_CONSUMER_SECRET, ) auth.set_access_token(twc.access_token, twc.access_token_secret) api = tweepy.API(auth) if args: btps = BrandTwitterProfile.objects.filter(twitter_id__in=args, first_load=True, deleted=False) else: btps = BrandTwitterProfile.objects.filter(first_load=True, deleted=False) error = False # do first load for btp in [b for b in btps]: profile = api.get_user(id=btp.twitter_id) btp.name = profile.name btp.screen_name = profile.screen_name btp.followers_count = profile.followers_count btp.friends_count = profile.friends_count btp.listed_count = profile.listed_count btp.favourites_count = profile.favourites_count btp.statuses_count = profile.statuses_count btp.save() evolution = EvolutionTwitterProfile( twitter_profile=btp, followers_count=btp.followers_count, friends_count=btp.friends_count, listed_count=btp.listed_count, favourites_count=btp.favourites_count, statuses_count=btp.statuses_count, ) evolution.save() self.stdout.write( u'Successfully updated Brand Twitter Profile: %s \n\n' % btp.twitter_id) page = 1 main_loop = True while main_loop: statuses = api.user_timeline(user_id=btp.twitter_id, page=page, count=200) if statuses: for status in statuses: try: twm = TwitterMessage.objects.get( twitter_profile=btp, twitter_id__exact=status.id_str, deleted=False, ) except TwitterMessage.DoesNotExist: try: twm = TwitterMessage( twitter_profile=btp, twitter_id=status.id_str, author_twitter_id=status.author.id_str, message=status.text, created_time=status.created_at, favorites=status.favorite_count, retweets=status.retweet_count, ) except Exception, e: err = APIError( app_name=u'metrics_social', model_name='TwitterMessage', error=u'%s: %s' % (Exception, str(e)), response=status.id_str, ) err.save() self.stdout.write( u'Inserted error message: %s %s' % (Exception, str(e))) else: twm.save() self.stdout.write(u'Inserted message %s' % twm.twitter_id) else: self.stdout.write( u'Message already exists: %s' % twm.twitter_id) main_loop = False break page += 1 # next page self.stdout.write(u'Going to next page: %s\n\n' % page) else: main_loop = False
def handle(self, *args, **options): try: twc = TwitterConnection.objects.filter(deleted=False)[0] except IndexError: self.stdout.write(u'Error: there is no TwitterConnection') twc = None if twc: # api client auth = tweepy.OAuthHandler( settings.TW_CONSUMER_KEY, settings.TW_CONSUMER_SECRET, ) auth.set_access_token(twc.access_token, twc.access_token_secret) api = tweepy.API(auth) if args: ctps = CampaignTwitterProfile.objects.filter(twitter_id__in=args, first_load=False, deleted=False) else: ctps = CampaignTwitterProfile.objects.filter(first_load=False, deleted=False) error = False # do first load for ctp in [c for c in ctps]: profile = api.get_user(id=ctp.twitter_id) ctp.name = profile.name ctp.screen_name = profile.screen_name ctp.followers_count = profile.followers_count ctp.friends_count = profile.friends_count ctp.listed_count = profile.listed_count ctp.favourites_count = profile.favourites_count ctp.statuses_count = profile.statuses_count ctp.save() evolution = EvolutionTwitterProfile( twitter_profile=ctp, followers_count=ctp.followers_count, friends_count=ctp.friends_count, listed_count=ctp.listed_count, favourites_count=ctp.favourites_count, statuses_count=ctp.statuses_count, ) evolution.save() self.stdout.write(u'Successfully updated Campaign Twitter Profile: %s \n\n' % ctp.twitter_id) try: page = APIPagination.objects.get( app_name=u'metrics_social', model_name='TwitterMessage', path=u'/%s/timeline' % ctp.twitter_id, ).offset page = int(page) except APIPagination.DoesNotExist: page = 1 main_loop = True while main_loop: statuses = api.user_timeline(user_id=ctp.twitter_id, page=page, count=200) if statuses: for status in statuses: try: twm = TwitterMessage.objects.get( twitter_profile=ctp, twitter_id__exact=status.id_str, deleted=False, ) except TwitterMessage.DoesNotExist: try: twm = TwitterMessage( twitter_profile=ctp, twitter_id=status.id_str, author_twitter_id=status.author.id_str, message=status.text, created_time=status.created_at, favorites=status.favorite_count, retweets=status.retweet_count, ) except Exception, e: err = APIError( app_name=u'metrics_social', model_name='TwitterMessage', error=u'%s: %s' % (Exception, str(e)), response=status.id_str, ) err.save() self.stdout.write(u'Inserted error message: %s %s' % (Exception, str(e))) else: twm.save() self.stdout.write(u'Inserted message %s' % twm.twitter_id) else: self.stdout.write(u'Message already exists: %s' % twm.twitter_id) try: stopped_at = APIPagination.objects.get( app_name=u'metrics_social', model_name='TwitterMessage', path=u'/%s/timeline' % ctp.twitter_id, ) stopped_at.offset = page stopped_at.save() except APIPagination.DoesNotExist: stopped_at = APIPagination( app_name=u'metrics_social', model_name='TwitterMessage', path=u'/%s/timeline' % ctp.twitter_id, offset=page, ) stopped_at.save() page += 1 # next page self.stdout.write(u'Going to next page: %s\n\n' % page) else: main_loop = False ctp.first_load = True ctp.save()
def handle(self, *args, **options): try: twc = TwitterConnection.objects.filter(deleted=False)[0] except IndexError: self.stdout.write(u'Error: there is no TwitterConnection') twc = None if twc: # api client auth = tweepy.OAuthHandler( settings.TW_CONSUMER_KEY, settings.TW_CONSUMER_SECRET, ) auth.set_access_token(twc.access_token, twc.access_token_secret) api = tweepy.API(auth) if args: ctps = CampaignTwitterProfile.objects.filter(twitter_id__in=args, first_load=True, deleted=False) else: ctps = CampaignTwitterProfile.objects.filter(first_load=True, deleted=False) error = False # do first load for ctp in [c for c in ctps]: profile = api.get_user(id=ctp.twitter_id) ctp.name = profile.name ctp.screen_name = profile.screen_name ctp.followers_count = profile.followers_count ctp.friends_count = profile.friends_count ctp.listed_count = profile.listed_count ctp.favourites_count = profile.favourites_count ctp.statuses_count = profile.statuses_count ctp.save() evolution = EvolutionTwitterProfile( twitter_profile=ctp, followers_count=ctp.followers_count, friends_count=ctp.friends_count, listed_count=ctp.listed_count, favourites_count=ctp.favourites_count, statuses_count=ctp.statuses_count, ) evolution.save() self.stdout.write(u'Successfully updated Campaign Twitter Profile: %s \n\n' % ctp.twitter_id) end_date = datetime.date.today() end_date = datetime.date.fromordinal(end_date.toordinal() - 1) statuses = TwitterMessage.objects.filter( twitter_profile=ctp, created_time__lte=datetime.datetime.combine(end_date, datetime.time()), loaded_retweets=False, deleted=False, ).order_by('-created_time') for status in [s for s in statuses]: if status.retweets > 0: objs = api.retweets(id=status.twitter_id, count=100) for obj in objs: try: twr, created = TwitterRetweet.objects.get_or_create( message=status, author_twitter_id=obj.author.id_str, defaults={ 'created_time': obj.created_at, }, ) twr.save() self.stdout.write(u'Inserted retweet %s' % obj.id_str) except Exception, e: err = APIError( app_name=u'metrics_social', model_name='TwitterRetweet', error=u'%s: %s' % (Exception, str(e)), response=obj.id_str, ) err.save() self.stdout.write(u'Inserted error message: %s %s' % (Exception, str(e))) status.loaded_retweets = True status.save() else: status.loaded_retweets = True status.save()
def handle(self, *args, **options): try: twc = TwitterConnection.objects.filter(deleted=False)[0] except IndexError: self.stdout.write(u'Error: there is no TwitterConnection') twc = None if twc: # api client auth = tweepy.OAuthHandler( settings.TW_CONSUMER_KEY, settings.TW_CONSUMER_SECRET, ) auth.set_access_token(twc.access_token, twc.access_token_secret) api = tweepy.API(auth) if args: atps = AdvertiserTwitterProfile.objects.filter(twitter_id__in=args, first_load=True, deleted=False) else: atps = AdvertiserTwitterProfile.objects.filter(first_load=True, deleted=False) error = False # do first load for atp in [a for a in atps]: profile = api.get_user(id=atp.twitter_id) atp.name = profile.name atp.screen_name = profile.screen_name atp.followers_count = profile.followers_count atp.friends_count = profile.friends_count atp.listed_count = profile.listed_count atp.favourites_count = profile.favourites_count atp.statuses_count = profile.statuses_count atp.save() evolution = EvolutionTwitterProfile( twitter_profile=atp, followers_count=atp.followers_count, friends_count=atp.friends_count, listed_count=atp.listed_count, favourites_count=atp.favourites_count, statuses_count=atp.statuses_count, ) evolution.save() self.stdout.write(u'Successfully updated Advertiser Twitter Profile: %s \n\n' % atp.twitter_id) end_date = datetime.date.today() end_date = datetime.date.fromordinal(end_date.toordinal() - 1) statuses = TwitterMessage.objects.filter( twitter_profile=atp, created_time__lte=datetime.datetime.combine(end_date, datetime.time()), loaded_retweets=False, deleted=False, ).order_by('-created_time') for status in [s for s in statuses]: if status.retweets > 0: objs = api.retweets(id=status.twitter_id, count=100) for obj in objs: try: twr, created = TwitterRetweet.objects.get_or_create( message=status, author_twitter_id=obj.author.id_str, defaults={ 'created_time': obj.created_at, }, ) twr.save() self.stdout.write(u'Inserted retweet %s' % obj.id_str) except Exception, e: err = APIError( app_name=u'metrics_social', model_name='TwitterRetweet', error=u'%s: %s' % (Exception, str(e)), response=obj.id_str, ) err.save() self.stdout.write(u'Inserted error message: %s %s' % (Exception, str(e))) status.loaded_retweets = True status.save() else: status.loaded_retweets = True status.save()