niharika17032001 commited on
Commit
147b594
Β·
1 Parent(s): f6362a3

commmit Dockerfile

Browse files
Files changed (4) hide show
  1. Dockerfile +50 -36
  2. app.py +2 -4
  3. requirements.txt +3 -1
  4. selimium_try.py +21 -70
Dockerfile CHANGED
@@ -1,50 +1,64 @@
1
- FROM python:3.9
 
2
 
3
- WORKDIR /code
4
-
5
- COPY ./requirements.txt /code/requirements.txt
6
-
7
- # Install required system dependencies
8
  RUN apt-get update && apt-get install -y \
9
- libnss3 \
10
- libnspr4 \
11
- libdbus-1-3 \
12
- libatk1.0-0 \
 
 
 
13
  libatk-bridge2.0-0 \
 
 
 
14
  libcups2 \
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  libxcomposite1 \
 
16
  libxdamage1 \
 
17
  libxfixes3 \
 
18
  libxrandr2 \
19
- libgbm1 \
20
- libxkbcommon0 \
21
- libasound2 \
22
- libatspi2.0-0 && \
23
- rm -rf /var/lib/apt/lists/*
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
- # Switch to the "user" user
34
- USER user
35
 
36
- # Set home and Playwright environment variables
37
- ENV HOME=/home/user \
38
- PATH=/home/user/.local/bin:$PATH \
39
- PLAYWRIGHT_BROWSERS_PATH=/home/user/.cache/ms-playwright
40
 
41
- # Explicitly create the Playwright cache directory with the correct user permissions
42
- RUN mkdir -p /home/user/.cache/ms-playwright
43
 
44
- # Copy the application code after switching to the user to maintain ownership
45
- COPY --chown=user . /home/user/app
46
 
47
- # Install Playwright and Chromium
48
- RUN pip install --no-cache-dir playwright && playwright install chromium
49
 
50
- CMD ["python", "app.py"]
 
 
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
- return selimium_try.main()
 
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 json
3
- import time
4
 
5
- COOKIE_FILE = "cookies.json"
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(headless=True)
64
- context = browser.new_context()
 
65
 
66
- # Load cookies before starting
67
- load_cookies(context)
 
68
 
69
- page = context.new_page()
70
- result=is_logged_in_youtube(page)
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 result
 
 
 
 
 
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()