Exemple #1
0
def test_double_add_company(redis_client):
    ' Should only add a company once '
    company = MockCompany(account='foo', name='bar', profile_image_url='baz')
    db.write_company(redis_client, company)
    db.write_company(redis_client, company)
    companies = db.get_companies(redis_client)
    assert len(companies) == 1
def get_companies_details():
    ''' will get full list of companies, with details '''
    accounts = db.get_companies(r)
    companies_details = []

    for account in accounts:

        number_of_tweets = db.get_number_of_tweets(r, account)
        if number_of_tweets < 500:  # TODO: Replace me with env constant
            continue

        company_details = db.get_company_details(r, account)
        company_details['number_of_tweets'] = number_of_tweets
        company_details['score'] = db.get_company_score(r, account)
        companies_details.append(company_details)

    return jsonify({'companies': companies_details})
if len(sys.argv) < 2:
    usage()

r = redis.StrictRedis(host='redis', decode_responses=True)

if sys.argv[1] == 'init':
    del sys.argv[1]  # required for following fileinput

    for line in fileinput.input():
        company_name = line.strip()
        add_company(r, company_name)

elif sys.argv[1] == 'sync':
    # process all companies if params are not given
    companies = [sys.argv[2]] if len(sys.argv) > 2 else list(
        db.get_companies(r))

    sample(companies, N_COMPANIES_PER_RUN)

    sync_results = [
        sync_tweets(api,
                    r,
                    account,
                    limit=N_TWEETS_PER_RUN,
                    max_tweets=MAX_TWEETS) for account in companies
    ]

    for result in chain(*sync_results):
        print(result['account'], '+', result['tweet'].id, '-',
              result['removed'])
else:
import redis
from lib import db
import sys

r = redis.StrictRedis(host='redis', decode_responses=True)
for company in db.get_companies(r):
    try:
        print('company', company, 'has', db.get_number_of_tweets(r, company),
              'tweets')
        print('removing unneccessary')
        for tweet_id in db.get_tweets_timeline(r, company, withscores=False):
            tweet_score = db.get_tweet_score(r, company, tweet_id)
            if (tweet_score == 0.0):
                print('removing', tweet_id, tweet_score)
                db.remove_tweets(r, company, tweet_id)

        print('company', company, 'has', db.get_number_of_tweets(r, company),
              'tweets')
        print('score before', db.get_company_score(r, company))
        db.update_company_overall_score(r, company)
        print('score after', db.get_company_score(r, company))
    except Exception:
        r.delete('companyscore:{0}'.format(company))
        pass
Exemple #5
0
def test_add_company(redis_client, foo_company):
    ' Adding a company adds it a the list of companies '
    company = MockCompany(account='foo', name='bar', profile_image_url='baz')
    db.write_company(redis_client, company)
    companies = db.get_companies(redis_client)
    assert b'foo' in companies