Skip to content

Watching video from puffer server (local or Stanford server) #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions tasks/video_watchers/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from .twitch_watcher import WatchTwitchStream, WatchTwitchStreamLinuxImplementation
from .vimeo_watcher import WatchVimeoVideo, WatchVimeoVideoLinuxImplementation
from .youtube_watcher import WatchYouTubeVideo, WatchYouTubeVideoLinuxImplementation
from .puffer_watcher import WatchPufferVideo, WatchPufferVideoLinuxImplementation
144 changes: 144 additions & 0 deletions tasks/video_watchers/puffer_watcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
# Use the provided docker files (ARM or AMD based on your system) to build the docker image

import os
import random
import subprocess
import time
from typing import Optional
from netunicorn.client.remote import RemoteClient, RemoteClientException
from netunicorn.base import Experiment, ExperimentStatus, Pipeline
from netunicorn.library.tasks.capture.tcpdump import StartCapture, StopNamedCapture
from netunicorn.library.tasks.upload.fileio import UploadToFileIO
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

def watch(
url: str, username: str, password:str, duration: Optional[int] = 100, chrome_location: Optional[str] = None, webdriver_arguments: Optional[list] = None
) -> Result[str, str]:

display_number = random.randint(100, 500)
xvfb_process = subprocess.Popen(
["Xvfb", f":{display_number}", "-screen", "0", "1920x1080x24"]
)
os.environ["DISPLAY"] = f":{display_number}"
options = Options()
options.add_argument("--no-sandbox")
options.add_argument("--disable-gpu")
options.add_argument("--disable-dev-shm-usage")
options.add_argument("--disable-web-security")
options.add_argument("--allow-running-insecure-content")
options.add_argument("--ignore-certificate-errors")
options.add_argument("--autoplay-policy=no-user-gesture-required")
driver = webdriver.Chrome(service=Service(), options=options)
time.sleep(1)
driver.get(url)
wait = WebDriverWait(driver, 10)

# Find and fill the username
username_field = wait.until(EC.presence_of_element_located((By.NAME, "username")))
username_field.send_keys(username)

# Find and fill the password
password_field = driver.find_element(By.NAME, "password")
password_field.send_keys(password)

# Find and check the checkbox
checkbox_label = driver.find_element(By.CSS_SELECTOR, "label[for='location']")
checkbox_label.click()

# Find and click the login button
login_button = driver.find_element(By.CSS_SELECTOR, "button[type='submit']")
login_button.click()

# Optionally, wait and check if login was successful
time.sleep(5) # Wait for a

if duration:
time.sleep(duration)
result = Success(f"Video finished by timeout: {duration} seconds")
else:
result = Success("Video finished by reaching the end")

driver.close()
xvfb_process.kill()
return result



class WatchPufferVideo(TaskDispatcher):
def __init__(
self,
video_url: str,
username: str,
password: str,
duration: Optional[int] = None,
chrome_location: Optional[str] = None,
webdriver_arguments: Optional[list] = None,
*args, **kwargs
):
super().__init__(*args, **kwargs)
self.video_url = video_url
self.username = username
self.password = password
self.duration = duration
self.chrome_location = chrome_location
self.webdriver_arguments = webdriver_arguments
self.linux_implementation = WatchVimeoVideoLinuxImplementation(
self.video_url,
self.username,
self.password,
self.duration,
self.chrome_location,
self.webdriver_arguments,
name=self.name
)

def dispatch(self, node: Node) -> Task:
if node.architecture in {Architecture.LINUX_AMD64, Architecture.LINUX_ARM64}:
return self.linux_implementation

raise NotImplementedError(
f'WatchPufferVideo is not implemented for architecture: {node.architecture}'
)


class WatchPufferVideoLinuxImplementation(Task):
requirements = [
"apt install -y python3-pip wget xvfb procps chromium chromium-driver",
"pip3 install selenium webdriver-manager",
]

def __init__(
self,
video_url: str,
username: str,
password: str,
duration: Optional[int] = None,
chrome_location: Optional[str] = None,
webdriver_arguments: Optional[list] = None,
*args,
**kwargs
):
self.video_url = video_url
self.username = username
self.password = password
self.duration = duration
self.chrome_location = chrome_location
if not self.chrome_location:
self.chrome_location = "/usr/bin/chromium"
self.webdriver_arguments = webdriver_arguments
super().__init__(*args, **kwargs)

def run(self):
return watch(self.video_url, self.username, self.password ,self.duration, self.chrome_location, self.webdriver_arguments)




if __name__ == "__main__":
# use the user name and password you have for puffer website
print(watch("http:128.111.5.228:8080/player/", "jaber" , "jaber-password",30))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, substitute with test/test or leave it empty
(P.S. I hope it was not the real password :D)