mirror of
https://gitlab.com/octospacc/TelegramIndex-Fork.git
synced 2025-06-05 22:09:12 +02:00
hide chat_id from path
This commit is contained in:
@ -21,6 +21,7 @@ except (KeyError, ValueError):
|
|||||||
try:
|
try:
|
||||||
chat_id_raw = os.environ["CHAT_ID"]
|
chat_id_raw = os.environ["CHAT_ID"]
|
||||||
chat_ids = [int(chat_id) for chat_id in chat_id_raw.split(' ')]
|
chat_ids = [int(chat_id) for chat_id in chat_id_raw.split(' ')]
|
||||||
|
alias_ids = []
|
||||||
except (KeyError, ValueError):
|
except (KeyError, ValueError):
|
||||||
print("Please set the CHAT_ID environment variable correctly")
|
print("Please set the CHAT_ID environment variable correctly")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
|
import random
|
||||||
|
import string
|
||||||
|
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
|
|
||||||
from .config import chat_ids
|
from .config import chat_ids, alias_ids
|
||||||
|
|
||||||
|
|
||||||
def setup_routes(app, handler):
|
def setup_routes(app, handler):
|
||||||
@ -9,7 +12,13 @@ def setup_routes(app, handler):
|
|||||||
web.get('/', h.home, name='home')
|
web.get('/', h.home, name='home')
|
||||||
]
|
]
|
||||||
for chat_id in chat_ids:
|
for chat_id in chat_ids:
|
||||||
p = f"/{chat_id}"
|
while True:
|
||||||
|
alias_id = ''.join([random.choice(string.ascii_letters + string.digits) for _ in range(len(str(chat_id)))])
|
||||||
|
if alias_id in alias_ids:
|
||||||
|
continue
|
||||||
|
alias_ids.append(alias_id)
|
||||||
|
break
|
||||||
|
p = f"/{alias_id}"
|
||||||
r = [
|
r = [
|
||||||
web.get(p, h.index),
|
web.get(p, h.index),
|
||||||
web.get(p + r"/{id:\d+}/view", h.info),
|
web.get(p + r"/{id:\d+}/view", h.info),
|
||||||
|
@ -3,10 +3,12 @@
|
|||||||
<h1 class=" my-2 text-2xl text-center font-bold text-green-400">
|
<h1 class=" my-2 text-2xl text-center font-bold text-green-400">
|
||||||
Available Sources
|
Available Sources
|
||||||
</h1>
|
</h1>
|
||||||
<div class="mx-auto my-2 px-4 w-full">
|
<div class="mx-auto my-2 p-2 w-full break-words">
|
||||||
|
<div class="block md:flex flex-wrap justify-center w-full">
|
||||||
{% for chat in chats %}
|
{% for chat in chats %}
|
||||||
<li class="w-full border border-blue-300 rounded bg-blue-100 hover:bg-blue-200 p-4 my-3 shadow-md"> <a href="/{{chat.id}}" > {{chat.name}}</a> </li>
|
<li class="w-full md:w-2/5 lg:w-1/4 border border-blue-300 rounded bg-blue-100 hover:bg-blue-200 p-3 my-3 md:mx-1 shadow-md"> <a href="/{{chat.id}}" > {{chat.name}} </a> </li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
{% include 'footer.html' %}
|
{% include 'footer.html' %}
|
||||||
|
21
app/views.py
21
app/views.py
@ -7,7 +7,7 @@ from telethon.tl import types
|
|||||||
from telethon.tl.custom import Message
|
from telethon.tl.custom import Message
|
||||||
|
|
||||||
from .util import get_file_name, get_human_size
|
from .util import get_file_name, get_human_size
|
||||||
from .config import chat_ids
|
from .config import chat_ids, alias_ids
|
||||||
|
|
||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
@ -22,12 +22,12 @@ class Views:
|
|||||||
@aiohttp_jinja2.template('home.html')
|
@aiohttp_jinja2.template('home.html')
|
||||||
async def home(self, req):
|
async def home(self, req):
|
||||||
if len(chat_ids) == 1:
|
if len(chat_ids) == 1:
|
||||||
raise web.HTTPFound(f"{chat_ids[0]}")
|
raise web.HTTPFound(f"{alias_ids[0]}")
|
||||||
chats = []
|
chats = []
|
||||||
for chat_id in chat_ids:
|
for chat_id, alias_id in zip(chat_ids, alias_ids):
|
||||||
chat = await self.client.get_entity(chat_id)
|
chat = await self.client.get_entity(chat_id)
|
||||||
chats.append({
|
chats.append({
|
||||||
'id': chat_id,
|
'id': alias_id,
|
||||||
'name': chat.title
|
'name': chat.title
|
||||||
})
|
})
|
||||||
return {'chats':chats}
|
return {'chats':chats}
|
||||||
@ -35,7 +35,8 @@ class Views:
|
|||||||
|
|
||||||
@aiohttp_jinja2.template('index.html')
|
@aiohttp_jinja2.template('index.html')
|
||||||
async def index(self, req):
|
async def index(self, req):
|
||||||
chat_id = int(req.rel_url.path.split('/')[1])
|
alias_id = req.rel_url.path.split('/')[1]
|
||||||
|
chat_id = chat_ids[alias_ids.index(alias_id)]
|
||||||
chat = await self.client.get_entity(chat_id)
|
chat = await self.client.get_entity(chat_id)
|
||||||
log_msg = ''
|
log_msg = ''
|
||||||
try:
|
try:
|
||||||
@ -74,7 +75,7 @@ class Views:
|
|||||||
insight = get_file_name(m)[:55],
|
insight = get_file_name(m)[:55],
|
||||||
date = m.date.isoformat(),
|
date = m.date.isoformat(),
|
||||||
size=get_human_size(m.file.size),
|
size=get_human_size(m.file.size),
|
||||||
url=req.rel_url.with_path(f"/{chat_id}/{m.id}/view")
|
url=req.rel_url.with_path(f"/{alias_id}/{m.id}/view")
|
||||||
)
|
)
|
||||||
elif m.message:
|
elif m.message:
|
||||||
entry = dict(
|
entry = dict(
|
||||||
@ -84,7 +85,7 @@ class Views:
|
|||||||
insight = m.raw_text[:55],
|
insight = m.raw_text[:55],
|
||||||
date = m.date.isoformat(),
|
date = m.date.isoformat(),
|
||||||
size=get_human_size(len(m.raw_text)),
|
size=get_human_size(len(m.raw_text)),
|
||||||
url=req.rel_url.with_path(f"/{chat_id}/{m.id}/view")
|
url=req.rel_url.with_path(f"/{alias_id}/{m.id}/view")
|
||||||
)
|
)
|
||||||
results.append(entry)
|
results.append(entry)
|
||||||
prev_page = False
|
prev_page = False
|
||||||
@ -120,7 +121,8 @@ class Views:
|
|||||||
@aiohttp_jinja2.template('info.html')
|
@aiohttp_jinja2.template('info.html')
|
||||||
async def info(self, req):
|
async def info(self, req):
|
||||||
file_id = int(req.match_info["id"])
|
file_id = int(req.match_info["id"])
|
||||||
chat_id = int(req.rel_url.path.split('/')[1])
|
alias_id = req.rel_url.path.split('/')[1]
|
||||||
|
chat_id = chat_ids[alias_ids.index(alias_id)]
|
||||||
message = await self.client.get_messages(entity=chat_id, ids=file_id)
|
message = await self.client.get_messages(entity=chat_id, ids=file_id)
|
||||||
if not message or not isinstance(message, Message):
|
if not message or not isinstance(message, Message):
|
||||||
log.debug(f"no valid entry for {file_id} in {chat_id}")
|
log.debug(f"no valid entry for {file_id} in {chat_id}")
|
||||||
@ -193,7 +195,8 @@ class Views:
|
|||||||
|
|
||||||
async def handle_request(self, req, head=False, thumb=False):
|
async def handle_request(self, req, head=False, thumb=False):
|
||||||
file_id = int(req.match_info["id"])
|
file_id = int(req.match_info["id"])
|
||||||
chat_id = int(req.rel_url.path.split('/')[1])
|
alias_id = req.rel_url.path.split('/')[1]
|
||||||
|
chat_id = chat_ids[alias_ids.index(alias_id)]
|
||||||
message = await self.client.get_messages(entity=chat_id, ids=file_id)
|
message = await self.client.get_messages(entity=chat_id, ids=file_id)
|
||||||
if not message or not message.file:
|
if not message or not message.file:
|
||||||
log.info(f"no result for {file_id} in {chat_id}")
|
log.info(f"no result for {file_id} in {chat_id}")
|
||||||
|
Reference in New Issue
Block a user