mirror of
https://gitlab.com/octospacc/TelegramIndex-Fork.git
synced 2025-02-18 04:20:51 +01:00
change home page layout and add api endpoints
This commit is contained in:
parent
41ea351adc
commit
d3ddbd14c7
@ -30,7 +30,8 @@ async def setup_routes(app, handler):
|
|||||||
h = handler
|
h = handler
|
||||||
client = h.client
|
client = h.client
|
||||||
routes = [
|
routes = [
|
||||||
web.get('/', h.home, name='home')
|
web.get('/', h.home),
|
||||||
|
web.get('/api', h.api_home),
|
||||||
]
|
]
|
||||||
index_all = index_settings['index_all']
|
index_all = index_settings['index_all']
|
||||||
index_private = index_settings['index_private']
|
index_private = index_settings['index_private']
|
||||||
@ -58,10 +59,13 @@ async def setup_routes(app, handler):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
p = f"/{alias_id}"
|
p = f"/{alias_id}"
|
||||||
|
p_api = '/api' + p
|
||||||
r = [
|
r = [
|
||||||
web.get(p, h.index),
|
web.get(p, h.index),
|
||||||
|
web.get(p_api, h.api_index),
|
||||||
web.get(p + r"/logo", h.logo),
|
web.get(p + r"/logo", h.logo),
|
||||||
web.get(p + r"/{id:\d+}/view", h.info),
|
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.get(p + r"/{id:\d+}/download", h.download_get),
|
||||||
web.head(p + r"/{id:\d+}/download", h.download_head),
|
web.head(p + r"/{id:\d+}/download", h.download_head),
|
||||||
web.get(p + r"/{id:\d+}/thumbnail", h.thumbnail_get),
|
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)
|
chat = await client.get_entity(chat_id)
|
||||||
alias_id = generate_alias_id(chat)
|
alias_id = generate_alias_id(chat)
|
||||||
p = f"/{alias_id}"
|
p = f"/{alias_id}"
|
||||||
|
p_api = '/api' + p
|
||||||
r = [
|
r = [
|
||||||
web.get(p, h.index),
|
web.get(p, h.index),
|
||||||
|
web.get(p_api, h.api_index),
|
||||||
web.get(p + r"/logo", h.logo),
|
web.get(p + r"/logo", h.logo),
|
||||||
web.get(p + r"/{id:\d+}/view", h.info),
|
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.get(p + r"/{id:\d+}/download", h.download_get),
|
||||||
web.head(p + r"/{id:\d+}/download", h.download_head),
|
web.head(p + r"/{id:\d+}/download", h.download_head),
|
||||||
web.get(p + r"/{id:\d+}/thumbnail", h.thumbnail_get),
|
web.get(p + r"/{id:\d+}/thumbnail", h.thumbnail_get),
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
<div class="mx-auto my-2 p-2 w-full">
|
<div class="mx-auto my-2 p-2 w-full">
|
||||||
<div class="block p-4 md:flex md:flex-wrap md:justify-center w-full text-center break-words">
|
<div class="block p-4 md:flex md:flex-wrap md:justify-center w-full text-center break-words">
|
||||||
{% for chat in chats %}
|
{% for chat in chats %}
|
||||||
<a title="{{chat.name}}" href="/{{chat.id}}" class="flex flex-col justify-around w-full min-h-full md:w-2/5 lg:w-1/4 rounded my-2 md:mx-1 shadow-md hover:shadow-lg hover:border-blue-300 hover:bg-blue-100">
|
<a title="{{chat.name}}" href="/{{chat.id}}" class="flex flex-col justify-around w-full min-h-full md:w-2/5 lg:w-1/5 rounded my-2 md:mx-1 shadow-md hover:shadow-lg hover:border hover:border-blue-300 hover:bg-blue-100">
|
||||||
|
|
||||||
<img src="/{{chat.id}}/logo?big=1" class="w-full rounded-full ">
|
<img src="/{{chat.id}}/logo?big=1" class="w-full rounded-full ">
|
||||||
<div class="p-4 rounded">{{chat.name}}</div>
|
<div class="p-4 rounded">{{chat.name}}</div>
|
||||||
|
@ -69,11 +69,11 @@
|
|||||||
</script>
|
</script>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
||||||
{% if caption %}
|
{% if caption_html %}
|
||||||
|
|
||||||
<div class="mx-auto mt-1">
|
<div class="mx-auto mt-1">
|
||||||
<div class="flex justify-center text-gray-300 font-mono">
|
<div class="flex justify-center text-gray-300 font-mono">
|
||||||
<p class="text-left w-full rounded p-4 bg-gray-900 shadow-lg"> {{ caption|safe }} </p>
|
<p class="text-left w-full rounded p-4 bg-gray-900 shadow-lg"> {{ caption_html|safe }} </p>
|
||||||
</div>
|
</div>
|
||||||
{% if reply_btns %}
|
{% if reply_btns %}
|
||||||
<div class="w-full block">
|
<div class="w-full block">
|
||||||
@ -100,7 +100,7 @@
|
|||||||
{% else %}
|
{% else %}
|
||||||
<div class="mx-auto flex flex-wrap justify-center w-full md:w-3/4 mt-1">
|
<div class="mx-auto flex flex-wrap justify-center w-full md:w-3/4 mt-1">
|
||||||
<div class="flex justify-center text-gray-300 font-mono">
|
<div class="flex justify-center text-gray-300 font-mono">
|
||||||
<p class="text-left rounded p-4 bg-gray-900 w-full shadow-lg"> {{ text|safe }} </p>
|
<p class="text-left rounded p-4 bg-gray-900 w-full shadow-lg"> {{ text_html|safe }} </p>
|
||||||
</div>
|
</div>
|
||||||
{% if reply_btns %}
|
{% if reply_btns %}
|
||||||
<div class="w-full block">
|
<div class="w-full block">
|
||||||
|
85
app/views.py
85
app/views.py
@ -22,22 +22,42 @@ class Views:
|
|||||||
self.client = client
|
self.client = client
|
||||||
|
|
||||||
|
|
||||||
@aiohttp_jinja2.template('home.html')
|
async def _home(self, req):
|
||||||
async def home(self, req):
|
|
||||||
if len(chat_ids) == 1:
|
|
||||||
raise web.HTTPFound(f"{chat_ids[0]['alias_id']}")
|
|
||||||
chats = []
|
chats = []
|
||||||
for chat in chat_ids:
|
for chat in chat_ids:
|
||||||
chats.append({
|
chats.append({
|
||||||
'id': chat['alias_id'],
|
'id': chat['alias_id'],
|
||||||
'name': chat['title']
|
'name': chat['title'],
|
||||||
|
'url': req.rel_url.path + f"/{chat['alias_id']}"
|
||||||
})
|
})
|
||||||
return {'chats':chats}
|
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']}")
|
||||||
|
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')
|
@aiohttp_jinja2.template('index.html')
|
||||||
async def index(self, req):
|
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 = [i for i in chat_ids if i['alias_id'] == alias_id][0]
|
||||||
chat_id = chat['chat_id']
|
chat_id = chat['chat_id']
|
||||||
chat_name = chat['title']
|
chat_name = chat['title']
|
||||||
@ -75,12 +95,12 @@ class Views:
|
|||||||
entry = dict(
|
entry = dict(
|
||||||
file_id=m.id,
|
file_id=m.id,
|
||||||
media=True,
|
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,
|
mime_type=m.file.mime_type,
|
||||||
insight = get_file_name(m)[:100],
|
insight = get_file_name(m)[:100],
|
||||||
date = m.date,
|
date = str(m.date),
|
||||||
size=get_human_size(m.file.size),
|
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:
|
elif m.message:
|
||||||
entry = dict(
|
entry = dict(
|
||||||
@ -88,9 +108,9 @@ class Views:
|
|||||||
media=False,
|
media=False,
|
||||||
mime_type='text/plain',
|
mime_type='text/plain',
|
||||||
insight = m.raw_text[:100],
|
insight = m.raw_text[:100],
|
||||||
date = m.date,
|
date = str(m.date),
|
||||||
size=get_human_size(len(m.raw_text)),
|
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:
|
if entry:
|
||||||
results.append(entry)
|
results.append(entry)
|
||||||
@ -101,7 +121,7 @@ class Views:
|
|||||||
if search_query:
|
if search_query:
|
||||||
query.update({'search':search_query})
|
query.update({'search':search_query})
|
||||||
prev_page = {
|
prev_page = {
|
||||||
'url': req.rel_url.with_query(query),
|
'url': str(req.rel_url.with_query(query)),
|
||||||
'no': offset_val
|
'no': offset_val
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -110,7 +130,7 @@ class Views:
|
|||||||
if search_query:
|
if search_query:
|
||||||
query.update({'search':search_query})
|
query.update({'search':search_query})
|
||||||
next_page = {
|
next_page = {
|
||||||
'url': req.rel_url.with_query(query),
|
'url': str(req.rel_url.with_query(query)),
|
||||||
'no': offset_val+2
|
'no': offset_val+2
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -121,15 +141,28 @@ class Views:
|
|||||||
'next_page': next_page,
|
'next_page': next_page,
|
||||||
'search': search_query,
|
'search': search_query,
|
||||||
'name' : chat['title'],
|
'name' : chat['title'],
|
||||||
'logo': req.rel_url.with_path(f"/{alias_id}/logo"),
|
'logo': f"/{alias_id}/logo",
|
||||||
'title' : "Index of " + chat_name
|
'title' : "Index of " + chat_name
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@aiohttp_jinja2.template('info.html')
|
@aiohttp_jinja2.template('info.html')
|
||||||
async def info(self, req):
|
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"])
|
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 = [i for i in chat_ids if i['alias_id'] == alias_id][0]
|
||||||
chat_id = chat['chat_id']
|
chat_id = chat['chat_id']
|
||||||
try:
|
try:
|
||||||
@ -169,34 +202,40 @@ class Views:
|
|||||||
media['image'] = True
|
media['image'] = True
|
||||||
|
|
||||||
if message.text:
|
if message.text:
|
||||||
caption = Markup.escape(message.raw_text).__str__().replace('\n', '<br>')
|
caption = message.raw_text
|
||||||
|
|
||||||
else:
|
else:
|
||||||
caption = False
|
caption = ''
|
||||||
|
caption_html = Markup.escape(caption).__str__().replace('\n', '<br>')
|
||||||
return_val = {
|
return_val = {
|
||||||
'found': True,
|
'found': True,
|
||||||
'name': file_name,
|
'name': file_name,
|
||||||
'id': file_id,
|
'id': file_id,
|
||||||
'size': file_size,
|
'size': file_size,
|
||||||
'media': media,
|
'media': media,
|
||||||
|
'caption_html': caption_html,
|
||||||
'caption': caption,
|
'caption': caption,
|
||||||
'title': f"Download | {file_name} | {file_size}",
|
'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:
|
elif message.message:
|
||||||
text = Markup.escape(message.raw_text).__str__().replace('\n', '<br>')
|
text = message.raw_text
|
||||||
|
text_html = Markup.escape(text).__str__().replace('\n', '<br>')
|
||||||
return_val = {
|
return_val = {
|
||||||
'found': True,
|
'found': True,
|
||||||
'media': False,
|
'media': False,
|
||||||
'text': text,
|
'text': text,
|
||||||
'reply_btns': reply_btns
|
'text_html': text_html,
|
||||||
|
'reply_btns': reply_btns,
|
||||||
|
'page_id': alias_id
|
||||||
}
|
}
|
||||||
else:
|
else:
|
||||||
return_val = {
|
return_val = {
|
||||||
'found':False,
|
'found':False,
|
||||||
'reason' : "Some kind of entry that I cannot display",
|
'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}")
|
log.debug(f"data for {file_id} in {chat_id} returned as {return_val}")
|
||||||
return 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)
|
log.debug(f"Error in getting profile picture in {chat_id}", exc_info=True)
|
||||||
photo = None
|
photo = None
|
||||||
if not photo:
|
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)
|
c = lambda : random.randint(0, 255)
|
||||||
color = tuple([c() for i in range(3)])
|
color = tuple([c() for i in range(3)])
|
||||||
im = Image.new("RGB", (W,H), color)
|
im = Image.new("RGB", (W,H), color)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user