File size: 3,219 Bytes
26741bb
 
0e49d61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#Filestream/__main__.py

import sys
import asyncio
import logging
import traceback
import logging.handlers as handlers
from FileStream.config import Telegram, Server
from aiohttp import web
from pyrogram import idle

from FileStream.bot import FileStream
from FileStream.Tools import Time_ISTKolNow
from FileStream.server import web_server
from FileStream.bot.clients import initialize_clients

logging.basicConfig(
    level=logging.INFO,
    datefmt="%d/%m/%Y %H:%M:%S",
    format=
    '[%(asctime)s] {%(pathname)s:%(lineno)d} %(levelname)s - %(message)s',
    handlers=[
        logging.StreamHandler(stream=sys.stdout),
        handlers.RotatingFileHandler("streambot.log",
                                     mode="a",
                                     maxBytes=104857600,
                                     backupCount=2,
                                     encoding="utf-8")
    ],
)

logging.getLogger("aiohttp").setLevel(logging.ERROR)
logging.getLogger("pyrogram").setLevel(logging.ERROR)
logging.getLogger("aiohttp.web").setLevel(logging.ERROR)

server = web.AppRunner(web_server())

loop = asyncio.get_event_loop()


async def start_services():
  print("****")
  if Telegram.SECONDARY:
    print("------------------ Starting as Secondary Server ------------------")
  else:
    print("------------------- Starting as Primary Server -------------------")
  print( "****")
  print("-------------------- Initializing Telegram Bot --------------------")

  await FileStream.start()
  bot_info = await FileStream.get_me()
  FileStream.id = bot_info.id
  FileStream.username = bot_info.username
  FileStream.fname = bot_info.first_name
  print("------------------------------ DONE ------------------------------")
  print("\n")
  print("---------------------- Initializing Clients ----------------------")
  await initialize_clients()
  print("------------------------------ DONE ------------------------------")
  print("\n")
  print("--------------------- Initializing Web Server ---------------------")
  await server.setup()
  await web.TCPSite(server, Server.BIND_ADDRESS, Server.PORT).start()
  print("------------------------------ DONE ------------------------------")
  print("\n")
  print("------------------------- Service Started -------------------------")
  print("Bot =>> {}".format(bot_info.first_name))
  if bot_info.dc_id:
    print("DC ID =>> {}".format(str(bot_info.dc_id)))
  print(" URL =>> {}".format(Server.URL))
  print("------------------------------------------------------------------")
  """
  all_sources = [
      Telegram.ULOG_GROUP, Telegram.FLOG_CHANNEL, Telegram.PFLOG_CHANNEL
  ]
  for source in all_sources:
    await FileStream.send_message(chat_id=source,
                                  text=f"Hi, I am Online @{ISTKolNow()}",
                                  disable_web_page_preview=True)
  """
  await idle()


async def cleanup():
  await server.cleanup()
  #await FileStream.stop()


if __name__ == "__main__":
  try:
    loop.run_until_complete(start_services())
  except KeyboardInterrupt:
    loop.stop()
    print("------------------------ Stopped Services ------------------------")
  except Exception as err:
    logging.error(traceback.format_exc())