RetniNet/index.py

275 lines
10 KiB
Python
Raw Normal View History

2022-06-16 20:22:48 +02:00
import psutil
2022-06-18 17:26:52 +02:00
import requests
2022-06-16 20:22:48 +02:00
import random
import pyfiglet
import socket
import threading
import datetime
import telebot
import time
import ast
import ffmpeg
import os
import python_weather
import asyncio
import json
import goslate
import urllib.request
import urllib.parse
import logging
import deepl
import spotipy
from spotipy.oauth2 import SpotifyClientCredentials
from youtube_search import YoutubeSearch
2022-06-18 11:58:15 +02:00
from dotenv import load_dotenv
2022-06-16 20:22:48 +02:00
from yt_dlp import YoutubeDL
from platform import system
from tqdm.auto import tqdm
from importlib.metadata import files
from dataclasses import dataclass
from jmespath import search
from telebot import types, telebot
from pytube import YouTube
from bs4 import BeautifulSoup
from gc import callbacks
import logging
import sys
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s [%(levelname)s] %(message)s",
handlers=[
logging.FileHandler("debug.log"),
logging.StreamHandler(sys.stdout)
]
)
2022-06-16 20:22:48 +02:00
2022-06-18 11:58:15 +02:00
load_dotenv()
API_TOKEN = os.getenv('BOT_TOKEN')
2022-06-16 20:22:48 +02:00
bot = telebot.TeleBot(API_TOKEN)
logging.info("Il bot si è avviato con successo!")
2022-06-16 20:22:48 +02:00
#Command /start
@bot.message_handler(commands=['start'])
def send_welcome(message):
2022-06-18 12:02:15 +02:00
chat_id = message.chat.id
logging.info("Triggered command START.")
2022-06-18 12:02:15 +02:00
bot.send_photo(chat_id, photo='https://i.imgur.com/6YPJBze.png')
2022-06-19 11:15:36 +02:00
messageText = "✋ Benvenuto su <b>RetniNet!</b>\n\n<b>RetniNet</b> è un bot privato per <b>automatizzare</b> e <b>semplificare</b> cose che facciamo quotidianamente. \n\n👨‍💻 Creato & sviluppato da @Stef58_Official"
bot.send_message(chat_id, messageText, parse_mode="HTML")
2022-06-16 20:22:48 +02:00
#Command /music
2022-06-16 20:22:48 +02:00
@bot.message_handler(commands=['music'])
def select_music(pm):
logging.info("Triggered command MUSIC.")
2022-06-16 20:22:48 +02:00
sent_msg = bot.send_message(pm.chat.id, "Inserisci il link della canzone:")
bot.register_next_step_handler(sent_msg, music_step)
2022-06-16 20:22:48 +02:00
def music_step(pm):
2022-06-19 12:33:15 +02:00
if pm.text.startswith('https://www.youtube.com/') or pm.text.startswith('https://youtu.be/'):
ytdl_opts = {
'format': 'bestaudio/best',
'outtmpl': 'music_cache/%(title)s.%(ext)s',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
elif pm.text.startswith('https://open.spotify.com/track/'):
ytdl_opts = {
'format': 'bestaudio/best',
'outtmpl': 'music_cache/%(title)s.%(ext)s',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
}
2022-06-19 12:33:15 +02:00
with YoutubeDL(ytdl_opts) as ydl:
message = pm.text
sp = spotipy.Spotify(auth_manager=SpotifyClientCredentials(client_id=os.getenv('SPOTIFY_ID'), client_secret=os.getenv('SPOTIFY_SECRET')))
track = sp.track(message)
name = track["name"]
artist = track["artists"][0]["name"]
results = YoutubeSearch(artist + " - " + name, max_results=1).to_json()
get_link = json.loads(results)
link = get_link['videos'][0]['url_suffix']
print_link = 'https://youtube.com' + link
url = print_link
2022-06-19 12:33:15 +02:00
info = ydl.extract_info(url, download=False)
name = info.get('title')
id = info.get('id')
ydl.download([id])
send_message = "🎶 La canzone <b>" + name + \
"</b> è stata scaricata con successo!"
2022-06-19 12:33:15 +02:00
bot.send_message(pm.chat.id, send_message, parse_mode="HTML")
bot.send_audio(pm.chat.id, audio=open("music_cache/" + name + '.mp3', 'rb'))
os.remove("music_cache/" + name + '.mp3')
logging.info("\"" + name + "\" è stato scaricato ed eliminato con successo!")
elif pm.text.startswith('https://youtube.com/') or pm.text.startswith('youtube.com/'):
bot.send_message(
pm.chat.id, "🚫 <b>Errore</b>\n\n<b>Il link</b> inserito non è valido!")
2022-06-19 12:33:15 +02:00
else:
try:
with YoutubeDL(ytdl_opts) as ydl:
url = pm.text
info = ydl.extract_info(url, download=False)
name = info.get('title')
id = info.get('id')
ydl.download([id])
send_message = "🎶 La canzone <b>" + name + \
"</b> è stata scaricata con successo!"
bot.send_message(pm.chat.id, send_message, parse_mode="HTML")
bot.send_audio(pm.chat.id, audio=open("music_cache/" + name + '.mp3', 'rb'))
os.remove("music_cache/" + name + '.mp3')
except:
chat = pm.chat.id
send_msg = "🚫 Errore!\n\n<b>Errore:</b> <i>Impossibile scaricare la canzone</i>"
bot.send_message(chat, send_msg, parse_mode="HTML")
else:
chat = pm.chat.id
bot.send_message(chat, pm.chat.id,"🚫 Devi inserire un <b>link</b> valido!")
2022-06-16 20:22:48 +02:00
#Command /meteo
@bot.message_handler(commands=['meteo'])
def meteo(pm):
logging.info("Triggered command METEO.")
sent_msg = bot.send_message(pm.chat.id, "🏙️ Inserisci la città:")
2022-06-16 20:22:48 +02:00
bot.register_next_step_handler(sent_msg, meteo_step)
2022-06-16 20:22:48 +02:00
def meteo_step(message):
translator = deepl.Translator(os.getenv('DEEPL_TOKEN'))
result = translator.translate_text('hi', target_lang='IT')
translated_text = result.text
2022-06-16 20:22:48 +02:00
city = message.text
2022-06-18 11:58:15 +02:00
token_weather = os.environ.get('WEATHER_TOKEN')
response = requests.get("https://api.openweathermap.org/data/2.5/weather?q="+city+",it&APPID="+token_weather).json()
weather = response['weather'][0]['main']
2022-06-16 20:22:48 +02:00
temp = response['main']['temp']
temp = temp - 273.15
weather_translate = translator.translate_text(weather, target_lang='IT')
result_weather = weather_translate.text
2022-06-16 20:22:48 +02:00
bot.send_message(message.chat.id, "🌡️ La temperatura in " + city + " è di " + str(temp) + "°C")
bot.send_message(message.chat.id, "🌧️ La condizione è " + result_weather)
2022-06-16 20:22:48 +02:00
#Command /stats
2022-06-16 20:22:48 +02:00
@bot.message_handler(commands=['stats'])
def uptime(message):
print("Triggered command STATS.")
cpuUsage = psutil.cpu_percent(interval=1)
ramTotal = int(psutil.virtual_memory().total/(1024*1024)) # GB
ramUsage = int(psutil.virtual_memory().used/(1024*1024)) # GB
ramFree = int(psutil.virtual_memory().free/(1024*1024)) # GB
2022-06-16 20:22:48 +02:00
ramUsagePercent = psutil.virtual_memory().percent
msg = '''
CPU & RAM Info
🟩 Utilizzo CPU = {} %
2022-06-16 20:22:48 +02:00
RAM
Totale = {} MB
Usato = {} MB
Libero = {} MB
In uso = {} %\n'''.format(cpuUsage, ramTotal, ramUsage, ramFree, ramUsagePercent)
bot.send_message(message.chat.id, msg)
2022-06-16 20:22:48 +02:00
#Command /pastebin
@bot.message_handler(commands=['pastebin'])
def pastebin(message):
logging.info("Triggered command PASTEBIN.")
sent_msg = bot.send_message(message.chat.id, "📋 Inserisci il testo:")
bot.register_next_step_handler(sent_msg, pastebin_step)
def pastebin_step(message):
chat = message.chat.id
text = message.text
site = 'https://pastebin.com/api/api_post.php'
2022-06-18 11:58:15 +02:00
dev_key = os.environ.get('PASTEBIN_TOKEN')
code = text
our_data = urllib.parse.urlencode(
{"api_dev_key": dev_key, "api_option": "paste", "api_paste_code": code})
our_data = our_data.encode()
resp = urllib.request.urlopen(site, our_data)
resp = resp.read()
send_msg = "📋 Il tuo <b>codice</b> è stato inviato con successo!\n\n<b>Link:</b> " + \
str(resp)
bot.send_message(chat, send_msg, parse_mode="HTML")
2022-06-18 17:26:52 +02:00
#Command /epicgames
2022-06-18 17:26:52 +02:00
@bot.message_handler(commands=['epicgames'])
def epicgames(message):
text = message.text
bot.send_message(
message.chat.id, "🎮 Vuoi vedere il gioco disponibile al momento o quello futuro? (disponibile/futuro)")
2022-06-18 17:26:52 +02:00
bot.register_next_step_handler(message, epicgames_step)
2022-06-18 17:26:52 +02:00
def epicgames_step(message):
text = message.text
if text == 'disponibile' or text == 'Disponibile':
logging.info("Triggered command EPICGAMES DISPONIBILE.")
# URL API
2022-06-18 17:26:52 +02:00
url = "https://api.plenusbot.xyz/epic_games?country=IT"
response = requests.get(url).json()
# Title of current games
2022-06-18 17:26:52 +02:00
current_games = response['currentGames'][0]['title']
# Image current games
2022-06-18 17:26:52 +02:00
image_currentgames = response['currentGames'][0]['keyImages'][0]['url']
# Description current games
2022-06-18 17:26:52 +02:00
current_games_description = response['currentGames'][0]['description']
# Token for translate
translator = deepl.Translator(os.getenv('DEEPL_TOKEN'))
# Translate description current games
description_translate = translator.translate_text(
current_games_description, target_lang='IT')
result_description = description_translate.text
2022-06-18 17:26:52 +02:00
send_img = bot.send_photo(message.chat.id, image_currentgames)
sent_msg = bot.send_message(message.chat.id, "🎮 Il gioco gratis di oggi è " + current_games + "\n\n" + result_description)
2022-06-18 17:26:52 +02:00
else:
logging.info("Triggered command EPICGAMES FUTURO.")
# URL API
2022-06-18 17:26:52 +02:00
url = "https://api.plenusbot.xyz/epic_games?country=IT"
response = requests.get(url).json()
# Title of future games
2022-06-18 17:26:52 +02:00
future_games1 = response['nextGames'][0]['title']
# Image future games
2022-06-18 17:26:52 +02:00
image_futuregames1 = response['nextGames'][0]['keyImages'][0]['url']
# Description future games
2022-06-18 17:26:52 +02:00
future_games_description1 = response['nextGames'][0]['description']
# Token for translate
translator = deepl.Translator(os.getenv('DEEPL_TOKEN'))
description_translate1 = translator.translate_text(future_games_description1, target_lang='IT')
result_description1 = description_translate1.text
2022-06-18 17:26:52 +02:00
send_img = bot.send_photo(message.chat.id, image_futuregames1)
sent_msg = bot.send_message(message.chat.id, "🎮 Il gioco futuro è " + future_games1 + "\n\n" + result_description1)
# Title of future games
2022-06-18 17:26:52 +02:00
future_games2 = response['nextGames'][1]['title']
# Image future games
2022-06-18 17:26:52 +02:00
image_futuregames2 = response['nextGames'][1]['keyImages'][0]['url']
# Description future games
2022-06-18 17:26:52 +02:00
future_games_description2 = response['nextGames'][1]['description']
# Traslate description future games
description_translate2 = translator.translate_text(future_games_description2, target_lang='IT')
result_description2 = description_translate2.text
2022-06-18 17:26:52 +02:00
send_img = bot.send_photo(message.chat.id, image_futuregames2)
sent_msg = bot.send_message(message.chat.id, "🎮 Il gioco futuro è " + future_games2 + "\n\n" + result_description2)
2022-06-19 11:15:36 +02:00
bot.polling()