Ejemplo n.º 1
0
def _process_muse(muse):
    """
    Processes a Muse's tweets,
    creating Tweet objects
    and saving them to the db.
    """
    username = muse.username
    logger.info('Collecting tweets for %s...' % username)

    try:
        tweets = twitter.tweets(username=username)
    except TweepError:
        return []

    new_tweets = []
    for tweet in tweets:
        data = {
            'body': tweet['body'],
            'tid': tweet['tid'],
            'username': username
        }
        t = Tweet(**data)
        try:
            t.save()
            new_tweets.append(tweet)
        except (NotUniqueError, DuplicateKeyError, OperationError):
            # Duplicate tweet
            pass
    return new_tweets
Ejemplo n.º 2
0
def _process_muse(muse):
    """
    Processes a Muse's tweets,
    creating Tweet objects
    and saving them to the db.
    """
    username = muse.username
    logger.info("Collecting tweets for %s..." % username)

    try:
        tweets = twitter.tweets(username=username)
    except TweepError:
        return []

    new_tweets = []
    for tweet in tweets:
        data = {"body": tweet["body"], "tid": tweet["tid"], "username": username}
        t = Tweet(**data)
        try:
            t.save()
            new_tweets.append(tweet)
        except (NotUniqueError, DuplicateKeyError, OperationError):
            # Duplicate tweet
            pass
    return new_tweets
Ejemplo n.º 3
0
def save_tweets(tweets, search_term, location_search_term=None):
    """
    Saves given tweet data in MongoDB database.

    :param tweets: Tweet data
    :param search_term: Search term entered by user
    :param location_search_term: Location search term entered by user (optional)
    """

    try:
        if location_search_term:
            # Cycle through list of Tweets
            for status in tweets:
                tweet_id = status['tweet_id']
                # Check if tweet already exists in db, skip this iteration if it does
                id_query = len(Tweet.objects(tweet_id=tweet_id))
                if id_query >= 1:
                    continue
                # Define record to save to db
                record = Tweet(
                    tweet_id=status['tweet_id'],
                    tweet_time=status['created_at'],
                    tweet_text=status['text'],
                    tweet_user=status['user'],
                    tweet_user_fullname=status['user_fullname'],
                    profile_image_url=status['profile_image_url'],
                    sentiment_type=status['sentiment'],
                    sentiment_score=status['sentimentScore'],
                    location_geo=status['location_geo'],
                    location_address=status['location_address'],
                    keyword_search_term=search_term,
                    location_search_term=location_search_term
                )
                # Save to DB
                record.save()
        else:
            # Cycle through list of processed Tweets
            for status in tweets:
                tweet_id = status['tweet_id']
                # Check if tweet already exists in db, skip this iteration if it does
                id_query = len(Tweet.objects(tweet_id=tweet_id))
                if id_query >= 1:
                    continue
                # Define record to save to db
                record = Tweet(
                    tweet_id=status['tweet_id'],
                    tweet_time=status['created_at'],
                    tweet_text=status['text'],
                    tweet_user=status['user'],
                    tweet_user_fullname=status['user_fullname'],
                    profile_image_url=status['profile_image_url'],
                    sentiment_type=status['sentiment'],
                    sentiment_score=status['sentimentScore'],
                    keyword_search_term=search_term
                )
                # Save to DB
                record.save()
    except Exception:
        raise Exception("Database error")
Ejemplo n.º 4
0
def new_tweet(request):
	if request.method == 'POST':
		tweet = Tweet(tweet = request.POST['tweet'], user=request.user)
		tweet.save()
		messages.success(request, 'Twiteado')
		return redirect(reverse('home'))

	return render(request, 'new_tweet.html')
Ejemplo n.º 5
0
	def on_data(self, data):
		all_data = json.loads(data)
		tweet = all_data["text"]
		username = all_data["user"]["screen_name"]
		language = all_data["lang"]

		# en
		# nl
		if 'nl' in language:
			if 'RT' not in tweet:
				t = Tweet(tweet_text=tweet, twitter_user=username, keyword=keyword, discovery_date=timezone.now())
				t.save()
				print username + ': ' + tweet
				print " "
		return True
