1
1
1danilacrazy12020-01-03 20:02:01
Python
1danilacrazy1, 2020-01-03 20:02:01

How to sort json file through python?

I've been trying to make a parser for my tg channel for 2 days. it remains to add a couple of lines that will sort the json file so that only those lines that will include #T21-#T47 remain in it, excluding #T28, #T29, #T30 from this list.
in the output you will need to enter your phone number, the code from the telegram and a link to the tg channel
https://t.me/statistika_21_classic


import configparser
import json

from telethon.sync import TelegramClient
from telethon import connection

# для корректного переноса времени сообщений в json
from datetime import date, datetime

# классы для работы с каналами
from telethon.tl.functions.channels import GetParticipantsRequest
from telethon.tl.types import ChannelParticipantsSearch

# класс для работы с сообщениями
from telethon.tl.functions.messages import GetHistoryRequest

# Считываем учетные данные
config = configparser.ConfigParser()
config.read("config.ini")

api_id   = config['Telegram']['api_id']
api_hash = config['Telegram']['api_hash']
username = config['Telegram']['username']

client = TelegramClient(username, api_id, api_hash)

client.start()

async def dump_all_messages(channel):
  """Записывает json-файл с информацией о всех сообщениях канала/чата"""
  offset_msg = 0    # номер записи, с которой начинается считывание
  limit_msg = 100   # максимальное число записей, передаваемых за один раз

  all_messages = []   # список всех сообщений
  total_messages = 0
  total_count_limit = 600 

  class DateTimeEncoder(json.JSONEncoder):
    '''Класс для сериализации записи дат в JSON'''
    def default(self, o):
      if isinstance(o, datetime):
        return o.isoformat()
      if isinstance(o, bytes):
        return list(o)
      return json.JSONEncoder.default(self, o)

  while True:
    history = await client(GetHistoryRequest(
      peer=channel,
      offset_id=offset_msg,
      offset_date=None, add_offset=0,
      limit=limit_msg, 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_msg = 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', encoding='utf8') as outfile:
     json.dump(all_messages, outfile, ensure_ascii=False, cls=DateTimeEncoder)


async def main():
  url = input("Введите ссылку на канал или чат: ")
  channel = await client.get_entity(url)
  await dump_all_messages(channel)


with client:
  client.loop.run_until_complete(main())

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question