Exemplo n.º 1
0
def urls_response(url: str, date_format: str = '%Y-%m-%d') -> Response:
    """Create the response namedtuple."""
    try:
        dictionary = url2dict(url)
    except (ContentTypeError, ContentLengthError) as e:
        logger.exception(url)
        return Response(sfnt='Could not process the request.',
                        ctnt=e,
                        error=100)
    dictionary['date_format'] = date_format
    return dictionary_to_response(dictionary)
Exemplo n.º 2
0
def waybackmachine_response(archive_url: str, date_format: str = '%Y-%m-%d'):
    """Create the response namedtuple."""
    m = URL_FULLMATCH(archive_url)
    if not m:
        # Could not parse the archive_url. Treat as an ordinary URL.
        return urls_response(archive_url, date_format)
    archive_year, archive_month, archive_day, original_url = \
        m.groups()
    parent_conn, child_conn = Pipe()
    original_process = Process(target=original_url2dict,
                               args=(original_url, child_conn))
    original_process.start()
    try:
        archive_dict = url2dict(archive_url)
    except (ContentTypeError, ContentLengthError) as e:
        logger.exception(archive_url)
        return Response(sfnt='Could not process the request.',
                        ctnt=e,
                        error=100)
    archive_dict['date_format'] = date_format
    archive_dict['url'] = original_url
    archive_dict['archive-url'] = archive_url
    archive_dict['archive-date'] = date(int(archive_year), int(archive_month),
                                        int(archive_day))
    original_dict = parent_conn.recv()
    original_process.join()
    if original_dict:
        # The original_process has been successful
        if (original_dict['title'] == archive_dict['title']
                or original_dict['soup-title'] == archive_dict['soup-title']):
            archive_dict.update(original_dict)
            archive_dict['dead-url'] = 'no'
        else:
            # and original title is the same as archive title. Otherwise it
            # means that the content probably has changed and the original data
            # cannot be trusted.
            archive_dict['dead-url'] = 'unfit'
    else:
        archive_dict['dead-url'] = 'yes'
    if archive_dict['website'] == 'Internet Archive':
        archive_dict['website'] = (urlparse(original_url).hostname.replace(
            'www.', ''))
    return dictionary_to_response(archive_dict)
Exemplo n.º 3
0
def logins(request, data):
    rep = Response.BaseResponse()
    obj = models.Userinfo.objects.filter(username=data['username'],
                                         passwd=data['passwd']).count()
    # print(request.session.get('check_code').upper(),data['check'].upper())
    if not obj:
        rep.data = "用户名密码错误"
        return rep
    if not request.session.get('check_code').upper() == data['check'].upper():
        rep.data = "验证码错误"
        return rep

    user_info_dict = {
        'username': data['username'],
        'passwd': data['passwd'],
    }
    request.session['is_login'] = True
    request.session.update(user_info_dict)
    rep.status = True
    return rep
Exemplo n.º 4
0
def doi_response(doi_or_url, pure=False, date_format='%Y-%m-%d') -> Response:
    """Create the response namedtuple."""
    if pure:
        doi = doi_or_url
    else:
        # unescape '&', '<', and '>' in doi_or_url
        # decode percent encodings
        decoded_url = unquote(unescape(doi_or_url))
        m = DOI_SEARCH(decoded_url)
        doi = m.group(1)
    url = 'https://doi.org/' + doi
    bibtex = get_bibtex(url)
    if bibtex == 'Resource not found.':
        logger.info('DOI could not be resolved.\n' + url)
        return Response(error=100,
                        sfnt='DOI could not be resolved.',
                        ctnt=bibtex)
    else:
        dictionary = bibtex_parse(bibtex)
        dictionary['date_format'] = date_format
        if lang == 'fa':
            dictionary['language'], dictionary['error'] = \
                detect_language(dictionary['title'])
        return dictionary_to_response(dictionary)
Exemplo n.º 5
0
#! /usr/bin/python
# -*- coding: utf-8 -*-
"""HTML skeleton of the application and its predefined responses."""

from string import Template
from datetime import date
from os import name as osname

from commons import Response

# Predefined responses
DEFAULT_RESPONSE = Response(sfn='Generated citation will appear here...',
                            cite='',
                            ref='',
                            error='0')

UNDEFINED_INPUT_RESPONSE = Response(
    sfn='Undefined input.',
    cite='Sorry, the input was not recognized. The error was logged.',
    ref='',
    error='100',
)

HTTPERROR_RESPONSE = Response(
    sfn='HTTP error:',
    cite='One or more of the web resources required to '
    'create this citation are not accessible at this moment.',
    ref='',
    error='100',
)
Exemplo n.º 6
0
def response_to_html(response: Response):
    """Insert the response into the HTML_TEMPLATE and return response_body."""
    return HTML_TEMPLATE.safe_substitute(**response._asdict())
Exemplo n.º 7
0
#! /usr/bin/python
# -*- coding: utf-8 -*-
"""HTML skeleton of the application and its predefined responses."""

from string import Template

from commons import Response

HTML_TEMPLATE = Template(open('html_fa.html', encoding='utf8').read())

# Predefined responses
DEFAULT_RESPONSE = Response(
    sfn='یادکرد ساخته‌شده اینجا نمایان خواهد شد...',
    cite='',
    ref='',
    error='0',
)
HTTPERROR_RESPONSE = Response(sfn='خطای اچ‌تی‌تی‌پی:',
                              cite='یک یا چند مورد از منابع اینترنتی مورد '
                              'نیاز برای ساخت این یادکرد در این لحظه '
                              'در دسترس نیستند و یا ورودی نامعتبر است.',
                              ref='',
                              error='100')
OTHER_EXCEPTION_RESPONSE = Response(
    sfn='خطای ناشناخته‌ای رخ داد..',
    cite='اطلاعات خطا در سیاهه ثبت شد.',
    ref='',
    error='100',
)
UNDEFINED_INPUT_RESPONSE = Response(
    sfn='ورودی تجزیه‌ناپذیر',