Ejemplo n.º 6
0
def done(request):
    """Login complete view, displays user data"""
    user = get_user(request.user)
    if not user.configured:
        return HttpResponseRedirect('/config/')  # Redirect after POST

    new_posttweet = False
    if request.method == 'POST':  # If the form has been submitted...
        tweetf = TweetForm(request.POST)  # A form bound to the POST data
        if tweetf.is_valid():  # All validation rules pass
            #Save the tweet in the table
            text = tweetf.cleaned_data['text']
            pub_date = datetime.utcnow()

            t = Tweet(text=text, pub_date=pub_date, user=user)
            t.save()
            new_posttweet = user.show_modal_new_tweet()
            if user.alwaysupdate:
                tweet = _(
                    "I saved a tweet that will be published when I die with http://foowill.com @foo_will"
                )
                try:
                    user.update_twitter_status(tweet)
                except TweepError:
                    count = Tweet.objects.filter(user=user).count()
                    user.update_twitter_status("%s (%d)" % (tweet, count))
                except:
                    pass
            user.posts = user.posts + 1
            user.save()
    else:
        tweetf = TweetForm()

    tweets = Tweet.objects.filter(user=user).order_by('-pub_date')
    updatetweetform = UpdateTweetForm()

    ctx = {
        'tweetform': tweetf,
        'tweets': tweets,
        'user': user,
        'updatetweetform': updatetweetform,
        'new_posttweet': new_posttweet,
    }

    return render_to_response('done.html', ctx, RequestContext(request))
Ejemplo n.º 7
0
def done(request):
    """Login complete view, displays user data"""
    user = get_user(request.user)
    if not user.configured:
        return HttpResponseRedirect('/config/') # Redirect after POST

    new_posttweet = False
    if request.method == 'POST': # If the form has been submitted...
        tweetf = TweetForm(request.POST) # A form bound to the POST data
        if tweetf.is_valid(): # All validation rules pass
            #Save the tweet in the table
            text = tweetf.cleaned_data['text']
            pub_date = datetime.utcnow() 
            
            t = Tweet(text=text, pub_date=pub_date, user=user)
            t.save()
            new_posttweet = user.show_modal_new_tweet()
            if user.alwaysupdate:
                tweet = _("I saved a tweet that will be published when I die with http://foowill.com @foo_will")
                try:
                    user.update_twitter_status(tweet)
                except TweepError:
                    count = Tweet.objects.filter(user=user).count()
                    user.update_twitter_status("%s (%d)" % (tweet, count))
                except:
                    pass
            user.posts = user.posts + 1
            user.save()
    else:
        tweetf = TweetForm()
        
    tweets = Tweet.objects.filter(user=user).order_by('-pub_date')
    updatetweetform = UpdateTweetForm()
    
    ctx = {
        'tweetform': tweetf,
        'tweets': tweets,
        'user': user,
        'updatetweetform': updatetweetform,
        'new_posttweet': new_posttweet,
    }
    
    return render_to_response('done.html', ctx, RequestContext(request))
