Created
May 11, 2024 20:27
-
-
Save dvygolov/f6aa546a8c0990a2f741b3192c435b41 to your computer and use it in GitHub Desktop.
This script can export all of your TG-channels from one folder into OPML format that can be imported into any RSS-reader. RSSHub.app is used for rss-friendly urls.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import xml.etree.ElementTree as ET | |
from telethon import TelegramClient | |
from telethon.tl.functions.messages import GetDialogFiltersRequest | |
from telethon.tl.types import InputPeerChannel, DialogFilter, DialogFilterDefault, DialogFilterChatlist | |
api_id = '<YOUR_APP_ID>' | |
api_hash = '<YOUR_APP_HASH>' | |
# Set up the Telethon client | |
client = TelegramClient('session_name', api_id, api_hash) | |
async def export_channels(folder_name): | |
# Connect to Telegram | |
await client.start() | |
print("Connected to Telegram!") | |
dialog_filters = await client(GetDialogFiltersRequest()) | |
channels = [] | |
# Filter channels based on the provided folder name | |
for filter in dialog_filters.filters: | |
if not isinstance(filter, DialogFilter) and not isinstance(filter, DialogFilterChatlist): | |
# print(filter.to_dict()) | |
continue | |
if filter.title != folder_name: | |
# print(filter.title+"\n"); | |
continue | |
for peer in filter.include_peers: | |
if isinstance(peer, InputPeerChannel): | |
channels.append(peer) | |
# OPML XML setup | |
root = ET.Element("opml", version="1.0") | |
head = ET.SubElement(root, "head") | |
title = ET.SubElement(head, "title") | |
title.text = "Exported Telegram Channels" | |
body = ET.SubElement(root, "body") | |
# Fetch channel details and create OPML entries | |
for channel in channels: | |
channel_entity = await client.get_entity(channel) | |
outline = ET.SubElement(body, "outline", { | |
"text": channel_entity.title, | |
"type": "rss", | |
"xmlUrl": f"https://rsshub.app/telegram/channel/{channel_entity.username if channel_entity.username else channel_entity.id}" | |
}) | |
# Save the OPML file | |
tree = ET.ElementTree(root) | |
tree.write("export.opml", encoding='utf-8', xml_declaration=True) | |
print("OPML file has been saved as export.opml") | |
if __name__ == '__main__': | |
folder_name = sys.argv[1] | |
client.loop.run_until_complete(export_channels(folder_name)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment