131 lines
3.7 KiB
Python
131 lines
3.7 KiB
Python
"""
|
|
This is a privacy oriented bot made to unshort
|
|
annoying links which display ads, open a lot of tabs,
|
|
and even mine data/cryptocurrency.
|
|
|
|
Last update: 01/08/2022(Bash)
|
|
"Miss you, Marina."
|
|
"""
|
|
|
|
from pyrogram import Client, filters
|
|
from groupwatcher import watch
|
|
from datetime import date as dt1
|
|
from datetime import datetime
|
|
|
|
import urlexpander
|
|
import pathlib
|
|
import json
|
|
import config
|
|
|
|
# bot object declaration
|
|
app = Client('urlExpander',
|
|
api_id=config.API_ID,
|
|
api_hash=config.API_HASH,
|
|
bot_token=config.BOT_TOKEN,
|
|
workers=64,
|
|
ipv6=True,)
|
|
|
|
CURR_DIR = pathlib.Path().resolve()
|
|
ASSETS = CURR_DIR / 'assets' # To be used in the future
|
|
|
|
print(f'Bot is running at: {CURR_DIR}')
|
|
|
|
hour = datetime.now()
|
|
today = dt1.today()
|
|
|
|
# indexing groups for greet message
|
|
groups = []
|
|
|
|
# reading groups.txt to send the greeting message if the group is not in
|
|
try:
|
|
with open(f'{CURR_DIR}/groupsin.txt', 'r') as e:
|
|
groups = [groups.append(group.replace('\n', '')) for group in e]
|
|
print(f'The bot is running in {len(groups)} groups.')
|
|
|
|
except FileNotFoundError:
|
|
# creating a plain text file for further use
|
|
with open('groupsin.txt', 'w') as f:
|
|
print('groupsin.txt file created in order to work properly')
|
|
pass
|
|
|
|
|
|
# expander command
|
|
@app.on_message(filters.command('expander') & filters.private)
|
|
def expand(client, message):
|
|
url = str(message.text).replace('/expander', '').strip()
|
|
# items will be used in the loop
|
|
items = []
|
|
|
|
# requesting data to gather links
|
|
api_request = urlexpander.unshort(url)
|
|
hops = api_request['hops']
|
|
|
|
# transforming hops in a list(it is actually a dict)
|
|
hops = list(hops.values())
|
|
|
|
# looping over hops
|
|
for i, hop in enumerate(hops, start=1):
|
|
link = f'{i}. {hop}'
|
|
url_original = f'{min(hops)}'
|
|
items.append(link)
|
|
|
|
res = items[-1]
|
|
res = str(res)
|
|
res = res[3:]
|
|
|
|
# "cleaning" items (as a string)
|
|
items = str(items)
|
|
items = items.replace('[', '')
|
|
items = items.replace(']', '')
|
|
items = items.replace("'", '')
|
|
items = items.replace(', ', '\n')
|
|
|
|
current_date = today.strftime("%d %B, %Y")
|
|
current_time = hour.strftime("%H:%M:%S")
|
|
|
|
data = f'{current_date} - {current_time}'
|
|
|
|
log = {
|
|
'date': data,
|
|
'api_response': api_request,
|
|
}
|
|
|
|
with open('logs.txt', 'a') as f:
|
|
json.dump(log, f)
|
|
f.write('\n')
|
|
f.close()
|
|
|
|
# bot message output
|
|
message.reply(f'URL Original:\n{url}\n\nURL Final:\n{res}\n\nRedirecionamentos:\n{items}', disable_web_page_preview=True)
|
|
|
|
# wiping data
|
|
del items
|
|
del api_request
|
|
del hops
|
|
del url
|
|
del log
|
|
del data
|
|
|
|
|
|
# group greeting
|
|
@app.on_message(filters.group)
|
|
def greet(client, message):
|
|
if str(message.chat.id) not in groups:
|
|
chat = message.chat.id
|
|
groups.append(str(chat))
|
|
with open('groupsin.txt', 'a') as e:
|
|
e.write(str(chat))
|
|
e.write('\n')
|
|
e.close()
|
|
app.send_photo(message.chat.id, f'{CURR_DIR}/assets/img.jpg', caption='Obrigado por adicionar-me ao vosso grupo.\nApenas deixe-me fazer o meu trabalho. Analisarei todas as mensagens a seguir em busca de serviços encurtadores de link registrados no nosso banco de dados.\nResponda uma mensagem contendo um link marcando-me ou utilize o /expander para eu analisar o link sem consultar o nosso banco de dados.\n\nUm oferecimento\n@terabitSec')
|
|
else:
|
|
watch(client, message)
|
|
|
|
# start command
|
|
@app.on_message(filters.command('start'))
|
|
def start(client, message):
|
|
message.reply(f'Olá, {message.from_user.first_name}!\n\nSeja bem-vindo ao urlExpander! Posso desencurtar links aqui também: use o comando /expander seulink/algumacoisa')
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app.run() |