add logging

This commit is contained in:
odysseusmax 2020-08-11 06:29:09 +00:00
parent 4d0a757c94
commit 0ae39532c3
6 changed files with 82 additions and 59 deletions

View File

@ -1,3 +1,51 @@
from .main import main
import asyncio
import pathlib
import logging
import aiohttp_jinja2
import jinja2
from aiohttp import web
from .telegram import Client
from .routes import setup_routes
from .views import Views
from .config import host, port, session_string, api_id, api_hash, debug
TEMPLATES_ROOT = pathlib.Path(__file__).parent / 'templates'
client = Client(session_string, api_id, api_hash)
logging.basicConfig(level=logging.DEBUG if debug else logging.INFO)
logging.getLogger("telethon").setLevel(logging.INFO if debug else logging.ERROR)
log = logging.getLogger("tg-index")
def setup_jinja(app):
loader = jinja2.FileSystemLoader(str(TEMPLATES_ROOT))
aiohttp_jinja2.setup(app, loader=loader)
async def start():
await client.start()
log.debug("telegram client started!")
async def stop(app):
await client.disconnect()
log.debug("telegram client disconnected!")
async def init():
server = web.Application()
await start()
setup_routes(server, Views(client))
setup_jinja(server)
server.on_cleanup.append(stop)
return server
def main():
loop = asyncio.get_event_loop()
app = loop.run_until_complete(init())
web.run_app(app, host=host, port=port)
main()

View File

@ -31,3 +31,4 @@ except (KeyError, ValueError):
sys.exit(1)
host = os.environ.get("HOST", "0.0.0.0")
debug = bool(os.environ.get("DEBUG"))

View File

@ -1,42 +0,0 @@
import asyncio
import pathlib
import aiohttp_jinja2
import jinja2
from aiohttp import web
from .telegram import Client
from .routes import setup_routes
from .views import Views
from .config import host, port, session_string, api_id, api_hash
TEMPLATES_ROOT = pathlib.Path(__file__).parent / 'templates'
client = Client(session_string, api_id, api_hash)
def setup_jinja(app):
loader = jinja2.FileSystemLoader(str(TEMPLATES_ROOT))
aiohttp_jinja2.setup(app, loader=loader)
async def start():
await client.start()
async def stop(app):
await client.disconnect()
async def init():
server = web.Application()
await start()
setup_routes(server, Views(client))
setup_jinja(server)
server.on_cleanup.append(stop)
return server
def main():
loop = asyncio.get_event_loop()
app = loop.run_until_complete(init())
web.run_app(app, host=host, port=port)

View File

@ -11,6 +11,7 @@ class Client(TelegramClient):
super().__init__(StringSession(session_string), *args, **kwargs)
self.log = logging.getLogger(__name__)
async def download(self, file, file_size, offset, limit):
part_size = 1024 * 1024
first_part_cut = offset % part_size
@ -27,11 +28,11 @@ class Client(TelegramClient):
yield chunk[:last_part_cut]
else:
yield chunk
self.log.debug(f"Part {part}/{last_part} (total {part_count}) downloaded")
self.log.debug(f"Part {part}/{last_part} (total {part_count}) served!")
part += 1
self.log.debug("download finished")
self.log.debug("serving finished")
except (GeneratorExit, StopAsyncIteration, asyncio.CancelledError):
self.log.debug("download interrupted")
self.log.debug("file serve interrupted")
raise
except Exception:
self.log.debug("download errored", exc_info=True)
self.log.debug("file serve errored", exc_info=True)

View File

@ -7,7 +7,7 @@
<script src="https://cdn.fluidplayer.com/v3/current/fluidplayer.min.js"></script>
<title>
{{title}}
{% if title %} {{title}} {% else %} Telegram Index {% endif %}
</title>
</head>

View File

@ -1,11 +1,16 @@
import logging
from aiohttp import web
import aiohttp_jinja2
from jinja2 import Markup
from telethon.tl import types
from telethon.tl.custom import Message
from .util import get_file_name, get_human_size
from .config import chat_id
from util import get_file_name, get_human_size
from config import chat_id
log = logging.getLogger(__name__)
class Views:
@ -16,14 +21,17 @@ class Views:
@aiohttp_jinja2.template('index.html')
async def index(self, req):
log_msg = ''
try:
offset_val = int(req.query.get('page', '1'))
except:
offset_val = 1
log_msg += f"page: {offset_val} | "
try:
search_query = req.query.get('search', '')
except:
search_query = ''
log_msg += f"search query: {search_query} | "
offset_val = 0 if offset_val <=1 else offset_val-1
try:
if search_query:
@ -31,7 +39,10 @@ class Views:
else:
messages = (await self.client.get_messages(chat_id, limit=20, add_offset=20*offset_val)) or []
except:
log.debug("failed to get messages", exc_info=True)
messages = []
log_msg += f"found {len(messages)} results"
log.debug(log_msg)
results = []
for m in messages:
if m.file and not isinstance(m.media, types.MessageMediaWebPage):
@ -79,7 +90,6 @@ class Views:
'cur_page' : offset_val+1,
'next_page': next_page,
'search': search_query,
'title': "Telegram Index"
}
@ -88,12 +98,12 @@ class Views:
file_id = int(req.match_info["id"])
message = await self.client.get_messages(entity=chat_id, ids=file_id)
if not message or not isinstance(message, Message):
print(type(message))
log.debug(f"no valid entry for {file_id} in {chat_id}")
return {
'found':False,
'reason' : "Entry you are looking for cannot be retrived!",
'title': "Telegram Index"
}
return_val = {}
if message.file and not isinstance(message.media, types.MessageMediaWebPage):
file_name = get_file_name(message)
file_size = get_human_size(message.file.size)
@ -114,7 +124,7 @@ class Views:
else:
caption = False
return {
return_val = {
'found': True,
'name': file_name,
'id': file_id,
@ -125,19 +135,20 @@ class Views:
}
elif message.message:
text = Markup.escape(message.raw_text).__str__().replace('\n', '<br>')
return {
return_val = {
'found': True,
'media': False,
'text': text,
'title': "Telegram Index"
}
else:
return {
return_val = {
'found':False,
'reason' : "Some kind of entry that I cannot display",
'title': "Telegram Index"
}
log.debug(f"data for {file_id} in {chat_id} returned as {return_val}")
return return_val
async def download_get(self, req):
return await self.handle_request(req)
@ -160,11 +171,13 @@ class Views:
message = await self.client.get_messages(entity=chat_id, ids=file_id)
if not message or not message.file:
log.info(f"no result for {file_id} in {chat_id}")
return web.Response(status=410, text="410: Gone. Access to the target resource is no longer available!")
if thumb and message.document:
thumbnail = message.document.thumbs
if not thumbnail:
log.info(f"no thumbnail for {file_id} in {chat_id}")
return web.Response(status=404, text="404: Not Found")
thumbnail = thumbnail[-1]
mime_type = 'image/jpeg'
@ -188,6 +201,7 @@ class Views:
if (limit > size) or (offset < 0) or (limit < offset):
raise ValueError("range not in acceptable format")
except ValueError:
log.info("Range Not Satisfiable", exc_info=True)
return web.Response(
status=416,
text="416: Range Not Satisfiable",
@ -198,6 +212,7 @@ class Views:
if not head:
body = self.client.download(media, size, offset, limit)
log.info(f"Serving file {message.id} in {chat_id} ; Range: {offset} - {limit}")
else:
body = None