Beispiel #1
0
def stats():

    from collections import Counter

    try:
        collection = mongo.create_collection()
        data = {
            'All Tweets': f'{collection.count():,}',
        }
        accounts = {
            'slpng_giants',
            'slpng_giants_be',
            'slpng_giants_bg',
            'slpng_giants_br',
            'slpng_giants_ca',
            'slpng_giants_ch',
            'slpng_giants_de',
            'slpng_giants_es',
            'slpng_giants_eu',
            'slpng_giants_fr',
            'slpng_giants_it',
            'slpng_giants_nl',
            'slpng_giants_no',
            'slpng_giants_nz',
            'slpng_giants_oz',
            'slpng_giants_se',
        }
        for account in sorted(accounts):
            data[f'@{account}'] = (
                f'{collection.find({"user.screen_name": account}).count():,}')

        counter = Counter()
        # for tweet in collection.find({"user.screen_name": 'slpng_giants'}):
        #     try:
        #         d = tweet['created_at']
        #         counter[f'Date({d.year}, {d.month - 1}, {d.day})'] += 1
        #     except Exception:
        #         continue
        date_count = sorted([f'[ new {k}, {v} ],' for k, v in counter.items()])
        return render_template('stats.html',
                               title='Stats',
                               data=data,
                               date_count='\n'.join(date_count))
    except Exception:
        TwiLogger.exception('TwiBrowser stats error: ')
        return redirect(url_for('index'))
Beispiel #2
0
def create_collection(db_name='slpng_giants', collection_name='tweets'):
    """
    Create collection for the given database. Skip an return early if collection
    exists.

    Args:
        db_name (str): Database name
        collection_name (str): Collection name

    Returns:
        Collection: Created collection.

    """
    try:
        client = MongoClient()
        if not is_connected(client):
            return
        db = client[db_name]
        if collection_name in db.list_collection_names():
            return db[collection_name]
        collection = db[collection_name]
        collection.create_index('id', unique=True)
        collection.create_index('created_at')
        collection.create_index('retweet_count')
        collection.create_index('favorite_count')
        collection.create_index('in_reply_to_status_id')
        collection.create_index('in_reply_to_user_id')
        collection.create_index('in_reply_to_screen_name')
        collection.create_index('entities.hashtags')
        collection.create_index('user.created_at')
        collection.create_index('user.screen_name')
        collection.create_index('user.id')
        collection.create_index('user.followers_count')
        collection.create_index('user.favourites_count')
        collection.create_index('user.verified')
        collection.create_index('user.statuses_count')
        collection.create_index([('full_text', TEXT)],
                                default_language='english')
        return collection
    except Exception:
        TwiLogger.exception('Unable to connect to MongoDB: ')
        return
Beispiel #3
0
 def run(self):
     """
     Fetches query from queue and executes it.
     """
     while True:
         self._query = self.queue.get()
         if self.query is None:
             TwiLogger.info(f'Terminating thread "{self.name}"')
             break
         while not self.query.done:
             try:
                 self.query.run()
             except Exception:
                 import traceback
                 TwiLogger.exception(traceback.format_exc())
                 break
             TwiLogger.info(self.query.fetch_log())
             time.sleep(.2)
         time.sleep(.5)
         self.queue.task_done()
Beispiel #4
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from twicorder.web.browser import app
from twicorder.utils import TwiLogger

if __name__ == '__main__':
    try:
        app.run('localhost')
    except Exception:
        TwiLogger.exception('TwiBrowser Error: ')