TelegramIndex-Fork/app/config.py

67 lines
2.1 KiB
Python
Raw Normal View History

2020-12-12 15:16:44 +01:00
from pathlib import Path
2020-12-17 04:16:10 +01:00
import platform
2020-08-13 06:05:46 +02:00
import traceback
2020-08-17 09:44:02 +02:00
import json
2020-08-10 09:27:52 +02:00
import sys
import os
try:
port = int(os.environ.get("PORT", "8080"))
2021-06-11 20:03:57 +02:00
except Exception as e:
print(e)
2020-08-10 09:27:52 +02:00
port = -1
if not 1 <= port <= 65535:
2020-12-17 04:16:10 +01:00
print(
"Please make sure the PORT environment variable is an integer between 1 and 65535"
)
2020-08-10 09:27:52 +02:00
sys.exit(1)
try:
api_id = int(os.environ["API_ID"])
api_hash = os.environ["API_HASH"]
except (KeyError, ValueError):
2020-08-13 06:05:46 +02:00
traceback.print_exc()
print("\n\nPlease set the API_ID and API_HASH environment variables correctly")
2020-08-10 09:27:52 +02:00
print("You can get your own API keys at https://my.telegram.org/apps")
sys.exit(1)
2020-09-20 18:38:52 +02:00
2020-08-10 09:27:52 +02:00
try:
2020-08-17 09:44:02 +02:00
index_settings_str = os.environ["INDEX_SETTINGS"].strip()
index_settings = json.loads(index_settings_str)
except:
2020-08-13 06:05:46 +02:00
traceback.print_exc()
2020-08-17 09:44:02 +02:00
print("\n\nPlease set the INDEX_SETTINGS environment variable correctly")
2020-08-10 09:27:52 +02:00
sys.exit(1)
try:
session_string = os.environ["SESSION_STRING"]
except (KeyError, ValueError):
2020-08-13 06:05:46 +02:00
traceback.print_exc()
print("\n\nPlease set the SESSION_STRING environment variable correctly")
2020-08-10 09:27:52 +02:00
sys.exit(1)
host = os.environ.get("HOST", "0.0.0.0")
2020-08-13 06:07:18 +02:00
debug = bool(os.environ.get("DEBUG"))
2020-12-12 15:16:44 +01:00
block_downloads = bool(os.environ.get("BLOCK_DOWNLOADS"))
results_per_page = int(os.environ.get("RESULTS_PER_PAGE", "20"))
2021-06-06 13:32:24 +02:00
logo_folder = Path("./Temp/logo/" if platform.system() == "Windows" else "/tmp/logo")
if not logo_folder.exists():
logo_folder.mkdir(parents=True)
username = os.environ.get("TGINDEX_USERNAME", "")
2021-05-22 16:00:08 +02:00
password = os.environ.get("PASSWORD", "")
2021-06-06 14:42:06 +02:00
SHORT_URL_LEN = int(os.environ.get("SHORT_URL_LEN", 3))
authenticated = bool(username and password)
2021-05-22 16:00:08 +02:00
SESSION_COOKIE_LIFETIME = int(os.environ.get("SESSION_COOKIE_LIFETIME") or "60")
try:
SECRET_KEY = os.environ["SECRET_KEY"]
if len(SECRET_KEY) != 32:
raise ValueError("SECRET_KEY should be exactly 32 charaters long")
2021-05-22 16:00:08 +02:00
except (KeyError, ValueError):
if authenticated:
traceback.print_exc()
print("\n\nPlease set the SECRET_KEY environment variable correctly")
sys.exit(1)
else:
SECRET_KEY = ""