def fetch_hashtag(self, **kwargs): try: # Connect to Twitter Search API twitter = Twython(settings.TWITTER_CONSUMER_KEY, access_token=settings.TWITTER_ACCESS_TOKEN) # Last fetching album = Album.objects.get(pk=kwargs['pk']) hashtag = '%23' + album.title print hashtag if album.last_fetching: last_fetching = album.last_fetching.strftime('%a %b %d %H:%M:%S %z %Y') since = album.last_fetching.strftime('%Y-%m-%d') else: last_fetching = datetime.datetime.today().strftime('%a %b %d %H:%M:%S %z %Y') since = datetime.datetime.today().strftime('%Y-%m-%d') ''' Retrieve most recent 100 photos from Twitter ''' result = twitter.search(q=hashtag, filter='images', since=since, count='100', result_type='recent') photos = [] check_exists = [] new_items = 0 for i in range(len(result['statuses'])): created_at = result['statuses'][i]['created_at'] # Create a 'photo' object photo = Photo() photo.album_id = album.id if result['statuses'][i]['entities'].has_key('media'): # and last_fetching < created_at: tweet = result['statuses'][i]['entities']['media'][0]['expanded_url'] media = result['statuses'][i]['entities']['media'][0]['media_url_https'] ''' We are trying to avoid duplicates for each fetch. 1. Store the media link in check_exists list. 2. If the media link is not in check_exists, assign it to the photo object. 3. Increment the 'new_items' counter. 4. Insert the link into check_exists list. ''' if media not in check_exists: photo.media = str(media) photo.tweet = str(tweet) photos.append(photo) new_items += 1 check_exists.append(str(media)) ''' Save all new object using one query (bulk_create). ''' Photo.objects.bulk_create(photos) ''' Update the last_fetching field to now. ''' album.last_fetching = datetime.datetime.now() album.save() ''' Send an e-mail to MANAGERS (settings.py) ''' total_photos = Photo.objects.filter(album_id__exact=album.id).count() new_photos_links = '\n'.join(map(str, check_exists)) subject = 'album: %s, new pic: %s' % (album.title, new_items) message = 'Total photos: %s \n\n%s' % (total_photos, new_photos_links) mail_managers(subject, message) # Redirect to /list/album/<album-id>/photos url = '%s%s/photos/' % (reverse('list_album_view'), album.id) return redirect(url) except TwythonError as error: print error