add_DDG_expr_responses.py 1.13 KB
# -*- coding:utf-8 -*-

import random
import time

from django.core.management.base import BaseCommand

from verification.duckduckgo import DuckDuckGo
from webapp.models import Expression, Source

# oznaczono: 1M, PSC
SOURCE = Source.objects.get(key='sjp')


class Command(BaseCommand):
    help = 'Add DDG entry information to expressions.'

    def handle(self, *args, **options):
        add_DDG_expr_responses_info()


def add_DDG_expr_responses_info():
    print (SOURCE)

    expressions = Expression.objects.filter(link__source=SOURCE, DDG_responses=1000)

    check_exprs_meanings = []
    duckduckgo = DuckDuckGo(25)

    for expr in expressions.order_by('text'):
        print (expr.text)
        time.sleep(random.uniform(2.0, 4.0))
        try:
            responses = duckduckgo.expression_responses_count(expr)
            expr.DDG_responses = responses
            expr.save()

            print (expr.DDG_responses, expr.orth_text)
        except RuntimeError:
            check_exprs_meanings.append(expr)

    if len(check_exprs_meanings) > 0:
        print ('Check again expressions %d: run script again!' % len(check_exprs_meanings))