add_DDG_entry_responses_info.py 2.23 KB
# -*- coding:utf-8 -*-

import random
import time

from django.core.management.base import BaseCommand

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


SOURCE = Source.objects.get(key='PSC')


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

    def handle(self, *args, **options):
        # check_meanings_with_multiple_sources()
        catchword_verification()
        add_DDG_entry_responses_info_meanings()


def check_meanings_with_multiple_sources():
    for meaning in Meaning.objects.order_by('id'):
        sources = []
        for expr in meaning.expressions.all():
            if expr.link.source.key not in sources:
                sources.append(expr.link.source.key)
        if len(sources) > 1:
            print u'!!!!!!!!!! [%s] !!!!!!!!!!' % ', '.join(sources)
            print meaning


def catchword_verification():
    print (SOURCE)

    for meaning in Meaning.objects.filter(expressions__link__source=SOURCE).distinct():
        if meaning.expressions.filter(is_catchword=True).count() > 1:
            print meaning


def add_DDG_entry_responses_info_meanings():
    print (SOURCE)

    meanings = Meaning.objects.filter(expressions__link__source=SOURCE).filter(expressions__DDG_entry_responses=1000).distinct()

    check_again_meanings = []
    duckduckgo = DuckDuckGo(25)

    for meaning in meanings.order_by('id'):
        print (meaning.id)
        for catchword in meaning.expressions.filter(is_catchword=True):

            for definition in meaning.expressions.exclude(pk=catchword.pk):
                time.sleep(random.uniform(2.0, 4.0))
                try:
                    responses = duckduckgo.entry_responses_count(catchword, definition)

                    definition.DDG_entry_responses = responses
                    definition.save()

                    catchword.DDG_entry_responses = responses
                    catchword.save()

                    print (definition.DDG_entry_responses, catchword.orth_text, definition.orth_text)
                except RuntimeError:
                    check_again_meanings.append(meaning)

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