Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/python3
- #-*- coding: utf-8 -*-
- from datetime import datetime
- import time
- import os
- import subprocess
- import argparse # argumenty programu
- # argumenty programu i jego opis
- parser = argparse.ArgumentParser(
- description='Program zarządzający odtwarzaniem muzyki',
- epilog='Autor: Leinnan')
- parser.add_argument('-d','--debug', dest='debug_mode',
- help='Wlacza w trybie debugowania',
- action='store_true')
- # rozmiar terminala
- rows, columns = os.popen('stty size', 'r').read().split()
- debug = parser.parse_args().debug_mode
- current_time = datetime.now();
- jest_aktualnie_przerwa = False
- # ascii text
- trwa_lekcja_tekst = """\033[31m __ ______ __ __ ______ __ ______
- /\ \ /\ ___\ /\ \/ / /\ ___\ /\ \ /\ __ \
- \ \ \____ \ \ __\ \ \ _"-. \ \ \____ _\_\ \ \ \ __ \
- \ \_____\ \ \_____\ \ \_\ \_\ \ \_____\ /\_____\ \ \_\ \_\
- \/_____/ \/_____/ \/_/\/_/ \/_____/ \/_____/ \/_/\/_/
- \033[0m"""
- trwa_przerwa_tekst = """\033[32m
- ______ ______ ______ ______ ______ __ __ ______
- /\ == \ /\ == \ /\___ \ /\ ___\ /\ == \ /\ \ _ \ \ /\ __ \
- \ \ _-/ \ \ __< \/_/ /__ \ \ __\ \ \ __< \ \ \/ ".\ \ \ \ __ \
- \ \_\ \ \_\ \_\ /\_____\ \ \_____\ \ \_\ \_\ \ \__/".~\_\ \ \_\ \_\
- \/_/ \/_/ /_/ \/_____/ \/_____/ \/_/ /_/ \/_/ \/_/ \/_/\/_/
- \033[0m"""
- po_lekcjach_tekst = """\033[32m
- __ __ ______ __ __ __ ______
- /\ \ _ \ \ /\ __ \ /\ \ /\ "-.\ \ /\ ___\
- \ \ \/ ".\ \ \ \ \/\ \ \ \ \____ \ \ \-. \ \ \ __\
- \ \__/".~\_\ \ \_____\ \ \_____\ \ \_\\"\_\ \ \_____\
- \/_/ \/_/ \/_____/ \/_____/ \/_/ \/_/ \/_____/
- \033[0m"""
- # klasa lekcji
- class lesson:
- def __init__(self, start_lekcji, koniec_lekcji, nr_lekcji):
- self.start_lekcji = current_time.replace(
- hour=int(start_lekcji[:2]),
- minute=int(start_lekcji[3:]),
- second=0, microsecond=0)
- self.koniec_lekcji = current_time.replace(
- hour=int(koniec_lekcji[:2]),
- minute=int(koniec_lekcji[3:]),
- second=0, microsecond=0)
- self.nr_lekcji = nr_lekcji
- # rozpiska lekcji
- lekcje = []
- lekcje.append(lesson("08:00","08:45","1"))
- lekcje.append(lesson("08:55","09:40","2"))
- lekcje.append(lesson("09:50","10:35","3"))
- lekcje.append(lesson("10:50","11:35","4"))
- lekcje.append(lesson("11:45","12:30","5"))
- lekcje.append(lesson("12:40","13:25","6"))
- lekcje.append(lesson("13:30","14:15","7"))
- lekcje.append(lesson("14:20","15:05","8"))
- # koniec rozpiski lekcji
- def leciMuzyka():
- isPlayingMusic = True;
- deadbeef_output = str(os.popen("deadbeef --nowplaying-tf \"%ispaused%\"").read())
- if "1" in deadbeef_output:
- isPlayingMusic = False;
- return isPlayingMusic
- def przelaczOdtwarzanie():
- os.system("deadbeef --toggle-pause")
- def jestUruchomionyOdtwarzacz():
- uruchomiony = True;
- if not os.popen("ps cax | grep deadbeef").read():
- uruchomiony = False
- return uruchomiony
- def uruchomOdtwarzacz():
- subprocess.Popen(["deadbeef", ""])
- def jestPrzerwa(lekcje_w_funkcji):
- przerwa = True
- for lekcja in lekcje_w_funkcji:
- if lekcja.koniec_lekcji > current_time > lekcja.start_lekcji:
- przerwa = False
- tekst_z_nr_lekcji = "Lekcja nr "
- tekst_z_nr_lekcji += str(lekcja.nr_lekcji)
- print('{: ^{dlugosc}}'.format(tekst_z_nr_lekcji, dlugosc = columns))
- if current_time.hour > 14 or current_time.hour < 7:
- przerwa = False
- return przerwa
- # 0 = przerwa
- # 1 = lekcja
- # 2 = po lekcjach
- def getStatus(lekcje_w_funkcji):
- status = 0
- for lekcja in lekcje_w_funkcji:
- if lekcja.koniec_lekcji > current_time > lekcja.start_lekcji:
- status = 1
- if current_time.hour > 14 or current_time.hour < 7:
- status = 2
- return status
- def writeStatusToFile(status, jest_aktualnie_przerwa):
- current_time = datetime.now()
- if status == 1:
- wpis_do_pliku = '\nPoczatek wpisow od godziny {}:{}\n'.format(str(current_time.hour).zfill(2),str(current_time.minute).zfill(2))
- elif status == 2:
- wpis_do_pliku = '{}:{} zmiana na {}\n'.format(str(current_time.hour).zfill(2),str(current_time.minute).zfill(2),jest_aktualnie_przerwa)
- with open('rozpiska.txt', 'a') as plik_tekstowy:
- plik_tekstowy.write(wpis_do_pliku)
- # funkcja obliczajaca przesuniecie czasu
- # tak by uruchamiajac w trybie debug zaczynalo
- def debugTimeMove():
- if current_time.hour >=8:
- debug_time = current_time.hour - 8
- else:
- debug_time = 8 - current_time.hour
- return debug_time
- ####################################
- ####################################
- # START WLASCIWY #
- ####################################
- ####################################
- # uruchamia odtwarzacz jesli nie jest wlaczony
- if not jestUruchomionyOdtwarzacz():
- uruchomOdtwarzacz()
- byla_poprzednio_przerwa = not jestPrzerwa(lekcje)
- jest_aktualnie_przerwa = True
- writeStatusToFile(1,0)
- debug_time_movement = debugTimeMove() + current_time.hour
- while True:
- # czysci ekran
- os.system('cls' if os.name == 'nt' else 'clear')
- byla_poprzednio_przerwa = jest_aktualnie_przerwa
- jest_aktualnie_przerwa = jestPrzerwa(lekcje)
- if byla_poprzednio_przerwa is not jest_aktualnie_przerwa:
- writeStatusToFile(2,jest_aktualnie_przerwa)
- tytulowy_tekst = '\033[93m{: <1} dzień tygodnia\t\033[0m{}:{}'.format(
- current_time.weekday() + 1,
- current_time.hour,
- current_time.minute)
- print('{: ^{dlugosc}}'.format(tytulowy_tekst, dlugosc = columns))
- if getStatus(lekcje) == 0:
- print("\n\n")
- print('{: ^{dlugosc}}'.format(trwa_przerwa_tekst, dlugosc = columns))
- elif getStatus(lekcje) == 2:
- print("\n\n")
- print('{: ^{dlugosc}}'.format(po_lekcjach_tekst, dlugosc = columns))
- else:
- print("\n\n")
- print('{: ^{dlugosc}}'.format(trwa_lekcja_tekst, dlugosc = columns))
- # sprawdzamy czy gra muzyka
- if leciMuzyka():
- print('{: ^{dlugosc}}'.format("\033[92mMuzyka jest odtwarzana\033[0m", dlugosc = columns))
- # jesli jest odtwarzana, a nie jest przerwa to zatrzymaj
- if not jest_aktualnie_przerwa or current_time.hour > 14 or current_time.hour < 8:
- przelaczOdtwarzanie()
- print("zatrzymujemy")
- else:
- print('{: ^{dlugosc}}'.format("\033[33mMuzyka jest zatrzymana\033[0m", dlugosc = columns))
- # jesli jest zatrzymana, a jest przerwa to odtwarzaj
- if jest_aktualnie_przerwa and 7 < current_time.hour < 15:
- przelaczOdtwarzanie()
- print("odtwarzamy")
- time.sleep(1)
- current_time = datetime.now();
- if debug:
- current_time = current_time.replace(
- hour=debug_time_movement + current_time.hour,
- minute=current_time.minute,
- second=current_time.second, microsecond=0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement