Format code with Black

Makes code PEP8 compliant
This commit is contained in:
Dmitry
2024-03-18 12:07:13 +01:00
committed by GitHub
parent 3636963d1a
commit 2782ce8f82

View File

@@ -1,4 +1,6 @@
import os, json, re
import os
import json
import re
from telethon.sync import TelegramClient, errors, functions
from telethon.tl.types import InputPhoneContact
from dotenv import load_dotenv
@@ -8,50 +10,76 @@ import click
load_dotenv()
def get_names(client: TelegramClient, phone_number: str) -> dict:
"""
Takes in a phone number and returns the associated user information if the user exists. It does so by first adding the user's phones to the contact list, retrieving the information, and then deleting the user from the contact list.
"""Take in a phone number and returns the associated user information if the user exists.
It does so by first adding the user's phones to the contact list, retrieving the
information, and then deleting the user from the contact list.
"""
result = {}
print(f'Checking: {phone_number=} ...', end="", flush=True)
print(f"Checking: {phone_number=} ...", end="", flush=True)
try:
# Create a contact
contact = InputPhoneContact(client_id = 0, phone = phone_number, first_name="", last_name="")
contact = InputPhoneContact(
client_id=0, phone=phone_number, first_name="", last_name=""
)
# Attempt to add the contact from the address book
contacts = client(functions.contacts.ImportContactsRequest([contact]))
users = contacts.to_dict().get('users', [])
users = contacts.to_dict().get("users", [])
number_of_matches = len(users)
if number_of_matches == 0:
result.update({"error": f'No response, the phone number is not on Telegram or has blocked contact adding.'})
result.update(
{
"error": "No response, the phone number is not on Telegram or has blocked contact adding."
}
)
elif number_of_matches == 1:
# Attempt to remove the contact from the address book
# Attempt to remove the contact from the address book.
# The response from DeleteContactsRequest contains more information than from ImportContactsRequest
del_user = client(functions.contacts.DeleteContactsRequest(id=[users[0].get('id')]))
user = del_user.to_dict().get('users')[0]
user_was_online = user.get('status', {}).get('was_online')
del_user = client(
functions.contacts.DeleteContactsRequest(id=[users[0].get("id")])
)
user = del_user.to_dict().get("users")[0]
user_was_online = user.get("status", {}).get("was_online")
# getting more information about the user
result.update({
"id": user.get('id'),
"username": user.get('username'),
"first_name": user.get('first_name'),
"last_name": user.get('last_name'),
"fake" : user.get('fake'),
"verified" : user.get('verified'),
"premium" : user.get('premium'),
"mutual_contact" : user.get('mutual_contact'),
"bot" : user.get('bot'),
"bot_chat_history" : user.get('bot_chat_history'),
"restricted" : user.get('restricted'),
"restriction_reason" : user.get('restriction_reason'),
"user_was_online": user_was_online.strftime("%Y-%m-%d %H:%M:%S %Z") if user_was_online else None
})
result.update(
{
"id": user.get("id"),
"username": user.get("username"),
"first_name": user.get("first_name"),
"last_name": user.get("last_name"),
"fake": user.get("fake"),
"verified": user.get("verified"),
"premium": user.get("premium"),
"mutual_contact": user.get("mutual_contact"),
"bot": user.get("bot"),
"bot_chat_history": user.get("bot_chat_history"),
"restricted": user.get("restricted"),
"restriction_reason": user.get("restriction_reason"),
"user_was_online": (
user_was_online.strftime("%Y-%m-%d %H:%M:%S %Z")
if user_was_online
else None
),
}
)
else:
result.update({"error": f'This phone number matched multiple Telegram accounts, which is unexpected. Please contact the developer: contact-tech@bellingcat.com'})
result.update(
{
"error": """This phone number matched multiple Telegram accounts,
which is unexpected. Please contact the developer: contact-tech@bellingcat.com"""
}
)
except TypeError as e:
result.update({"error": f"TypeError: {e}. --> The error might have occurred due to the inability to delete the {phone_number=} from the contact list."})
result.update(
{
"error": f"TypeError: {e}. --> The error might have occurred due to the inability to delete the {phone_number=} from the contact list."
}
)
except Exception as e:
result.update({"error": f"Unexpected error: {e}."})
raise
@@ -61,10 +89,10 @@ def get_names(client: TelegramClient, phone_number: str) -> dict:
def validate_users(client: TelegramClient, phone_numbers: str) -> dict:
"""
Takes in a string of comma separated phone numbers and tries to get the user information associated with each phone number.
Take in a string of comma separated phone numbers and try to get the user information associated with each phone number.
"""
if not phone_numbers or not len(phone_numbers):
phone_numbers = input('Enter the phone numbers to check, separated by commas: ')
phone_numbers = input("Enter the phone numbers to check, separated by commas: ")
result = {}
phones = [re.sub(r"\s+", "", p, flags=re.UNICODE) for p in phone_numbers.split(",")]
try:
@@ -77,38 +105,82 @@ def validate_users(client: TelegramClient, phone_numbers: str) -> dict:
return result
def login(api_id: str | None, api_hash: str | None, phone_number: str | None) -> TelegramClient:
def login(
api_id: str | None, api_hash: str | None, phone_number: str | None
) -> TelegramClient:
"""Create a telethon session or reuse existing one"""
print('Logging in...', end="", flush=True)
API_ID = api_id or os.getenv('API_ID') or input('Enter your API ID: ')
API_HASH = api_hash or os.getenv('API_HASH') or input('Enter your API HASH: ')
PHONE_NUMBER = phone_number or os.getenv('PHONE_NUMBER') or input('Enter your phone number: ')
print("Logging in...", end="", flush=True)
API_ID = api_id or os.getenv("API_ID") or input("Enter your API ID: ")
API_HASH = api_hash or os.getenv("API_HASH") or input("Enter your API HASH: ")
PHONE_NUMBER = (
phone_number or os.getenv("PHONE_NUMBER") or input("Enter your phone number: ")
)
client = TelegramClient(PHONE_NUMBER, API_ID, API_HASH)
client.connect()
if not client.is_user_authorized():
client.send_code_request(PHONE_NUMBER)
try:
client.sign_in(PHONE_NUMBER, input('Enter the code (sent on telegram): '))
client.sign_in(PHONE_NUMBER, input("Enter the code (sent on telegram): "))
except errors.SessionPasswordNeededError:
pw = getpass('Two-Step Verification enabled. Please enter your account password: ')
pw = getpass(
"Two-Step Verification enabled. Please enter your account password: "
)
client.sign_in(password=pw)
print("Done.")
return client
def show_results(output: str, res: dict) -> None:
print(json.dumps(res, indent=4))
with open(output, 'w') as f:
with open(output, "w") as f:
json.dump(res, f, indent=4)
print(f"Results saved to {output}")
@click.command(epilog='Check out the docs at github.com/bellingcat/telegram-phone-number-checker for more information.')
@click.option('--phone-numbers', '-p', help='List of phone numbers to check, separated by commas', type=str)
@click.option('--api-id', help='Your Telegram app api_id', type=str, prompt="Enter your Telegram App app_id", envvar='API_ID', show_envvar=True)
@click.option('--api-hash', help='Your Telegram app api_hash', type=str, prompt="Enter your Telegram App api_hash", hide_input=True, envvar='API_HASH', show_envvar=True)
@click.option('--api-phone-number', help='Your phone number', type=str, prompt="Enter the number associated with your Telegram account", envvar='PHONE_NUMBER', show_envvar=True)
@click.option('--output', help='Filename to store results', default="results.json", show_default=True, type=str)
def main_entrypoint(phone_numbers: str, api_id: str, api_hash: str, api_phone_number: str, output: str) -> None:
@click.command(
epilog="Check out the docs at github.com/bellingcat/telegram-phone-number-checker for more information."
)
@click.option(
"--phone-numbers",
"-p",
help="List of phone numbers to check, separated by commas",
type=str,
)
@click.option(
"--api-id",
help="Your Telegram app api_id",
type=str,
prompt="Enter your Telegram App app_id",
envvar="API_ID",
show_envvar=True,
)
@click.option(
"--api-hash",
help="Your Telegram app api_hash",
type=str,
prompt="Enter your Telegram App api_hash",
hide_input=True,
envvar="API_HASH",
show_envvar=True,
)
@click.option(
"--api-phone-number",
help="Your phone number",
type=str,
prompt="Enter the number associated with your Telegram account",
envvar="PHONE_NUMBER",
show_envvar=True,
)
@click.option(
"--output",
help="Filename to store results",
default="results.json",
show_default=True,
type=str,
)
def main_entrypoint(
phone_numbers: str, api_id: str, api_hash: str, api_phone_number: str, output: str
) -> None:
"""
Check to see if one or more phone numbers belong to a valid Telegram account.
@@ -130,7 +202,8 @@ def main_entrypoint(phone_numbers: str, api_id: str, api_hash: str, api_phone_nu
API_HASH=1234abcd5678efgh1234abcd567
PHONE_NUMBER=+15555555555
See the official Telegram docs at https://core.telegram.org/api/obtaining_api_id for more information on obtaining an API ID.
See the official Telegram docs at https://core.telegram.org/api/obtaining_api_id
for more information on obtaining an API ID.
\b
Recommendations:
@@ -144,5 +217,5 @@ def main_entrypoint(phone_numbers: str, api_id: str, api_hash: str, api_phone_nu
show_results(output, res)
if __name__ == '__main__':
main_entrypoint()
if __name__ == "__main__":
main_entrypoint()