Skip to content
This repository was archived by the owner on Oct 23, 2019. It is now read-only.

added types for scraping and fixed a bug that would make exporter to … #114

Open
wants to merge 1 commit 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
4 changes: 4 additions & 0 deletions config.ini.example
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,7 @@ LibraryLogLevel = WARNING
# Sets proxy support
# Proxy = socks5://user:[email protected]:1080
# Proxy = http://127.0.0.1:8080

# Sets types which can be group, user or channel
# Should be comma separated
FromType = user, channel
4 changes: 3 additions & 1 deletion telegram_export/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ def load_config(filename):
"""Load config from the specified file and return the parsed config"""
# Get a path to the file. If it was specified, it should be fine.
# If it was not specified, assume it's config.ini in the script's dir.
config_dir = appdirs.user_config_dir("telegram-export")
if os.path.isfile('config.ini'):
filename = os.path.join(os.getcwd(), 'config.ini')

if not filename:
config_dir = appdirs.user_config_dir("telegram-export")
filename = os.path.join(config_dir, 'config.ini')

if not os.path.isfile(filename):
Expand Down
14 changes: 12 additions & 2 deletions telegram_export/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import logging
import re

from telethon.tl import types
from async_generator import yield_, async_generator
from telethon import utils

from telegram_export.utils import FROM_TYPES
from .downloader import Downloader


Expand Down Expand Up @@ -34,6 +34,10 @@ async def get_entities_iter(mode, in_list, client):
assert client is not None
async for ent in entities_from_str(client.get_input_entity, in_list):
await yield_(ent)
if mode == 'from_type':
assert client is not None
async for ent in entities_from_str(client.get_input_entity, in_list):
await yield_(ent)
elif mode == 'blacklist':
assert client is not None
avoid = set()
Expand Down Expand Up @@ -77,6 +81,12 @@ async def start(self):
self.dumper.config['Blacklist'],
self.client):
await self.downloader.start(entity)
elif 'FromType' in self.dumper.config:
for dialog in await self.client.get_dialogs(limit=None):
# self.logger.info(type(dialog.entity))
from_types = tuple(FROM_TYPES[elem.strip()] for elem in self.dumper.config['FromType'].split(','))
if isinstance(dialog.entity, from_types):
await self.downloader.start(dialog.entity)
else:
# Neither blacklist nor whitelist - get all
for dialog in await self.client.get_dialogs(limit=None):
Expand Down
6 changes: 6 additions & 0 deletions telegram_export/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@
'video/mp4': '.mp4', # To avoid ".m4v"
}

FROM_TYPES = {
'group': types.Chat,
'user': types.User,
'channel': types.Channel
}


def encode_msg_entities(entities):
"""
Expand Down