Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update for pyrogram v2 #68

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 31 additions & 47 deletions cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
API_HASH = os.getenv('API_HASH', None) or input('Enter your Telegram API hash: ')

app = Client("client", api_id=API_ID, api_hash=API_HASH)
app.start()

if not os.path.exists(cachePath):
with open(cachePath, "w") as cacheFile:
Expand Down Expand Up @@ -53,19 +52,16 @@ def chunks(l, n):
yield l[i:i + n]

@staticmethod
def get_all_chats():
dialogs = app.get_dialogs(pinned_only=True)
async def get_all_chats():
async with app:
dialogs = []
async for dialog in app.get_dialogs():
dialogs.append(dialog.chat)
return dialogs

dialog_chunk = app.get_dialogs()
while len(dialog_chunk) > 0:
dialogs.extend(dialog_chunk)
dialog_chunk = app.get_dialogs(offset_date=dialogs[-1].top_message.date-1)

return [d.chat for d in dialogs]

def select_groups(self, recursive=0):
chats = self.get_all_chats()
groups = [c for c in chats if c.type in ('group', 'supergroup')]
async def select_groups(self, recursive=0):
chats = await self.get_all_chats()
groups = [c for c in chats if c.type.name in ('GROUP, SUPERGROUP')]

print('Delete all your messages in')
for i, group in enumerate(groups):
Expand Down Expand Up @@ -101,60 +97,48 @@ def select_groups(self, recursive=0):
if recursive == 1:
self.run()

def run(self):
async def run(self):
for chat in self.chats:
peer = app.resolve_peer(chat.id)
chat_id = chat.id
message_ids = []
add_offset = 0

while True:
q = self.search_messages(peer, add_offset)
message_ids.extend(msg.id for msg in q['messages'])
messages_count = len(q['messages'])
print(f'Found {messages_count} of your messages in "{chat.title}"')
q = await self.search_messages(chat_id, add_offset)
message_ids.extend(msg.id for msg in q)
messages_count = len(q)
print(f'Found {len(message_ids)} of your messages in "{chat.title}"')
if messages_count < self.search_chunk_size:
break
add_offset += self.search_chunk_size

self.delete_messages(chat.id, message_ids)
await self.delete_messages(chat_id=chat.id, message_ids=message_ids)

def delete_messages(self, chat_id, message_ids):
async def delete_messages(self, chat_id, message_ids):
print(f'Deleting {len(message_ids)} messages with message IDs:')
print(message_ids)
for chunk in self.chunks(message_ids, self.delete_chunk_size):
try:
app.delete_messages(chat_id=chat_id, message_ids=chunk)
async with app:
await app.delete_messages(chat_id=chat_id, message_ids=chunk)
except FloodWait as flood_exception:
sleep(flood_exception.x)

def search_messages(self, peer, add_offset):
print(f'Searching messages. OFFSET: {add_offset}')
return app.send(
Search(
peer=peer,
q='',
filter=InputMessagesFilterEmpty(),
min_date=0,
max_date=0,
offset_id=0,
add_offset=add_offset,
limit=self.search_chunk_size,
max_id=0,
min_id=0,
hash=0,
from_id=InputPeerSelf()
),
sleep_threshold=60
)
async def search_messages(self, chat_id, add_offset):
async with app:
messages = []
print(f'Searching messages. OFFSET: {add_offset}')
async for message in app.search_messages(chat_id=chat_id, offset=add_offset, from_user="me", limit=100):
messages.append(message)
return messages


if __name__ == '__main__':
async def main():
try:
deleter = Cleaner()
deleter.select_groups()
deleter.run()
await deleter.select_groups()
await deleter.run()
except UnknownError as e:
print(f'UnknownError occured: {e}')
print('Probably API has changed, ask developers to update this utility')
finally:
app.stop()

app.run(main())
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
pyrogram==1.4
pyrogram==2.0.106
tgcrypto # Optional, for faster cleaning. You could remove this line