コード例 #1
0
    def test_cdn_download():
        client = TelegramClient(None, api_id, api_hash)
        client.session.set_dc(0, '149.154.167.40', 80)
        assert client.connect()

        try:
            phone = '+999662' + str(randint(0, 9999)).zfill(4)
            client.send_code_request(phone)
            client.sign_up('22222', 'Test', 'DC')

            me = client.get_me()
            data = os.urandom(2 ** 17)
            client.send_file(
                me, data,
                progress_callback=lambda c, t:
                    print('test_cdn_download:uploading {:.2%}...'.format(c/t))
            )
            msg = client.get_message_history(me)[1][0]

            out = BytesIO()
            client.download_media(msg, out)
            assert sha256(data).digest() == sha256(out.getvalue()).digest()

            out = BytesIO()
            client.download_media(msg, out)  # Won't redirect
            assert sha256(data).digest() == sha256(out.getvalue()).digest()

            client.log_out()
        finally:
            client.disconnect()
コード例 #2
0
class CommandlineInterface:
    def __init__(self, config='~/.telegram-cloner.json'):
        self._config = ConfigParser(config).parse()

        self._tg_client = TelegramClient(self._config['app']['session'],
                                         self._config['telegram']['api_id'],
                                         self._config['telegram']['api_hash'])
        self._tg_client.start()

    def recent_media(self, username, limit=10):
        for m in self._tg_client.iter_messages(username, limit):
            filename = self._tg_client.download_media(m)
            if filename:
                print('Filename:', filename)
コード例 #3
0
    def execute(self):
        api_id = 263724
        api_hash = "3a8ae22a8e6981038a370c7149d51fc2"

        sn_path = Path.join(Path.dirname(__file__), 'session_name')
        client = TelegramClient(sn_path, api_id, api_hash)
        client.connect()
        username = '******'
        try:
            for message in client.get_messages(username, limit=1):
                if not DBWorker.get_telega_id_date(message.id):

                    file = client.download_media(message, Config().type_paths.get('import'))
                    FSWorker.log('From telega was downloaded: "{}"'.format(file))
                    DBWorker.write_telega_id(message.id)
        except Exception as err:
            FSWorker.log("Fucken bitch dead again. BIIIIITCH")
