add_DDG_expr_responses.py
1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# -*- 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))