# -*- coding: utf-8 -*- from flask import request, make_response from werkzeug import secure_filename import pbclient import os import pyutil import requests import tempfile import shutil class Wikilibras: def __init__(self, configuration, template_env): self.config = configuration self.env = template_env self.__setup_pb_client() def __setup_pb_client(self): pbclient.set('endpoint', self.config['PYBOSSA_ENDPOINT']) pbclient.set('api_key', self.config['PYBOSSA_API_KEY']) def __find_project(self, app_short_name): projects = pbclient.find_project(short_name=app_short_name) return projects[0] if len(projects) > 0 else None def __setup_project(self, project): self.__update_project_info(project) def __update_project_info(self, project): template = self.env.get_template('template.html') project.info['task_presenter'] = template.render(server=self.config['HOST_STATIC_FILES_ENDPOINT'], server_backend=self.config['HOST_ENDPOINT'], app_shortname=self.config['PYBOSSA_APP_SHORT_NAME'], api_host=self.config['API_HOST'], homepage_url=self.config['HOMEPAGE_URL'], upload_sign_host=self.config['UPLOAD_SIGN_HOST']) project.info['thumbnail'] = self.config['HOST_STATIC_FILES_ENDPOINT'] + "/img/thumbnail.png" project.info['sched'] = "incremental" project.info['published'] = True project.allow_anonymous_contributors = False pbclient.update_project(project) def create_project(self): app_short_name = self.config['PYBOSSA_APP_SHORT_NAME'] project = self.__find_project(app_short_name) result_msg = "" if (project): result_msg = "The project " + app_short_name + " was already created." else: project = pbclient.create_project(self.config['PYBOSSA_APP_NAME'], app_short_name, self.config['PYBOSSA_APP_DESCRIPTION']) if (project): self.__setup_project(project) result_msg = "The project " + app_short_name + " was created." else: result_msg = "The project " + app_short_name + " couldn't be created. Check the server log for details." pyutil.log(result_msg) return result_msg def update_project(self): app_short_name = self.config['PYBOSSA_APP_SHORT_NAME'] project = self.__find_project(app_short_name) self.__update_project_info(project) result_msg = "The project " + app_short_name + " was updated." pyutil.log(result_msg) return result_msg def __find_task(self, project_id, task_id): tasks = pbclient.find_tasks(project_id, id=task_id) return tasks[0] if len(tasks) > 0 else None def __find_taskruns(self, project_id, task_id): return pbclient.find_taskruns(project_id, id=task_id) def __number_of_taskruns(self, project_id, task_id): taskruns = self.__find_taskruns(project_id, task_id) return len(taskruns) def __close_task(self, project_id, task_id): pyutil.log("Closing the task with ID=" + str(task_id) + ".") task = self.__find_task(project_id, task_id) number_of_taskruns = self.__number_of_taskruns(project_id, task_id) task.n_answers = number_of_taskruns + 1 pbclient.update_task(task) return "The task with ID=" + str(task_id) + " was closed." def get_file(self, url, filename): r = requests.get(url, stream = True) if (r.status_code == 200): with open(filename, 'wb') as f: for chunk in r.iter_content(chunk_size = 1024): if chunk: f.write(chunk) return True return False def finish_task(self): task_id = request.form['task_id'] api_host = self.config['API_HOST'] api_dbhost = self.config['API_DB_HOST'] user_id = request.form['user_id'] sign_name = request.form['sign_name'] project_id = request.form['project_id'] number_of_approval = int(request.form['number_of_approval']) agreement_number = self.config['AGREEMENT_NUMBER'] result_msg = "" code = 200 if (number_of_approval >= agreement_number): tmp_dir = tempfile.NamedTemporaryFile().name if not os.path.exists(tmp_dir): os.makedirs(tmp_dir) blend_path = os.path.join(tmp_dir, sign_name + ".blend") video_path = os.path.join(tmp_dir, sign_name + ".webm") blend_downloaded = self.get_file(('%s/public/%s/%s.blend' % (api_host, user_id, sign_name)), blend_path) video_downloaded = self.get_file(('%s/public/%s/%s.webm' % (api_host, user_id, sign_name)), video_path) if (blend_downloaded and video_downloaded): files = [ ("video", (video_path, open(video_path,"rb"))), ("video", (blend_path, open(blend_path, "rb"))) ] values = { "nome": sign_name, "selo": 1, "wikilibras": True, "overwrite": True } r = requests.post(("%s/addsinal" % (api_dbhost)), files=files, data=values) shutil.rmtree(tmp_dir) result_msg = self.__close_task(project_id, task_id) else: result_msg = "The task with ID=" + str(task_id) + " didn't reach the agreement number yet." pyutil.log(result_msg) return make_response(result_msg, code)