From bb3b3f762f4dc54417e169ec17cc139205315c68 Mon Sep 17 00:00:00 2001 From: Anes Belfodil Date: Mon, 9 Dec 2024 16:08:44 -0500 Subject: [PATCH] Fix SSL issue --- .github/workflows/executable.yml | 2 +- connection.py | 4 ++-- http_client.py | 10 ++++++++++ requirements.txt | 1 + wallpaper.py | 7 ++++--- 5 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 http_client.py diff --git a/.github/workflows/executable.yml b/.github/workflows/executable.yml index ed7fcf6..3cd9703 100644 --- a/.github/workflows/executable.yml +++ b/.github/workflows/executable.yml @@ -20,7 +20,7 @@ jobs: - uses: actions/checkout@v4 - uses: actions/setup-python@v5 with: - python-version: 3.13 + python-version: 3.12 - run: python -m pip install -r requirements.txt - run: python -m PyInstaller --onefile --noconsole randwall - uses: actions/upload-artifact@v4 diff --git a/connection.py b/connection.py index 449940b..01b4058 100644 --- a/connection.py +++ b/connection.py @@ -1,11 +1,11 @@ -from urllib.request import urlopen, Request +from http_client import get from log import get_logger from time import sleep logger = get_logger(__name__) def _is_connected(): - urlopen(Request("https://1.1.1.1", headers={"User-Agent": "randwall/1.0"})) + get("https://1.1.1.1") try: return True except: diff --git a/http_client.py b/http_client.py new file mode 100644 index 0000000..ac3949b --- /dev/null +++ b/http_client.py @@ -0,0 +1,10 @@ +import ssl +import certifi +from urllib.request import urlopen, Request + + +def get(*args, **kwargs): + return urlopen( + Request(*args, **kwargs, method="GET", headers={"User-Agent": "randwall/1.0"}), + context=ssl.create_default_context(cafile=certifi.where()), + ) diff --git a/requirements.txt b/requirements.txt index f81c70d..63ff304 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,4 @@ +certifi platformdirs==4.3.6 pyinstaller==6.11.1 watchfiles==1.0.0 diff --git a/wallpaper.py b/wallpaper.py index 4c2a6c8..61a3e3d 100644 --- a/wallpaper.py +++ b/wallpaper.py @@ -1,8 +1,9 @@ import json import platform import os + +from http_client import get from log import get_logger -from urllib.request import urlopen, Request from urllib.parse import urlencode from config import config import subprocess @@ -89,7 +90,7 @@ def _get_random_wallpaper(): logger.info(f"Fetching random wallpaper from https://wallhaven.cc") try: - with urlopen(Request(api_url + urlencode(params), headers={"User-Agent": "randwall/1.0"})) as f: + with get(api_url + urlencode(params)) as f: response = f.read().decode("utf-8") data = json.loads(response) image_data = data["data"][0] @@ -110,7 +111,7 @@ def _download_wallpaper(wallpaper): logger.info(f"Downloading {wallpaper['url']} at {image_path}") try: - with urlopen(Request(wallpaper["path"], headers={"User-Agent": "randwall/1.0"})) as f: + with get(wallpaper["path"]) as f: image = f.read() except: return