mark_to_delete.py
1.74 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
44
45
46
47
48
49
50
#-*- coding:utf-8 -*-
import random
import time
from django.core.management.base import BaseCommand
from verification.duckduckgo import DuckDuckGo
from webapp.models import Meaning, MeaningStatus
SOURCE = 'szarada'
class Command(BaseCommand):
help = 'Mark expressions to delete.'
def handle(self, *args, **options):
mark_to_delete()
def mark_to_delete():
duckduckgo = DuckDuckGo()
meanings = Meaning.objects.filter(expressions__link__source__key=SOURCE).all()
while meanings:
meanings = check_meanings(duckduckgo, meanings)
def check_meanings(duckduckgo, meanings):
check_again_meanings = []
for meaning in meanings:
meaning.comment = ''
meaning.status = None
meaning.save()
for catchword in meaning.expressions.filter(is_catchword=True):
for expression in meaning.expressions.exclude(is_catchword=True):
time.sleep(random.uniform(0.5, 3.0))
try:
if not duckduckgo.check_entry(catchword, expression):
print 'Erase:\t', catchword.orth_text, u'\t-->\t', expression.orth_text
if not meaning.status:
meaning.comment = u'Do usunięcia:\t%s' % expression.orth_text
meaning.status = MeaningStatus.objects.get(key='delete')
meaning.save()
else:
print 'OK:\t', catchword.orth_text, u'\t-->\t', expression.orth_text
except RuntimeError:
print 'Try again:\t', catchword.orth_text, u'\t-->\t', expression.orth_text
check_again_meanings.append(meaning)
break
return check_again_meanings