From d3ddbd14c771945bb3f4f73553d931b551d86dac Mon Sep 17 00:00:00 2001 From: odysseusmax Date: Mon, 7 Sep 2020 18:29:48 +0530 Subject: [PATCH] change home page layout and add api endpoints --- app/routes.py | 9 ++++- app/templates/home.html | 2 +- app/templates/info.html | 6 +-- app/views.py | 89 +++++++++++++++++++++++++++++------------ 4 files changed, 76 insertions(+), 30 deletions(-) diff --git a/app/routes.py b/app/routes.py index 5290eb6..24fa43f 100644 --- a/app/routes.py +++ b/app/routes.py @@ -30,7 +30,8 @@ async def setup_routes(app, handler): h = handler client = h.client routes = [ - web.get('/', h.home, name='home') + web.get('/', h.home), + web.get('/api', h.api_home), ] index_all = index_settings['index_all'] index_private = index_settings['index_private'] @@ -58,10 +59,13 @@ async def setup_routes(app, handler): continue p = f"/{alias_id}" + p_api = '/api' + p r = [ web.get(p, h.index), + web.get(p_api, h.api_index), web.get(p + r"/logo", h.logo), web.get(p + r"/{id:\d+}/view", h.info), + web.get(p_api + r"/{id:\d+}/view", h.api_info), web.get(p + r"/{id:\d+}/download", h.download_get), web.head(p + r"/{id:\d+}/download", h.download_head), web.get(p + r"/{id:\d+}/thumbnail", h.thumbnail_get), @@ -73,10 +77,13 @@ async def setup_routes(app, handler): chat = await client.get_entity(chat_id) alias_id = generate_alias_id(chat) p = f"/{alias_id}" + p_api = '/api' + p r = [ web.get(p, h.index), + web.get(p_api, h.api_index), web.get(p + r"/logo", h.logo), web.get(p + r"/{id:\d+}/view", h.info), + web.get(p_api + r"/{id:\d+}/view", h.api_info), web.get(p + r"/{id:\d+}/download", h.download_get), web.head(p + r"/{id:\d+}/download", h.download_head), web.get(p + r"/{id:\d+}/thumbnail", h.thumbnail_get), diff --git a/app/templates/home.html b/app/templates/home.html index e3cf3d5..acfe6b1 100644 --- a/app/templates/home.html +++ b/app/templates/home.html @@ -6,7 +6,7 @@
{% for chat in chats %} - +
{{chat.name}}
diff --git a/app/templates/info.html b/app/templates/info.html index 0e10e73..fab71c0 100644 --- a/app/templates/info.html +++ b/app/templates/info.html @@ -69,11 +69,11 @@ {% endif %} - {% if caption %} + {% if caption_html %}
-

{{ caption|safe }}

+

{{ caption_html|safe }}

{% if reply_btns %}
@@ -100,7 +100,7 @@ {% else %}
-

{{ text|safe }}

+

{{ text_html|safe }}

{% if reply_btns %}
diff --git a/app/views.py b/app/views.py index 1b8f642..5d44388 100644 --- a/app/views.py +++ b/app/views.py @@ -21,23 +21,43 @@ class Views: def __init__(self, client): self.client = client + + async def _home(self, req): + chats = [] + for chat in chat_ids: + chats.append({ + 'id': chat['alias_id'], + 'name': chat['title'], + 'url': req.rel_url.path + f"/{chat['alias_id']}" + }) + return {'chats':chats} + @aiohttp_jinja2.template('home.html') async def home(self, req): if len(chat_ids) == 1: raise web.HTTPFound(f"{chat_ids[0]['alias_id']}") - chats = [] - for chat in chat_ids: - chats.append({ - 'id': chat['alias_id'], - 'name': chat['title'] - }) - return {'chats':chats} + return await self._home(req) + + + async def api_home(self, req): + data = await self._home(req) + return web.json_response(data) @aiohttp_jinja2.template('index.html') async def index(self, req): - alias_id = req.rel_url.path.split('/')[1] + return await self._index(req) + + + async def api_index(self, req): + data = await self._index(req, True) + return web.json_response(data) + + + async def _index(self, req, api=False): + alias_pos = 2 if api else 1 + alias_id = req.rel_url.path.split('/')[alias_pos] chat = [i for i in chat_ids if i['alias_id'] == alias_id][0] chat_id = chat['chat_id'] chat_name = chat['title'] @@ -75,12 +95,12 @@ class Views: entry = dict( file_id=m.id, media=True, - thumbnail=req.rel_url.with_path(f"/{alias_id}/{m.id}/thumbnail"), + thumbnail=f"/{alias_id}/{m.id}/thumbnail", mime_type=m.file.mime_type, insight = get_file_name(m)[:100], - date = m.date, + date = str(m.date), size=get_human_size(m.file.size), - url=req.rel_url.with_path(f"/{alias_id}/{m.id}/view") + url=req.rel_url.path + f"/{m.id}/view" ) elif m.message: entry = dict( @@ -88,9 +108,9 @@ class Views: media=False, mime_type='text/plain', insight = m.raw_text[:100], - date = m.date, + date = str(m.date), size=get_human_size(len(m.raw_text)), - url=req.rel_url.with_path(f"/{alias_id}/{m.id}/view") + url=req.rel_url.path + f"/{m.id}/view" ) if entry: results.append(entry) @@ -101,7 +121,7 @@ class Views: if search_query: query.update({'search':search_query}) prev_page = { - 'url': req.rel_url.with_query(query), + 'url': str(req.rel_url.with_query(query)), 'no': offset_val } @@ -110,7 +130,7 @@ class Views: if search_query: query.update({'search':search_query}) next_page = { - 'url': req.rel_url.with_query(query), + 'url': str(req.rel_url.with_query(query)), 'no': offset_val+2 } @@ -121,15 +141,28 @@ class Views: 'next_page': next_page, 'search': search_query, 'name' : chat['title'], - 'logo': req.rel_url.with_path(f"/{alias_id}/logo"), + 'logo': f"/{alias_id}/logo", 'title' : "Index of " + chat_name } @aiohttp_jinja2.template('info.html') async def info(self, req): + return await self._info(req) + + + async def api_info(self, req): + data = await self._info(req, True) + if not data['found']: + return web.Response(status=404, text="404: Not Found") + + return web.json_response(data) + + + async def _info(self, req, api=False): file_id = int(req.match_info["id"]) - alias_id = req.rel_url.path.split('/')[1] + alias_pos = 2 if api else 1 + alias_id = req.rel_url.path.split('/')[alias_pos] chat = [i for i in chat_ids if i['alias_id'] == alias_id][0] chat_id = chat['chat_id'] try: @@ -169,34 +202,40 @@ class Views: media['image'] = True if message.text: - caption = Markup.escape(message.raw_text).__str__().replace('\n', '
') - + caption = message.raw_text else: - caption = False + caption = '' + caption_html = Markup.escape(caption).__str__().replace('\n', '
') return_val = { 'found': True, 'name': file_name, 'id': file_id, 'size': file_size, 'media': media, + 'caption_html': caption_html, 'caption': caption, 'title': f"Download | {file_name} | {file_size}", - 'reply_btns': reply_btns + 'reply_btns': reply_btns, + 'thumbnail': f"/{alias_id}/{file_id}/thumbnail", + 'download_url': f"/{alias_id}/{file_id}/download", + 'page_id': alias_id } elif message.message: - text = Markup.escape(message.raw_text).__str__().replace('\n', '
') + text = message.raw_text + text_html = Markup.escape(text).__str__().replace('\n', '
') return_val = { 'found': True, 'media': False, 'text': text, - 'reply_btns': reply_btns + 'text_html': text_html, + 'reply_btns': reply_btns, + 'page_id': alias_id } else: return_val = { 'found':False, 'reason' : "Some kind of entry that I cannot display", } - #return_val.update({'reply_btns': reply_btns}) log.debug(f"data for {file_id} in {chat_id} returned as {return_val}") return return_val @@ -212,7 +251,7 @@ class Views: log.debug(f"Error in getting profile picture in {chat_id}", exc_info=True) photo = None if not photo: - W, H = (160, 160) if req.query.get('big', None) else (80, 80) + W, H = (160, 160) c = lambda : random.randint(0, 255) color = tuple([c() for i in range(3)]) im = Image.new("RGB", (W,H), color)