Commit
Β·
147b594
1
Parent(s):
f6362a3
commmit Dockerfile
Browse files- Dockerfile +50 -36
- app.py +2 -4
- requirements.txt +3 -1
- selimium_try.py +21 -70
Dockerfile
CHANGED
|
@@ -1,50 +1,64 @@
|
|
| 1 |
-
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
COPY ./requirements.txt /code/requirements.txt
|
| 6 |
-
|
| 7 |
-
# Install required system dependencies
|
| 8 |
RUN apt-get update && apt-get install -y \
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
| 13 |
libatk-bridge2.0-0 \
|
|
|
|
|
|
|
|
|
|
| 14 |
libcups2 \
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
libxcomposite1 \
|
|
|
|
| 16 |
libxdamage1 \
|
|
|
|
| 17 |
libxfixes3 \
|
|
|
|
| 18 |
libxrandr2 \
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
|
| 26 |
-
|
| 27 |
-
# Set up a new user named "user" with user ID 1000
|
| 28 |
-
RUN useradd -m -u 1000 user
|
| 29 |
-
|
| 30 |
-
# Set the working directory to the user's home directory
|
| 31 |
-
WORKDIR /home/user/app
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
|
| 36 |
-
#
|
| 37 |
-
|
| 38 |
-
PATH=/home/user/.local/bin:$PATH \
|
| 39 |
-
PLAYWRIGHT_BROWSERS_PATH=/home/user/.cache/ms-playwright
|
| 40 |
|
| 41 |
-
#
|
| 42 |
-
RUN
|
| 43 |
|
| 44 |
-
# Copy the application code
|
| 45 |
-
COPY
|
| 46 |
|
| 47 |
-
# Install
|
| 48 |
-
RUN
|
| 49 |
|
| 50 |
-
|
|
|
|
|
|
| 1 |
+
# Use a Python base image
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
|
| 4 |
+
# Install necessary system dependencies
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
RUN apt-get update && apt-get install -y \
|
| 6 |
+
curl \
|
| 7 |
+
gnupg \
|
| 8 |
+
ca-certificates \
|
| 9 |
+
wget \
|
| 10 |
+
unzip \
|
| 11 |
+
fonts-liberation \
|
| 12 |
+
libasound2 \
|
| 13 |
libatk-bridge2.0-0 \
|
| 14 |
+
libatk1.0-0 \
|
| 15 |
+
libc6 \
|
| 16 |
+
libcairo2 \
|
| 17 |
libcups2 \
|
| 18 |
+
libdbus-1-3 \
|
| 19 |
+
libexpat1 \
|
| 20 |
+
libfontconfig1 \
|
| 21 |
+
libgbm1 \
|
| 22 |
+
libgcc1 \
|
| 23 |
+
libgdk-pixbuf2.0-0 \
|
| 24 |
+
libgl1 \
|
| 25 |
+
libgtk-3-0 \
|
| 26 |
+
libnspr4 \
|
| 27 |
+
libnss3 \
|
| 28 |
+
libpango-1.0-0 \
|
| 29 |
+
libpangocairo-1.0-0 \
|
| 30 |
+
libstdc++6 \
|
| 31 |
+
libx11-6 \
|
| 32 |
+
libx11-xcb1 \
|
| 33 |
+
libxcb1 \
|
| 34 |
libxcomposite1 \
|
| 35 |
+
libxcursor1 \
|
| 36 |
libxdamage1 \
|
| 37 |
+
libxext6 \
|
| 38 |
libxfixes3 \
|
| 39 |
+
libxi6 \
|
| 40 |
libxrandr2 \
|
| 41 |
+
libxrender1 \
|
| 42 |
+
libxss1 \
|
| 43 |
+
libxtst6 \
|
| 44 |
+
lsb-release \
|
| 45 |
+
xdg-utils \
|
| 46 |
+
&& rm -rf /var/lib/apt/lists/*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
+
# Set the working directory
|
| 49 |
+
WORKDIR /app
|
| 50 |
|
| 51 |
+
# Copy the requirements file
|
| 52 |
+
COPY requirements.txt .
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# Install Python dependencies
|
| 55 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 56 |
|
| 57 |
+
# Copy the application code
|
| 58 |
+
COPY . .
|
| 59 |
|
| 60 |
+
# Install playwright browsers
|
| 61 |
+
RUN playwright install --with-deps chromium
|
| 62 |
|
| 63 |
+
# Command to run the script
|
| 64 |
+
CMD ["python", "app.py"]
|
app.py
CHANGED
|
@@ -2,12 +2,10 @@ import gradio as gr
|
|
| 2 |
import os
|
| 3 |
import selimium_try
|
| 4 |
|
| 5 |
-
# Ensure a writable directory for Gradio flagging
|
| 6 |
-
FLAGGING_DIR = "/home/user/app/flagged"
|
| 7 |
-
os.makedirs(FLAGGING_DIR, exist_ok=True)
|
| 8 |
|
| 9 |
def greet(name):
|
| 10 |
-
|
|
|
|
| 11 |
|
| 12 |
def run():
|
| 13 |
demo = gr.Interface(fn=greet, inputs="text", outputs="text", flagging_dir=FLAGGING_DIR)
|
|
|
|
| 2 |
import os
|
| 3 |
import selimium_try
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
def greet(name):
|
| 7 |
+
selimium_try.main()
|
| 8 |
+
return name
|
| 9 |
|
| 10 |
def run():
|
| 11 |
demo = gr.Interface(fn=greet, inputs="text", outputs="text", flagging_dir=FLAGGING_DIR)
|
requirements.txt
CHANGED
|
@@ -1 +1,3 @@
|
|
| 1 |
-
gradio
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
playwright
|
| 3 |
+
requests
|
selimium_try.py
CHANGED
|
@@ -1,82 +1,33 @@
|
|
| 1 |
from playwright.sync_api import sync_playwright
|
| 2 |
-
import
|
| 3 |
-
import time
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
def load_cookies(context):
|
| 9 |
-
"""Loads cookies from a file if available."""
|
| 10 |
-
try:
|
| 11 |
-
with open(COOKIE_FILE, "r") as f:
|
| 12 |
-
cookies = json.load(f)
|
| 13 |
-
context.add_cookies(cookies)
|
| 14 |
-
print("β
Cookies loaded successfully.")
|
| 15 |
-
except FileNotFoundError:
|
| 16 |
-
print("β οΈ No cookies found. Logging in manually.")
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
def save_cookies(context):
|
| 20 |
-
"""Saves cookies to a file."""
|
| 21 |
-
cookies = context.cookies()
|
| 22 |
-
with open(COOKIE_FILE, "w") as f:
|
| 23 |
-
json.dump(cookies, f)
|
| 24 |
-
print("β
Cookies saved successfully.")
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
def is_logged_in_youtube(page):
|
| 28 |
-
"""Checks if the user is logged into YouTube."""
|
| 29 |
-
page.goto("https://www.youtube.com", wait_until="domcontentloaded")
|
| 30 |
-
time.sleep(2) # Wait for elements to load
|
| 31 |
-
try:
|
| 32 |
-
page.wait_for_selector("button#avatar-btn", timeout=5000)
|
| 33 |
-
print("β
You are already logged in to YouTube!")
|
| 34 |
-
return True
|
| 35 |
-
except:
|
| 36 |
-
print("β You are NOT logged in to YouTube.")
|
| 37 |
-
return False
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
def login_to_youtube(page):
|
| 41 |
-
"""Logs into YouTube manually if needed."""
|
| 42 |
-
page.goto("https://accounts.google.com/signin/v2/identifier?service=youtube")
|
| 43 |
-
|
| 44 |
-
# Enter email
|
| 45 |
-
page.fill("input[type='email']", "your-email@gmail.com")
|
| 46 |
-
page.click("button:has-text('Next')")
|
| 47 |
-
time.sleep(3)
|
| 48 |
-
|
| 49 |
-
# Enter password
|
| 50 |
-
page.fill("input[type='password']", "your-password") # β οΈ Use a secure method for passwords
|
| 51 |
-
page.click("button:has-text('Next')")
|
| 52 |
-
time.sleep(5)
|
| 53 |
-
|
| 54 |
-
if is_logged_in_youtube(page):
|
| 55 |
-
print("β
Login successful.")
|
| 56 |
-
return True
|
| 57 |
-
else:
|
| 58 |
-
print("β Login failed.")
|
| 59 |
-
return False
|
| 60 |
-
|
| 61 |
-
def main():
|
| 62 |
with sync_playwright() as p:
|
| 63 |
-
browser = p.chromium.launch(
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
|
|
|
|
| 68 |
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
# if not is_logged_in_youtube(page):
|
| 73 |
-
# if login_to_youtube(page):
|
| 74 |
-
# save_cookies(context)
|
| 75 |
|
|
|
|
|
|
|
| 76 |
|
| 77 |
browser.close()
|
| 78 |
-
return
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
if __name__ == "__main__":
|
| 82 |
main()
|
|
|
|
| 1 |
from playwright.sync_api import sync_playwright
|
| 2 |
+
import sys
|
|
|
|
| 3 |
|
| 4 |
+
def search_youtube_title(search_term):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
with sync_playwright() as p:
|
| 6 |
+
browser = p.chromium.launch()
|
| 7 |
+
page = browser.new_page()
|
| 8 |
+
page.goto("https://www.youtube.com/")
|
| 9 |
|
| 10 |
+
# Type the search term and press Enter
|
| 11 |
+
page.fill("input#search", search_term)
|
| 12 |
+
page.press("input#search", "Enter")
|
| 13 |
|
| 14 |
+
# Wait for the search results to load
|
| 15 |
+
page.wait_for_selector("#contents ytd-video-renderer")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
# Extract the title of the first video
|
| 18 |
+
first_video_title = page.query_selector("#contents ytd-video-renderer #video-title").inner_text() if page.query_selector("#contents ytd-video-renderer #video-title") else "Title not found"
|
| 19 |
|
| 20 |
browser.close()
|
| 21 |
+
return first_video_title
|
| 22 |
+
|
| 23 |
+
def main():
|
| 24 |
+
if len(sys.argv) > 1:
|
| 25 |
+
search_term = sys.argv[1]
|
| 26 |
+
title = search_youtube_title(search_term)
|
| 27 |
|
| 28 |
+
print(f"Title of the first video: {title}")
|
| 29 |
+
else:
|
| 30 |
+
print("Please provide a search term as a command-line argument.")
|
| 31 |
|
| 32 |
if __name__ == "__main__":
|
| 33 |
main()
|