def __createFactPage(fact_id, user_id, book_id, page_id, top_pos, left_pos, bottom_pos, right_pos):
    user_name = __get_user_name(user_id)
    server_url = app.config['URL_TEMPLATES']
    fact_url = "http://" + app.config['PYBOSSA_HOST'] + "/mb/api/fact/" + str(fact_id)
    book_list_url = "http://" + app.config['PYBOSSA_HOST'] + "/mb/collaborate/"
    book_app_url=  "http://" + app.config['PYBOSSA_HOST'] + "/pybossa/app/" + book_id + "_tt1/newtask"
    page_url = "http://www.archive.org/download/%s/page/n%d_w%d_h%d" % (book_id, page_id, 550, 700)
    
    bookInfo = meb_util.get_archive_book_data(book_id)
    book_title = bookInfo['title'].encode('utf-8')
    top_pos = str(top_pos)
    left_pos = str(left_pos)
    bottom_pos = str(bottom_pos)
    right_pos = str(right_pos)

    text = ""
    templateArch = urllib2.urlopen(urllib2.Request(server_url + "/templates/facts-template.html"))

    for line in templateArch.readlines():
        line = line.replace("#server", server_url)
        line = line.replace("#book_app_url", book_app_url)
        line = line.replace("#book_list_url", book_list_url)
        line = line.replace("#fact_url", fact_url)
        line = line.replace("#page_url", page_url)
        
        line = line.replace("#user_name", user_name)
        line = line.replace("#book_title", book_title)
        line = line.replace("#top_pos", top_pos)
        line = line.replace("#left_pos", left_pos)
        line = line.replace("#bottom_pos", bottom_pos)
        line = line.replace("#right_pos", right_pos)

        text += line
    return text
def create_apps(book_id):
    """
    Celery queued task that creates tt_apps and tt1 tasks

    :arg book_id: Internet archive book id
    :returns: book indicating if the applications were created
    :rtype: bool

    """

    try:
        imgs = meb_util.get_tt_images(book_id)
        bookInfo = meb_util.get_archive_book_data(book_id)
        
        app_tt_select = Apptt_select(short_name=book_id + "_tt1", title=bookInfo['title'], book_info=bookInfo)
        app_tt_meta = Apptt_meta(short_name=book_id + "_tt2", title=bookInfo['title'])
        app_tt_struct = Apptt_struct(short_name=book_id + "_tt3", title=bookInfo['title'])
        app_tt_transcribe = Apptt_transcribe(short_name=book_id + "_tt4", title=bookInfo['title'])
        
        app_tt_select.add_app_infos(bookInfo)
        app_tt_meta.add_app_infos(bookInfo)
        app_tt_struct.add_app_infos(bookInfo)
        app_tt_transcribe.add_app_infos(bookInfo)
        
        if len(app_tt_select.get_tasks()) == 0:
            for img in imgs:
                task = app_tt_select.add_task(img)
                workflow_transaction_info = dict(task_id_1=task.id, task_id_2=None, task_id_3=None, task_id_4=None)
                data_mngr.record_workflow_transaction(workflow_transaction_info)

        return True

    except Exception as e:
        return False
 def test_get_archive_book_data_01(self):
     try:
         data = meb_util.get_archive_book_data("rpparaiba1918")
         
         self.assertEquals(data["bookid"], "rpparaiba1918")
         self.assertEquals(data["img"], "http://www.archive.org/download/rpparaiba1918/page/n7_w100_h100")
         self.assertEquals(data["title"], u"Mensagem apresentada à Assembléa Legislativa do Estado da Parahyba na abertura da 3a. sessão ordinaria da 8a. legislatura, a 1o. de setembro de 1918, pelo Dr. Francisco Camillo de Hollanda, presidente do Estado. (Vol. 1917)")
         self.assertEquals(data["publisher"], u"[Imprensa Oficial - Parahyba do Norte")
         self.assertEquals(data["contributor"], u"Biblioteca do Ministério da Fazenda no Rio de Janeiro")
         self.assertEquals(data["volume"], u"1917")
     except Exception as e:
         print e
         assert False
def book_progress(bookid):
    con = None
    bookInfo = meb_util.get_archive_book_data(bookid)
    book_title = bookInfo['title'].encode('utf-8')
    result = dict(title=book_title, done=0, total=0)
    
    try:
        con = __create_db_connection(app.config['PYBOSSA_DB'])
        cursor = con.cursor()
        query = "SELECT * FROM (SELECT COUNT(*) done_task_runs FROM task_run WHERE app_id in (SELECT id FROM app WHERE short_name like '" + bookid + "%')) as tmp1, (SELECT COUNT(*)*2 total_task_runs FROM task WHERE app_id in (SELECT id FROM app WHERE short_name like '" + bookid + "%')) as tmp2"

        cursor.execute(query)

        row = cursor.fetchone()
        result['done'] = row[0]
        result['total'] = row[1]

        con.commit()
    except psycopg2.DatabaseError, e:
        print 'Error %s' % e
        if template_type:
            new_template = meb_util.set_url(
                urllib2.urlopen(
                    urllib2.Request(
                        flask_app.config['URL_TEMPLATES']
                        + "/templates/" + template_type)),
                app_short_name)
            
            new_long_desc_template = meb_util.set_url(
                urllib2.urlopen(
                    urllib2.Request(
                        flask_app.config['URL_TEMPLATES'] + 
                                "/templates/" +
                                long_desc_type)), 
                     app_short_name)

            app.info['task_presenter'] = new_template
            app.info['thumbnail'] = app.info['thumbnail'].replace("https://alfa.pybossa.socientize.eu", "https://localhost")
            
            app.info['newtask'] = app.info['newtask'].replace("https://alfa.pybossa.socientize.eu", "https://localhost")
            app.info['newtask'] = app.info['newtask'].replace("http://alfa.pybossa.socientize.eu", "https://localhost")
            app.info['newtask'] = app.info['newtask'].replace("https://alfa2.pybossa.socientize.eu", "https://localhost")
            app.info['newtask'] = app.info['newtask'].replace("http://alfa2.pybossa.socientize.eu", "https://localhost")
            
            app.long_description = new_long_desc_template
             
            book_id = app_short_name[:-4]
            bookInfo = meb_util.get_archive_book_data(book_id)
            app.name = bookInfo['title'] + " " + app_name
            pbclient.update_app(app)
def get_book_data(book_id):
    return get_archive_book_data(book_id)