コード例 #1
0
def download_asset(old_path, new_fname, dest_path):
    """Returns msg, if error"""
    if old_path.startswith("http"):
        location = old_path
    else:
        try:
            location = urljoin(config.get("STATIC_URL_FILE"), old_path.strip())
        except ValueError as exc:
            return 'cannot join URL parts "%s" and "%s": %s' % (
                config.get("STATIC_URL_FILE"),
                old_path,
                exc,
            )

    # Verifica se o arquivo ja foi baixado anteriormente
    filename_m, ext_m = files.extract_filename_ext_by_path(old_path)
    dest_path_file = os.path.join(dest_path,
                                  "%s%s" % (new_fname.strip(), ext_m))
    if os.path.exists(dest_path_file):
        logger.info("Arquivo ja baixado: %s", dest_path_file)
        return

    try:
        request_file = request.get(location,
                                   timeout=int(config.get("TIMEOUT") or 10))
    except request.HTTPGetError as e:
        try:
            msg = str(e)
        except TypeError:
            msg = "Unknown error"
        logger.error(e)
        return msg
    else:
        files.write_file_binary(dest_path_file, request_file.content)
コード例 #2
0
def ext_issue(code, **ext_params):

    issue = request.get(
        "%s/issue" % config.AM_URL_API,
        params={"collection": config.get("SCIELO_COLLECTION"), "code": code},
    ).json()
    obj_issue = Issue(issue)
コード例 #3
0
def ext_identifiers():

    journals_id = request.get(
        "%s/journal/identifiers/" % config.get("AM_URL_API"),
        params={
            "collection": config.get("SCIELO_COLLECTION")
        },
    ).json()
    return journals_id
コード例 #4
0
def ext_journal(issn):

    journal = request.get(
        "%s/journal" % config.get("AM_URL_API"),
        params={
            "collection": config.get("SCIELO_COLLECTION"),
            "issn": issn
        },
    ).json()
    return Journal(journal[0])
コード例 #5
0
def ext_identifiers(issn_journal):
    articles_id = request.get(
        "%s/article/identifiers/" % config.get("AM_URL_API"),
        params={
            "collection": config.get("SCIELO_COLLECTION"),
            "issn": issn_journal
        },
    )
    if articles_id:
        return articles_id.json()
コード例 #6
0
def ext_article(code, **ext_params):
    params = ext_params
    params.update({
        "collection": config.get("SCIELO_COLLECTION"),
        "code": code
    })
    try:
        article = request.get("%s/article" % config.get("AM_URL_API"),
                              params=params)
    except request.HTTPGetError:
        logger.error("Erro coletando dados do artigo PID %s" % code)
    else:
        return article
コード例 #7
0
def ext_journal(issn):
    try:
        journal = request.get(
            "%s/journal" % config.get("AM_URL_API"),
            params={
                "collection": config.get("SCIELO_COLLECTION"),
                "issn": issn
            },
        )
    except request.HTTPGetError:
        logger.error("Journal nao encontrado: %s: %s" %
                     (config.get("SCIELO_COLLECTION"), issn))
    else:
        return Journal(journal.json()[0])
コード例 #8
0
def download_asset(old_path, new_fname, dest_path):
    """Returns msg, if error"""
    location = urljoin(config.get("STATIC_URL_FILE"), old_path)
    try:
        request_file = request.get(location,
                                   timeout=int(config.get("TIMEOUT") or 10))
    except request.HTTPGetError as e:
        try:
            msg = str(e)
        except TypeError:
            msg = "Unknown error"
        logger.error(e)
        return msg
    else:
        filename_m, ext_m = files.extract_filename_ext_by_path(old_path)
        files.write_file_binary(
            os.path.join(dest_path, "%s%s" % (new_fname, ext_m)),
            request_file.content)
コード例 #9
0
def migrate_logos_to_website(session, website_img_dir):
    """Read all Journals from Website MongoDB collection and, for each one, get journal
    logo from current website, save to website media directory, create an image record
    in SQLite Image Table and update journal document with logo URL.

    session: SQLite DB session created in `connect_to_databases`
    website_img_dir: Website media directory
    """
    journals = Journal.objects.all()
    if len(journals) == 0:
        raise exceptions.NoJournalInWebsiteError(
            "No journals in Website Database. Migrate Isis Journals first.")

    for journal in journals:
        logger.debug("Journal acronym %s", journal.acronym)
        logo_old_filename = "glogo.gif"
        logo_url = "{}img/revistas/{}/glogo.gif".format(
            config.get("STATIC_URL_FILE"), journal.acronym)
        try:
            logger.debug("Getting Journal logo in %s", logo_url)
            request_file = request.get(logo_url,
                                       timeout=int(
                                           config.get("TIMEOUT") or 10))
        except request.HTTPGetError as e:
            try:
                msg = str(e)
            except TypeError:
                msg = "Unknown error"
            logger.error(msg)
        else:
            logo_filename = "_".join([journal.acronym, logo_old_filename])
            dest_path_file = os.path.join(website_img_dir, logo_filename)
            logger.debug("Saving Journal logo in %s", dest_path_file)
            files.write_file_binary(dest_path_file, request_file.content)

            image_path = "images/%s" % logo_filename
            logger.debug("Saving logo as image in %s", image_path)
            session.add(Image(name=logo_filename, path=image_path))
            session.commit()

            journal.logo_url = "/media/%s" % image_path
            logger.debug("Updating Journal with logo_url %s", journal.logo_url)
            journal.save()
コード例 #10
0
    def test_get(self, mk_requests):

        expected = {"params": {"collection": "spa"}}
        request.get("http://api.test.com", **expected)
        mk_requests.get.assert_called_once_with("http://api.test.com",
                                                **expected)