From d6251c0020b49e24524ccbc459e006943aa834e5 Mon Sep 17 00:00:00 2001 From: TienNM Date: Fri, 29 Mar 2024 22:31:46 +0700 Subject: [PATCH] [Add] init --- .env.example | 3 +++ .gitignore | 4 ++++ README.md | 23 +++++++++++++++++++++++ common.py | 22 ++++++++++++++++++++++ config.py | 11 +++++++++++ main.py | 33 +++++++++++++++++++++++++++++++++ requirements.txt | 5 +++++ 7 files changed, 101 insertions(+) create mode 100644 .env.example create mode 100644 common.py create mode 100644 config.py create mode 100644 main.py create mode 100644 requirements.txt diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..9122e7b --- /dev/null +++ b/.env.example @@ -0,0 +1,3 @@ +PHONE=+1234567890 +API_ID=123456789 +API_HASH=0123456789abcdef0123456789abcdef diff --git a/.gitignore b/.gitignore index 68bc17f..67aee0b 100644 --- a/.gitignore +++ b/.gitignore @@ -158,3 +158,7 @@ cython_debug/ # and can be added to the global gitignore or merged into this file. For a more nuclear # option (not recommended) you can uncomment the following to ignore the entire idea folder. #.idea/ + +.idea/ +*.session +*.csv diff --git a/README.md b/README.md index dd1fcee..a82e382 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,25 @@ # export-telegram-group-members + Export Telegram group members + +## How to use + +1. Clone this repository: + +```bash +git clone https://github.com/tiennm99/export-telegram-group-members.git +``` + +2. Install requirements: + +```bash +pip install -r requirements.txt +``` + +3. Create a new Telegram app at [https://my.telegram.org](https://my.telegram.org) and get the `api_id` and `api_hash`. +4. Copy `.env.example` to `.env` and fill in the `api_id`, `api_hash`, and `phone`. +5. Run the script: + +```bash +python main.py +``` diff --git a/common.py b/common.py new file mode 100644 index 0000000..847395e --- /dev/null +++ b/common.py @@ -0,0 +1,22 @@ +import csv +import os +import re +from datetime import datetime + +now = datetime.now() +folder_name = now.strftime('%Y-%m-%d %H-%M-%S') +os.mkdir(folder_name) + + +def remove_special_characters(string): + return re.sub('[<>:"/\\\\|?*]', '', string) + + +def export_csv(group_name, members): + filename = remove_special_characters(group_name) + '.csv' + path = os.path.join(folder_name, filename) + with open(path, 'w', encoding='UTF-8') as f: + writer = csv.writer(f, delimiter=',', lineterminator='\n') + writer.writerow(['id', 'username', 'first_name', 'last_name']) + for member in members: + writer.writerow([member.id, member.username, member.first_name, member.last_name]) diff --git a/config.py b/config.py new file mode 100644 index 0000000..5cd2c9c --- /dev/null +++ b/config.py @@ -0,0 +1,11 @@ +import os + +from dotenv import load_dotenv + +load_dotenv() + +phone = os.getenv('PHONE') +api_id = os.getenv('API_ID') +api_hash = os.getenv('API_HASH') + +group_names_to_export = ['sample1', 'sample2'] # Add the group names you want to export here diff --git a/main.py b/main.py new file mode 100644 index 0000000..99c3a20 --- /dev/null +++ b/main.py @@ -0,0 +1,33 @@ +import getpass + +from telethon.errors import SessionPasswordNeededError +from telethon.sync import TelegramClient + +from common import * +from config import * + +client = TelegramClient(phone, api_id, api_hash) + +client.connect() +if not client.is_user_authorized(): + client.send_code_request(phone) + try: + client.sign_in(code=input('Enter code: ')) + except SessionPasswordNeededError: + client.sign_in(password=getpass.getpass()) + +client.start(phone) + +dialogs = client.get_dialogs() +for dialog in dialogs: + if not dialog.is_group: + print('skip ' + dialog.name + ' because it is not a group.') + continue + if dialog.name not in group_names_to_export: + print('skip ' + dialog.name + ' because it is not in the list of group names to export.') + continue + print('exporting ' + dialog.name) + members = client.get_participants(dialog) + members.sort(key=lambda x: x.id) + export_csv(dialog.name, members) + print('export ' + dialog.name + ' done.') diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..ad642db --- /dev/null +++ b/requirements.txt @@ -0,0 +1,5 @@ +pyaes==1.6.1 +pyasn1==0.6.0 +python-dotenv==1.0.1 +rsa==4.9 +Telethon==1.34.0