示例#1
0
def run(codec, body):
    """
    run codec for transform body
    """
    if body == 'testdata':
        response = make_response('OK')
        response.mimetype = "text/plain"
        return response

    lines = body.splitlines()
    subj = lines[0]
    text = '\n'.join(lines[1:])

    try:
        if CODECS[codec] is None:
            from modules import store
            ret = store(codec, subj, text)
        else:
            pmod = importlib.import_module("modules.{}".format(codec))
            ret = pmod.start(subj, text)

    except Exception:
        from models import SavedSource
        SavedSource(label=codec, subject=subj, body=text).put()
        raise

    response = make_response(ret)
    response.mimetype = "text/plain"
    return response
示例#2
0
def cut_text(text):
    """
    extract and return news text
    """
    for phrase in TERMINALS:
        if phrase in text:
            return text[:text.index(phrase)]

    SavedSource(label=LABEL, subject='cut_text', body=text).put()
    return text
示例#3
0
def by_subj(subj, body, text, label, prefix, handlers):  # pylint: disable=too-many-arguments
    """
    process message by subject
    """
    for marks, func in handlers:
        if is_present(marks, subj):
            return prefix + '\n'.join(func(subj, text))

    # unknown subject, save to db for analizing and return default answer
    SavedSource(label=label, subject=subj, body=body).put()

    return prefix + subj + '\n' + text
示例#4
0
def start(subj, body):
    """
    parse Twitter message
    """
    lines = body.splitlines()
    for index, line in enumerate(lines):
        if line in SUBJECTS:
            text = '\n'.join(lines[index + 1:])
            return TITLE + line + '\n' + cut_text(text)

    SavedSource(label=LABEL, subject=subj, body=body).put()

    return TITLE + subj
示例#5
0
def e_message(subj, text):
    """
    message
    """
    txt = convert(text, extract_link=True).replace(NBSP, ' ')
    ret = [txt]

    if is_present(MARK_PHOTO + [MARK_PHOTO_REF], txt):
        ret = make_link(txt, MARK_PHOTO_REF, 'новое фото')
    elif is_present(MARK_NOTE + [MARK_NOTE_REF], txt):
        ret = make_link(txt, MARK_NOTE_REF, 'новая заметка')
    else:
        SavedSource(label='ok_text', subject=subj, body=text).put()

    return ret
示例#6
0
def start(subj, body):
    """
    parse LiveJournal message
    """
    text = convert(body, extract_link=True).replace(NBSP, ' ')
    link = ''
    if MARK_END in text:
        pos = text.index(MARK_END)
        title = text[:pos]
        link = '\n' + BUTTONS + '\n' + "[Читать]({})".format(
            text[pos:].split()[0])
        text = title
    else:
        SavedSource(label=LABEL, subject=subj, body=body).put()

    return 'LiveJournal: ' + subj + '\n' + text + link
示例#7
0
def e_live(subj, text):
    """
    'is live now:' in subject
    """
    blogger, title = subj.split(SUBJ_LIVE)
    url = ''
    link = title

    for line in text.splitlines():
        if line.startswith(POST_URL):
            url = line
            break

    if url:
        link = "[{}]({})".format(title, url)
    else:
        SavedSource(label=LABEL, subject=subj, body=text).put()

    return [blogger, '', MARKUP, link]
示例#8
0
def e_upload(subj, text):
    """
    'just uploaded a video' in subject
    """
    blogger, _tmp = subj.split(SUBJ_UPLOAD)
    url = ''
    messages = []

    for line in text.splitlines():
        if line.startswith(POST_URL):
            url = line
            break
        else:
            messages.append(line)

    if url:
        blogger = "[{}]({})".format(blogger, url)
    else:
        SavedSource(label=LABEL, subject=subj, body=text).put()

    return [blogger, '', MARKUP, ' '.join(messages)]
示例#9
0
    def e_post(subj, text):
        """
        post
        """
        link = ''
        title = ''
        citate = ''

        lines = iter(text.splitlines())
        for line in lines:
            if line.startswith(mark):
                link = "[{}]({})".format(mark, next(lines))
            elif line.startswith(prefix):
                title = line
            elif line in ['Посетить группу', 'Читать публикацию']:
                citate = read_citate(lines)
                break

        if not all([link, title]):
            SavedSource(label=LABEL, subject=subj, body=text).put()

        return [title, '', citate, BUTTONS, link]
示例#10
0
def start(subj, body):
    """
    parse Reddit message
    """
    text = convert(body, extract_link=True).replace(NBSP, ' ')

    if MARK_CLICK not in text:
        SavedSource(label=LABEL, subject=subj, body=body).put()
        return 'Reddit: ' + subj + '\n' + text

    border = text.index(MARK_CLICK)
    head = text[:border]
    tail = text[border:]

    if MARK_POSTED in head:
        head = ' '.join(head[head.index(MARK_POSTED) +
                             len(MARK_POSTED):].strip().split()[1:])
    link = "[Read]({})".format(tail.split()[0])

    if not head:
        head = subj

    return 'Reddit: ' + head + '\n' + BUTTONS + '\n' + link
示例#11
0
def store(label, subj, body):
    """
    default handler for store incoming messages
    """
    SavedSource(label=label, subject=subj, body=body).put()
    return subj + '\n' + convert(body, extract_link=True).replace(NBSP, ' ')