import requests
import time
import smtplib
import smtplib

# Import the email modules we'll need
from email.mime.text import MIMEText

# Open a plain text file for reading.  For this example, assume that
# the text file contains only ASCII characters.
# with open(textfile, 'rb') as fp:
#     # Create a text/plain message
#     msg = MIMEText(fp.read())

# me == the sender's email address
# you == the recipient's email address
# msg['Subject'] = 'The contents of %s' % textfile
# msg['From'] = me
# msg['To'] = you

# # Send the message via our own SMTP server, but don't include the
# # envelope header.
# s = smtplib.SMTP('localhost')
# s.sendmail(me, [you], msg.as_string())
# s.quit()

# def notify_if_stream_down(stream_url, check_interval_secs):
#     while True:
#         response = requests.get(stream_url)
#         print(response.status_code)
#         if response.status_code != 200:
#             msg="the stream goes down !!!!"
#             msg['Subject'] = f'Stream at {stream_url} is down!'
#             msg['From'] = "azizimohamedamine1998@gmail.com"
#             msg['To'] = "azizi1mohamed99amine8@gmail.com"

#             # Send the message via our own SMTP server, but don't include the
#             # envelope header.
#             s = smtplib.SMTP('localhost')
#             s.sendmail("azizimohamedamine1998@gmail.com", ["azizi1mohamed99amine8@gmail.com"], msg.as_string())
#             s.quit()
#             # Stream is down, send notification email
#             # server = smtplib.SMTP('smtp.gmail.com', 587)
#             # server.starttls()
#             # server.login(email_address, email_password)
#             # message = f'Stream at {stream_url} is down!'
#             # server.sendmail(email_address, email_address, message)
#             # server.quit()
#             # return
#         else:
#             # Stream is up, wait for the next check interval
#             time.sleep(check_interval_secs)
#             print('goes down')


# notify_if_stream_down("http://173.212.199.208/El_Hiwar_Ettounsi_HLS/index.m3u8", 10)


#while True:

# response = requests.get("http://173.212.199.208/El_Hiwar_Ettounsi_HLS/index.m3u8")
# print(response.status_code)
# print('done')


import subprocess
import time

# def check_stream(url):
#     while True:
#         try:
#             cmd = f"ffprobe -i {url} -show_streams -select_streams v -loglevel quiet"
#             subprocess.check_output(cmd, shell=True)
#             print('the live is going well')
#         except subprocess.CalledProcessError:
#             print('Stream stopped')
#             # Stream is no longer available
#             #send_notification("Stream stopped")
#             break
#         time.sleep(3) # Check every 30 seconds


#check_stream("http://173.212.199.208/El_Hiwar_Ettounsi_HLS/index.m3u8")

# url="http://173.212.199.208/copy_hiwar_stream/index.m3u8"
# cmd = f"ffprobe -i {url} -show_streams -select_streams v -loglevel quiet"
# subprocess.check_output(cmd, shell=True)

# print(subprocess.check_output(cmd, shell=True))

import psutil
# from plyer import notification

# def check_disk_usage(disk, threshold):
#     """Checks the disk usage and sends a notification if it exceeds the threshold."""
#     usage = psutil.disk_usage(disk)
#     percentage = int((usage.used / usage.total) * 100)

#     if percentage > threshold:
#         message = f"The disk {disk} is {percentage}% full"
#         notification.notify(title="Disk usage warning", message=message, timeout=10)

# # Example usage: check the disk usage of the root directory ("/") and send a notification if it's more than 90% full.
# check_disk_usage("/", 90)
# disk="/dev/sda4"
# usage = psutil.disk_usage(disk)
# percentage = int((usage.used / usage.total) * 100)
# print(percentage)
import telegram
import asyncio
my_token = '5522894419:AAE9CeONlV2lfmUGqbXGZAHC-l1Z90TMuD4'

async def send(msg, chat_id, token=my_token):
    """
    Send a message to a telegram user or group specified on chatId
    chat_id must be a number!
    """
    bot = telegram.Bot(token=token)
    await bot.sendMessage(chat_id=chat_id, text=msg)
while True:
        l=[]
        for partition in psutil.disk_partitions():
                usage = psutil.disk_usage(partition.mountpoint)
                percentage = int((usage.used / usage.total) * 100)
                l.append(percentage)

        if sum(l)==85:
                asyncio.run(send("we should remove some thing from the new server", "5461527672", token=my_token))
                time.sleep(1000)
        if sum(l)==90:
                asyncio.run(send("IT'S 95%,IT IS URGENT we should remove some thing from the new server", "5461527672", token=my_token))
                time.sleep(1000)

        if sum(l)==95:
                asyncio.run(send("IT'S 98%, IT IS VERY URGENT, NOW we should remove some thing from the new server", "5461527672", token=my_token))
                time.sleep(1000)
#print(sum(l))



