Skip to content

Commit

Permalink
Split into many files and add license
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Dec 11, 2019
1 parent 70d9485 commit a00b3e5
Show file tree
Hide file tree
Showing 13 changed files with 1,113 additions and 280 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
*.session
*.session-journal

__pycache__
*.pyc
661 changes: 661 additions & 0 deletions LICENSE

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,21 @@
# tgfilestream
A Telegram bot that can stream Telegram files to users over HTTP.

## Setup
Install dependencies (see [requirements.txt](/requirements.txt)), configure
environment variables (see below) and run with `python3 -m tgfilestream`.

A reverse proxy is recommended to add TLS. When using a reverse proxy, keep
`HOST` as-is, but add the publicly accessible URL to `PUBLIC_URL`. The URL
should include the protocol, e.g. `https://example.com`.

### Environment variables
* `TG_API_ID` (required) - Your Telegram API ID.
* `TG_API_HASH` (required) - Your Telegram API hash.
* `TG_SESSION_NAME` (defaults to `tgfilestream`) - The name of the Telethon session file to use.
* `PORT` (defaults to `8080`) - The port to listen at.
* `HOST` (defaults to `localhost`) - The host to listen at.
* `PUBLIC_URL` (defaults to `http://localhost:8080`) - The prefix for links that the bot gives.
* `TRUST_FORWARD_HEADERS` (defaults to false) - Whether or not to trust X-Forwarded-For headers when logging requests.
* `DEBUG` (defaults to false) - Whether or not to enable extra prints.
* `LOG_CONFIG` - Path to a Python basic log config. Overrides `DEBUG`.
6 changes: 4 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import setuptools

from tgfilestream import __version__

try:
long_desc = open("README.md").read()
except IOError:
long_desc = "Failed to read README.md"

setuptools.setup(
name="tgfilestream",
version="0.1.0",
version=__version__,
url="https://mau.dev/tulir/tgfilestream",

author="Tulir Asokan",
Expand Down Expand Up @@ -40,6 +42,6 @@
],
entry_points="""
[console_scripts]
tgfilestream=tgfilestream:main
tgfilestream=tgfilestream.__main__:main
""",
)
278 changes: 0 additions & 278 deletions tgfilestream.py

This file was deleted.

3 changes: 3 additions & 0 deletions tgfilestream/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
__version__ = "0.1.0"
__author__ = "Tulir Asokan <[email protected]>"

61 changes: 61 additions & 0 deletions tgfilestream/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# tgfilestream - A Telegram bot that can stream Telegram files to users over HTTP.
# Copyright (C) 2019 Tulir Asokan
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import asyncio
import sys

from aiohttp import web

from .telegram import client
from .web_routes import routes
from .config import host, port, public_url
from .log import log

server = web.Application()
server.add_routes(routes)
runner = web.AppRunner(server)

loop = asyncio.get_event_loop()


async def start() -> None:
await client.start()

await runner.setup()
await web.TCPSite(runner, host, port).start()


async def stop() -> None:
await runner.cleanup()
await client.disconnect()


try:
loop.run_until_complete(start())
except Exception:
log.fatal("Failed to initialize", exc_info=True)
sys.exit(2)

log.info("Initialization complete")
log.debug(f"Listening at http://{host}:{port}")
log.debug(f"Public URL prefix is {public_url}")

try:
loop.run_forever()
except KeyboardInterrupt:
loop.run_until_complete(stop())
except Exception:
log.fatal("Fatal error in event loop", exc_info=True)
sys.exit(3)
Loading

0 comments on commit a00b3e5

Please sign in to comment.