Compare commits

..

No commits in common. "f4818798639e1d987335337bf7cd513feddcdd54" and "f65292d891f90cbe6aae9dfeeaa158447f58cdde" have entirely different histories.

10 changed files with 66 additions and 192 deletions

View File

@ -17,7 +17,7 @@ logs/
# Ignore Docker files # Ignore Docker files
Dockerfile Dockerfile
docker-compose.yaml docker-compose.yml
# Ignore build directories # Ignore build directories
dist/ dist/

View File

@ -1,6 +1,7 @@
from scraper import scrap from scraper import scrap
import os import os
import json import json
import time
urls = ["https://digitalprojects.wpi.art/monet/artworks"] urls = ["https://digitalprojects.wpi.art/monet/artworks"]
hrefs = [] hrefs = []

View File

@ -1,15 +1,18 @@
services: services:
webscraper: webscraper:
build: build:
context: . context: ./app
dockerfile: ./docker/scripts/Dockerfile
container_name: webscraper container_name: webscraper
volumes: volumes:
- .:/usr/src/app - ./app:/usr/src/app
command: develop:
- tail watch:
- -f - path: ./app/requirements.txt
- /dev/null action: rebuild
- path: ./app
target: /usr/src/app
action: sync+restart
command: tail -f /dev/null
selenium-hub: selenium-hub:
image: "selenium/hub:3.141.59" image: "selenium/hub:3.141.59"
container_name: selenium-hub container_name: selenium-hub

View File

@ -1,33 +0,0 @@
import subprocess
def quitCondition(command: str) -> bool:
return command in ["q", "quit", "exit", "stop"]
def helpCondition(command: str) -> bool:
return command in ["h", "help"]
def clearCondition(command: str) -> bool:
return command in ["c", "clear", "cls"]
def systemCommand(command: str) -> str:
words = command[slice(1, len(command))].split()
if words[0] == "":
return "Command not found. Write 'h' for help."
return subprocess.run(
f'docker exec -it webscraper {" ".join(words)}',
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
).stdout.decode()
def restartCondition(command: str) -> bool:
return command in ["r", "restart"]
def runCondition(command: str) -> bool:
return command in ["run"]

View File

@ -1,25 +0,0 @@
import subprocess
def run_command(command: str) -> str:
process = subprocess.run(
command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
if process.returncode != 0:
print(f"Error running command: {command}")
return process.stderr.decode()
return process.stdout.decode()
def main():
print("Starting Docker Compose services...\n")
run_command("docker compose -f ../app/docker-compose.yaml up -d")
print(run_command("docker exec -it webscraper python main.py"))
print("Stopping and removing Docker Compose services...")
run_command("docker compose -f ../app/docker-compose.yaml down")
if __name__ == "__main__":
main()

View File

@ -1,67 +0,0 @@
import commands
import subprocess
import sys
import threading
def prompt():
while True:
command = input("> ")
if commands.quitCondition(command):
print("Stopping and removing Docker Compose services...")
subprocess.run(
"docker compose -f ../app/docker-compose.yaml down",
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
break
if commands.helpCondition(command):
print(
"""
["h", "help"], - for help.
["q", "quit", "exit", "stop"], - to stop program.
["c", "clear", "cls"], - to clear console.
["r", "restart"], - to restart Docker Compose services.
["run"], - to run main.py in docker container.
["$..."], - to evaluate command in docker container.
"""
)
continue
if commands.clearCondition(command):
print("\n" * 100)
continue
if command.startswith("$"):
print(commands.systemCommand(command))
continue
if commands.restartCondition(command):
print("Restarting Docker Compose services...")
subprocess.run(
"docker compose -f ../app/docker-compose.yaml down",
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
subprocess.run(
"docker compose -f ../app/docker-compose.yaml up -d",
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
print("Composed!")
continue
if commands.runCondition(command):
print("Running main.py...")
print(
subprocess.run(
"docker exec -it webscraper python main.py",
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
).stdout.decode()
)
continue
else:
print("Command not found. Write 'h' for help.")
continue
sys.exit(0)

View File

@ -1,59 +0,0 @@
import subprocess
import time
import os
import threading
from threads.prompt import prompt
def run_command(command: str) -> str:
process = subprocess.run(
command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
if process.returncode != 0:
print(f"Error running command: {command}")
return process.stderr.decode()
return process.stdout.decode()
thread = threading.Thread(target=prompt)
def main():
print("Starting Docker Compose services...")
run_command("docker compose -f ../app/docker-compose.yaml up -d")
print("Composed!\n")
print("Running main.py...")
print(run_command("docker exec -it webscraper python main.py"))
print(
"\n\nWrite 'q' to stop program. Don't stop with 'Ctrl + C' otherwise docker container will be still on."
)
print("For help write 'h'.")
print("\nWatching for changes...")
thread.start()
path_to_watch = "/home/paprykdev/uni/webscraper/app"
before = {
f: os.stat(os.path.join(path_to_watch, f)).st_mtime
for f in os.listdir(path_to_watch)
if f.endswith(".py")
}
while True:
if threading.active_count() == 1:
break
time.sleep(1)
after = {
f: os.stat(os.path.join(path_to_watch, f)).st_mtime
for f in os.listdir(path_to_watch)
if f.endswith(".py")
}
for f in before:
if before[f] != after[f]:
print(f"\nDetected change in {f}")
print("Running main.py...")
print(run_command("docker exec -it webscraper python main.py"))
before[f] = after[f]
if __name__ == "__main__":
main()

47
start.py Normal file
View File

@ -0,0 +1,47 @@
import subprocess
import time
def run_command(command):
result = subprocess.run(
command, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
if result.returncode != 0:
print(f"Error running command: {command}")
print(result.stderr.decode())
return result.stdout.decode()
def wait_for_webscraper():
while True:
result = run_command("docker compose ps -q webscraper")
container_id = result.strip()
if not container_id:
print("Webscraper container not found.")
break
status = run_command(
f"docker inspect --format '{{.State.Status}}' {container_id}"
)
if status.strip() == "exited":
print("Webscraper has finished.")
break
print("Waiting for webscraper to finish...")
time.sleep(3)
def main():
print("Starting Docker Compose services...")
run_command("docker compose up -d")
wait_for_webscraper()
print("Stopping and removing Docker Compose services...")
run_command("docker compose down")
if __name__ == "__main__":
main()

7
start.sh Executable file
View File

@ -0,0 +1,7 @@
#!/bin/bash
docker compose up -d
docker compose wait webscraper > /dev/null
# docker compose down