Esempio n. 1
0
 def __init__(self):
     '''
     Add init() before send_alerts() to ensure it always use the same
     celery instance, despite flask multithrading
     '''
     tipi_tasks.init()
     tipi_tasks.alerts.send_alerts.apply_async()
Esempio n. 2
0
def save_alert(payload):
    alert = Alert.objects(email=payload['email']).first()
    if not alert:
        alert = Alert(
                id=generate_id(payload['email']),
                email=payload['email']
                )
        _add_search_to_alert(payload['search'], alert)
    else:
        searches = [s.search for s in alert.searches]
        search_exists = False
        for search in searches:
            if payload['search'] == search:
                search_exists = True
                break
        if search_exists:
            return
        _add_search_to_alert(payload['search'], alert)

    result = alert.save()
    if not result:
        raise Exception

    '''
    Add init() before validate() to ensure it always use the same
    celery instance, despite flask multithrading
    '''
    tipi_tasks.init()
    tipi_tasks.validate.send_validation_emails.apply_async()
Esempio n. 3
0
 def get(self, id):
     """Returns tagging task's result"""
     try:
         tipi_tasks.init()
         return tipi_tasks.tagger.check_status_task(id)
     except Exception:
         return {'Error': 'No task found'}, 404
    def tag_initiatives(self,
                        initiatives,
                        tags,
                        merge=False,
                        send_alerts=True):
        total = len(initiatives)
        for index, initiative in enumerate(initiatives):
            try:
                log.info(f"Tagging initiative {index+1} of {total}")
                tipi_tasks.init()
                title_result = tipi_tasks.tagger.extract_tags_from_text(
                    initiative['title'], tags)
                if 'result' not in title_result.keys():
                    continue
                title_result = title_result['result']
                if 'content' not in initiative:
                    result = title_result
                else:
                    text = '.'.join(initiative['content'])
                    body_result = tipi_tasks.tagger.extract_tags_from_text(
                        text, tags)
                    if 'result' not in body_result.keys():
                        continue
                    body_result = body_result['result']
                    result = self.__merge_results(title_result, body_result)
                new_tags = list(
                    map(
                        lambda x: Tag(topic=x['topic'],
                                      subtopic=x['subtopic'],
                                      tag=x['tag'],
                                      times=x['times']), result['tags']))

                if merge:
                    topics = list(set(initiative['topics'] + result['topics']))
                    new_tags = self.merge_tags(initiative['tags'], new_tags)
                else:
                    topics = result['topics']

                initiative['tags'] = new_tags
                initiative['topics'] = topics
                self.__delete_topics_with_one_tag_ocurrence(initiative)
                initiative['tagged'] = True
                initiative.save()
                if len(result['topics']) > 0 and USE_ALERTS and send_alerts:
                    create_alert(initiative)
            except Exception as e:
                log.error(f"Error tagging {initiative['id']}: {e}")
Esempio n. 5
0
    def post(self):
        """Returns a list of topics and tags matching the text."""
        try:
            cache_key = Config.CACHE_TAGS
            tags = cache.get(cache_key)
            if tags is None:
                tags = get_tags()
                cache.set(cache_key, tags, timeout=5 * 60)
            tags = codecs.encode(pickle.dumps(tags), "base64").decode()
            tipi_tasks.init()
            text = ''
            if 'text' in request.form and request.form['text']:
                text = request.form['text']
            else:
                if 'file' in request.files:
                    file_input = request.files['file']
                    with tempfile.NamedTemporaryFile(
                            prefix='tipiscanner_',
                            suffix=splitext(file_input.filename)[1]) as f:
                        f.write(file_input.stream.read())
                        text = process(f.name).decode('utf-8').strip()
                        f.close()
                    if not text:
                        abort(
                            400,
                            "Error al obtener el texto del fichero proporcionado. Pruebe con otro fichero."
                        )
            text_length = len(text.split())

            if text_length >= Config.TAGGER_MAX_WORDS:
                task = tipi_tasks.tagger.extract_tags_from_text.apply_async(
                    (text, tags))
                eta_time = int((text_length / 1000) * 4)
                task_id = task.id
                result = {
                    'status': 'PROCESSING',
                    'task_id': task_id,
                    'estimated_time': eta_time
                }
            else:
                result = tipi_tasks.tagger.extract_tags_from_text(text, tags)
            return result
        except Exception as e:
            if hasattr(e, 'code') and hasattr(e, 'description'):
                abort(e.code, e.description)
            else:
                abort(500, "Internal server error")
Esempio n. 6
0
def hello():
    tipi_tasks.init()
    tipi_tasks.test_task.apply_async((3, 2))
    return "Hello World!"