-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
executable file
·147 lines (111 loc) · 4.14 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env python3
import subprocess
import time
import random
import json
import requests
import isodate
from datetime import timedelta
VIDEO_PLAY_TIME = 30*60
REST_TIME = 30*60
MAX_DURATION_BUFFER_VALUE = 60
MORNING = 9
END_OF_CRAZY_TIME = 21
BED_TIME = 22
with open('keys.json') as f:
keys = json.load(f)
YOUTUBE_API_KEY = keys['youtube_data_api']
def load_videos(videos_file):
video_ids = []
with open(videos_file) as fp:
line = fp.readline()
while line:
line = line.strip()
if not line.startswith("#") and len(line) > 0:
video_ids.append(line[32:])
line = fp.readline()
return video_ids
def is_good_time_for_video():
current_time = time.localtime()
return current_time.tm_hour >= MORNING and current_time.tm_hour <= END_OF_CRAZY_TIME
def is_good_time_for_night_time_video():
current_time = time.localtime()
return current_time.tm_hour >= END_OF_CRAZY_TIME and current_time.tm_hour <= BED_TIME
def wake_display():
subprocess.run(["xset", "dpms", "force", "on"])
def sleep_display():
subprocess.run(["xset", "dpms", "force", "off"])
def query_video_duration(video_id):
url = f"https://www.googleapis.com/youtube/v3/videos?id={video_id}&part=contentDetails&key={YOUTUBE_API_KEY}"
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36'}
r = requests.get(url=url, headers=headers, timeout=5)
if r.status_code != 200:
print("Failed to fetch video details")
return None
response_json = r.json()
duration_string = response_json["items"][0]["contentDetails"]["duration"]
return isodate.parse_duration(duration_string)
def compute_start_point_in_seconds(duration):
max_playback_time = timedelta(seconds=VIDEO_PLAY_TIME)
print(f"Video play time max: {max_playback_time}")
possible_start_time_range = duration - max_playback_time
possible_start_time_range -= timedelta(seconds=MAX_DURATION_BUFFER_VALUE)
if possible_start_time_range <= timedelta():
return 0
return random.randrange(possible_start_time_range.seconds)
def launch_video(video_id, video_duration):
if video_duration is None:
start_point = 0
video_play_time = VIDEO_PLAY_TIME
else:
start_point = compute_start_point_in_seconds(video_duration)
video_play_time = min(VIDEO_PLAY_TIME, video_duration.seconds - MAX_DURATION_BUFFER_VALUE)
print(f"Will start {video_id} at {start_point}s and play for {video_play_time}s")
url = f'https://www.youtube.com/embed/{video_id}?autoplay=1&start={start_point}'
firefox_process_args = [
"firefox",
f'--kiosk={url}'
]
chrome_process_args = [
"google-chrome",
#"--use-gl=desktop",
"--enable-features=VaapiVideoDecoder",
"--kiosk",
"--autoplay-policy=no-user-gesture-required",
url,
]
args = firefox_process_args
#args = chrome_process_args
printable_args = " ".join(args)
print(f"Spawning process {printable_args}")
process = subprocess.Popen(args)
print(f"Spawned process {process.pid}")
time.sleep(video_play_time)
process.terminate()
def play_next_video_loop():
video_ids = None
if is_good_time_for_video():
print("Lets find a good cat video...")
video_ids = load_videos("videos.dat")
elif is_good_time_for_night_time_video():
print("Lets find a good night time cat video...")
video_ids = load_videos("night_time.dat")
if video_ids is not None:
video_id = random.choice(video_ids)
video_duration = query_video_duration(video_id)
print(f"Video {video_id} has duration {video_duration}")
wake_display()
launch_video(video_id, video_duration)
else:
print("Too early to watch cat videos")
sleep_display()
time.sleep(REST_TIME)
def main_loop():
while True:
try:
play_next_video_loop()
except BaseException as e:
print(f"Exception! {e}")
time.sleep(REST_TIME)
if __name__ == '__main__':
main_loop()