TelegramIndex-Fork/app/views/__init__.py

60 lines
1.4 KiB
Python
Raw Normal View History

import base64
import hashlib
2020-12-12 15:16:44 +01:00
import random
import string
from ..config import SHORT_URL_LEN
2020-12-12 15:16:44 +01:00
from .home_view import HomeView
from .wildcard_view import WildcardView
from .download import Download
from .index_view import IndexView
from .info_view import InfoView
from .logo_view import LogoView
from .thumbnail_view import ThumbnailView
2021-05-22 16:00:08 +02:00
from .login_view import LoginView
from .logout_view import LogoutView
from .middlewhere import middleware_factory
2020-12-12 15:16:44 +01:00
2021-05-22 16:00:08 +02:00
class Views(
HomeView,
Download,
IndexView,
InfoView,
LogoView,
ThumbnailView,
WildcardView,
LoginView,
LogoutView,
):
2020-12-12 15:16:44 +01:00
def __init__(self, client):
self.client = client
2021-05-22 16:00:08 +02:00
self.chat_ids = {}
2020-12-12 15:16:44 +01:00
def generate_alias_id(self, chat):
chat_id = chat.id
title = chat.title
while True:
# alias_id = "".join(
# [
# random.choice(string.ascii_letters + string.digits)
# for _ in range(len(str(chat_id)))
# ]
# )
orig_id = f"{title}{chat_id}" # the original id
alias_id = base64.urlsafe_b64encode(hashlib.md5(orig_id.encode()).digest())[:SHORT_URL_LEN].decode()
2021-05-22 16:00:08 +02:00
if alias_id in self.chat_ids:
2020-12-12 15:16:44 +01:00
continue
2021-05-22 16:00:08 +02:00
self.chat_ids[alias_id] = {
"chat_id": chat_id,
"alias_id": alias_id,
"title": title,
}
2020-12-12 15:16:44 +01:00
return alias_id