Esempio n. 1
0
async def view_entry(request):
    entry_name = request.match_info.get('entry')
    entry_data = await database.get_entry(name=entry_name)

    if entry_data:
        entry_id = entry_data['_id']
        title = entry_data['title']
        content = entry_data.get('content', '[no content]')
        unlisted = entry_data.get('unlisted', False)
        history = entry_data.get('history', [])
        image = entry_data.get('image')
    else:
        print(entry_name, 'not found')
        return web.HTTPNotFound()

    url_title = utils.url_title(title)

    if url_title != entry_name:
        print('Redirected', entry_name, 'to', url_title)
        return web.HTTPFound('/entry/' + url_title)

    return Template('entry.html',
                    title=title,
                    content=content,
                    id=entry_id,
                    unlisted=unlisted,
                    history=history,
                    image=image,
                    back_location='/')
Esempio n. 2
0
async def create_entry_embed(entry_data, author_id=None):
	html = entry_data['content']
	# html = await utils.before_show_text(html)
	content = utils.html_to_markdown(html)

	content_ending = ''
	# if author_id in EDITOR_IDS:
	# 	entry_id = entry_data['_id']
	# 	content_ending = f'\n\n*[Click here to edit]({BASE_URL}/edit?id={entry_id})*'

	if len(content) > 2048:
		content = content[:2045 - len(content_ending)] + '...'
	content += content_ending
	title = utils.url_title(entry_data['title'])
	view_url = f'{BASE_URL}/entry/{title}'
	view_url = view_url
	embed = discord.Embed(
		title=entry_data['title'],
		description=content,
		url=view_url
	)
	if entry_data.get('image'):
		embed.set_thumbnail(url=entry_data['image']['src'])

	return embed
Esempio n. 3
0
async def api_entry(request):
    entry_name = utils.url_title(request.match_info.get('entry'))
    entry_data = await database.get_entry(name=entry_name)

    data = await create_response(entry_data)
    if not data:
        raise web.HTTPNotFound()
    if data['slug'] != entry_name:
        return web.HTTPFound('/api/entry/' + data['slug'])

    return json_response(data)
Esempio n. 4
0
async def show_entry(message, *args):
    search_query = ' '.join(args)
    found = await database.search_entries(search_query, limit=1)
    if found:
        embed = await create_entry_embed(found[0], author_id=message.author.id)
        await message.send(embed=embed)
    else:
        embed = discord.Embed(title="This entry doesn't exist")
        search_query_url_encoded = utils.url_title(search_query)
        edit_url = f'{BASE_URL}/edit?title={search_query_url_encoded}'
        edit_url = edit_url
        if message.author.id in EDITOR_IDS:
            embed.description = f'[Click here to write it!]({edit_url})'
        await message.send(embed=embed)
async def view_entry(request):
    entry_name = request.match_info.get('entry')
    entry_data = await database.get_entry(name=entry_name)

    if entry_data:
        entry_id = entry_data['_id']
        title = entry_data['title']
        content = entry_data.get('content', '[no content]')
        nohtml_content = entry_data.get("nohtml_content", "")
        unlisted = entry_data.get('unlisted', False)
        history = entry_data.get('history', [])
        image = entry_data.get('image')
    else:
        print(entry_name, 'not found')
        return web.HTTPNotFound()

    url_title = utils.url_title(title)

    if url_title != entry_name:
        if (not unlisted):
            print('Redirected', entry_name, 'to', url_title)
            return web.HTTPFound('/entry/' + url_title)
        else:
            print(
                f'{entry_name} ({url_title}) is unlisted so it was not redirected'
            )

    sid_cookie = request.cookies.get('sid')
    if sid_cookie:
        discord_id = await database.get_editor_session(sid_cookie)
    else:
        discord_id = None
    is_editor = discord_id in EDITOR_IDS
    if discord_id:
        if entry_data.get('owner_id') == int(discord_id):
            is_editor = True

    return Template(
        'entry.html',
        title=title,
        content=content,
        nohtml_content=nohtml_content,
        id=entry_id,
        unlisted=unlisted,
        history=history,
        image=image,
        is_editor=is_editor,
        back_location='/',
    )
Esempio n. 6
0
async def new_entry(message, *args):
    search_query = ' '.join(args)
    search_query_url_encoded = utils.url_title(search_query)
    edit_url = f'{BASE_URL}/edit?title={search_query_url_encoded}'
    edit_url = edit_url
    if message.author.id in EDITOR_IDS:
        embed = discord.Embed(
            title="Write " + search_query,
            description=f'[Click here to write it!]({edit_url})')
        found = await database.search_entries(search_query, limit=1)
        if found:
            embed.set_footer(
                'Alert: There may be an entry with the same/similar name or topic.'
            )
    else:
        embed = discord.Embed(title="This command is editor only")
    await message.send(embed=embed)
