commit ef4be04d6efb5b23f58ee4d58812cb6e97376531 Author: Eljakim Herrewijnen Date: Sun May 29 12:29:31 2022 +0200 Initial commit. Services for Kdg and Youtube implemented diff --git a/config.ini b/config.ini new file mode 100644 index 0000000..9945e81 --- /dev/null +++ b/config.ini @@ -0,0 +1,8 @@ +[main] +streaming_method = youtube + +[kerk] +name = immanuel kapel +youtube_channel_id = UCud4rRtOf2e146HZWDfj6YA +kerkdienstgemist_id = 2112-Immanuelkapel + diff --git a/live.html b/live.html new file mode 100644 index 0000000..c77aab6 --- /dev/null +++ b/live.html @@ -0,0 +1,71 @@ + + + Immanuelkapel + + + + + + + + + + + + + + + + + Kerkdienstgemist.nl + + + + + + + + + + + + + + + + +
+
+ +
+
+
+
+
+
+ + + +
+

De website werkt mogelijk niet goed

+

Uw browser is verouderd. Gebruik een andere browser voor een betere ervaring en beveiliging

+ Kies andere browser + +

Website may not work properly

+

This browser is out of date. Use a different browser for a better experience and security

+ Choose other browser + +
+ + + + +
+ + + + + diff --git a/main.py b/main.py new file mode 100755 index 0000000..0c26d6a --- /dev/null +++ b/main.py @@ -0,0 +1,41 @@ +#! /usr/bin/python3 +from streamer import Streamer +import argparse + +if __name__ == "__main__": + parser = argparse.ArgumentParser() + parser.add_argument("config", help="Path to config.ini file", default="config.ini") + + 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 diff --git a/streamer.py b/streamer.py new file mode 100644 index 0000000..0736d74 --- /dev/null +++ b/streamer.py @@ -0,0 +1,46 @@ +import selenium +from streaming.kerkomroep import 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 + +class Streamer: + def __init__(self, config_path="config.ini") -> None: + self.config = ConfigParser() + self.config.read(config_path) + self.load_config_values() + self.setup_web_driver() + + def load_config_values(self): + self.streaming_method = self.config.get('main', 'streaming_method').lower() + + self.kerk_name = self.config.get('kerk', 'name') + self.youtube_id = self.config.get('kerk', 'youtube_channel_id') + 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) + else: + #Default to youtube + self.stream_service = Youtube(self.youtube_id) + + def setup_web_driver(self): + self.options = Options() + self.options.add_argument("start-maximized") + self.options.add_experimental_option("detach", True) + self.driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=self.options) + + #implicit wait TODO add as config + 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) diff --git a/streaming/base_stream.py b/streaming/base_stream.py new file mode 100644 index 0000000..0283ce8 --- /dev/null +++ b/streaming/base_stream.py @@ -0,0 +1,24 @@ +from selenium import webdriver +from selenium.webdriver.common.by import By +from selenium.webdriver.support import expected_conditions as EC +from selenium.webdriver.support.ui import WebDriverWait +import time + +class StreamService: + def __init__(self) -> None: + pass + + def livestream_url(self): + raise NotImplemented() + + def is_live(self): + raise NotImplemented() + + def get_xpath(self): + raise NotImplemented() + + def click_video_play(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 new file mode 100644 index 0000000..463e421 --- /dev/null +++ b/streaming/kerkomroep.py @@ -0,0 +1,37 @@ +import urllib.request + +from streaming.base_stream import * + +IMMANUELKAPEL_URL = "https://kerkdienstgemist.nl/stations/2112-Immanuelkapel/" +ICHTHUSKERK_URL = "https://kerkdienstgemist.nl/stations/2110-Ichthuskerk-Ridderkerk/" +LIVESTREAM_URL = "events/live" +XPATH= "/html/body/div[5]/div/article/div/div/div[1]/div[1]/div/div[2]" + +class KerkOmroep(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 + + def livestream_url(self): + return f"https://kerkdienstgemist.nl/stations/{self.kerk_id}/{LIVESTREAM_URL}" + + def is_live(self): + # TODO add check to see when the livestream goes up + return True + + def get_xpath(self): + return XPATH + + def click_video_play(self, driver): + while True: + try: + button = driver.find_element(by=By.XPATH, value=self.get_xpath()) + if button != None: + break + except: + pass + time.sleep(1) + button.click() + + def post_load_actions(self, driver): + self.click_video_play(driver) diff --git a/streaming/youtube.py b/streaming/youtube.py new file mode 100644 index 0000000..27e2407 --- /dev/null +++ b/streaming/youtube.py @@ -0,0 +1,34 @@ +from streaming.base_stream import * + +ICHTHUSKERK_ID = "UCIh6v7QDfo8FBYAifTMrD3A" +IMMANUELKAPEL_ID = "UCud4rRtOf2e146HZWDfj6YA" +START_FULL_WINDOW = "?start=68" #Note, this is not full screen, but full window + +class Youtube(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 post_load_actions(self): + return + + def post_load_actions(self, driver): + self.bypass_cookies(driver) + + def bypass_cookies(self, driver): + if 'consent.youtube.com' in driver.current_url: + while True: + try: + accept_button = driver.find_elements(by=By.XPATH, value='//*[contains(text(),"Accept all")]')[1] + if accept_button != None: + break + 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 diff --git a/tests/kerkdienstgemist.ini b/tests/kerkdienstgemist.ini new file mode 100644 index 0000000..7765f43 --- /dev/null +++ b/tests/kerkdienstgemist.ini @@ -0,0 +1,8 @@ +[main] +streaming_method = kerkdienstgemist + +[kerk] +name = immanuel kapel +youtube_channel_id = UCud4rRtOf2e146HZWDfj6YA +kerkdienstgemist_id = 2112-Immanuelkapel + diff --git a/tests/youtube.ini b/tests/youtube.ini new file mode 100644 index 0000000..9945e81 --- /dev/null +++ b/tests/youtube.ini @@ -0,0 +1,8 @@ +[main] +streaming_method = youtube + +[kerk] +name = immanuel kapel +youtube_channel_id = UCud4rRtOf2e146HZWDfj6YA +kerkdienstgemist_id = 2112-Immanuelkapel +