From 6cc774aa7c562f21a83651317faba411553bbb1c Mon Sep 17 00:00:00 2001 From: Eljakim Herrewijnen Date: Wed, 27 Jul 2022 18:18:18 +0200 Subject: [PATCH] Youtube stream will be checked for live status now --- config.ini | 3 ++- main.py | 31 +------------------------------ notlive.html | 9 +++++++++ streamer.py | 25 ++++++++++++++----------- streaming/base_stream.py | 5 ++++- streaming/kerkomroep.py | 5 ++++- streaming/youtube.py | 34 +++++++++++++++++++++++++++------- tests/kerkdienstgemist.ini | 3 ++- tests/youtube.ini | 3 ++- 9 files changed, 65 insertions(+), 53 deletions(-) create mode 100644 notlive.html diff --git a/config.ini b/config.ini index 9945e81..77e826b 100644 --- a/config.ini +++ b/config.ini @@ -1,5 +1,6 @@ [main] -streaming_method = youtube +streaming_method = web +streaming_service = youtube [kerk] name = immanuel kapel diff --git a/main.py b/main.py index 0c26d6a..1b9cfba 100755 --- a/main.py +++ b/main.py @@ -9,33 +9,4 @@ if __name__ == "__main__": args = parser.parse_args() streamer = Streamer(args.config) - streamer.stream() - exit(0) - - omr = Youtube("UCIh6v7QDfo8FBYAifTMrD3A") - - #Update driver - driver = webdriver.Chrome(service=Service(ChromeDriverManager().install())) - - #Start kerkomroep - options = Options() - options.add_argument("start-maximized") - options.add_experimental_option("detach", True) - driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options) - - #Open livestream url - driver.get(omr.livestream_url()) - - driver.timeouts.implicit_wait = 3 - - # while True: - # try: - # button = driver.find_element(by=By.XPATH, value=omr.get_xpath()) - # if button != None: - # break - # except: - # pass - # time.sleep(1) - - # actionChains = ActionChains(driver) - # actionChains.move_to_element(button).click().perform() \ No newline at end of file + streamer.stream() \ No newline at end of file diff --git a/notlive.html b/notlive.html new file mode 100644 index 0000000..ade6c53 --- /dev/null +++ b/notlive.html @@ -0,0 +1,9 @@ + + + + Immanuelkapel + + +

Waiting to go live

