[Add] init

This commit is contained in:
tiennm99 committed 2024-03-29 22:31:46 +07:00
1 parent 54b067fdf0
commit d6251c0020
7 files changed
+101

No files matched your search

+3
View File
@@ -0,0 +1,3 @@
PHONE=+1234567890
API_ID=123456789
API_HASH=0123456789abcdef0123456789abcdef
+4
View File
@@ -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
+23
View File
@@ -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
```
+22
View File
@@ -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])
+11
View File
@@ -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
+33
View File
@@ -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.')
+5
View File
@@ -0,0 +1,5 @@
pyaes==1.6.1
pyasn1==0.6.0
python-dotenv==1.0.1
rsa==4.9
Telethon==1.34.0