Exemple #1
0
def manage_db():
    """Manage Databases View"""
    mongo_hashtags = Collection.hashtags()
    sqlite_hashtags = TweetsModel.distinct_hashtags()
    return render_template('manage_db/dashboard.html',
                           mongo_hashtags=mongo_hashtags,
                           sqlite_hashtags=sqlite_hashtags)
def manage_db():
    """Manage Databases View"""
    for attempt in range(0, 3):
        try:
            mongo_hashtags = Collection.hashtags()
        except AutoReconnect:
            time.sleep(2)
    sqlite_hashtags = TweetsModel.distinct_hashtags()
    return render_template('manage_db/dashboard.html',
                           mongo_hashtags=mongo_hashtags,
                           sqlite_hashtags=sqlite_hashtags)
    def post(self):

        mongo_hashtags = len(Collection.hashtags())
        if mongo_hashtags >= 10:
            return {
                "error":
                "MongoDB max capacity (10 already reached). Remove some Collections first:"
            }

        # Upload tweets to MongoDB
        mongodb = Collection()
        keyword = request.form.get('trend')

        if keyword[0] != '#':
            keyword = '#' + keyword
        else:
            pass
        count = int(request.form.get('count'))

        # Prevent overpopulating by deleting previously loaded tweets
        Collection.delete_by_hashtag(hashtag=keyword)

        for tweet in tweepy.Cursor(twitter_api.search, q=keyword).items(count):
            data = {}
            data['text'] = tweet.text
            data['hashtag'] = keyword
            data['created_at'] = tweet.created_at
            data['retweet_count'] = tweet.retweet_count
            try:
                mongodb.insert_data(data)
            except Exception as e:
                pass

        results = []
        for tweet in tweepy.Cursor(twitter_api.search, q=keyword).items(count):
            try:
                dict_ = {
                    "user": tweet.user.name[:25],
                    "text": tweet.text,
                    "created_at": str(tweet.created_at),
                    "retweet_count": tweet.retweet_count,
                    "keyword": keyword
                }
                results.append(dict_)
            except Exception as e:
                pass

        return results