def handle(self, **options): """ Prints out a count of number of tweets by screen_name for occupydenver. """ # Get current datetime as a string to append to the file. # This should allow us to know when it was run and keep from overwriting. now = datetime.datetime.now().strftime('%Y%m%d%H%M') # Create a CSV fiile counting tweets by users f = open('ODhashtag_users_%s.txt' % now, 'wb') csv_writer = UnicodeWriter(f) csv_writer.writerow(['Screen Name', 'Num od Tweets']) tweets = Tweet.objects.filter(tags__name='occupydenver').order_by('twitter_user') # Django has problems with aggregation across generic relationships # So we'll have to read it into a dict and count it there. # https://docs.djangoproject.com/en/dev/ref/contrib/contenttypes/#generic-relations-and-aggregation tweeters = defaultdict(int) for tweet in tweets: tweeters[tweet.twitter_user.screen_name] += 1 for user, count in tweeters.items(): csv_writer.writerow([user.encode('utf-8'), '%s' % count])
def handle(self, **options): """ """ # Get current datetime as a string to append to the file. # This should allow us to know when it was run and keep from overwriting. now = datetime.datetime.now().strftime('%Y%m%d%H%M') # Create a CSV fiile counting tweets by users f = open('userlocations_%s.csv' % now, 'wb') writer = UnicodeWriter(f) writer.writerow(['Name', 'Location', 'Tweet Count']) user_list = TwitterUser.objects.annotate(tweetcount=Count('tweet')) for user in user_list: if user.location: writer.writerow([user.screen_name, user.location, '%s' % user.tweetcount])
def handle(self, **options): """ """ # Get current datetime as a string to append to the file. # This should allow us to know when it was run and keep from overwriting. now = datetime.datetime.now().strftime('%Y%m%d%H%M') # Create a CSV fiile counting tweets by users f = open('usergeo_%s.csv' % now, 'wb') writer = UnicodeWriter(f) writer.writerow(['Username', 'Tweet', 'DateTweeted', 'Coordinates(lat/long)']) geotweets = TwitterGeo.objects.all() for geotweet in geotweets: tweetdate = geotweet.tweet.created_at.strftime('%Y-%m-%d %H:%M:%S') location = "%s, %s" % (geotweet.latitude, geotweet.longitude) writer.writerow([ geotweet.tweet.twitter_user.screen_name, geotweet.tweet.text, tweetdate, location, ])
def handle(self, **options): """ This is helps write some basic aggregation data about users and their tweets. It should accomplish 2 things. * Save a list of individual users screen_name, location, # of tweets * Save a list of # of tweets, # of users with those number of tweets. Sandemous Highschool Football Rules! """ # Get current datetime as a string to append to the file. # This should allow us to know when it was run and keep from overwriting. now = datetime.datetime.now().strftime("%Y%m%d%H%M") # Create a CSV fiile counting tweets by users f = open("hashtagcount_%s.csv" % now, "wb") writer = UnicodeWriter(f) writer.writerow(["HashTag", "Count"]) tag_list = Tag.objects.annotate(count=Count("taggit_taggeditem_items")).order_by("-count") for tag in tag_list: writer.writerow([tag.name, "%s" % tag.count])
def handle(self, **options): """ This is helps write some basic aggregation data about users and their tweets. It should accomplish 2 things. * Save a list of individual users screen_name, location, # of tweets * Save a list of # of tweets, # of users with those number of tweets. Sandemous Highschool Football Rules! """ # Get current datetime as a string to append to the file. # This should allow us to know when it was run and keep from overwriting. now = datetime.datetime.now().strftime("%Y%m%d%H%M") # Create a CSV fiile counting tweets by users by_user = open("tweetcount_by_user_%s.csv" % now, "wb") by_user_writer = UnicodeWriter(by_user) by_user_writer.writerow(["screen_name", "location", "total tweets"]) tweet_counter = {} users = TwitterUser.objects.annotate(tweetcount=Count("tweet")) for user in users: by_user_writer.writerow([user.screen_name, "%s" % user.location, "%s" % user.tweetcount]) if user.tweetcount in tweet_counter: # Doing it this way so I don't have to do another DB query. tweet_counter[user.tweetcount] += 1 # increment it if it exists else: tweet_counter[user.tweetcount] = 1 # init it if it doesn't exist by_user.close() # Create a CSV file aggregating users by their number of tweets total_tweets = open("by_total_tweets_%s.csv" % now, "wb") total_tweets_writer = UnicodeWriter(total_tweets) total_tweets_writer.writerow(["Tweet Count", "Number of Users"]) for tweetcount, users in tweet_counter.items(): total_tweets_writer.writerow(["%s" % tweetcount, "%s" % users]) total_tweets.close()