def __init__(self, options=None): self.get_configuration(options) pbclient.set('api_key', self.options.api_key) pbclient.set('endpoint', self.options.api_url) if self.options.verbose: print('Running against PyBosssa instance at: %s' % self.options.api_url) print('Using API-KEY: %s' % self.options.api_key) if self.options.create_app: pbclient.create_app(self.app_config['name'], self.app_config['short_name'], self.app_config['description']) self.setup_app() else: self.find_app_by_short_name() if self.options.create_task: self.create_task() if self.options.update_template: print "Updating app template" # discard return value self.setup_app() if self.options.update_tasks: def tasks(): offset = 0 limit = 100 while True: tasks = pbclient.get_tasks(self.app.id, offset=offset, limit=limit) if len(tasks) == 0: break for task in tasks: yield task offset += len(tasks) def update_task(task, count): print "Updating task: %s" % task.id if 'n_answers' in task.info: del (task.info['n_answers']) task.n_answers = self.options.update_tasks pbclient.update_task(task) count[0] += 1 print "Updating task n_answers" find_app_by_short_name() n_tasks = [0] [update_task(t, n_tasks) for t in tasks()] print "%s Tasks have been updated!" % n_tasks[0]
def __init__(self, options = None): self.get_configuration(options) pbclient.set('api_key', self.options.api_key) pbclient.set('endpoint', self.options.api_url) if self.options.verbose: print('Running against PyBosssa instance at: %s' % self.options.api_url) print('Using API-KEY: %s' % self.options.api_key) if self.options.create_app: pbclient.create_app(self.app_config['name'], self.app_config['short_name'], self.app_config['description']) self.setup_app() else: self.find_app_by_short_name() if self.options.create_task: self.create_task() if self.options.update_template: print "Updating app template" # discard return value self.setup_app() if self.options.update_tasks: def tasks(): offset = 0 limit = 100 while True: tasks = pbclient.get_tasks(self.app.id, offset=offset, limit=limit) if len(tasks) == 0: break for task in tasks: yield task offset += len(tasks) def update_task(task, count): print "Updating task: %s" % task.id if 'n_answers' in task.info: del(task.info['n_answers']) task.n_answers = self.options.update_tasks pbclient.update_task(task) count[0] += 1 print "Updating task n_answers" find_app_by_short_name() n_tasks = [0] [update_task(t, n_tasks) for t in tasks()] print "%s Tasks have been updated!" % n_tasks[0]
app.long_description = contents('long_description.html') app.info['task_presenter'] = contents('template.html') app.info['thumbnail'] = app_config['thumbnail'] app.info['tutorial'] = contents('tutorial.html') try: response = pbclient.update_app(app) check_api_error(response) return app except: format_error("pbclient.update_app", response) if options.create_app: try: response = pbclient.create_app(app_config['name'], app_config['short_name'], app_config['description']) check_api_error(response) except: format_error("pbclient.create_app", response) app = setup_app() print ("You can import now the tasks directly from the PyBossa server:") print "%s/app/%s/tasks/import?template=epicollect" % (options.api_url, app_config['short_name']) if options.update_template: print "Updating app template" # discard return value setup_app()
def run(app_config, options): def check_api_error(api_response): """Check if returned API response contains an error""" if type(api_response) == dict and (api_response.get('status') == 'failed'): raise exceptions.HTTPError def format_error(module, error): """Format the error for the given module""" logging.error(module) # Beautify JSON error if type(error) == list: print "Application not found" else: print json.dumps(error, sort_keys=True, indent=4, separators=(',', ': ')) exit(1) def find_app_by_short_name(): try: response = pbclient.find_app(short_name=app_config['short_name']) check_api_error(response) return response[0] except: format_error("pbclient.find_app", response) def setup_app(): app = find_app_by_short_name() app.long_description = contents('long_description.html') app.info['task_presenter'] = contents('template.html') app.info['thumbnail'] = app_config['thumbnail'] app.info['tutorial'] = contents('tutorial.html') app.category_id = 8 try: response = pbclient.update_app(app) check_api_error(response) return app except: format_error("pbclient.update_app", response) def create_photo_task(app, photo, question, priority=0): # Data for the tasks # task_info = dict(question=question) # task_info.update(photo) task_info = photo try: response = pbclient.create_task(app.id, task_info, priority_0=priority) check_api_error(response) except: format_error("pbclient.create_task", response) def add_photo_tasks(app): # First of all we get the URL photos # Then, we have to create a set of tasks for the application # For this, we get first the photo URLs from Flickr photos = get_flickr_set_photos( options.photoset_id) # photos = get_flickr_photos() question = app_config['question'] #[create_photo_task(app, p, question, priority=random.random()) for p in photos] for p in photos: create_photo_task(app, p, question) print "Creating task..." sleep(4) pbclient.set('api_key', options.api_key) pbclient.set('endpoint', options.api_url) if options.verbose: print('Running against PyBosssa instance at: %s' % options.api_url) print('Using API-KEY: %s' % options.api_key) if options.create_app or options.add_more_tasks: if options.create_app: try: response = pbclient.create_app(app_config['name'], app_config['short_name'], app_config['description']) check_api_error(response) app = setup_app() except: format_error("pbclient.create_app", response) else: app = find_app_by_short_name() add_photo_tasks(app) if options.update_template: print "Updating app template" # discard return value setup_app() if options.update_tasks: def tasks(app): offset = 0 limit = 100 while True: try: tasks = pbclient.get_tasks(app.id, offset=offset, limit=limit) check_api_error(tasks) if len(tasks) == 0: break for task in tasks: yield task offset += len(tasks) except: format_error("pbclient.get_tasks", response) def update_task(task, count): print "Updating task: %s" % task.id if 'n_answers' in task.info: del (task.info['n_answers']) task.n_answers = options.update_tasks try: response = pbclient.update_task(task) check_api_error(response) count[0] += 1 except: format_error("pbclient.update_task", response) print "Updating task n_answers" app = find_app_by_short_name() n_tasks = [0] [update_task(t, n_tasks) for t in tasks(app)] print "%s Tasks have been updated!" % n_tasks[0]
def run(app_config, options): def check_api_error(api_response): """Check if returned API response contains an error""" if type(api_response) == dict and (api_response.get("status") == "failed"): raise exceptions.HTTPError def format_error(module, error): """Format the error for the given module""" logging.error(module) # Beautify JSON error if type(error) == list: print "Application not found" else: print json.dumps(error, sort_keys=True, indent=4, separators=(",", ": ")) exit(1) def find_app_by_short_name(): try: response = pbclient.find_app(short_name=app_config["short_name"]) check_api_error(response) return response[0] except: format_error("pbclient.find_app", response) def setup_app(): app = find_app_by_short_name() app.long_description = contents("long_description.html") app.category_id = 5 app.hidden = 1 app.info["task_presenter"] = contents("template.html") app.info["thumbnail"] = app_config["thumbnail"] app.info["tutorial"] = contents("tutorial.html") try: response = pbclient.update_app(app) check_api_error(response) return app except: format_error("pbclient.update_app", response) def create_photo_task(app, photo, question, priority=0): # Data for the tasks task_info = photo try: response = pbclient.create_task(app.id, task_info, priority_0=priority) check_api_error(response) except: format_error("pbclient.create_task", response) def add_photo_tasks(app): # First of all we get the URL photos # Then, we have to create a set of tasks for the application # For this, we get first the photo URLs from Flickr photos = get_flickr_set_photos(options.photoset_id) question = app_config["question"] # [create_photo_task(app, p, question, priority=random.random()) for p in photos] # Estimate how many minutes it has to wait before reaching the limit # Limit is 300 tasks per 15 minutes wait = (15 * 60) / 300 if len(photos) > 300: wait = wait + 1 # Adding one second will take 20 minutes to add 300 tasks print "Wait %s seconds between each create_task request" % wait for p in photos: create_photo_task(app, p, question, priority=0) time.sleep(wait) pbclient.set("api_key", options.api_key) pbclient.set("endpoint", options.api_url) if options.verbose: print ("Running against PyBosssa instance at: %s" % options.api_url) print ("Using API-KEY: %s" % options.api_key) if options.create_app or options.add_more_tasks: if options.create_app: try: response = pbclient.create_app(app_config["name"], app_config["short_name"], app_config["description"]) check_api_error(response) app = setup_app() except: format_error("pbclient.create_app", response) else: app = find_app_by_short_name() add_photo_tasks(app) if options.update_template: print "Updating app template" # discard return value setup_app() if options.update_tasks: def tasks(app): offset = 0 limit = 100 while True: try: tasks = pbclient.get_tasks(app.id, offset=offset, limit=limit) check_api_error(tasks) if len(tasks) == 0: break for task in tasks: yield task offset += len(tasks) except: format_error("pbclient.get_tasks", response) def update_task(task, count): print "Updating task: %s" % task.id if "n_answers" in task.info: del (task.info["n_answers"]) task.n_answers = options.update_tasks try: response = pbclient.update_task(task) check_api_error(response) count[0] += 1 except: format_error("pbclient.update_task", response) print "Updating task n_answers" app = find_app_by_short_name() n_tasks = [0] [update_task(t, n_tasks) for t in tasks(app)] print "%s Tasks have been updated!" % n_tasks[0]
if not options.api_key: parser.error("You must supply an API-KEY to create an \ applicationa and tasks in PyBossa") else: pbclient.set('api_key', options.api_key) if (options.verbose): print('Running against PyBosssa instance at: %s' % options.api_url) print('Using API-KEY: %s' % options.api_key) if not options.n_answers: options.n_answers = 30 if options.create_app: pbclient.create_app('Flickr Person Finder', 'flickrperson', 'Do you see a human in this photo?') app = pbclient.find_app(short_name='flickrperson')[0] app.long_description = open('long_description.html').read() app.info['task_presenter'] = open('template.html').read() app.info[ 'thumbnail'] = "http://img37.imageshack.us/img37/156/flickrpersonthumbnail.png" pbclient.update_app(app) # First of all we get the URL photos photos = get_flickr_photos() # Finally, we have to create a set of tasks for the application # For this, we get first the photo URLs from Flickr for i in xrange(100): for photo in photos: # Data for the tasks task_info = dict(question="Do you see a human in this photo?",
def __init__(self, options = None): if options: self.options = options else: self.handle_arguments() if self.options.list: for appjson in glob.glob(os.path.join(os.path.dirname(__file__), "*", "app.json")): with open(appjson) as f: config = json.load(f) config['dirname'] = os.path.split(os.path.split(appjson)[0])[1] print """%(dirname)s %(name)s %(description)s """ % config return self.get_configuration() self.handle_result(pbclient.set('api_key', self.options.api_key)) self.handle_result(pbclient.set('endpoint', self.options.api_url)) if self.options.verbose: print('Running against PyBosssa instance at: %s' % self.options.api_url) print('Using API-KEY: %s' % self.options.api_key) if self.options.create_app: self.handle_result( pbclient.create_app(self.app_config['name'], self.app_config['short_name'], self.app_config['description'])) self.setup_app() else: self.find_app_by_short_name() if self.options.load_tasks: self.load_tasks() if self.options.create_task: self.create_task() if self.options.update_template: print "Updating app template" # discard return value self.setup_app() if self.options.update_tasks: def tasks(): offset = 0 limit = 100 while True: tasks = self.handle_result(pbclient.get_tasks(self.app.id, offset=offset, limit=limit)) if len(tasks) == 0: break for task in tasks: yield task offset += len(tasks) def update_task(task, count): print "Updating task: %s" % task.id if 'n_answers' in task.info: del(task.info['n_answers']) task.n_answers = self.options.update_tasks self.handle_result(pbclient.update_task(task)) count[0] += 1 print "Updating task n_answers" self.find_app_by_short_name() n_tasks = [0] [update_task(t, n_tasks) for t in tasks()] print "%s Tasks have been updated!" % n_tasks[0]
def run(app_config, options): def check_api_error(api_response): """Check if returned API response contains an error""" if type(api_response) == dict and (api_response.get('status') == 'failed'): raise exceptions.HTTPError def format_error(module, error): """Format the error for the given module""" logging.error(module) # Beautify JSON error if type(error) == list: print "Application not found" else: print json.dumps(error, sort_keys=True, indent=4, separators=(',', ': ')) exit(1) def find_app_by_short_name(): try: response = pbclient.find_app(short_name=app_config['short_name']) check_api_error(response) return response[0] except: format_error("pbclient.find_app", response) def setup_app(): app = find_app_by_short_name() app.long_description = contents('long_description.html') app.info['task_presenter'] = contents('template.html') app.info['thumbnail'] = app_config['thumbnail'] #Cuidado que tira el icono de la aplicacion app.info['tutorial'] = contents('tutorial.html') try: response = pbclient.update_app(app) check_api_error(response) return app except: format_error("pbclient.update_app", response) def create_photo_task(app, photo, question, priority=0): # Data for the tasks task_info = dict( n_answers=options.n_answers, idiss=photo['idISS'], link_big=photo['link_big'], link_small=photo['link_small'], linkData=photo['linkData'], citylon=photo['citylon'], citylat=photo['citylat'], focal=photo['focal']) try: response = pbclient.create_task(app.id, task_info, priority_0=priority) check_api_error(response) except: #response = pbclient.create_task(app.id, task_info, priority_0=priority) format_error("pbclient.create_task", response) def add_photo_tasks(app): photos = get_iss_photos( ) question = app_config['question'] [create_photo_task(app, p, question, priority=random.random()) for p in photos] pbclient.set('api_key', options.api_key) pbclient.set('endpoint', options.api_url) if options.verbose: print('Running against PyBosssa instance at: %s' % options.api_url) print('Using API-KEY: %s' % options.api_key) if options.create_app or options.add_more_tasks: if options.create_app: try: response = pbclient.create_app(app_config['name'], app_config['short_name'], app_config['description']) check_api_error(response) app = setup_app() except: format_error("pbclient.create_app", response) else: app = find_app_by_short_name() add_photo_tasks(app) if options.update_template: print "Updating app template" # discard return value setup_app() if options.update_tasks: def tasks(app): offset = 0 limit = 100 while True: try: tasks = pbclient.get_tasks(app.id, offset=offset, limit=limit) check_api_error(tasks) if len(tasks) == 0: break for task in tasks: yield task offset += len(tasks) except: format_error("pbclient.get_tasks", response) def update_task(task, count): print "Updating task: %s" % task.id if 'n_answers' in task.info: del(task.info['n_answers']) task.n_answers = options.update_tasks try: response = pbclient.update_task(task) check_api_error(response) count[0] += 1 except: format_error("pbclient.update_task", response) print "Updating task n_answers" app = find_app_by_short_name() n_tasks = [0] [update_task(t, n_tasks) for t in tasks(app)] print "%s Tasks have been updated!" % n_tasks[0]
def run(app_config, options): def find_app_by_short_name(): return pbclient.find_app(short_name=app_config['short_name'])[0] def setup_app(): app = find_app_by_short_name() app.long_description = contents('long_description.html') app.info['task_presenter'] = contents('template.html') app.info['thumbnail'] = app_config['thumbnail'] app.info['tutorial'] = contents('tutorial.html') pbclient.update_app(app) return app def create_tweet_task(app, tweet, question): # Data for the tasks task_info = dict(question=question, n_answers=options.n_answers, tweet=tweet) pbclient.create_task(app.id, task_info) def add_tweet_tasks(app): tweets = get_tweets() question = app_config['question'] [create_tweet_task(app, t, question) for t in tweets] pbclient.set('api_key', options.api_key) pbclient.set('endpoint', options.api_url) if options.verbose: print('Running against PyBosssa instance at: %s' % options.api_url) print('Using API-KEY: %s' % options.api_key) if options.create_app or options.add_more_tasks: if options.create_app: pbclient.create_app(app_config['name'], app_config['short_name'], app_config['description']) app = setup_app() else: app = find_app_by_short_name() add_tweet_tasks(app) if options.update_template: print "Updating app template" # discard return value setup_app() if options.update_tasks: def tasks(app): offset = 0 limit = 100 while True: tasks = pbclient.get_tasks(app.id, offset=offset, limit=limit) if len(tasks) == 0: break for task in tasks: yield task offset += len(tasks) def update_task(task, count): print "Updating task: %s" % task.id if 'n_answers' in task.info: del (task.info['n_answers']) task.n_answers = options.update_tasks pbclient.update_task(task) count[0] += 1 print "Updating task n_answers" app = find_app_by_short_name() n_tasks = [0] [update_task(t, n_tasks) for t in tasks(app)] print "%s Tasks have been updated!" % n_tasks[0]
def run(app_config, options): def find_app_by_short_name(): return pbclient.find_app(short_name=app_config['short_name'])[0] def setup_app(): app = find_app_by_short_name() app.long_description = contents('long_description.html') app.info['task_presenter'] = contents('template.html') app.info['thumbnail'] = app_config['thumbnail'] app.info['tutorial'] = contents('tutorial.html') pbclient.update_app(app) return app def create_msg_task(app, msg, question): # Data for the tasks # msgs_text and msgs_html are lists, hence 'msgs' not 'msg'. # msg_subject and msg_date are simple strings. task_info = dict(question=question, n_answers=options.n_answers, msgs_text=msg['msgs_text'], msgs_html=msg['msgs_html'], msg_subject=msg['msg_subject'], msg_date=msg['msg_date']) print task_info['msg_subject'] print len(pbclient.get_tasks(app.id)) # from erpy.ipshell import ipshell # ipshell('here') # sys.exit() # return pbclient.create_task(app.id, task_info) def add_msg_tasks(app): # First of all we get the emails. # Then, we have to create a set of tasks for the application # For this, we get first the email messages from local offlineimap dir. msgs = get_emails() question = app_config['question'] [create_msg_task(app, m, question) for m in msgs] pbclient.set('api_key', options.api_key) pbclient.set('endpoint', options.api_url) if options.verbose: print('Running against PyBosssa instance at: %s' % options.api_url) print('Using API-KEY: %s' % options.api_key) if options.create_app or options.add_more_tasks: if options.create_app: pbclient.create_app(app_config['name'], app_config['short_name'], app_config['description']) app = setup_app() else: app = find_app_by_short_name() add_msg_tasks(app) if options.update_template: print "Updating app template" # discard return value setup_app() if options.update_tasks: def tasks(app): offset = 0 limit = 100 while True: tasks = pbclient.get_tasks(app.id, offset=offset, limit=limit) if len(tasks) == 0: break for task in tasks: yield task offset += len(tasks) def update_task(task, count): print "Updating task: %s" % task.id if 'n_answers' in task.info: del (task.info['n_answers']) task.n_answers = options.update_tasks pbclient.update_task(task) count[0] += 1 print "Updating task n_answers" app = find_app_by_short_name() n_tasks = [0] [update_task(t, n_tasks) for t in tasks(app)] print "%s Tasks have been updated!" % n_tasks[0]
def run(app_config, options): def find_app_by_short_name(): return pbclient.find_app(short_name=app_config['short_name'])[0] def setup_app(): app = find_app_by_short_name() app.long_description = contents('long_description.html') app.info['task_presenter'] = contents('template.html') app.info['thumbnail'] = app_config['thumbnail'] app.info['tutorial'] = contents('tutorial.html') pbclient.update_app(app) return app def create_photo_task(app, photo, question): # Data for the tasks task_info = dict(question=question, n_answers=options.n_answers, link=photo) pbclient.create_task(app.id, task_info) def add_photo_tasks(app): # First of all we get the URL photos # Then, we have to create a set of tasks for the application # For this, we get first the photo URLs from Flickr photos = [ 'http://i.imgur.com/4RvBQIF.png', 'http://i.imgur.com/rRPPKKq.png', 'http://i.imgur.com/GYsQWYQ.png', 'http://i.imgur.com/gcfzAAz.png', 'http://i.imgur.com/gcfzAAz.png', 'http://i.imgur.com/14tJo6K.png', 'http://i.imgur.com/oozn3FT.png', 'http://i.imgur.com/dWWLc33.png', 'http://i.imgur.com/5RFsuhC.png', 'http://i.imgur.com/ZRQHoVM.png', 'http://i.imgur.com/3cUziEs.png' ] question = app_config['question'] [create_photo_task(app, p, question) for p in photos] pbclient.set('api_key', options.api_key) pbclient.set('endpoint', options.api_url) if options.verbose: print('Running against PyBosssa instance at: %s' % options.api_url) print('Using API-KEY: %s' % options.api_key) if options.create_app or options.add_more_tasks: if options.create_app: pbclient.create_app(app_config['name'], app_config['short_name'], app_config['description']) app = setup_app() else: app = find_app_by_short_name() add_photo_tasks(app) if options.update_template: print "Updating app template" # discard return value setup_app() if options.update_tasks: def tasks(app): offset = 0 limit = 100 while True: tasks = pbclient.get_tasks(app.id, offset=offset, limit=limit) if len(tasks) == 0: break for task in tasks: yield task offset += len(tasks) def update_task(task, count): print "Updating task: %s" % task.id if 'n_answers' in task.info: del (task.info['n_answers']) task.n_answers = options.update_tasks pbclient.update_task(task) count[0] += 1 print "Updating task n_answers" app = find_app_by_short_name() n_tasks = [0] [update_task(t, n_tasks) for t in tasks(app)] print "%s Tasks have been updated!" % n_tasks[0]
def run(app_config, options): def check_api_error(api_response): """Check if returned API response contains an error""" if type(api_response) == dict and (api_response.get('status') == 'failed'): raise exceptions.HTTPError def format_error(module, error): """Format the error for the given module""" logging.error(module) # Beautify JSON error if type(error) == list: print "Application not found" else: print json.dumps(error, sort_keys=True, indent=4, separators=(',', ': ')) exit(1) def find_app_by_short_name(): try: response = pbclient.find_app(short_name=app_config['short_name']) check_api_error(response) return response[0] except: format_error("pbclient.find_app", response) def setup_app(): app = find_app_by_short_name() app.long_description = contents('long_description.html') app.info['task_presenter'] = contents('template.html') app.info['thumbnail'] = app_config['thumbnail'] app.info['tutorial'] = contents('tutorial.html') try: response = pbclient.update_app(app) check_api_error(response) return app except: format_error("pbclient.update_app", response) def create_graph_task(app, graph, question, priority=0): # Data for the tasks task_info = graph try: response = pbclient.create_task(app.id, task_info, priority_0=priority, n_answers=options.n_answers) #if int(response.headers['X-Rate-Limit']) < 10: # print "We are close to hit the maximum rate limit" # print "Sleeping 5 minutes before adding more tasks" # sleep(300) check_api_error(response) except: format_error("pbclient.create_task", response) def add_graph_levels(app): # First of all we get the URL photos # Then, we have to create a set of tasks for the application # For this, we get first the photo URLs from Flickr data = graphs.get() question = app_config['question'] [create_graph_task(app, g, question, priority=random.random()) for g in data] pbclient.set('api_key', options.api_key) pbclient.set('endpoint', options.api_url) if options.verbose: print('Running against PyBosssa instance at: %s' % options.api_url) print('Using API-KEY: %s' % options.api_key) if options.create_app or options.add_more_tasks: if options.create_app: try: response = pbclient.create_app(app_config['name'], app_config['short_name'], app_config['description']) check_api_error(response) app = setup_app() except: format_error("pbclient.create_app", response) else: app = find_app_by_short_name() add_graph_levels(app) if options.update_template: print "Updating app template" # discard return value setup_app() if options.update_tasks: def tasks(app): offset = 0 limit = 100 while True: try: tasks = pbclient.get_tasks(app.id, offset=offset, limit=limit) check_api_error(tasks) if len(tasks) == 0: break for task in tasks: yield task offset += len(tasks) except: format_error("pbclient.get_tasks", response) def update_task(task, count): print "Updating task: %s" % task.id if 'n_answers' in task.info: del(task.info['n_answers']) task.n_answers = options.update_tasks try: response = pbclient.update_task(task) check_api_error(response) count[0] += 1 except: format_error("pbclient.update_task", response) print "Updating task n_answers" app = find_app_by_short_name() n_tasks = [0] [update_task(t, n_tasks) for t in tasks(app)] print "%s Tasks have been updated!" % n_tasks[0]
if not options.api_key: parser.error("You must supply an API-KEY to create an \ applicationa and tasks in PyBossa") else: pbclient.set('api_key', options.api_key) if (options.verbose): print('Running against PyBosssa instance at: %s' % options.api_url) print('Using API-KEY: %s' % options.api_key) if not options.n_answers: options.n_answers = 30 if options.create_app: pbclient.create_app('Flickr Person Finder', 'flickrperson', 'Do you see a human in this photo?') app = pbclient.find_app(short_name='flickrperson')[0] app.long_description = open('long_description.html').read() app.info['task_presenter'] = open('template.html').read() app.info['thumbnail'] = "http://img37.imageshack.us/img37/156/flickrpersonthumbnail.png" pbclient.update_app(app) # First of all we get the URL photos photos = get_flickr_photos() # Finally, we have to create a set of tasks for the application # For this, we get first the photo URLs from Flickr for i in xrange(100): for photo in photos: # Data for the tasks task_info = dict(question="Do you see a human in this photo?",
def run(app_config, options): def find_app_by_short_name(): return pbclient.find_app(short_name=app_config['short_name'])[0] def setup_app(): app = find_app_by_short_name() app.long_description = contents('long_description.html') app.info['task_presenter'] = contents('template.html') app.info['thumbnail'] = app_config['thumbnail'] app.info['tutorial'] = contents('tutorial.html') pbclient.update_app(app) return app def create_msg_task(app, msg, question): # Data for the tasks # msgs_text and msgs_html are lists, hence 'msgs' not 'msg'. # msg_subject and msg_date are simple strings. task_info = dict(question=question, n_answers=options.n_answers, msgs_text=msg['msgs_text'], msgs_html=msg['msgs_html'], msg_subject=msg['msg_subject'], msg_date=msg['msg_date']) print task_info['msg_subject'] print len(pbclient.get_tasks(app.id)) # from erpy.ipshell import ipshell # ipshell('here') # sys.exit() # return pbclient.create_task(app.id, task_info) def add_msg_tasks(app): # First of all we get the emails. # Then, we have to create a set of tasks for the application # For this, we get first the email messages from local offlineimap dir. msgs = get_emails() question = app_config['question'] [create_msg_task(app, m, question) for m in msgs] pbclient.set('api_key', options.api_key) pbclient.set('endpoint', options.api_url) if options.verbose: print('Running against PyBosssa instance at: %s' % options.api_url) print('Using API-KEY: %s' % options.api_key) if options.create_app or options.add_more_tasks: if options.create_app: pbclient.create_app(app_config['name'], app_config['short_name'], app_config['description']) app = setup_app() else: app = find_app_by_short_name() add_msg_tasks(app) if options.update_template: print "Updating app template" # discard return value setup_app() if options.update_tasks: def tasks(app): offset = 0 limit = 100 while True: tasks = pbclient.get_tasks(app.id, offset=offset, limit=limit) if len(tasks) == 0: break for task in tasks: yield task offset += len(tasks) def update_task(task, count): print "Updating task: %s" % task.id if 'n_answers' in task.info: del(task.info['n_answers']) task.n_answers = options.update_tasks pbclient.update_task(task) count[0] += 1 print "Updating task n_answers" app = find_app_by_short_name() n_tasks = [0] [update_task(t, n_tasks) for t in tasks(app)] print "%s Tasks have been updated!" % n_tasks[0]
print("Using default template: template.html") options.template = "template.html" if not options.tutorial: print("Using default tutorial template: tutorial.html") options.tutorial= "tutorial.html" if (options.verbose): print('Running against PyBosssa instance at: %s' % options.api_url) print('Using API-KEY: %s' % options.api_key) if (options.create): app = pbclient.find_app(short_name='deforestedareas') if len(app)!=0 : pbclient.delete_app(app[0].id) pbclient.create_app('Deforestation', 'deforestedareas', 'Help us to find deforested areas in the forest') app = pbclient.find_app(short_name='deforestedareas')[0] print app.id app.long_description = open('long_description.html').read() app.info['task_presenter'] = open(options.template).read() app.info['thumbnail'] = "http://img204.imageshack.us/img204/6598/deforestation.png" app.info['tutorial'] = open(options.tutorial).read() pbclient.update_app(app) # Now add some tasks i = 0 for t in completed_tasks: #maxVert, maxHor, minVert, minHor = map(float, tile.split(" ")) #data = [minHor, minVert, maxHor, maxVert] #t = dict(question="Mark deforested areas in this tile", # name=str(i),
def run(app_config, options): def check_api_error(api_response): """Check if returned API response contains an error""" if type(api_response) == dict and (api_response.get('status') == 'failed'): raise exceptions.HTTPError def format_error(module, error): """Format the error for the given module""" logging.error(module) # Beautify JSON error if type(error) == list: print "Application not found" else: print json.dumps(error, sort_keys=True, indent=4, separators=(',', ': ')) exit(1) def find_app_by_short_name(): try: response = pbclient.find_app(short_name=app_config['short_name']) check_api_error(response) return response[0] except: format_error("pbclient.find_app", response) def setup_app(): app = find_app_by_short_name() app.long_description = contents('long_description.html') app.info['task_presenter'] = contents('template.html') app.info['thumbnail'] = app_config['thumbnail'] app.info['tutorial'] = contents('tutorial.html') try: response = pbclient.update_app(app) check_api_error(response) return app except: format_error("pbclient.update_app", response) #TODO: Change this to save two URL photos, the question answered def create_photo_task(app, photo, question, priority=0): # Data for the tasks #task_info = dict(question=question, # n_answers=options.n_answers, # link=photo['link'], # url_m=photo['url_m'], # url_b=photo['url_b']) task_info = dict(question=question, n_answers=options.n_answers, url_c=photo['url_c'], url_a=photo['url_a']) try: response = pbclient.create_task(app.id, task_info, priority_0=priority) check_api_error(response) except: format_error("pbclient.create_task", response) def add_photo_tasks(app): # First of all we get the URL photos # Then, we have to create a set of tasks for the application # For this, we get first the photo URLs from Flickr photos = get_photos() question = app_config['question'] [create_photo_task(app, p, question, priority=random.random()) for p in photos]#TODO: change this to support local photos! pbclient.set('api_key', options.api_key) pbclient.set('endpoint', options.api_url) if options.verbose: print('Running against PyBosssa instance at: %s' % options.api_url) print('Using API-KEY: %s' % options.api_key) if options.create_app or options.add_more_tasks: if options.create_app: try: print(app_config['name']) print(app_config['short_name']) print(app_config['description']) print(options) response = pbclient.create_app(app_config['name'], app_config['short_name'], app_config['description']) check_api_error(response) app = setup_app() except: format_error("pbclient.create_app", response) else: app = find_app_by_short_name() add_photo_tasks(app) if options.update_template: print "Updating app template" # discard return value setup_app() if options.update_tasks: def tasks(app): offset = 0 limit = 100 while True: try: tasks = pbclient.get_tasks(app.id, offset=offset, limit=limit) check_api_error(tasks) if len(tasks) == 0: break for task in tasks: yield task offset += len(tasks) except: format_error("pbclient.get_tasks", response) def update_task(task, count): print "Updating task: %s" % task.id if 'n_answers' in task.info: del(task.info['n_answers']) task.n_answers = options.update_tasks try: response = pbclient.update_task(task) check_api_error(response) count[0] += 1 except: format_error("pbclient.update_task", response) print "Updating task n_answers" app = find_app_by_short_name() n_tasks = [0] [update_task(t, n_tasks) for t in tasks(app)] print "%s Tasks have been updated!" % n_tasks[0]
pbclient.update_app(app) return app def create_photo_task(app, photo, question): # Data for the tasks task_info = dict( question=question, n_answers=options.n_answers, link=photo["link"], url_m=photo["url_m"], url_b=photo["url_b"], ) pbclient.create_task(app.id, task_info) if options.create_app: pbclient.create_app(app_config["name"], app_config["short_name"], app_config["description"]) app = setup_app() # First of all we get the URL photos # Then, we have to create a set of tasks for the application # For this, we get first the photo URLs from Flickr photos = get_flickr_photos() question = app_config["question"] # Batch creation for i in xrange(1): [create_photo_task(app, p, question) for p in photos] else: if options.add_more_tasks:
def run(app_config, options): def find_app_by_short_name(): return pbclient.find_app(short_name=app_config['short_name'])[0] def setup_app(): app = find_app_by_short_name() app.long_description = contents('long_description.html') app.info['task_presenter'] = contents('template.html') app.info['thumbnail'] = app_config['thumbnail'] app.info['tutorial'] = contents('tutorial.html') pbclient.update_app(app) return app def create_video_task(app, oembed, question): # Data for the tasks task_info = dict(question=question, n_answers=options.n_answers, oembed=oembed) pbclient.create_task(app.id, task_info) def add_video_tasks(app): # First of all we get the URL video # Then, we have to create a set of tasks for the application # For this, we get first the video URLs from Vimeo oembeds = get_videos(tags=options.tags) question = app_config['question'] [create_video_task(app, o, question) for o in oembeds] pbclient.set('api_key', options.api_key) pbclient.set('endpoint', options.api_url) if options.verbose: print('Running against PyBosssa instance at: %s' % options.api_url) print('Using API-KEY: %s' % options.api_key) if options.create_app or options.add_more_tasks: if options.create_app: pbclient.create_app(app_config['name'], app_config['short_name'], app_config['description']) app = setup_app() else: app = find_app_by_short_name() add_video_tasks(app) if options.update_template: print "Updating app template" # discard return value setup_app() if options.update_tasks: def tasks(app): offset = 0 limit = 100 while True: tasks = pbclient.get_tasks(app.id, offset=offset, limit=limit) if len(tasks) == 0: break for task in tasks: yield task offset += len(tasks) def update_task(task, count): print "Updating task: %s" % task.id if 'n_answers' in task.info: del(task.info['n_answers']) task.n_answers = options.update_tasks pbclient.update_task(task) count[0] += 1 print "Updating task n_answers" app = find_app_by_short_name() n_tasks = [0] [update_task(t, n_tasks) for t in tasks(app)] print "%s Tasks have been updated!" % n_tasks[0]
def run(app_config, options): def find_app_by_short_name(): return pbclient.find_app(short_name=app_config['short_name'])[0] def setup_app(): app = find_app_by_short_name() app.long_description = contents('long_description.html') app.info['task_presenter'] = contents('template.html') app.info['thumbnail'] = app_config['thumbnail'] app.info['tutorial'] = contents('tutorial.html') pbclient.update_app(app) return app def create_photo_task(app, photo, question): # Data for the tasks task_info = dict(question=question, n_answers=options.n_answers, link=photo) pbclient.create_task(app.id, task_info) def add_photo_tasks(app): # First of all we get the URL photos # Then, we have to create a set of tasks for the application # For this, we get first the photo URLs from Flickr photos = ['http://i.imgur.com/4RvBQIF.png', 'http://i.imgur.com/rRPPKKq.png', 'http://i.imgur.com/GYsQWYQ.png', 'http://i.imgur.com/gcfzAAz.png', 'http://i.imgur.com/gcfzAAz.png', 'http://i.imgur.com/14tJo6K.png', 'http://i.imgur.com/oozn3FT.png', 'http://i.imgur.com/dWWLc33.png', 'http://i.imgur.com/5RFsuhC.png', 'http://i.imgur.com/ZRQHoVM.png', 'http://i.imgur.com/3cUziEs.png'] question = app_config['question'] [create_photo_task(app, p, question) for p in photos] pbclient.set('api_key', options.api_key) pbclient.set('endpoint', options.api_url) if options.verbose: print('Running against PyBosssa instance at: %s' % options.api_url) print('Using API-KEY: %s' % options.api_key) if options.create_app or options.add_more_tasks: if options.create_app: pbclient.create_app(app_config['name'], app_config['short_name'], app_config['description']) app = setup_app() else: app = find_app_by_short_name() add_photo_tasks(app) if options.update_template: print "Updating app template" # discard return value setup_app() if options.update_tasks: def tasks(app): offset = 0 limit = 100 while True: tasks = pbclient.get_tasks(app.id, offset=offset, limit=limit) if len(tasks) == 0: break for task in tasks: yield task offset += len(tasks) def update_task(task, count): print "Updating task: %s" % task.id if 'n_answers' in task.info: del(task.info['n_answers']) task.n_answers = options.update_tasks pbclient.update_task(task) count[0] += 1 print "Updating task n_answers" app = find_app_by_short_name() n_tasks = [0] [update_task(t, n_tasks) for t in tasks(app)] print "%s Tasks have been updated!" % n_tasks[0]
def run(app_config, options): def find_app_by_short_name(): return pbclient.find_app(short_name=app_config['short_name'])[0] def setup_app(): app = find_app_by_short_name() app.long_description = contents('long_description.html') app.info['task_presenter'] = contents('template.html') app.info['thumbnail'] = app_config['thumbnail'] app.info['tutorial'] = contents('tutorial.html') pbclient.update_app(app) return app def create_phrasal_verb_task(app, phrasal_verb, exemplo_parte_1, verbo, exemplo_parte_2, particula, exemplo_parte_3, question1, question2): # Data for the tasks task_info = dict(question1=question1, question2=question2, n_answers=5, phrasal_verb=phrasal_verb, exemplo_parte_1=exemplo_parte_1, verbo=verbo, exemplo_parte_2=exemplo_parte_2, particula=particula, exemplo_parte_3=exemplo_parte_3) pbclient.create_task(app.id, task_info) def add_phrasal_verbs_tasks(app): question1 = app_config['question1'] question2 = app_config['question2'] phrasal_verbs = get_phrasal_verbs() for phrasal_verb_object in phrasal_verbs: phrasal_verb = phrasal_verb_object['phrasal_verb'] exemplo_parte_1 = phrasal_verb_object['exemplo_parte_1'] verbo = phrasal_verb_object['verbo'] exemplo_parte_2 = phrasal_verb_object['exemplo_parte_2'] particula = phrasal_verb_object['particula'] exemplo_parte_3 = phrasal_verb_object['exemplo_parte_3'] create_phrasal_verb_task(app, phrasal_verb, exemplo_parte_1, verbo, exemplo_parte_2, particula, exemplo_parte_3, question1, question2) pbclient.set('api_key', options.api_key) pbclient.set('endpoint', options.api_url) if options.verbose: print('Running against PyBosssa instance at: %s' % options.api_url) print('Using API-KEY: %s' % options.api_key) if options.create_app or options.add_more_tasks: if options.create_app: pbclient.create_app(app_config['name'], app_config['short_name'], app_config['description']) app = setup_app() else: app = find_app_by_short_name() add_phrasal_verbs_tasks(app) if options.update_template: print "Updating app template" # discard return value setup_app() if options.update_tasks: def tasks(app): offset = 0 limit = 100 while True: tasks = pbclient.get_tasks(app.id, offset=offset, limit=limit) if len(tasks) == 0: break for task in tasks: yield task offset += len(tasks) def update_task(task, count): print "Updating task: %s" % task.id if 'n_answers' in task.info: del(task.info['n_answers']) task.n_answers = options.update_tasks pbclient.update_task(task) count[0] += 1 print "Updating task n_answers" app = find_app_by_short_name() n_tasks = [0] [update_task(t, n_tasks) for t in tasks(app)] print "%s Tasks have been updated!" % n_tasks[0]
if not options.api_key: parser.error("You must supply an API-KEY to create an \ applicationa and tasks in PyBossa") else: pbclient.set('api_key', options.api_key) if (options.verbose): print('Running against PyBosssa instance at: %s' % options.api_url) print('Using API-KEY: %s' % options.api_key) if not options.n_answers: options.n_answers = 2 if options.create_app: import csv pbclient.create_app(app_config['name'], app_config['short_name'], app_config['description']) app = pbclient.find_app(short_name=app_config['short_name'])[0] app.long_description = open('long_description.html').read() app.info['task_presenter'] = open('template.html').read() app.info['thumbnail'] = app_config['thumbnail'] app.info['tutorial'] = open('tutorial.html').read() pbclient.update_app(app) with open('PabloPh_latest_UN_051212_5_38PM.csv', 'rb') as csvfile: csvreader = csv.reader(csvfile, delimiter=',') # Each row has the following format # tweetid, # text # date # username # userid
def run(app_config, options): def check_api_error(api_response): """Check if returned API response contains an error""" if type(api_response) == dict and (api_response.get('status') == 'failed'): raise exceptions.HTTPError def format_error(module, error): """Format the error for the given module""" logging.error(module) # Beautify JSON error if type(error) == list: print "Application not found" else: print json.dumps(error, sort_keys=True, indent=4, separators=(',', ': ')) exit(1) def find_app_by_short_name(): try: response = pbclient.find_app(short_name=app_config['short_name']) check_api_error(response) return response[0] except: format_error("pbclient.find_app", response) def setup_app(): app = find_app_by_short_name() app.long_description = contents('long_description.html') app.info['task_presenter'] = contents('template.html') app.info['thumbnail'] = app_config['thumbnail'] app.info['tutorial'] = contents('tutorial.html') try: response = pbclient.update_app(app) check_api_error(response) return app except: format_error("pbclient.update_app", response) pbclient.set('api_key', options.api_key) pbclient.set('endpoint', options.api_url) if options.verbose: print('Running against PyBosssa instance at: %s' % options.api_url) print('Using API-KEY: %s' % options.api_key) if options.create_app: try: print "Creating app" response = pbclient.create_app(app_config['name'], app_config['short_name'], app_config['description']) check_api_error(response) app = setup_app() except: format_error("pbclient.create_app", response) if options.update_template: print "Updating app template" # discard return value setup_app() if options.update_tasks: def tasks(app): offset = 0 limit = 100 while True: try: tasks = pbclient.get_tasks(app.id, offset=offset, limit=limit) check_api_error(tasks) if len(tasks) == 0: break for task in tasks: yield task offset += len(tasks) except: format_error("pbclient.get_tasks", response) def update_task(task, count): print "Updating task: %s" % task.id if 'n_answers' in task.info: del(task.info['n_answers']) task.n_answers = options.update_tasks try: response = pbclient.update_task(task) check_api_error(response) count[0] += 1 except: format_error("pbclient.update_task", response) print "Updating task n_answers" app = find_app_by_short_name() n_tasks = [0] [update_task(t, n_tasks) for t in tasks(app)] print "%s Tasks have been updated!" % n_tasks[0]