-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathtelegram_collect.py
More file actions
104 lines (89 loc) · 4.26 KB
/
Copy pathtelegram_collect.py
File metadata and controls
104 lines (89 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import json
import re
import os
import requests
from telethon import TelegramClient
from telethon.sessions import StringSession
import asyncio
import logging
# تنظیمات لاگ
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
# اطلاعات API تلگرام از متغیرهای محیطی
api_id = int(os.getenv('TELEGRAM_API_ID'))
api_hash = os.getenv('TELEGRAM_API_HASH')
session_string = os.getenv('TELEGRAM_SESSION')
# مسیر فایلها
channels_file = 'channels.json' # فایل لیست کانالهای تلگرام
sublinks_file = 'sublinks.json' # فایل لیست سابلینکها
output_file = 'config.txt' # فایل خروجی برای کانفیگها
# تابع برای دریافت کانفیگها از سابلینک
def fetch_configs_from_sublink(url, protocols):
try:
response = requests.get(url.split('#')[0]) # حذف بخش #VIBE%7CPOWER
response.raise_for_status()
configs = response.text.strip().split('\n')
# فیلتر کردن کانفیگها بر اساس پروتکلهای مجاز
filtered_configs = [
config for config in configs
if config and re.match(r'(vless|vmess|trojan|ss|tuic|hy2):\/\/[^\s]+', config)
and config.split('://')[0] in protocols
]
logging.info(f"Fetched {len(filtered_configs)} configs from {url}")
return filtered_configs
except Exception as e:
logging.error(f"Error fetching sublink {url}: {e}")
return []
# تابع اصلی
async def main():
# ایجاد کلاینت تلگرام با StringSession
client = TelegramClient(StringSession(session_string), api_id, api_hash)
try:
# اتصال به تلگرام
await client.start()
logging.info("Successfully connected to Telegram")
# لیست کانفیگها
configs = []
# 1. جمعآوری کانفیگها از کانالهای تلگرام
try:
with open(channels_file, 'r', encoding='utf-8') as f:
channels_data = json.load(f)
for channel_name, protocols in channels_data.items():
logging.info(f"Processing channel: {channel_name}")
try:
channel_entity = await client.get_entity(f"@{channel_name}")
async for message in client.iter_messages(channel_entity, limit=100):
if message.text:
config_matches = re.findall(r'(vless|vmess|trojan|ss|tuic|hy2):\/\/[^\s]+', message.text)
for config in config_matches:
if config.split('://')[0] in protocols:
configs.append(config)
except Exception as e:
logging.error(f"Error processing channel {channel_name}: {e}")
except FileNotFoundError:
logging.warning(f"{channels_file} not found, skipping Telegram channels")
# 2. جمعآوری کانفیگها از سابلینکها
try:
with open(sublinks_file, 'r', encoding='utf-8') as f:
sublinks_data = json.load(f)
for sublink in sublinks_data.get('sublinks', []):
url = sublink.get('url')
protocols = sublink.get('protocols', [])
sublink_configs = fetch_configs_from_sublink(url, protocols)
configs.extend(sublink_configs)
except FileNotFoundError:
logging.warning(f"{sublinks_file} not found, skipping sublinks")
# حذف موارد تکراری
configs = list(dict.fromkeys(configs))
# ذخیره کانفیگها در config.txt با تگ شمارهدار
with open(output_file, 'w', encoding='utf-8') as f:
for idx, config in enumerate(configs, start=1):
tagged_config = f"{config}#@SiNAVM-{idx}"
f.write(tagged_config + '\n')
logging.info(f"Saved {len(configs)} configs to {output_file}")
except Exception as e:
logging.error(f"Error: {e}")
finally:
await client.disconnect()
# اجرای اسکریپت
if __name__ == '__main__':
asyncio.run(main())