Ejemplo n.º 8
0
    def save_tweet(self, tweet):
        if len(User.objects.all().filter(id_str=tweet["user"]["id_str"])) == 0:
            user_obj = User()
            user_obj.id_str = tweet["user"]["id_str"]
            user_obj.name = tweet["user"]["name"]
            user_obj.screen_name = tweet["user"]["screen_name"]
            user_obj.location = tweet["user"]["location"]
            user_obj.url = tweet["user"]["url"]
            user_obj.description = tweet["user"]["description"]
            user_obj.verified = tweet["user"]["verified"]
            user_obj.follower = tweet["user"]["followers_count"]
            user_obj.statuses_count = tweet["user"]["statuses_count"]
            user_obj.created_at = self.str_to_date(tweet["user"]["created_at"])
            user_obj.profile_image_url = tweet["user"]["profile_image_url"]
            user_obj.save()

            user = user_obj
        else:
            user = User.objects.all().filter(id_str=tweet["user"]["id_str"])[0]
        
        if len(Tweet.objects.all().filter(id_str=tweet["id_str"])) == 0:
            tweet_obj = Tweet()
            tweet_obj.id_str = tweet["id_str"]
            tweet_obj.created_at = self.str_to_date(tweet["created_at"])
            tweet_obj.tag = self.tag
            tweet_obj.user = user
            tweet_obj.text = tweet["text"]
            tweet_obj.in_reply_to_status_id = tweet["in_reply_to_status_id"]
            tweet_obj.in_reply_to_user_id = tweet["in_reply_to_user_id"]
            tweet_obj.in_reply_to_screen_name = tweet["in_reply_to_screen_name"]
            tweet_obj.geo = tweet["geo"]
            tweet_obj.coordinates = tweet["coordinates"]
            tweet_obj.place = tweet["place"]

            if tweet["is_quote_status"] and "quoted_status" in tweet:
                if  len(Tweet.objects.all().filter(id_str=tweet["quoted_status"]["id_str"])) == 0:
                    if len(User.objects.all().filter(id_str=tweet["quoted_status"]["user"]["id_str"])) == 0:
                        quoted_user_obj = User()
                        quoted_user_obj.id_str = tweet["quoted_status"]["user"]["id_str"]
                        quoted_user_obj.name = tweet["quoted_status"]["user"]["name"]
                        quoted_user_obj.screen_name = tweet["quoted_status"]["user"]["screen_name"]
                        quoted_user_obj.location = tweet["quoted_status"]["user"]["location"]
                        quoted_user_obj.url = tweet["quoted_status"]["user"]["url"]
                        quoted_user_obj.description = tweet["quoted_status"]["user"]["description"]
                        quoted_user_obj.verified = tweet["quoted_status"]["user"]["verified"]
                        quoted_user_obj.follower = tweet["quoted_status"]["user"]["followers_count"]
                        quoted_user_obj.statuses_count = tweet["quoted_status"]["user"]["statuses_count"]
                        quoted_user_obj.created_at = self.str_to_date(tweet["quoted_status"]["user"]["created_at"])
                        quoted_user_obj.profile_image_url = tweet["quoted_status"]["user"]["profile_image_url"]
                        quoted_user_obj.save()
                    else:
                        quoted_user_obj = User.objects.all().filter(id_str=tweet["user"]["id_str"])[0]
                    
                    quoted_obj = Tweet()
                    quoted_obj.id_str = tweet["id_str"]
                    quoted_obj.created_at = self.str_to_date(tweet["quoted_status"]["created_at"])
                    quoted_obj.user = quoted_user_obj
                    quoted_obj.tag = self.tag
                    quoted_obj.text = tweet["quoted_status"]["text"]
                    quoted_obj.in_reply_to_status_id = tweet["quoted_status"]["in_reply_to_status_id"]
                    quoted_obj.in_reply_to_user_id = tweet["quoted_status"]["in_reply_to_user_id"]
                    quoted_obj.in_reply_to_screen_name = tweet["quoted_status"]["in_reply_to_screen_name"]
                    quoted_obj.geo = tweet["quoted_status"]["geo"]
                    quoted_obj.coordinates = tweet["quoted_status"]["coordinates"]
                    quoted_obj.place = tweet["quoted_status"]["place"]

                    if "quote_count" in tweet:
                        quoted_obj.quote_count = tweet["quote_count"]
                    if "reply_count" in tweet:
                        quoted_obj.reply_count = tweet["reply_count"]
                    if "retweet_count" in tweet:
                        quoted_obj.retweet_count = tweet["retweet_count"]
                    if "favorite_count" in tweet:
                        quoted_obj.favorite_count = tweet["favorite_count"]

                    quoted_obj.entities = tweet["quoted_status"]["entities"]
                    quoted_obj.save()
                else:
                    quoted_obj = Tweet.objects.all().filter(id_str=tweet["quoted_status"]["id_str"])[0]
                
                tweet_obj.quoted_status_id = tweet["quoted_status_id_str"]
                tweet_obj.is_quote_status = tweet["is_quote_status"]
                tweet_obj.quoted_status = quoted_obj
                if "quoted_status_permalink" in tweet:
                    tweet_obj.quoted_status_permalink = tweet["quoted_status_permalink"]["url"]
                tweet_obj.quoted_status_id = tweet["quoted_status_id"]
            
            if "quote_count" in tweet:
                tweet_obj.quote_count = tweet["quote_count"]
            if "reply_count" in tweet:
                tweet_obj.reply_count = tweet["reply_count"]
            if "retweet_count" in tweet:
                tweet_obj.retweet_count = tweet["retweet_count"]
            if "favorite_count" in tweet:
                tweet_obj.favorite_count = tweet["favorite_count"]
            tweet_obj.entities = tweet["entities"]
            tweet_obj.save()

            return tweet_obj