import configparser
import json
import asyncio
from datetime import date, datetime

from telethon import TelegramClient
from telethon.errors import SessionPasswordNeededError
from telethon.tl.functions.messages import (GetHistoryRequest)
from telethon.tl.types import (
    PeerChannel
)


# some functions to parse json date
class DateTimeEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, datetime):
            return o.isoformat()

        if isinstance(o, bytes):
            return list(o)

        return json.JSONEncoder.default(self, o)


# Reading Configs
config = configparser.ConfigParser()
config.read("config.ini")


# Setting configuration values
api_id = 15595904
api_hash = 'ea9e7d3c60f18d0f5b7615cd8c8f260c'
username = '@daxa_tyan'

phone = '+79251215424'

# Create the client and connect
client = TelegramClient(username, api_id, api_hash)
async def main(phone):
    await client.start()
    print("Client Created")
    # Ensure you're authorized
    if await client.is_user_authorized() == False:
        await client.send_code_request(phone)
        try:
            await client.sign_in(phone, input('Enter the code: '))
        except SessionPasswordNeededError:
            await client.sign_in(password=input('Password: '))

    me = await client.get_me()


    user_input_channel_list = ['vtbmyinvestments', 'angelovcap', 'tinkoff_invest_official', 'lemonfortea',
                          'investfuture', 'SberInvestments', 'Coin_Post', 'investingcorp', 'banksta', 'profinansy',
                               'markettwits', 'AK47pfl', 'if_stocks', 'INSTARDING', 'alfawealth', 'Kondrashovinvest',
                               'forbesrussia', 'cbrstocks', 'investorbiz', 'tinkoffjournal', 'ifinvest', 'dohod',
                               'alfa_investments', 'selfinvestor', 'nastya_docs']
    all_messages = []

    for user_input_channel in user_input_channel_list:
        if user_input_channel.isdigit():
            entity = PeerChannel(int(user_input_channel))
        else:
            entity = user_input_channel

        my_channel = await client.get_entity(entity)

        offset_id = 0
        limit = 100

        total_messages = 0
        total_count_limit = 0

        while True:
            print("Current Offset ID is:", offset_id, "; Total Messages:", total_messages)
            history = await client(GetHistoryRequest(
                peer=my_channel,
                offset_id=offset_id,
                offset_date=None,
                add_offset=0,
                limit=limit,
                max_id=0,
                min_id=0,
                hash=0
            ))
            if not history.messages:
                break
            messages = history.messages
            for message in messages:
                all_messages.append(message.to_dict())
            offset_id = messages[len(messages) - 1].id
            total_messages = len(all_messages)
            if total_count_limit != 0 and total_messages >= total_count_limit:
                break

        with open('channel_messages.json', 'w') as outfile:
            json.dump(all_messages, outfile, cls=DateTimeEncoder)

with client:
    client.loop.run_until_complete(main(phone))
import configparser
import json
import asyncio
from datetime import date, datetime

from telethon import TelegramClient
from telethon.errors import SessionPasswordNeededError
from telethon.tl.functions.messages import (GetHistoryRequest)
from telethon.tl.types import (
    PeerChannel
)


# some functions to parse json date
class DateTimeEncoder(json.JSONEncoder):
    def default(self, o):
        if isinstance(o, datetime):
            return o.isoformat()

        if isinstance(o, bytes):
            return list(o)

        return json.JSONEncoder.default(self, o)


# Reading Configs
config = configparser.ConfigParser()
config.read("config.ini")


# Setting configuration values
api_id = 15595904
api_hash = 'ea9e7d3c60f18d0f5b7615cd8c8f260c'
username = '@daxa_tyan'

phone = '+79251215424'

# Create the client and connect
client = TelegramClient(username, api_id, api_hash)
async def main(phone):
    await client.start()
    print("Client Created")
    # Ensure you're authorized
    if await client.is_user_authorized() == False:
        await client.send_code_request(phone)
        try:
            await client.sign_in(phone, input('Enter the code: '))
        except SessionPasswordNeededError:
            await client.sign_in(password=input('Password: '))

    me = await client.get_me()


    user_input_channel_list = ['vtbmyinvestments', 'angelovcap', 'tinkoff_invest_official', 'lemonfortea',
                          'investfuture', 'SberInvestments', 'Coin_Post', 'investingcorp', 'banksta', 'profinansy',
                               'markettwits', 'AK47pfl', 'if_stocks', 'INSTARDING', 'alfawealth', 'Kondrashovinvest',
                               'forbesrussia', 'cbrstocks', 'investorbiz', 'tinkoffjournal', 'ifinvest', 'dohod',
                               'alfa_investments', 'selfinvestor', 'nastya_docs']
    all_messages = []

    for user_input_channel in user_input_channel_list:
        if user_input_channel.isdigit():
            entity = PeerChannel(int(user_input_channel))
        else:
            entity = user_input_channel

        my_channel = await client.get_entity(entity)

        offset_id = 0
        limit = 100

        total_messages = 0
        total_count_limit = 0

        while True:
            print("Current Offset ID is:", offset_id, "; Total Messages:", total_messages)
            history = await client(GetHistoryRequest(
                peer=my_channel,
                offset_id=offset_id,
                offset_date=None,
                add_offset=0,
                limit=limit,
                max_id=0,
                min_id=0,
                hash=0
            ))
            if not history.messages:
                break
            messages = history.messages
            for message in messages:
                all_messages.append(message.to_dict())
            offset_id = messages[len(messages) - 1].id
            total_messages = len(all_messages)
            if total_count_limit != 0 and total_messages >= total_count_limit:
                break

        with open('channel_messages.json', 'w') as outfile:
            json.dump(all_messages, outfile, cls=DateTimeEncoder)

with client:
    client.loop.run_until_complete(main(phone))
# some functions to parse json date # Reading Configs # Setting configuration values # Create the client and connect # Ensure you're authorized