+ + diff --git a/streamer.py b/streamer.py index 0736d74..4570891 100644 --- a/streamer.py +++ b/streamer.py @@ -1,15 +1,11 @@ -import selenium -from streaming.kerkomroep import KerkOmroep +from streaming.kerkomroep import Web_KerkOmroep from configparser import ConfigParser - from selenium import webdriver -from selenium.webdriver import ActionChains from selenium.webdriver.chrome.service import Service from selenium.webdriver.chrome.options import Options from webdriver_manager.chrome import ChromeDriverManager -from selenium.webdriver.common.by import By -from streaming.youtube import Youtube +from streaming.youtube import Web_Youtube class Streamer: def __init__(self, config_path="config.ini") -> None: @@ -19,6 +15,7 @@ class Streamer: self.setup_web_driver() def load_config_values(self): + self.streaming_service = self.config.get('main', 'streaming_service').lower() self.streaming_method = self.config.get('main', 'streaming_method').lower() self.kerk_name = self.config.get('kerk', 'name') @@ -26,11 +23,11 @@ class Streamer: self.kdg_id = self.config.get('kerk', 'kerkdienstgemist_id') #Setup streamer based on streaming_method - if self.streaming_method == "kerkdienstgemist": - self.stream_service = KerkOmroep(self.kdg_id) + if self.streaming_service == "kerkdienstgemist": + self.stream_service = Web_KerkOmroep(self.kdg_id) else: #Default to youtube - self.stream_service = Youtube(self.youtube_id) + self.stream_service = Web_Youtube(self.youtube_id) def setup_web_driver(self): self.options = Options() @@ -42,5 +39,11 @@ class Streamer: self.driver.timeouts.implicit_wait = 3 def stream(self): - self.driver.get(self.stream_service.livestream_url()) - self.stream_service.post_load_actions(self.driver) + if self.streaming_method == "video": + self.stream_service.pre_load_actions(self.driver) + else: + #Default to web + self.stream_service.pre_load_actions(self.driver) + self.driver.get(self.stream_service.livestream_url()) + self.stream_service.post_load_actions(self.driver) + diff --git a/streaming/base_stream.py b/streaming/base_stream.py index 0283ce8..47c6326 100644 --- a/streaming/base_stream.py +++ b/streaming/base_stream.py @@ -4,7 +4,7 @@ from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.ui import WebDriverWait import time -class StreamService: +class Web_StreamService: def __init__(self) -> None: pass @@ -19,6 +19,9 @@ class StreamService: def click_video_play(self): raise NotImplemented() + + def pre_load_actions(self): + raise NotImplemented() def post_load_actions(self, driver): raise NotImplemented() \ No newline at end of file diff --git a/streaming/kerkomroep.py b/streaming/kerkomroep.py index 463e421..d23a8cc 100644 --- a/streaming/kerkomroep.py +++ b/streaming/kerkomroep.py @@ -7,7 +7,7 @@ ICHTHUSKERK_URL = "https://kerkdienstgemist.nl/stations/2110-Ichthuskerk-Ridderk LIVESTREAM_URL = "events/live" XPATH= "/html/body/div[5]/div/article/div/div/div[1]/div[1]/div/div[2]" -class KerkOmroep(StreamService): +class Web_KerkOmroep(Web_StreamService): def __init__(self, kerk_id=IMMANUELKAPEL_URL) -> None: self.kerk_id = kerk_id #URL that points to the church inside the kerkomroep environment pass @@ -32,6 +32,9 @@ class KerkOmroep(StreamService): pass time.sleep(1) button.click() + + def pre_load_actions(self, driver): + pass def post_load_actions(self, driver): self.click_video_play(driver) diff --git a/streaming/youtube.py b/streaming/youtube.py index 27e2407..5f90c06 100644 --- a/streaming/youtube.py +++ b/streaming/youtube.py @@ -1,21 +1,42 @@ +from fcntl import F_SEAL_SEAL +import requests from streaming.base_stream import * +import os, time ICHTHUSKERK_ID = "UCIh6v7QDfo8FBYAifTMrD3A" IMMANUELKAPEL_ID = "UCud4rRtOf2e146HZWDfj6YA" START_FULL_WINDOW = "?start=68" #Note, this is not full screen, but full window -class Youtube(StreamService): +class Web_Youtube(Web_StreamService): def __init__(self, id=IMMANUELKAPEL_ID) -> None: self.id = id def livestream_url(self): return f"https://www.youtube.com/channel/{self.id}/live{START_FULL_WINDOW}" - def is_live(self): - return True + def channel_url(self): + return f"https://www.youtube.com/channel/{self.id}" - def post_load_actions(self): - return + def is_live(self): + #https://github.com/bogeta11040/if-youtube-channel-live + page = requests.get(f"https://www.youtube.com/channel/{self.id}", cookies={'CONSENT': 'YES+42'}) + data = page.content.decode() + if "hqdefault_live.jpg" in data: + return True + return False + + def wait_until_live(self): + while True: + try: + if self.is_live(): + break + except: + print("Failed to get live status of stream!") + time.sleep(10) + + def pre_load_actions(self, driver): + driver.get(f"file:{os.getcwd()}/notlive.html") + self.wait_until_live() def post_load_actions(self, driver): self.bypass_cookies(driver) @@ -30,5 +51,4 @@ class Youtube(StreamService): except: pass time.sleep(1) - accept_button.click() - #driver.find_element_by_xpath('//button[@aria-label="Agree to the use of cookies and other data for the purposes described"]').click() \ No newline at end of file + accept_button.click() \ No newline at end of file diff --git a/tests/kerkdienstgemist.ini b/tests/kerkdienstgemist.ini index 7765f43..e9276e5 100644 --- a/tests/kerkdienstgemist.ini +++ b/tests/kerkdienstgemist.ini @@ -1,5 +1,6 @@ [main] -streaming_method = kerkdienstgemist +streaming_method = web +streaming_service = kerkdienstgemist [kerk] name = immanuel kapel diff --git a/tests/youtube.ini b/tests/youtube.ini index 9945e81..ec84f70 100644 --- a/tests/youtube.ini +++ b/tests/youtube.ini @@ -1,5 +1,6 @@ [main] -streaming_method = youtube +streaming_method = video +streaming_service = youtube [kerk] name = immanuel kapel