コード例 #4
0
api_hash 你的api hash
channel_link 要下载图片的频道链接
proxy 将localhost改成代理地址,1080改成代理端口
picture_storage_path 图片下载到的路径
'''

api_id = 80xxxxx
api_hash = "3066c1322d6931a85470xxxxxx"
channel_link = "https://t.me/xx"
proxy =(socks.SOCKS5,"0.0.0.0",1080) #不需要代理的话删掉该行
picture_storage_path = "/"
# ==========================================
client = TelegramClient('my_session',api_id=api_id,api_hash=api_hash).start()
    
photos = client.get_messages(channel_link,None,max_id=100000,min_id=0, filter=InputMessagesFilterPhotos)
    
total = len(photos)
index = 0
for photo in photos:
#    filename = picture_storage_path + "/" +str(photo.text)+"_"+str(photo.id) + ".jpg" #名字_id
    filename = picture_storage_path + "/" +str(photo.id) + ".jpg"
    index = index + 1
    print("downloading:", index, "/", total, " : ", filename)
    if  os.path.exists(filename)==False:
        client.download_media(photo, filename)
        print('done!')
    else:
        print('exist!')
client.disconnect()
print("Done.")
コード例 #5
0
ファイル: telegram.py プロジェクト: vanshwassan/harpoon
    def run(self, conf, args, plugins):
        session_file = os.path.join(os.path.expanduser("~"),
                                    ".config/harpoon/telegram")
        client = TelegramClient(session_file, int(conf['Telegram']['id']),
                                conf['Telegram']['hash'])
        # FIXME : do not connect if it's help
        client.connect()
        if not client.is_user_authorized():
            client.send_code_request(conf['Telegram']['phone'])
            code_ok = False
            while not code_ok:
                code = input("Enter Telegram code:")
                try:
                    code_ok = client.sign_in(conf['Telegram']['phone'], code)
                except SessionPasswordNeededError:
                    # FIXME: getpass is not imported, that would not work
                    password = getpass(
                        'Two step verification enabled. Please enter your password: '******'subcommand'):
            if args.subcommand == 'id':
                try:
                    res = client.get_entity(args.ID)
                    print(
                        json.dumps(res.to_dict(),
                                   sort_keys=True,
                                   indent=4,
                                   default=json_serial))
                except ValueError:
                    print('Identifier not found')
            elif args.subcommand == "messages":
                entity = client.get_entity(args.ID)
                messages = client.get_messages(entity, args.limit)
                users = {}
                if args.dump:
                    if not os.path.exists(args.dump):
                        os.makedirs(args.dump)
                if args.format == "text":
                    if len(messages) == 0:
                        print("No messages in this channel")
                    else:
                        print("%i messages downloaded:" % len(messages))
                        for msg in messages:
                            if isinstance(msg,
                                          telethon.tl.types.MessageService):
                                if isinstance(
                                        msg.action, telethon.tl.types.
                                        MessageActionChatEditPhoto):
                                    print("[%s] Channel Photo Changed" %
                                          msg.date.isoformat())
                                elif isinstance(
                                        msg.action, telethon.tl.types.
                                        MessageActionChannelCreate):
                                    print("[%s] Channel Created" %
                                          msg.date.isoformat())
                                elif isinstance(
                                        msg.action, telethon.tl.types.
                                        MessageActionChatAddUser):
                                    print(
                                        "[%s] Add Users To the Chat - %s" %
                                        (msg.date.isoformat(), ", ".join(
                                            [str(a)
                                             for a in msg.action.users])))
                                elif isinstance(
                                        msg.action, telethon.tl.types.
                                        MessageActionChatDeleteUser):
                                    print(
                                        "[%s] Remove User from the chat - %i" %
                                        (msg.date.isoformat(),
                                         msg.action.user_id))
                                else:
                                    print("[%s] Message Service: %s" %
                                          (msg.date.isoformat(),
                                           msg.action.message))
                            else:
                                if msg.media is None:
                                    if entity.megagroup:
                                        if msg.from_id not in users:
                                            users[msg.
                                                  from_id] = client.get_entity(
                                                      msg.from_id)
                                        print(
                                            "[%s][%i - @%s] %s" %
                                            (msg.date.isoformat(), msg.from_id,
                                             users[msg.from_id].username,
                                             msg.message))
                                    else:
                                        print("[%s] %s (%i views)" %
                                              (msg.date.isoformat(),
                                               msg.message, msg.views))
                                else:
                                    if msg.views:
                                        print(
                                            "[%s] Media (%i views)" %
                                            (msg.date.isoformat(), msg.views))
                                    else:
                                        print("[%s] Media" %
                                              (msg.date.isoformat()))
                                    if args.dump:
                                        if not os.path.exists(
                                                os.path.join(
                                                    args.dump,
                                                    str(msg.id) + '.jpg')):
                                            client.download_media(
                                                msg.media,
                                                os.path.join(
                                                    args.dump, str(msg.id)))
                                            time.sleep(7)
                elif args.format == "json":
                    msg = [m.to_dict() for m in messages]
                    print(
                        json.dumps(msg,
                                   sort_keys=True,
                                   indent=4,
                                   default=json_serial))
                    if args.dump:
                        for msg in messages:
                            if msg.media is None:
                                if not os.path.exists(
                                        os.path.join(args.dump,
                                                     str(msg.id) + '.jpg')):
                                    client.download_media(
                                        msg.media,
                                        os.path.join(args.dump, str(msg.id)))
                                    time.sleep(7)
                elif args.format == "csv":
                    if entity.megagroup:
                        # Chat
                        w = csv.writer(sys.stdout, delimiter=';')
                        w.writerow([
                            "Date", "id", "Username", "userid", "Type",
                            "Message"
                        ])
                        for m in messages:
                            if m.from_id not in users:
                                users[m.from_id] = client.get_entity(m.from_id)
                            if isinstance(m, telethon.tl.types.MessageService):
                                w.writerow([
                                    m.date.isoformat(), m.id,
                                    users[m.from_id].username, m.from_id,
                                    m.__class__.__name__,
                                    m.action.__class__.__name__
                                ])
                            else:
                                w.writerow([
                                    m.date.isoformat(), m.id,
                                    users[m.from_id].username, m.from_id,
                                    m.__class__.__name__, m.message
                                ])
                    else:
                        w = csv.writer(sys.stdout, delimiter=';')
                        w.writerow([
                            "Date", "id", "Type", "Information", "Media",
                            "Views"
                        ])
                        for m in messages:
                            if isinstance(m, telethon.tl.types.MessageService):
                                if isinstance(
                                        m.action, telethon.tl.types.
                                        MessageActionChatEditPhoto):
                                    w.writerow([
                                        m.date.isoformat(), m.id,
                                        m.__class__.__name__,
                                        "Channel Photo Changed", "No", ""
                                    ])
                                elif isinstance(
                                        m.action, telethon.tl.types.
                                        MessageActionChannelCreate):
                                    w.writerow([
                                        m.date.isoformat(), m.id,
                                        m.__class__.__name__,
                                        "Channel Created", "No", ""
                                    ])
                                else:
                                    w.writerow([
                                        m.date.isoformat(), m.id,
                                        m.__class__.__name__,
                                        m.action.__class__.__name__, "No", ""
                                    ])
                            else:
                                if m.media is None:
                                    # message
                                    w.writerow([
                                        m.date.isoformat(), m.id,
                                        m.__class__.__name__, m.message, "No",
                                        m.views
                                    ])
                                else:
                                    w.writerow([
                                        m.date.isoformat(), m.id,
                                        m.__class__.__name__, m.message, "Yes",
                                        m.views
                                    ])
                                    if args.dump:
                                        if not os.path.exists(
                                                os.path.join(
                                                    args.dump,
                                                    str(m.id) + '.jpg')):
                                            client.download_media(
                                                m.media,
                                                os.path.join(
                                                    args.dump, str(m.id)))
                                            # Sleep to avoid being banned
                                            time.sleep(7)
                else:
                    print("Not implemented yet, sorry!")
            elif args.subcommand == "users":
                # List users from a group
                try:
                    entity = client.get_entity(args.ID)
                    offset = 0
                    limit = args.limit
                    all_participants = []

                    while True:
                        participants = client.invoke(
                            GetParticipantsRequest(
                                entity,
                                ChannelParticipantsSearch(''),
                                offset,
                                limit,
                                hash=0))
                        if not participants.users:
                            break
                        all_participants.extend(participants.users)
                        offset += len(participants.users)
                        time.sleep(
                            1
                        )  # This line seems to be optional, no guarantees!
                except ChatAdminRequiredError:
                    print(
                        "You don't have required access rights to get this list"
                    )
                else:
                    if args.format == "text":
                        for p in all_participants:
                            print("[+] User: %s (%s %s)" %
                                  (p.username, p.first_name, p.last_name))
                    elif args.format == "json":
                        users = [u.to_dict() for u in all_participants]
                        print(
                            json.dumps(users,
                                       sort_keys=True,
                                       indent=4,
                                       default=json_serial))
                    else:
                        print("Not implemented yet, sorry!")

            else:
                self.parser.print_help()
        else:
            self.parser.print_help()
コード例 #6
0
ファイル: test_01.py プロジェクト: xuberance137/filter
bot = telegram.Bot(token=API_TOKEN)
print(bot.get_me())

client = TelegramClient('test_session', API_ID, API_HASH)
client.connect()

# If you already have a previous 'session_name.session' file, skip this.
client.sign_in(phone=PHONE)
me = client.sign_in(code=79076)  # Put whatever code you received here.

print(me.stringify())
client.send_message('sensefilter', 'Hello! Talking to you from Telethon')

#client(JoinChannelRequest(channel))
total, messages, senders = client.get_message_history('attractor137')
x = client.download_media(messages[0])
print(x)

from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty
from time import sleep

dialogs = []
users = []
chats = []
messages = []

last_date = None
chunk_size = 200
while True:
    result = client(
コード例 #7
0
class SonarTelegram():

    def __init__(self, loop, api_id, api_hash, collection, session_name, endpoint):
        self.loop = loop or asyncio.get_event_loop()
        self.sonar = SonarClient()
        self.api_id = api_id
        self.api_hash = api_hash
        self.telegram = TelegramClient(session_name,
                                       self.api_id,
                                       self.api_hash,
                                       loop=self.loop)
        self.data = {}

    async def get_jsondialogs(self):
        dialogs_list = []
        dialogs = self.telegram.iter_dialogs()
        async for dialog in dialogs:
            dialog_json = await self.create_dialog_schema(dialog)
            dialogs_list.append(dialog_json)
        return dialogs_list

    async def create_dialog_schema(self, dialog):
        return json.dumps({
            "name": dialog.name,
            "date": dialog.date.isoformat(),
            "last_message": dialog.message.message,
            "entity_id": dialog.id,
        })

    async def import_message(self, entity):
        if not (isinstance(entity.to_id, PeerChat) or isinstance(entity.to_id, PeerChannel)):
            user_id = entity.from_id
            full = await self.telegram(GetFullUserRequest(user_id))
            entity.username = full.user.username
            entity.first_name = full.user.first_name
            print(entity.username, entity.first_name, entity.message)
        entity_json = json.dumps(entity, cls=teleJSONEncoder)
        id = await self.put_message(entity_json)
        return id

    async def import_entity(self, entity_id, collection_name="telegram"):
        ids = []
        await self.ensure_collection(collection_name)
        await self.ensure_types()
        entities = self.telegram.iter_messages(entity_id)
        async for entity in entities:
            if not (isinstance(entity.to_id, PeerChat) or isinstance(entity.to_id, PeerChannel)):
                user_id = entity.from_id
                full = await self.telegram(GetFullUserRequest(user_id))
                entity.username = full.user.username
                entity.first_name = full.user.first_name
                print(entity.username, entity.first_name, entity.message)
            entity_json = json.dumps(entity, cls=teleJSONEncoder)
            id = await self.put_message(entity_json)
            ids.append(id)

    async def get_info(self):
        return await self.sonar.info()

    async def ensure_collection(self, name):
        self.collection = await self.sonar.create_collection(name)

    async def ensure_types(self):
        types = self.collection.schema.list_types()
        if 'telegram.plainMessage' not in types:
            pp.pprint("putting telegram types")
            await self.load_schemata()

    async def load_schemata(self):
        with open('./schemas/telSchema_MessageMediaPhoto.json') as json_file:
            data = json.load(json_file)
            print(data)
            self.collection.schema.add({'telegram.photoMessage': data})
        with open('./schemas/telSchema_MessageMediaVideo.json') as json_file:
            data = json.load(json_file)
            self.collection.schema.add({'telegram.videoMessage': data})
        with open('./schemas/telSchema_MessageMediaAudio.json') as json_file:
            data = json.load(json_file)
            self.collection.schema.add({'telegram.audioMessage': data})
        with open('./schemas/telSchema_MessageMediaPlain.json') as json_file:
            data = json.load(json_file)
            print(data)
            self.collection.schema.add({'telegram.plainMessage':  data})
        with open('./schemas/telSchema_MessageMediaDocument.json') as json_file:
            data = json.load(json_file)
            self.collection.schema.add({'telegram.documentMessage': data})
        return True

    async def put_message(self, message):
        ''' TODO: Save the remaining types
        (for example the Schema for channels without user_id) in
        ../types and adjust the if-queries here
        '''
        # TODO: get file and push it to fs
        msg = json.loads(message)
        if msg['media'] is None:
            schema = "telegram.plainMessage"
        else:
            if 'MessageMediaAudio' in msg['media']:
                media_id = msg.get('media').get('MessageMediaAudio').get('id')
                schema = "telegram.audioMessage"
            elif 'MessageMediaVideo' in msg['media']:
                media_id = msg.get('media').get('MessageMediaVideo').get('id')
                schema = "telegram.videoMessage"
            elif 'MessageMediaPhoto' in msg['media']:
                media_id = msg.get('media').get('MessageMediaPhoto').get('id')
                schema = "telegram.audioPhoto"
            elif 'MessageMediaDocument' in msg['media']:
                media_id = msg.get('id')
                schema = "telegram.documentMessage"
            else:
                print(json.dumps(msg['media'], cls=teleJSONEncoder))
                return None
            file_bytes = self.telegram.download_media(msg, "./file")
            # TODO: how to handle the file bytes coroutine object?
            print(media_id, str(file_bytes))
        id = await self.collection.put({
            "schema": schema,
            "value": msg,
            "id": "telegram." + str(msg["id"])
        })
        print(id)
        return id
コード例 #8
0
import socks
import telethon.sync
from telethon import TelegramClient
from tqdm import tqdm

# Go to my.telegram.org, register desktop app and copy id and hash
api_id = 123456
api_hash = 'qwertyqwerty12345123456'

# If you need proxy
proxy_type = socks.SOCKS5
proxy_host = 'IP'
proxy_port = 1111
proxy = (proxy_type, proxy_host, proxy_port)

# Go to chat settings and generate invite link
# NOTE: your group chat must be a supergroup, otherwise script can't get messages
chat_link = 'https://t.me/joinchat/qwertyqwerty'

# 'download' it's just a session name, you can write anything here
client = TelegramClient('download', api_id, api_hash, proxy=proxy)
client.start()
messages = client.get_messages(chat_link, limit=1000)

print(len(messages))
for message in tqdm(messages):
    client.download_media(message)
コード例 #9
0
                       min_id=lastmessage,
                       hash=0))
 if len(messages.messages) > 0:
     logger.debug('New Messages: ')
     logger.debug(messages)
     for m in messages.messages:
         datetime = m.date.strftime('%Y-%m-%d %H:%M:%S')
         if m.id > lastmessage:
             lastmessage = m.id
         try:
             logger.info("Relaying Message {}".format(m.id))
             media = m.media
             if media is not None:
                 logger.info("Will download image")
                 logger.debug(media)
                 download_res = tclient.download_media(
                     media, './downloads/')
                 logger.info("Download done: {}".format(download_res))
                 files = {'file': (open(download_res, 'rb'))}
                 response = requests.post(url, files=files)
                 logger.debug(response.text)
                 os.remove(download_res)
                 logger.debug("File deleted")
             if not m.message == '':
                 if everyone:
                     msgText = "@everyone {}".format(m.message)
                 else:
                     msgText = "{}".format(m.message)
                 msg = Webhook(url, msg=msgText)
                 msg.post()
         except:
             logger.info('Ignoring empty message {} action: {}'.format(
コード例 #10
0
                    "file_name", "")
                if file_name.endswith("epub"):
                    print(
                        f" - [{msg.id} / {msg.media.document.date}] {file_name} ...",
                        end="",
                    )
                    if (len(
                            connection.execute(
                                f"select * from Messages where msg_id = {msg.id}"
                            ).fetchall()) > 0):
                        print("message already in the DB")
                    elif glob.glob("books/" + file_name):
                        print("file already present")
                    else:

                        client.download_media(message=msg, file="books/")
                        connection.execute(
                            f'insert into Messages values ( {msg.id}, "{file_name}" );'
                        )
                        connection.commit()
                        print("\u2713")
                    # if glob.glob(f"books/{file_name}"):
                    #    print(get_epub_info(f"books/{file_name}"))
            except KeyError:
                print("Unable to download ")
                print(msg.media.document)
            except KeyboardInterrupt:
                print("\n\ngot CTRL+C, stopping ... ")
                connection.close()
                sys.exit(0)
コード例 #11
0
ファイル: Main.py プロジェクト: BlueKenny/telepygram
class Main:
    def __init__(self):
        print("init")

        version = "20"
        if not os.path.exists(data_dir + "/version"):
            print("Writing new database")
            open(data_dir + "/version", "w").write("0")
        checkversion = str(open(data_dir + "/version", "r").readlines()[0])
        if not version == checkversion:
            print(
                "Database version is incompatible with new version, recreating it"
            )
            if os.path.exists(data_dir + "/data.db"):
                os.remove(data_dir + "/data.db")
            open(data_dir + "/version", "w").write(version)

        ldb.connect()

        try:
            ldb.create_tables([Info])
        except:
            print("Info table exists in ldb")
        try:
            ldb.create_tables([Dialogs])
        except:
            print("Dialogs table exists in ldb")
        try:
            ldb.create_tables([Chats])
        except:
            print("Chats table exists in ldb")
        try:
            ldb.create_tables([Uploads])
        except:
            print("Uploads table exists in ldb")

        ldb.close()

        self.isConnected = False
        self.ChatPartner = ""
        self.ChatPartnerID = ""
        self.ChatForceReload = False
        self.LastChatList = ""
        self.LastDialogList = ""

        self.threadTryConnect = threading.Thread(name="Connect",
                                                 target=self.Connect,
                                                 args=())

        self.getDialogs()

        self.tryConnect()

    def tryConnect(self):
        print("tryConnect")
        print(threading.enumerate())
        if not self.threadTryConnect.isAlive():
            print("starting Thread Connect")
            self.threadTryConnect.start()
        else:
            print("Waiting for Connection, Thread Connect is Already running")

    def Connect(self):
        print("Connect")

        api_id = 291651
        api_hash = '0f734eda64f8fa7dea8ed9558fd447e9'

        self.client = TelegramClient(data_dir + "/telepygram.db", api_id,
                                     api_hash)

        self.phoneNumber = ""

        print("Connect to Telegram")
        try:
            isConnected = self.client.connect()
        except:
            isConnected = False

        if isConnected:
            pyotherside.send("onlineStatus", True)

            isAuthorized = self.client.is_user_authorized()
            print("Authorized: " + str(isAuthorized))

            if not isAuthorized:
                pyotherside.send("changeFrame", "Phone")
                print("pyotherside.send(changeFrame, Phone)")

            return True
        else:
            pyotherside.send("onlineStatus", False)
            return False

    def SetChatPartner(self, name, id):
        print("SetChatPartner(name: " + str(name) + ", " + str(id) + ")")
        self.ChatPartner = name
        self.ChatPartnerID = id
        self.ChatForceReload = True
        self.getChat()

    def setPhoneNumber(self, phoneNumber):
        self.phoneNumber = phoneNumber
        print("setPhoneNumber(" + str(phoneNumber) + ")")
        self.client.send_code_request(phoneNumber)
        pyotherside.send("changeFrame", "Code")
        print("pyotherside.send(changeFrame, Code)")

    def setPhoneCode(self, phoneCode):
        print("setPhoneCode(" + str(phoneCode) + ")")
        self.client.sign_in(self.phoneNumber, phoneCode)
        pyotherside.send("changeFrame", "Dialogs")
        print("pyotherside.send(changeFrame, Dialogs)")
        self.getDialogs()

    def getDialogs(self):
        print("getDialogs function in Main.py")

        Dialoge = []
        try:
            ldb.connect()
        except:
            True
        AllDialogs = Dialogs.select()
        ldb.close()

        if not AllDialogs.exists():
            Dialoge.append({
                "name": "LOADING\nPLEASE WAIT",
                "chat_identification": "",
                "status": "",
                "timestamp": "",
                "data_dir": data_dir
            })
        else:
            for dialog in AllDialogs:
                user_status = dialog.status
                #UserStatusOffline(was_online=datetime.utcfromtimestamp(1525351401))
                if "UserStatusOnline" in user_status:  #Online
                    user_status = "green"
                    lastonlinetime = ""
                else:
                    if user_status == "UserStatusRecently()":  # Was online Recently
                        user_status = "orange"
                        lastonlinetime = ""
                    else:
                        print("User Status is " + str(user_status))
                        user_status = "red"
                        lastonlinetime = ""
                        if "UserStatusOffline" in dialog.status:
                            lastonlinetime = modules.BlueFunc.ElapsedTime(
                                dialog.status.split(
                                    "UserStatusOffline(was_online=datetime.utcfromtimestamp("
                                )[-1].replace("))", ""))

                Dialoge.append({
                    "name": dialog.name,
                    "chat_identification": dialog.identification,
                    "status": user_status,
                    "timestamp": lastonlinetime,
                    "data_dir": data_dir
                })

        #print("pyotherside.send(antwortGetDialogs, " + str(Dialoge) + ")")
        pyotherside.send("antwortGetDialogs", Dialoge)

    def reloadDialogs(self):
        print("reloadDialogs function in Main.py")

        try:
            Dialoge = []
            for dialog in self.client.get_dialogs():
                dialog_name = utils.get_display_name(dialog.entity)
                dialog_identification = dialog.entity.id
                try:
                    dialog_status = dialog.entity.status
                except:
                    dialog_status = "0"

                #print(" ")
                #print(dialog_name)
                #print(dialog.entity)
                #print(" ")

                Dialoge.append({"name": dialog_name, "status": dialog_status})
                try:
                    ldb.connect()
                except:
                    True
                query = Dialogs.select().where(
                    (Dialogs.identification == str(dialog_identification)))
                ldb.close()
                if not query.exists():
                    print("create Dialog entry " + dialog_name)
                    threading.Thread(target=self.downloadProfilePhoto,
                                     args=[dialog.entity])
                    NewDialog = Dialogs.create(
                        name=dialog_name,
                        identification=dialog_identification,
                        status=dialog_status,
                        dialog=str(dialog))
                    NewDialog.save()
                else:
                    # ToDO Test if changes

                    #print("Dialog Changed " + dialog_name)
                    ChangedDialog = Dialogs(
                        name=dialog_name,
                        identification=dialog_identification,
                        status=dialog_status,
                        dialog=str(dialog))
                    ChangedDialog.save()

                    # if no Picture
                    if not os.path.exists(data_dir + "/Pictures/Profiles/" +
                                          str(dialog_identification) + ".jpg"):
                        threading.Thread(target=self.downloadProfilePhoto,
                                         args=[dialog.entity]).start()

            #print("Dialoge: " + str(Dialoge))
            if not self.LastDialogList == Dialoge:
                self.LastDialogList = Dialoge
                self.getDialogs()

        except:
            print("getDialogs failed, no connection ?")
            self.tryConnect()

    def deleteProfilePhoto(self, id):
        print("deleteProfilePhoto(" + str(id) + ")")
        if os.path.exists(data_dir + "/Pictures/Profiles/" + str(id) + ".jpg"):
            os.remove(data_dir + "/Pictures/Profiles/" + str(id) + ".jpg")

    def downloadProfilePhoto(self, Entity):
        print("Start downloadProfilePhoto")
        print(" ")
        print(Entity)

        try:
            Image = self.client.download_profile_photo(
                Entity,
                file=data_dir + "/Pictures/Profiles/" + str(Entity.id),
                download_big=True)
            #if str(Image) == "None": self.deleteProfilePhoto(Entity.id)
            print("Image: " + str(Image))
        except:
            print("downloadProfilePhoto failed no connection ?")
            self.tryConnect()

    def sendChat(self, message):
        try:
            ldb.connect()
        except:
            True
        MessageToSend = Uploads.create(text=message,
                                       chat_id=self.ChatPartnerID)
        MessageToSend.save()
        ldb.close()
        self.getChat()

    def trySending(self):
        try:
            try:
                ldb.connect()
            except:
                True
            query = Uploads.select()
            for messages in query:
                print("Sending (" + str(messages.id) + "): " +
                      str(messages.text))
                try:
                    self.client.send_message(int(messages.chat_id),
                                             messages.text)
                    messages.delete().execute()
                except:
                    print("Error Sending")
            ldb.close()
        except:
            print("trySending failed, no connection ?")
            self.tryConnect()

    def getChat(self):
        print("getChat")
        ChatList = []
        try:
            ldb.connect()
        except:
            True
        AllChats = Chats.select().where(
            Chats.chat_id == self.ChatPartnerID).order_by(Chats.identification)

        if not AllChats.exists():
            ChatList.append({
                "chattext": "LOADING\nPLEASE WAIT",
                "out": True,
                "sender": "Telepygram",
                "read": False,
                "media": "",
                "with_media": False
            })
        else:
            for message in AllChats:
                if message.media == "":
                    with_media = False
                    media_is_video = False
                    media_is_image = False
                else:
                    with_media = True
                    medianame = str(message.media).split("/")[-1]
                    mediaformat = medianame.split(".")[-1]

                    know_videos = ["ogv", "3gp"]
                    if mediaformat in know_videos:
                        media_is_video = True
                        media_is_image = False
                    else:
                        media_is_video = False
                        media_is_image = True

                ChatList.append({
                    "media_is_video": media_is_video,
                    "media_is_image": media_is_image,
                    "chattext": message.text,
                    "out": message.out,
                    "sender": message.user_name,
                    "read": False,
                    "media": message.media,
                    "with_media": with_media
                })

        query = Uploads.select().where(
            Uploads.chat_id == self.ChatPartnerID).order_by(Uploads.id)
        ldb.close()

        for MessageToUpload in query:
            ChatList.append({
                "chattext": MessageToUpload.text,
                "out": True,
                "sender": "SENDING",
                "read": True,
                "media": ""
            })

        if not self.LastChatList == ChatList or self.ChatForceReload:
            pyotherside.send("antwortGetChat", ChatList, self.ChatPartner)
            self.LastChatList = ChatList
            self.ChatForceReload = False
        else:
            print("LastChatList = ChatList")

    def reloadChat(self, LoadNewMessages):
        print("reloadChat")
        if True:  #try:
            Dialog = GetFullUserRequest(
                self.client.get_entity(int(self.ChatPartnerID)))
            print("This Dialog " + str(Dialog))

            print("LoadNewMessages: " + str(LoadNewMessages))
            if LoadNewMessages:
                Messages = self.client.iter_messages(self.ChatPartner,
                                                     limit=10)
            else:
                try:
                    ldb.connect()
                except:
                    True

                AllSavedMessages = Chats.select().where(
                    Chats.chat_id == self.ChatPartnerID)
                SavedMessagesList = []
                for msg in AllSavedMessages:
                    SavedMessagesList.append(msg.identification)

                ldb.close()

                LastMessageLoaded = int(min(SavedMessagesList))
                print("LastMessageLoaded: " + str(LastMessageLoaded))
                Messages = self.client.iter_messages(
                    self.ChatPartner, offset_id=LastMessageLoaded, limit=10)

            for message in Messages:
                try:
                    ldb.connect()
                except:
                    True
                print("message.id " + str(message.id))
                query = Chats.select().where(
                    Chats.identification == str(message.id))
                ldb.close()
                if not query.exists():
                    print(message)
                    print(" ")

                    #print("User Name :" + str(self.client.get_entity(message.from_id)))
                    names = []
                    try:
                        user_firstname = str(
                            self.client.get_entity(message.from_id).first_name)
                    except:
                        user_firstname = self.ChatPartner
                    try:
                        user_lastname = str(
                            self.client.get_entity(message.from_id).last_name)
                    except:
                        user_lastname = "None"
                    if not user_firstname == "None":
                        names.append(user_firstname)
                    if not user_lastname == "None": names.append(user_lastname)
                    username = "******".join(names)

                    message_text = message.message
                    if message_text == None:
                        message_text = ""
                        print("action " + str(message.action))
                        if "MessageActionChatJoinedByLink" in str(
                                message.action):
                            message_text = "joined by invite link"
                        if "MessageActionChatAddUser" in str(message.action):
                            message_text = "joined"

                    username_id = str(message.from_id)
                    #print("username_id: " + username_id)

                    try:
                        message_media = str(message.media)
                    except:
                        message_media = "None"
                    if not str(message_media) == "None":
                        print("message_media: " + str(message_media))
                        file = data_dir + "/Media/" + str(message.id)
                        try:
                            file = self.client.download_media(
                                message.media,
                                file=file,
                                progress_callback=None)
                            message_media = file
                        except:
                            print("Error in Downlaod")

                    else:
                        message_media = ""

                    NewChat = Chats.create(identification=message.id,
                                           chat_id=self.ChatPartnerID,
                                           user_name=username,
                                           user_id=username_id,
                                           text=message_text,
                                           out=message.out,
                                           media=str(message_media),
                                           total_message=message)
                    NewChat.save()

                    self.getChat()
コード例 #12
0
ファイル: watch.py プロジェクト: karlmcguire/hawk
all_c = alert_c + images_c
all_c.sort()
looking = 0

fout = open('tgimages.csv', 'w')

for item in all_c:

    print("at", item[0], looking)

    if item[1] == "A":
        looking = 1

    elif item[1] == "I":
        if looking:
            looking = 0
            print("downloaded image post", item[0])
            client.download_media(posts[item[0]].media,
                                  "./tg/images/" + str(item[0]))
            fout.write(str(item[0]) + "," + str(posts[item[0]].date) + "\n")
        else:
            looking = 0
            print("downloaded image post backup", item[0])
            client.download_media(posts[item[0]].media,
                                  "./tg/images/others/" + str(item[0]))
        looking = 0

    else:
        print("Error")
コード例 #13
0
    for dialog in dialogs_content:
        audios = dialogs_content[dialog]["audios"]

        if (len(audios) > 0):
            #Prepare path to export
            audios_path = os.path.join(exports_folder, "audios",
                                       parse_file_name(dialog))
            if not os.path.exists(audios_path):
                os.makedirs(audios_path)

            log("Exporting {} audios from \"{}\"...".format(
                len(audios), dialog))

        for id, audio in enumerate(audios):
            client.download_media(audio, os.path.join(audios_path, str(id)))

    log("=====================================")
    log("")

#Exporting photos
if is_photos:
    log("Exporting photos...")
    log("=====================================")

    for dialog in dialogs_content:
        photos = dialogs_content[dialog]["photos"]
        if (len(photos) > 0):
            #Prepare path to export
            photos_path = os.path.join(exports_folder, "photos",
                                       parse_file_name(dialog))
コード例 #14
0
    try:
        fout = open('out/chat.xml', 'x')
        break
    except FileExistsError:
        get = input("Override out/chat.xml [y/n]?")
        if get == 'y' or get == 'Y':
            #delete
            os.remove('out/chat.xml')
        elif get == 'n' or get == 'N':
            #exit
            print("Bye.")
            exit(2)

fout.write('<chat>\n')
for c in chat:
    dl = saveMessage(c, fout, len(toDL), me.id)
    if dl != None:
        if dl.media != None:
            #here is something to download later
            toDL.append(DelayedDownload(len(toDL), c))
fout.write('</chat>\n')
fout.close()

createFolder('out/media/')
print('Chat structure stored. Downloading Media - this may take a while!')
for dl in toDL:
    print('.', end='')
    client.download_media(dl.message, "out/media/" + str(dl.id))
print('\n', len(toDL), " Media Files Downloaded.")
print('End.')
コード例 #15
0
ファイル: telegramApi.py プロジェクト: mrbesen/TelegramTUI
class TelegramApi:
    client = None
    dialogs = []
    messages = []

    need_update_message = 0
    need_update_online = 0
    need_update_current_user = -1
    need_update_read_messages = 0

    def __init__(self):
        config = configparser.ConfigParser()
        config.read('config.ini')
        api_id = config.get('telegram_api', 'api_id')
        api_hash = config.get('telegram_api', 'api_hash')
        workers = config.get('telegram_api', 'workers')
        session_name = config.get('telegram_api', 'session_name')

        self.timezone = int(config.get('other', 'timezone'))
        self.message_dialog_len = int(config.get('app', 'message_dialog_len'))

        # proxy settings
        if config.get('proxy', 'type') == "HTTP":
            proxy_type = socks.HTTP
        elif config.get('proxy', 'type') == "SOCKS4":
            proxy_type = socks.SOCKS4
        elif config.get('proxy', 'type') == "SOCKS5":
            proxy_type = socks.SOCKS5
        else:
            proxy_type = None
        proxy_addr = config.get('proxy', 'addr')
        proxy_port = int(config.get('proxy', 'port')) if config.get('proxy', 'port').isdigit() else None
        proxy_username = config.get('proxy', 'username')
        proxy_password = config.get('proxy', 'password')

        proxy = (proxy_type, proxy_addr, proxy_port, False, proxy_username, proxy_password)

        # create connection
        self.client = TelegramClient(session_name, api_id, api_hash, update_workers=int(workers),
                                     spawn_read_thread=True, proxy=proxy)
        self.client.start()

        self.me = self.client.get_me()
        self.dialogs = self.client.get_dialogs(limit=self.message_dialog_len)
        self.messages = len(self.dialogs) * [None]
        self.online = len(self.dialogs) * [""]
        self.messages[0] = self.client.get_message_history(self.dialogs[0].entity, limit=self.message_dialog_len)

        # event for new messages
        @self.client.on(events.NewMessage)
        def my_event_handler(event):
            for i in range(len(self.dialogs)):
                # if event message from user
                if hasattr(self.dialogs[i].dialog.peer, 'user_id') and hasattr(event._chat_peer, 'user_id') and \
                        self.dialogs[i].dialog.peer.user_id == event._chat_peer.user_id:
                    self.event_message(i)
                # from chat
                elif hasattr(self.dialogs[i].dialog.peer, 'chat_id') and hasattr(event._chat_peer, 'chat_id') and \
                        self.dialogs[i].dialog.peer.chat_id == event._chat_peer.chat_id:
                    self.event_message(i)
                # from chat
                elif hasattr(self.dialogs[i].dialog.peer, 'channel_id') and hasattr(event._chat_peer, 'channel_id') and \
                        self.dialogs[i].dialog.peer.channel_id == event._chat_peer.channel_id:
                    self.event_message(i)
                # other
                else:
                    pass

        # event for read messages
        @self.client.on(events.Raw())
        def my_event_handler(event):
            if hasattr(event, 'confirm_received') and hasattr(event, 'max_id'):
                for i in range(len(self.dialogs)):
                    # from user
                    if hasattr(self.dialogs[i].dialog.peer, 'user_id') and hasattr(event.peer, 'user_id') and \
                            self.dialogs[i].dialog.peer.user_id == event.peer.user_id:
                        self.dialogs[i].dialog.read_outbox_max_id = event.max_id
                        self.need_update_current_user = i
                    # from chat
                    elif hasattr(self.dialogs[i].dialog.peer, 'chat_id') and hasattr(event.peer, 'chat_id') and \
                            self.dialogs[i].dialog.peer.chat_id == event.peer.chat_id:
                        self.dialogs[i].dialog.read_outbox_max_id = event.max_id
                        self.need_update_current_user = i
                    # other
                    else:
                        pass
                self.need_update_read_messages = 1

        # event for online/offline
        @self.client.on(events.UserUpdate(chats=None, blacklist_chats=False))
        def my_event_handler(event):
            for i in range(len(self.dialogs)):
                if hasattr(self.dialogs[i].dialog.peer, 'user_id') and hasattr(event._chat_peer, 'user_id') and \
                        self.dialogs[i].dialog.peer.user_id == event._chat_peer.user_id:
                    # I think need little bit change this
                    if event.online:
                        self.online[i] = "Online"
                    elif event.last_seen is not None:
                        self.online[i] = "Last seen at " + str(event.last_seen + (timedelta(self.timezone) // 24))
                    else:
                        self.online[i] = ""
                    self.need_update_current_user = i

            self.need_update_online = 1

    def event_message(self, user_id):
        if self.messages[user_id] is None:
            self.get_messages(user_id)
            new_message = self.client.get_message_history(self.dialogs[user_id].entity,
                                                          min_id=self.messages[user_id][0].id - 1)
        else:
            new_message = self.client.get_message_history(self.dialogs[user_id].entity,
                                                          min_id=self.messages[user_id][0].id)

        for j in range(len(new_message) - 1, -1, -1):
            self.messages[user_id].insert(0, new_message[j])
            self.dialogs[user_id].unread_count += 1

        self.messages[user_id].sort(key=lambda x: x.id, reverse=True)
        self.remove_duplicates(self.messages[user_id])

        self.need_update_message = 1
        self.need_update_current_user = user_id

    def get_messages(self, user_id):
        if self.messages[user_id] is None:
            data = self.client.get_message_history(self.dialogs[user_id].entity, limit=self.message_dialog_len)
            # need check exceptions
            self.messages[user_id] = data
            self.messages[user_id].sort(key=lambda x: x.id, reverse=True)
            return data
        else:
            return self.messages[user_id]

    def get_message_by_id(self, user_id, message_id):
        for i in range(len(self.messages[user_id])):
            if self.messages[user_id][i].id == message_id:
                return self.messages[user_id][i]
        # return self.client.get_message_history(self.dialogs[user_id].entity, limit=1, min_id=message_id-1)

    def delete_message(self, user_id, message_id):
        self.client.delete_messages(self.dialogs[user_id].entity, message_id)

    def download_media(self, media, path):
        return self.client.download_media(media, path)

    def message_send(self, message, user_id, reply=None):
        data = self.client.send_message(self.dialogs[user_id].entity, message, reply_to=reply)
        # read message
        self.client.send_read_acknowledge(self.dialogs[user_id].entity, max_id=data.id)

        # save message
        new_message = self.client.get_message_history(self.dialogs[user_id].entity, min_id=(data.id - 1))

        for j in range(len(new_message) - 1, -1, -1):
            self.messages[user_id].insert(0, new_message[j])

        self.messages[user_id].sort(key=lambda x: x.id, reverse=True)
        self.remove_duplicates(self.messages[user_id])

    def file_send(self, file, user_id, func):
        data = self.client.send_file(self.dialogs[user_id].entity, file, progress_callback=func)

        # save message
        new_message = self.client.get_message_history(self.dialogs[user_id].entity, min_id=(data.id - 1))

        for j in range(len(new_message) - 1, -1, -1):
            self.messages[user_id].insert(0, new_message[j])

        self.messages[user_id].sort(key=lambda x: x.id, reverse=True)
        self.remove_duplicates(self.messages[user_id])

    def read_all_messages(self, user_id):
        if hasattr(self.messages[user_id][0], 'id'):
            self.client.send_read_acknowledge(self.dialogs[user_id].entity,
                                              max_id=self.messages[user_id][0].id)

    def remove_duplicates(self, messages):
        i = 0
        while i < len(messages) - 1:
            if messages[i].id == messages[i + 1].id:
                del messages[i]
                i = i - 1

            i = i + 1

        return messages
コード例 #16
0
ファイル: telegramApi.py プロジェクト: Starkad/TelegramTUI
class TelegramApi:
    client = None
    dialogs = []
    messages = []

    need_update_message = 0
    need_update_online = 0
    need_update_current_user = -1
    need_update_read_messages = 0

    def __init__(self):
        config = configparser.ConfigParser()
        config.read('config.ini')
        api_id = config.get('telegram_api', 'api_id')
        api_hash = config.get('telegram_api', 'api_hash')
        workers = config.get('telegram_api', 'workers')
        session_name = config.get('telegram_api', 'session_name')

        self.timezone = int(config.get('other', 'timezone'))
        self.message_dialog_len = int(config.get('app', 'message_dialog_len'))

        # proxy settings
        if config.get('proxy', 'type') == "HTTP":
            proxy_type = socks.HTTP
        elif config.get('proxy', 'type') == "SOCKS4":
            proxy_type = socks.SOCKS4
        elif config.get('proxy', 'type') == "SOCKS5":
            proxy_type = socks.SOCKS5
        else:
            proxy_type = None
        proxy_addr = config.get('proxy', 'addr')
        proxy_port = int(config.get('proxy', 'port')) if config.get('proxy', 'port').isdigit() else None
        proxy_username = config.get('proxy', 'username')
        proxy_password = config.get('proxy', 'password')

        proxy = (proxy_type, proxy_addr, proxy_port, False, proxy_username, proxy_password)

        # create connection
        self.client = TelegramClient(session_name, api_id, api_hash, update_workers=int(workers),
                                     spawn_read_thread=True, proxy=proxy)
        self.client.start()

        self.me = self.client.get_me()
        self.dialogs = self.client.get_dialogs(limit=self.message_dialog_len)
        self.messages = len(self.dialogs) * [None]
        self.online = len(self.dialogs) * [""]
        self.messages[0] = self.client.get_message_history(self.dialogs[0].entity, limit=self.message_dialog_len)

        # event for new messages
        @self.client.on(events.NewMessage)
        def my_event_handler(event):
            for i in range(len(self.dialogs)):
                # if event message from user
                if hasattr(self.dialogs[i].dialog.peer, 'user_id') and hasattr(event._chat_peer, 'user_id') and \
                        self.dialogs[i].dialog.peer.user_id == event._chat_peer.user_id:
                    self.event_message(i)
                # from chat
                elif hasattr(self.dialogs[i].dialog.peer, 'chat_id') and hasattr(event._chat_peer, 'chat_id') and \
                        self.dialogs[i].dialog.peer.chat_id == event._chat_peer.chat_id:
                    self.event_message(i)
                # from chat
                elif hasattr(self.dialogs[i].dialog.peer, 'channel_id') and hasattr(event._chat_peer, 'channel_id') and \
                        self.dialogs[i].dialog.peer.channel_id == event._chat_peer.channel_id:
                    self.event_message(i)
                # other
                else:
                    pass

        # event for read messages
        @self.client.on(events.Raw(types=None))
        def my_event_handler(event):
            if hasattr(event, 'confirm_received') and hasattr(event, 'max_id'):
                for i in range(len(self.dialogs)):
                    # from user
                    if hasattr(self.dialogs[i].dialog.peer, 'user_id') and hasattr(event.peer, 'user_id') and \
                            self.dialogs[i].dialog.peer.user_id == event.peer.user_id:
                        self.dialogs[i].dialog.read_outbox_max_id = event.max_id
                        self.need_update_current_user = i
                    # from chat
                    elif hasattr(self.dialogs[i].dialog.peer, 'chat_id') and hasattr(event.peer, 'chat_id') and \
                            self.dialogs[i].dialog.peer.chat_id == event.peer.chat_id:
                        self.dialogs[i].dialog.read_outbox_max_id = event.max_id
                        self.need_update_current_user = i
                    # other
                    else:
                        pass
                self.need_update_read_messages = 1

        # event for online/offline
        @self.client.on(events.UserUpdate(chats=None, blacklist_chats=False))
        def my_event_handler(event):
            for i in range(len(self.dialogs)):
                if hasattr(self.dialogs[i].dialog.peer, 'user_id') and hasattr(event._chat_peer, 'user_id') and \
                        self.dialogs[i].dialog.peer.user_id == event._chat_peer.user_id:
                    # I think need little bit change this
                    if event.online:
                        self.online[i] = "Online"
                    elif event.last_seen is not None:
                        self.online[i] = "Last seen at " + str(event.last_seen + (timedelta(self.timezone) // 24))
                    else:
                        self.online[i] = ""
                    self.need_update_current_user = i

            self.need_update_online = 1

    def event_message(self, user_id):
        if self.messages[user_id] is None:
            self.get_messages(user_id)
            new_message = self.client.get_message_history(self.dialogs[user_id].entity,
                                                          min_id=self.messages[user_id][0].id - 1)
        else:
            new_message = self.client.get_message_history(self.dialogs[user_id].entity,
                                                          min_id=self.messages[user_id][0].id)

        for j in range(len(new_message) - 1, -1, -1):
            self.messages[user_id].insert(0, new_message[j])
            self.dialogs[user_id].unread_count += 1

        self.messages[user_id].sort(key=lambda x: x.id, reverse=True)
        self.remove_duplicates(self.messages[user_id])

        self.need_update_message = 1
        self.need_update_current_user = user_id

    def get_messages(self, user_id):
        if self.messages[user_id] is None:
            data = self.client.get_message_history(self.dialogs[user_id].entity, limit=self.message_dialog_len)
            # need check exceptions
            self.messages[user_id] = data
            self.messages[user_id].sort(key=lambda x: x.id, reverse=True)
            return data
        else:
            return self.messages[user_id]

    def get_message_by_id(self, user_id, message_id):
        for i in range(len(self.messages[user_id])):
            if self.messages[user_id][i].id == message_id:
                return self.messages[user_id][i]
        # return self.client.get_message_history(self.dialogs[user_id].entity, limit=1, min_id=message_id-1)

    def delete_message(self, user_id, message_id):
        self.client.delete_messages(self.dialogs[user_id].entity, message_id)

    def download_media(self, media, path):
        return self.client.download_media(media, path)

    def message_send(self, message, user_id, reply=None):
        data = self.client.send_message(self.dialogs[user_id].entity, message, reply_to=reply)
        # read message
        self.client.send_read_acknowledge(self.dialogs[user_id].entity, max_id=data.id)

        # save message
        new_message = self.client.get_message_history(self.dialogs[user_id].entity, min_id=(data.id - 1))

        for j in range(len(new_message) - 1, -1, -1):
            self.messages[user_id].insert(0, new_message[j])

        self.messages[user_id].sort(key=lambda x: x.id, reverse=True)
        self.remove_duplicates(self.messages[user_id])

    def file_send(self, file, user_id, func):
        data = self.client.send_file(self.dialogs[user_id].entity, file, progress_callback=func)

        # save message
        new_message = self.client.get_message_history(self.dialogs[user_id].entity, min_id=(data.id - 1))

        for j in range(len(new_message) - 1, -1, -1):
            self.messages[user_id].insert(0, new_message[j])

        self.messages[user_id].sort(key=lambda x: x.id, reverse=True)
        self.remove_duplicates(self.messages[user_id])

    def read_all_messages(self, user_id):
        if hasattr(self.messages[user_id][0], 'id'):
            self.client.send_read_acknowledge(self.dialogs[user_id].entity,
                                              max_id=self.messages[user_id][0].id)

    def remove_duplicates(self, messages):
        i = 0
        while i < len(messages) - 1:
            if messages[i].id == messages[i + 1].id:
                del messages[i]
                i = i - 1

            i = i + 1

        return messages
コード例 #17
0
# сюда перешел, свои вводные получил https://my.telegram.org
api_id = 77777777 # свой получай
api_hash = 'durovpriexalnasadovod'  #тоже свой получай

#укажи прокси, я поднял ssh туннель через putty
client = TelegramClient('session_name', api_id, api_hash, proxy=(socks.SOCKS5, 'localhost', 5555))

phone_number = '+777777777777' #свой телефон вбивай
client.connect()
if not client.is_user_authorized():
    client.send_code_request(phone_number)
    me = client.sign_in(phone_number, input('Enter code: '))

channel_username = sys.argv[1] # чат\канал
# загружаем косарь сообщений. Можно увеличить/уменьшить
msgs = client.get_messages(channel_username, 1000, filter=InputMessagesFilterDocument)
for msg in msgs:
    if msg.media is not None:        
        if os.path.exists('./'+channel_username+'/'+msg.media.document.attributes[0].file_name) and os.path.getsize('./'+channel_username+'/'+msg.media.document.attributes[0].file_name) == msg.media.document.size:
            print('File: '+ msg.media.document.attributes[0].file_name + ' exists and size is ok, skipping...')
            continue
        else:
            try:
                fn = msg.media.document.attributes[0].file_name.replace('\n',' ').replace('\r', '').replace('\\', ' ')
                client.download_media(msg,file='./'+channel_username+'/'+fn)
            except FloodWaitError as e:
                print('Flood waited for', e.seconds)
                quit(1)
        print('Downloaded file: '+ msg.media.document.attributes[0].file_name)
print('Downloading complete')
コード例 #18
0
def download_media(pSection_config):

    if not 'name' in pSection_config:
        logging.error('The Section Name not especified! Aborting section')
        return

    ## Geting download path
    if not 'download_path' in pSection_config:
        lDownload_path = gDownload_path + '/' + pSection_config['name']
    else:
        lDownload_path = pSection_config['download_path']
        if lDownload_path[1] != '/':
            lDownload_path = gRoot_path + lDownload_path

    if not os.path.isdir(lDownload_path):
        os.mkdir(lDownload_path)
        logging.info('Creating Download Dir: ' + lDownload_path)
    else:
        logging.info('Using Download Dir: ' + lDownload_path)

    lDownload_path = lDownload_path + '/' if lDownload_path[-1] != '/' else ''

    lApi_id = pSection_config[
        'api_id'] if 'api_id' in pSection_config else gApi_id
    lApi_hash = pSection_config[
        'api_hash'] if 'api_hash' in pSection_config else gApi_hash
    lPhone_number = pSection_config[
        'phone_number'] if 'phone_number' in pSection_config else gPhone_number

    if not (lApi_hash and lApi_id and lPhone_number):
        logging.error('Authentication data not Set!')
        logging.error('Stopping import for' + pSection_config['name'])
        return

    if not 'channel_username' in pSection_config or not pSection_config[
            'channel_username']:
        logging.warning('Channel User Name not valid for ' +
                        pSection_config['name'])
        return

    lSession_name = lPhone_number[1:]
    lLimit = int(pSection_config['messages_limit']
                 ) if 'messages_limit' in pSection_config else 10
    lExtensions = pSection_config[
        'filter'] if 'filter' in pSection_config else None
    lTimestamp_name = pSection_config[
        'timestamp_name'] if 'timestamp_name' in pSection_config else gTimestamp_name

    # Log in to Telegram and create a client object to use for getting data

    # Create the client and connect
    client = TelegramClient(lSession_name, lApi_id, lApi_hash)
    client.start()
    logging.info("Client Created")

    # Ensure you're authorized
    if not client.is_user_authorized():
        logging.info('Requesting new Code!')
        client.send_code_request(lPhone_number)
        try:
            logging.info('Gettign from user new telegram code')
            client.sign_in(lPhone_number, input('Enter the code: '))
        except SessionPasswordNeededError:
            logging.info('Getting MFA Password')
            client.sign_in(password=input('Password: '******'Starting get Messages one by one')
    #Get last "messages_limit" messages from channel/group
    lItemsCount = 0
    for msg in client.get_messages(pSection_config['channel_username'],
                                   limit=lLimit):
        if msg.media is not None:
            if lExtensions:
                if msg.file.ext in lExtensions:
                    logging.info(msg.sender.title, msg.text)
                    lMediaFile = msg.file.name
                    if lMediaFile == None:
                        lMediaFile = lDownload_path + msg.file.media.date.strftime(
                            '%Y-%m-%d_%I-%M-%S') + msg.file.ext
                    else:
                        if lTimestamp_name:
                            lMediaFile = lDownload_path + msg.file.media.date.strftime(
                                '%Y-%m-%d_%I-%M-%S-') + lMediaFile
                        else:
                            lMediaFile = lDownload_path + lMediaFile

                    logging.info('Download File: ' + lMediaFile)
                    if os.path.isfile(lMediaFile):
                        localSize = os.path.getsize(lMediaFile)
                        remoteSize = msg.file.size
                        if localSize < remoteSize:
                            logging.info('Deleting incompleted Download!')
                            os.remove(lMediaFile)
                        else:
                            logging.info(
                                'Skiping Download: File already exists!')
                            continue
                    client.download_media(message=msg, file=lMediaFile)
                    lItemsCount += 1
                else:
                    logging.info('Ignoring Media "' + msg.text +
                                 '" due Filter Config')

    logging.info('Finish process')
    logging.info('Items Downloaded: ' + str(lItemsCount))