Esempio n. 7
0
async def create_response(entry_data, preview=False):
    if entry_data:
        entry_id = entry_data['_id']
        title = entry_data['title']
        content = entry_data.get('content', '[no content]')
        unlisted = entry_data.get('unlisted', False)
        image = entry_data.get('image')
        markdown = entry_data.get('markdown')
        no_html = entry_data.get('nohtml_content')
        content = await utils.before_show_text(content)
        markdown = utils.html_to_markdown(content)
        owner_id = entry_data.get('owner_id')
    else:
        return web.HTTPNotFound()

    url_title = utils.url_title(title)

    if preview:
        return {
            'title': title,
            'preview': utils.remove_html(content),
            'html': content,
            'id': entry_id,
            'image': image
        }
    else:
        return {
            'slug': url_title,
            'id': entry_id,
            'title': title,
            'html': content,
            'unlisted': unlisted,
            'image': image,
            'markdown': markdown,
            'no_html': no_html,
            'owner_id': owner_id,
        }
Esempio n. 8
0
async def create_entry_embed(entry_data, author_id=None, raw_entry=False):
    if (not raw_entry):
        html = entry_data['content']
        content = utils.html_to_markdown(html)

        content_ending = ''

        if len(content) > 2048:
            content = content[:2045 - len(content_ending)] + '...'
        content += content_ending
        title = utils.url_title(entry_data['title'])
        view_url = f'{BASE_URL}/entry/{title}'
        view_url = view_url
        embed = discord.Embed(title=entry_data['title'],
                              description=content,
                              url=view_url)
        if entry_data.get('image'):
            embed.set_thumbnail(url=entry_data['image']['src'])

        return embed
    else:
        entry_data = dict(entry_data)
        content = entry_data['nohtml_content']
        content_ending = ''
        if len(content) > 1024:
            content = content[:1021 - len(content_ending)] + '...'
        try:
            del entry_data['image']
        except:
            pass
        entry_data['nohtml_content'] = content
        del entry_data['content']
        del entry_data['history']
        return utils.embed_from_dict(entry_data,
                                     color=0x00ff00,
                                     title="raw entry data")
Esempio n. 9
0
def title_for_url():
  """
  Returns the title of a web page given its url.
  """
  url = request.form.get('url')
  return jsonify({'title': url_title(url)})
Esempio n. 10
0
 def test_url_title(self):
   assert url_title('broken_url') == 'broken_url'
   assert url_title('http://www.google.com') == 'Google'
Esempio n. 11
0
async def view_entry(request):
    entry_name = request.match_info.get('entry')
    entry_data = await database.get_entry(name=entry_name)

    if entry_data:
        entry_id = entry_data['_id']
        title = entry_data['title']
        content = entry_data.get('content', '[no content]')
        nohtml_content = entry_data.get('nohtml_content', '')
        unlisted = entry_data.get('unlisted', False)
        history = entry_data.get('history', [])
        image = entry_data.get('image')
    else:
        print(entry_name, 'not found')
        return web.HTTPNotFound()

    url_title = utils.url_title(title)

    if url_title != entry_name:
        if (not unlisted):
            print('Redirected', entry_name, 'to', url_title)
            return web.HTTPFound('/entry/' + url_title)
        else:
            print(
                f'{entry_name} ({url_title}) is unlisted so it was not redirected'
            )

    sid_cookie = request.cookies.get('sid')
    if sid_cookie:
        discord_id = await database.get_editor_session(sid_cookie)
    else:
        discord_id = None
    is_editor = discord_id in EDITOR_IDS
    if discord_id:
        if entry_data.get('owner_id') == int(discord_id):
            is_editor = True

    article_text = None
    translated = False

    #ok you figure this out imma do templating
    article_text = None
    translated = False
    lang = None
    if_lang = request.query.get('lang', False) != False

    if lang in language_codes:
        try:
            cached = entry_id in translated_cache
            if cached:
                if lang in translated_cache[entry_id]:
                    article_text = translated_cache[entry_id][lang]
                    translated = True
                    print('used lang translation cache')
                else:
                    cached = False
            if not cached and language_translator:
                translation = language_translator.translate(
                    text=nohtml_content,
                    model_id='en-{}'.format(lang)).get_result()
                article_text = translation['translations'][0]['translation']
                try:
                    translated_cache[entry_id][lang] = article_text
                except:
                    translated_cache[entry_id] = {lang: article_text}
                translated = True

        except Exception as e:
            print(e)
    elif if_lang:
        translated = True
        article_text = 'Translations are currently disabled'

    return Template(
        'entry.html',
        title=title,
        content=content,
        nohtml_content=nohtml_content,
        id=entry_id,
        unlisted=unlisted,
        history=history,
        image=image,
        is_editor=is_editor,
        back_location='/',
        article_text=article_text,
        translated=translated,
    )