wikilibras.py
5.32 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# -*- 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)