duckduckgo.py 2.63 KB
import codecs
import os

from tempfile import mkdtemp, mkstemp
from subprocess import check_call

import settings


class DuckDuckGo:

    def check_expression(self, expression):
        tmp_folder = mkdtemp()
        tmp_response_file, tmp_response_filename = mkstemp(dir=tmp_folder)
        tmp_error_file, tmp_error_filename = mkstemp(dir=tmp_folder)

        expression_query = self.__get_expression_query(expression)

        check_call(['ddgr',
                    '-n', str(settings.DUCKDUCKGO_MIN_HITS),
                    '--json',
                    expression_query], stdout=tmp_response_file, stderr=tmp_error_file)

        os.close(tmp_response_file)
        response_reader = codecs.open(tmp_response_filename, 'rt', encoding='utf-8')

        os.close(tmp_error_file)
        error_reader = codecs.open(tmp_error_filename, 'rt', encoding='utf-8')
        for line in error_reader:
            raise RuntimeError('Error: 403')

        linecount = 0
        for line in response_reader:
            linecount += 1

        if linecount > 5*settings.DUCKDUCKGO_MIN_HITS:
            return True

        return False

    def check_entry(self, catchword, definition):
        tmp_folder = mkdtemp()
        tmp_response_file, tmp_response_filename = mkstemp(dir=tmp_folder)
        tmp_error_file, tmp_error_filename = mkstemp(dir=tmp_folder)

        catchword_query = self.__get_expression_query(catchword)
        definition_query = self.__get_expression_query(definition)

        check_call(['ddgr',
                    '-n', str(settings.DUCKDUCKGO_MIN_HITS),
                    '--json',
                    catchword_query, definition_query], stdout=tmp_response_file, stderr=tmp_error_file)

        os.close(tmp_response_file)
        response_reader = codecs.open(tmp_response_filename, 'rt', encoding='utf-8')

        os.close(tmp_error_file)
        error_reader = codecs.open(tmp_error_filename, 'rt', encoding='utf-8')
        for line in error_reader:
            raise RuntimeError('Error: 403')

        linecount = 0
        for line in response_reader:
            linecount += 1

        if linecount > 5*settings.DUCKDUCKGO_MIN_HITS:
            return True

        return False

    def __get_expression_query(self, expression):
        expr = ''
        expr_segments = expression.segments.order_by('position_in_expr')
        for expr_seg in expr_segments:

            orth = expr_seg.orth
            if expr_seg.ctag == 'interp' and expr_seg.orth != '"':
                orth = u'\\%s' % orth

            if expr_seg.has_nps:
                expr += orth
            else:
                expr += ' %s' % orth

        return u'\"' + expr + u'\"'