66from threading import Thread
77
88global cast
9+ global mc
910global NameOfCast
1011global TempsDePause
1112global Medias
@@ -34,38 +35,45 @@ def start_cast(stop_connection = False):
3435 except :
3536 pass
3637
37-
3838# Share an image (from a URL) to the Chromecast
39- def show_media (url ):
39+ def show_media (url , x = 0 , duration = 10 ): #duration is in seconds, by default set at 10 (for mp4)
4040 global cast
41+ global mc
4142 mc = cast .media_controller
4243 #si url contient "http" alors c'est une image
4344 if "jpg" in url :
4445 mc .play_media (url , 'image/jpeg' )
46+ mc .block_until_active ()
47+ #if mp4, play video and wait for it to finish (get time from CSV file)
48+ elif "mp4" in url :
49+ mc .play_media (url , 'video/mp4' )
50+ mc .block_until_active ()
51+ if Medias [x ][1 ] != "" :
52+ duration = int (Medias [x ][1 ])
53+ print ("Found duration in csv file (" , duration ,"s)" )
54+ else :
55+ print ("No duration found in csv file, set to default (" , duration ,"s)" )
56+ time .sleep (int (duration ))
57+
4558 else :
4659 mc .play_media (url , 'image/jpeg' )
47- #
48- mc .block_until_active ()
60+ mc .block_until_active ()
4961 return mc .status
50- # Stop the media playing on the Chromecast and disconnect
51- """
52- def stop_cast():
53- global cast
54- cast.quit_app()
55- cast.disconnect()
56- """
62+
5763# Loop through the images in the list (url(s) inside of csv file)
5864def loop (number , Repetition = 1 , EndAfterLoop = False ):
5965 for i in range (Repetition ):
6066 for x in range (number ):
61- show_media (Medias [x ][0 ])
67+ show_media (Medias [x ][0 ], x )
68+
6269 time .sleep (TempsDePause )
6370 if EndAfterLoop == True :
6471 start_cast (stop_connection = True )
6572
66- ####
67- #UI
68- ####
73+ def clear_log (): # Clear the log file
74+ with open ("log.txt" , "w" ) as file :
75+ file .write ("" )
76+ ##### UI ####
6977def UI ():
7078 def Send ():
7179 global NameOfCast
@@ -75,6 +83,14 @@ def Send():
7583 Repetition = int (text3 .get ())
7684 print ("NameOfCast:" ,NameOfCast )
7785 print ("TempsDePause:" ,TempsDePause )
86+
87+ # Save the inputs to a text file
88+ with open ("log.txt" , "w" ) as file :
89+ file .write (f"Name of Chromecast: { NameOfCast } \n " )
90+ file .write (f"Time between photos (s): { TempsDePause } \n " )
91+ file .write (f"Repetition: { Repetition } \n " )
92+ file .write (f"End after loop: { checkbox_var .get ()} \n " )
93+
7894 thread = Thread (target = start_and_loop , args = (Repetition ,))
7995 thread .start ()
8096
@@ -85,26 +101,59 @@ def start_and_loop(Repetition):
85101 else :
86102 loop (len (Medias ), Repetition )
87103
104+ def quit ():
105+ print ("Quit" )
106+ start_cast (stop_connection = True )
107+ time .sleep (1 )
108+ root .destroy ()
109+
88110 # create UI
89111 root = tk .Tk ()
90112 root .title ("Chromecast-URL" )
91113 root .geometry ("400x300" )
92114 root .resizable (False , False )
93115 root .configure (background = '#2B2B2B' )
94116
95- #text
96- text1_label = tk .Label (root , text = "Name of Chromecast:" )
97- text1_label .pack ()
98- text1 = tk .Entry (root )
99- text1 .pack ()
100- text2_label = tk .Label (root , text = "Time between photos (s):" )
101- text2_label .pack ()
102- text2 = tk .Entry (root )
103- text2 .pack ()
104- text3_label = tk .Label (root , text = "Repetition:" )
105- text3_label .pack ()
106- text3 = tk .Entry (root )
107- text3 .pack ()
117+ # Load previous input values from file
118+ try :
119+ with open ("log.txt" , "r" ) as file :
120+ lines = file .readlines ()
121+ # Write the values of the file to variables
122+ name_of_chromecast = lines [0 ].split (":" )[1 ].strip ()
123+ time_between_photos = lines [1 ].split (":" )[1 ].strip ()
124+ repetition = lines [2 ].split (":" )[1 ].strip ()
125+ #end_after_loop = lines[3].split(":")[1].strip()
126+
127+ # Set the values in the input fields
128+ text1_label = tk .Label (root , text = "Name of Chromecast:" )
129+ text1_label .pack ()
130+ text1 = tk .Entry (root )
131+ text1 .insert (0 , name_of_chromecast )
132+ text1 .pack ()
133+ text2_label = tk .Label (root , text = "Time between photos (s):" )
134+ text2_label .pack ()
135+ text2 = tk .Entry (root )
136+ text2 .insert (0 , time_between_photos )
137+ text2 .pack ()
138+ text3_label = tk .Label (root , text = "Repetition:" )
139+ text3_label .pack ()
140+ text3 = tk .Entry (root )
141+ text3 .insert (0 , repetition )
142+ text3 .pack ()
143+
144+ except :
145+ text1_label = tk .Label (root , text = "Name of Chromecast:" )
146+ text1_label .pack ()
147+ text1 = tk .Entry (root )
148+ text1 .pack ()
149+ text2_label = tk .Label (root , text = "Time between photos (s):" )
150+ text2_label .pack ()
151+ text2 = tk .Entry (root )
152+ text2 .pack ()
153+ text3_label = tk .Label (root , text = "Repetition:" )
154+ text3_label .pack ()
155+ text3 = tk .Entry (root )
156+ text3 .pack ()
108157
109158 #create another checkbox
110159 checkbox_var = tk .IntVar (value = 1 )
@@ -115,16 +164,14 @@ def start_and_loop(Repetition):
115164 submit_button = tk .Button (root , text = "Submit" , command = Send )
116165 submit_button .pack ()
117166
118- def quit ():
119- print ("Quit" )
120- start_cast (stop_connection = True )
121- time .sleep (1 )
122- root .destroy ()
123-
124167 # Create the quit button
125168 quit_button = tk .Button (root , text = "Quit and end connection" , command = quit )
126169 quit_button .pack ()
127170
171+ # Create the clear log button
172+ clear_log_button = tk .Button (root , text = "Clear log" , command = clear_log )
173+ clear_log_button .pack ()
174+
128175 # When the window is closed, quit the application
129176 root .protocol ("WM_DELETE_WINDOW" , quit )
130177
0 commit comments