Skip to content

Commit

Permalink
Full refactoring.
Browse files Browse the repository at this point in the history
- Bug Fixes
- Refactoring the whole code
- Introducing testing
### TODO complete the message group processing
### Tests
### TODO introducing bot analysis
### TODO introducing chat group overlaps
  • Loading branch information
aaarghhh committed Jan 8, 2023
1 parent a177971 commit bb0a108
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 35 deletions.
84 changes: 52 additions & 32 deletions src/telepathy/telepathy.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import configparser
import asyncio

from utils import (
from src.telepathy.utils import (
print_banner,
color_print_green,
populate_user,
Expand Down Expand Up @@ -48,7 +48,7 @@
from telethon.utils import get_display_name, get_message_id
from alive_progress import alive_bar
from colorama import Fore, Style
from const import telepathy_file
from src.telepathy.const import telepathy_file

"""
try:
Expand Down Expand Up @@ -102,8 +102,7 @@ def __init__(
comprehensive,
media,
json,
translate,
alphanumeric,
translate
):
self.client = client
self._target = target
Expand Down Expand Up @@ -179,17 +178,17 @@ async def retrieve_entity(self, _target):
current_entity = None
try:
current_entity = await self.client.get_entity(_target)
except:
except Exception as exx:
pass
if not current_entity:
try:
current_entity = await self.client.get_entity(PeerChannel(_target))
except:
except Exception as exx:
pass
if not current_entity:
try:
current_entity = await self.client.get_entity(PeerChat(_target))
except:
except Exception as exx:
pass
return current_entity

Expand Down Expand Up @@ -295,8 +294,8 @@ async def retrieve_entity_info(self, _handle, _check_user=False):
else:
_result["chat_type"] = "Chat"

if _result["entity.username"]:
_result["group_url"] = "http://t.me/" + self._entity.username
if _result["entity"].username:
_result["group_url"] = "http://t.me/" + _result["entity"].username
_result["group_username"] = _result["entity"].username
web_req = parse_html_page(_result["group_url"])
elif "https://t.me/" in _handle:
Expand Down Expand Up @@ -331,7 +330,7 @@ async def retrieve_entity_info(self, _handle, _check_user=False):
_result["datepost"] = parse_tg_date(message.date)
_result["date"] = _result["date"]
_result["mtime"] = _result["mtime"]
_result["first_post"] = _result["timestamp"]
_result["first_post"] = _result["datepost"]["timestamp"]
break

if _result["entity"].restriction_reason is not None:
Expand Down Expand Up @@ -392,6 +391,7 @@ async def retrieve_chat_group_entity(self, _handle):
web_req = parse_html_page(self._group_url)
self._group_username = "Private group"
else:
web_req = None
self._group_url, self._group_username = "Private group", "Private group"

if web_req:
Expand All @@ -414,9 +414,9 @@ async def retrieve_chat_group_entity(self, _handle):

async for message in self.client.iter_messages(_handle, reverse=True):
self._datepost = parse_tg_date(message.date)
self._date = self.datepost["date"]
self._mtime = self.datepost["mtime"]
self._first_post = self.datepost["timestamp"]
self._date = self._datepost["date"]
self._mtime = self._datepost["mtime"]
self._first_post = self._datepost["timestamp"]
break

if self._entity.restriction_reason is not None:
Expand Down Expand Up @@ -531,6 +531,7 @@ async def analyze_group_channel(self, _target=None):
if not _target:
_target = self._target
_target = clean_private_invite(_target)
self._found_participants = 0
await self.retrieve_chat_group_entity(_target)

if self.basic and not self.comp_check:
Expand Down Expand Up @@ -666,7 +667,12 @@ async def f_export(self):
except AttributeError:
pass

async def retrieve_history(self):
async def retrieve_self_history(self,_target):
if not _target:
_target = self._target
_target = clean_private_invite(_target)
await self.retrieve_chat_group_entity(_target)

get_history = GetHistoryRequest(
peer=self._entity,
offset_id=0,
Expand All @@ -675,8 +681,9 @@ async def retrieve_history(self):
limit=1,
max_id=0,
min_id=0,
hash=0
)
history = self.client(get_history)
history = await self.client(get_history)
if isinstance(history, Messages):
count = len(history.messages)
else:
Expand Down Expand Up @@ -771,6 +778,7 @@ async def process_group_channel_messages(self, _target):

except Exception as e:
if e is ChannelPrivateError:
private_count += 1
print("Private channel")
continue

Expand Down Expand Up @@ -1522,6 +1530,8 @@ class Telepathy_cli:

alt = None
target_type = None
export = False


def __init__(
self,
Expand Down Expand Up @@ -1553,8 +1563,7 @@ def __init__(
self.translate_check = True if translate else False
self.last_date, self.chunk_size, self.user_language = None, 1000, "en"
self.bot = bot is not None
self.alt = alt

self.alt = 0 if alt is None else int(alt)
self.filetime = datetime.datetime.now().strftime("%Y_%m_%d-%H_%M")
self.filetime_clean = str(self.filetime)

Expand All @@ -1576,23 +1585,33 @@ def __init__(
self.t = " "

self.triangulate = True if triangulate_membership else False
self.telepathy_file = self.config_p["telepathy"]["telepathy_files"]
self.json_file = os.path.join(
self.telepathy_file, self.config_p["telepathy"]["json_file"]
)
self.login = os.path.join(
self.telepathy_file, self.config_p["telepathy"]["login"]
)
self.log_file = os.path.join(
self.telepathy_file, self.config_p["telepathy"]["log_file"]
)
self.export_file = os.path.join(
self.telepathy_file, self.config_p["telepathy"]["export_file"]
)

if "telepathy" in self.config_p.keys():
self.telepathy_file = self.config_p["telepathy"]["telepathy_files"]
self.json_file = os.path.join(
self.telepathy_file, self.config_p["telepathy"]["json_file"]
)
self.login = os.path.join(
self.telepathy_file, self.config_p["telepathy"]["login"]
)
self.log_file = os.path.join(
self.telepathy_file, self.config_p["telepathy"]["log_file"]
)
self.export_file = os.path.join(
self.telepathy_file, self.config_p["telepathy"]["export_file"]
)
else:
self.telepathy_file = os.path.join("..","src","telepathy","telepathy_files")
self.json_file = os.path.join(self.telepathy_file,"json_files")
self.login = os.path.join(self.telepathy_file,"login.txt")
self.log_file = os.path.join(self.telepathy_file,"log.csv")
self.export_file = os.path.join(self.telepathy_file,"export.csv")

self.create_path(self.telepathy_file)
self.target = target
self.create_tg_client()


@staticmethod
def create_path(path_d):
if not os.path.exists(path_d):
Expand Down Expand Up @@ -1636,7 +1655,7 @@ def create_tg_client(self):
else:
self.api_id, self.api_hash, self.phone_number = self.retrieve_alt()
"""End of API details"""
self.client = TelegramClient(self.phone_number, self.api_id, self.api_hash)
self.client = TelegramClient(os.path.join(self.telepathy_file,"{}.session".format(self.phone_number)), self.api_id, self.api_hash)

async def connect_tg_client_and_run(self):
await self.client.connect()
Expand Down Expand Up @@ -1960,7 +1979,8 @@ def cli(
translate,
triangulate_membership,
)
loop = asyncio.get_event_loop()
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(telepathy_cli.connect_tg_client_and_run())


Expand Down
4 changes: 2 additions & 2 deletions src/telepathy/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from colorama import Fore, Style
from googletrans import Translator
from const import __version__, user_agent
from src.telepathy.const import __version__, user_agent
import requests
import textwrap
from bs4 import BeautifulSoup
Expand Down Expand Up @@ -321,7 +321,7 @@ def create_file_report(save_dir, name, type, extension, file_time, append_time=T
def clean_private_invite(url):
if "https://t.me/+" in url:
return url.replace("https://t.me/+", "https://t.me/joinchat/")

return url

def evaluate_reactions(message):
total_reactions = 0
Expand Down
58 changes: 57 additions & 1 deletion tests/test_telepathy.py
Original file line number Diff line number Diff line change
@@ -1 +1,57 @@
import pytest
import pytest
from src.telepathy.telepathy import Group_Chat_Analisys, Telepathy_cli
import asyncio


@pytest.fixture
def detail_to_group_basic():
return {
"target": "@test15",
"comprehensive": False,
"media": False,
"forwards": False,
"user": False,
"export": False,
"bot": None,
"location": None,
"alt": None,
"json": False,
"replies": False,
"translate": False,
"triangulate_membership": False,
}

def test_channel_group_basic(detail_to_group_basic):
tele_cli = Telepathy_cli(
detail_to_group_basic["target"],
detail_to_group_basic["comprehensive"],
detail_to_group_basic["media"],
detail_to_group_basic["forwards"],
detail_to_group_basic["user"],
detail_to_group_basic["bot"],
detail_to_group_basic["location"],
detail_to_group_basic["alt"],
detail_to_group_basic["json"],
detail_to_group_basic["export"],
detail_to_group_basic["replies"],
detail_to_group_basic["translate"],
detail_to_group_basic["triangulate_membership"],
)
group_chan = Group_Chat_Analisys(
target=detail_to_group_basic["target"],
client=tele_cli.client,
log_file=tele_cli.log_file,
filetime=tele_cli.filetime,
replies=tele_cli.reply_analysis,
forwards=tele_cli.forwards_check,
comprehensive=tele_cli.comp_check,
media=tele_cli.media_archive,
json=tele_cli.json_check,
translate=tele_cli.translate_check,
)

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(tele_cli.client.connect())
loop.run_until_complete(group_chan.retrieve_self_history(None))
assert group_chan.history_count > 0

0 comments on commit bb0a108

Please sign in to comment.