Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2016
2,279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.64 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. #-*- coding: utf-8 -*-
  4. from datetime import datetime
  5. import time
  6. import os
  7. import subprocess
  8. import argparse # argumenty programu
  9.  
  10. # argumenty programu i jego opis
  11. parser = argparse.ArgumentParser(
  12.                             description='Program zarządzający odtwarzaniem muzyki',
  13.                             epilog='Autor: Leinnan')
  14. parser.add_argument('-d','--debug', dest='debug_mode',
  15.                     help='Wlacza w trybie debugowania',
  16.                     action='store_true')
  17. # rozmiar terminala
  18. rows, columns = os.popen('stty size', 'r').read().split()
  19.  
  20. debug = parser.parse_args().debug_mode
  21. current_time = datetime.now();
  22. jest_aktualnie_przerwa = False    
  23.  
  24.  
  25. # ascii text
  26. trwa_lekcja_tekst = """\033[31m __         ______     __  __     ______       __     ______    
  27. /\ \      /\ ___\  /\ \/ /    /\ ___\    /\ \  /\ __ \  
  28. \ \ \____  \ \ __\  \ \ _"-.  \ \ \____   _\_\ \ \ \ __ \
  29. \ \_____\ \ \_____\ \ \_\ \_\ \ \_____\ /\_____\ \ \_\ \_\
  30.  \/_____/   \/_____/   \/_/\/_/   \/_____/ \/_____/   \/_/\/_/
  31.                                                                \033[0m"""
  32. trwa_przerwa_tekst = """\033[32m
  33. ______   ______     ______     ______     ______     __     __     ______    
  34. /\ == \ /\ == \  /\___  \  /\ ___\  /\ == \  /\ \ _ \ \  /\ __ \  
  35. \ \ _-/ \ \ __<   \/_/  /__  \ \ __\  \ \ __<   \ \ \/ ".\ \ \ \ __ \
  36. \ \_\   \ \_\ \_\  /\_____\ \ \_____\ \ \_\ \_\ \ \__/".~\_\ \ \_\ \_\
  37.  \/_/     \/_/ /_/   \/_____/   \/_____/   \/_/ /_/   \/_/   \/_/   \/_/\/_/
  38.                                                                              
  39. \033[0m"""
  40. po_lekcjach_tekst = """\033[32m
  41. __     __     ______     __         __   __     ______    
  42. /\ \ _ \ \  /\ __ \  /\ \      /\ "-.\ \  /\ ___\  
  43. \ \ \/ ".\ \ \ \ \/\ \ \ \ \____  \ \ \-.  \ \ \ __\  
  44. \ \__/".~\_\ \ \_____\ \ \_____\ \ \_\\"\_\ \ \_____\
  45.  \/_/   \/_/   \/_____/   \/_____/   \/_/ \/_/   \/_____/
  46. \033[0m"""
  47.                                                            
  48.  
  49.  
  50.  
  51. # klasa lekcji
  52. class lesson:
  53.     def __init__(self, start_lekcji, koniec_lekcji, nr_lekcji):
  54.         self.start_lekcji = current_time.replace(
  55.                                     hour=int(start_lekcji[:2]),
  56.                                     minute=int(start_lekcji[3:]),
  57.                                     second=0, microsecond=0)
  58.         self.koniec_lekcji = current_time.replace(
  59.                                     hour=int(koniec_lekcji[:2]),
  60.                                     minute=int(koniec_lekcji[3:]),
  61.                                     second=0, microsecond=0)
  62.         self.nr_lekcji = nr_lekcji
  63.  
  64.  
  65. # rozpiska lekcji
  66. lekcje = []
  67. lekcje.append(lesson("08:00","08:45","1"))
  68. lekcje.append(lesson("08:55","09:40","2"))
  69. lekcje.append(lesson("09:50","10:35","3"))
  70. lekcje.append(lesson("10:50","11:35","4"))
  71. lekcje.append(lesson("11:45","12:30","5"))
  72. lekcje.append(lesson("12:40","13:25","6"))
  73. lekcje.append(lesson("13:30","14:15","7"))
  74. lekcje.append(lesson("14:20","15:05","8"))
  75.  
  76. # koniec rozpiski lekcji
  77. def leciMuzyka():
  78.     isPlayingMusic = True;
  79.     deadbeef_output = str(os.popen("deadbeef --nowplaying-tf \"%ispaused%\"").read())
  80.     if "1" in deadbeef_output:
  81.         isPlayingMusic = False;
  82.     return isPlayingMusic
  83.  
  84. def przelaczOdtwarzanie():
  85.     os.system("deadbeef  --toggle-pause")
  86.    
  87. def jestUruchomionyOdtwarzacz():
  88.     uruchomiony = True;
  89.     if not os.popen("ps cax | grep deadbeef").read():
  90.         uruchomiony = False
  91.     return uruchomiony
  92.    
  93. def uruchomOdtwarzacz():
  94.     subprocess.Popen(["deadbeef", ""])
  95.  
  96. def jestPrzerwa(lekcje_w_funkcji):
  97.     przerwa = True
  98.     for lekcja in lekcje_w_funkcji:
  99.         if lekcja.koniec_lekcji > current_time > lekcja.start_lekcji:
  100.             przerwa = False
  101.             tekst_z_nr_lekcji = "Lekcja nr "
  102.             tekst_z_nr_lekcji += str(lekcja.nr_lekcji)
  103.             print('{: ^{dlugosc}}'.format(tekst_z_nr_lekcji, dlugosc = columns))
  104.     if current_time.hour > 14 or current_time.hour < 7:
  105.         przerwa = False
  106.     return przerwa
  107.  
  108.  
  109. # 0 = przerwa
  110. # 1 = lekcja
  111. # 2 = po lekcjach
  112. def getStatus(lekcje_w_funkcji):
  113.     status = 0
  114.     for lekcja in lekcje_w_funkcji:
  115.         if lekcja.koniec_lekcji > current_time > lekcja.start_lekcji:
  116.             status = 1
  117.     if current_time.hour > 14 or current_time.hour < 7:
  118.         status = 2
  119.     return status
  120.    
  121. def writeStatusToFile(status, jest_aktualnie_przerwa):
  122.     current_time = datetime.now()
  123.     if status == 1:
  124.         wpis_do_pliku = '\nPoczatek wpisow od godziny {}:{}\n'.format(str(current_time.hour).zfill(2),str(current_time.minute).zfill(2))
  125.     elif status == 2:
  126.         wpis_do_pliku = '{}:{} zmiana na {}\n'.format(str(current_time.hour).zfill(2),str(current_time.minute).zfill(2),jest_aktualnie_przerwa)
  127.     with open('rozpiska.txt', 'a') as plik_tekstowy:
  128.         plik_tekstowy.write(wpis_do_pliku)
  129.    
  130. # funkcja obliczajaca przesuniecie czasu
  131. # tak by uruchamiajac w trybie debug zaczynalo
  132. def debugTimeMove():
  133.     if current_time.hour >=8:
  134.         debug_time = current_time.hour - 8
  135.     else:
  136.         debug_time = 8 - current_time.hour
  137.     return debug_time
  138.    
  139. ####################################
  140. ####################################
  141. #          START WLASCIWY          #
  142. ####################################
  143. ####################################    
  144. # uruchamia odtwarzacz jesli nie jest wlaczony
  145. if not jestUruchomionyOdtwarzacz():
  146.     uruchomOdtwarzacz()
  147.  
  148. byla_poprzednio_przerwa = not jestPrzerwa(lekcje)
  149. jest_aktualnie_przerwa = True
  150. writeStatusToFile(1,0)
  151. debug_time_movement = debugTimeMove() + current_time.hour
  152.  
  153. while True:
  154.     # czysci ekran
  155.     os.system('cls' if os.name == 'nt' else 'clear')
  156.     byla_poprzednio_przerwa = jest_aktualnie_przerwa
  157.     jest_aktualnie_przerwa = jestPrzerwa(lekcje)
  158.     if byla_poprzednio_przerwa is not jest_aktualnie_przerwa:
  159.         writeStatusToFile(2,jest_aktualnie_przerwa)
  160.  
  161.    
  162.     tytulowy_tekst = '\033[93m{: <1} dzień tygodnia\t\033[0m{}:{}'.format(
  163.                             current_time.weekday() + 1,
  164.                             current_time.hour,
  165.                             current_time.minute)
  166.     print('{: ^{dlugosc}}'.format(tytulowy_tekst, dlugosc = columns))
  167.     if getStatus(lekcje) == 0:
  168.         print("\n\n")
  169.         print('{: ^{dlugosc}}'.format(trwa_przerwa_tekst, dlugosc = columns))
  170.     elif getStatus(lekcje) == 2:
  171.         print("\n\n")
  172.         print('{: ^{dlugosc}}'.format(po_lekcjach_tekst, dlugosc = columns))
  173.     else:
  174.         print("\n\n")
  175.         print('{: ^{dlugosc}}'.format(trwa_lekcja_tekst, dlugosc = columns))
  176.    
  177.     # sprawdzamy czy gra muzyka
  178.     if leciMuzyka():
  179.         print('{: ^{dlugosc}}'.format("\033[92mMuzyka jest odtwarzana\033[0m", dlugosc = columns))
  180.         # jesli jest odtwarzana, a nie jest przerwa to zatrzymaj
  181.         if not jest_aktualnie_przerwa or current_time.hour > 14 or current_time.hour < 8:
  182.             przelaczOdtwarzanie()
  183.             print("zatrzymujemy")
  184.     else:
  185.         print('{: ^{dlugosc}}'.format("\033[33mMuzyka jest zatrzymana\033[0m", dlugosc = columns))
  186.         # jesli jest zatrzymana, a jest przerwa to odtwarzaj
  187.         if jest_aktualnie_przerwa and 7 < current_time.hour < 15:
  188.             przelaczOdtwarzanie()
  189.             print("odtwarzamy")
  190.     time.sleep(1)
  191.     current_time = datetime.now();
  192.     if debug:
  193.         current_time = current_time.replace(
  194.                                     hour=debug_time_movement + current_time.hour,
  195.                                     minute=current_time.minute,
  196.                                     second=current_time.second, microsecond=0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement