Skip to content
This repository has been archived by the owner on Dec 16, 2024. It is now read-only.

Commit

Permalink
rewrite firefox setup
Browse files Browse the repository at this point in the history
  • Loading branch information
MaKraMc committed May 7, 2024
1 parent 16beb0a commit 3ac36ec
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
venv/
__pycache__/
geckodriver
6 changes: 4 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ ENV TZ=Europe/London
RUN apk add --no-cache firefox
RUN apk add --no-cache tesseract-ocr
RUN apk add --no-cache tesseract-ocr-data-eng
#Zlib is required for armv7
RUN apk add zlib-dev


#Copy the code
WORKDIR /app
Expand All @@ -19,6 +18,9 @@ COPY . /app
#Install the python requirements
RUN pip3 install -r /app/requirements.txt

#Install geckodriver
RUN python3 /app/src/setup.py

#Make sure the startup script is executable
RUN chmod +x /app/startup.sh

Expand Down
2 changes: 0 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ packaging==24.0
pillow==10.3.0
PySocks==1.7.1
pytesseract==0.3.10
python-dotenv==1.0.1
requests==2.31.0
selenium==4.20.0
sniffio==1.3.1
Expand All @@ -18,5 +17,4 @@ trio==0.25.0
trio-websocket==0.11.1
typing-extensions==4.11.0
urllib3==2.2.1
webdriver-manager==4.0.1
wsproto==1.2.0
11 changes: 7 additions & 4 deletions src/loadFirefox.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
from selenium.webdriver.firefox.service import Service
from selenium.webdriver.firefox.options import Options

from webdriver_manager.firefox import GeckoDriverManager


#Load the browser
def loadFirefox():
options = Options()
options.add_argument("--headless")

return webdriver.Firefox(service=Service(GeckoDriverManager().install()),
options=options)
return webdriver.Firefox(service=Service("/app/geckodriver"), options=options)

if __name__ == "__main__":
driver = loadFirefox()
driver.get("https://example.com")
print(driver.title)
driver.quit()
67 changes: 67 additions & 0 deletions src/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import requests
from os import path, remove
from platform import machine
import tarfile

def main():
# Fetch the latest release assets for the repository
print("Setting up geckodriver")
fetch_latest("mozilla", "geckodriver")


def get_architecture():
# Get the machine architecture
arch = machine()
if arch == "x86_64":
return "linux64"
elif arch == "AMD64":
return "linux64"
elif arch == "arm64":
return "linux-aarch64"
elif arch == "aarch64":
return "linux-aarch64"
else:
raise ValueError(f"Unsupported architecture: {arch}")

def download_asset(url):
print(f"Downloading: {url}")
# Extract the filename from the URL
filename = path.join("./", path.basename(url))
# Download the asset
response = requests.get(url)
if response.status_code == 200:
#Save the asset to the current directory
with open(filename, 'wb') as file:
file.write(response.content)
# Extract the contents of the archive
print(f"Extracting to: {filename}")
with tarfile.open(filename, 'r:gz') as tar:
tar.extractall()
# Remove the archive file
remove(filename)

# Check for the geckodriver binary
if path.exists("geckodriver"):
print("geckodriver is setup")
else:
exit("Failed to download geckodriver binary")
else:
exit(f"Failed to download asset: {response.status_code}")

def fetch_latest(owner, repo):
# Fetch information about the latest release
url = f"https://api.github.com/repos/{owner}/{repo}/releases/latest"
response = requests.get(url)
if response.status_code == 200:
data = response.json()
# Download assets associated with the release
architecture = get_architecture()
for asset in data.get('assets', []):
# Download the asset if it matches the architecture and is a gz archive
if architecture in asset["name"] and asset["name"].endswith('.gz'):
download_asset(asset['browser_download_url'])
else:
exit(f"Failed to fetch latest release: {response.status_code}")

if __name__ == "__main__":
main()

0 comments on commit 3ac36ec

Please sign in to comment.