Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from telethon import TelegramClient
- from my_config import api_id, api_hash, phone_number, gs_url
- import json, gspread
- # Настройки
- posts_to_get = 100
- channels_to_review = ['https://t.me/rusbrief'] # Через запятую, еслм надо, это список
- creds = 'amedia-415820-d9db0852c5c9.json'
- client = TelegramClient('anon', api_id, api_hash)
- async def main(posts_to_get, channels_to_review):
- k=0
- out_lst = []
- for channel in channels_to_review:
- async for message in client.iter_messages(channel):
- emotions_dict = {}
- if message.reactions is not None:
- for r in message.reactions.results:
- emotions_dict[r.reaction.emoticon] = r.count
- out_lst.append([str(message.date.strftime('%Y-%m-%d %H:%M:%S')),
- f'{channel}/{message.id}',
- message.raw_text,
- json.dumps(emotions_dict, ensure_ascii=False)])
- if k > posts_to_get: break
- else: k += 1
- gc = gspread.service_account(filename=creds)
- sh = gc.open_by_url(gs_url)
- worksheet = sh.worksheet("Лист1")
- # Проверка, если ли такой пост уже в базе. Если есть, удаляем из списка на добавление
- all_values_in_gs = worksheet.get_all_values()
- existing_rows = []
- if len(all_values_in_gs) > 1:
- existing_rows = set([x[1] for x in all_values_in_gs])
- out_lst = [x for x in out_lst if x[1] not in existing_rows]
- worksheet.append_rows(out_lst, table_range='A1')
- with client:
- client.start(phone_number)
- client.loop.run_until_complete(main(posts_to_get, channels_to_review))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement