From f0ab6a38e4b1141b30c0843b338d4973b0e2cdb0 Mon Sep 17 00:00:00 2001 From: kmille Date: Tue, 22 Oct 2024 22:27:33 +0200 Subject: [PATCH] Enable Tor support When env FREETAR_ENABLE_TOR=1 is set, use socks5://localhost:9050 proxy when sending requests to UG. Workaround, as UG started to block ips (403 cloudflare). --- README.md | 2 ++ freetar/ug.py | 13 +++++++++++-- poetry.lock | 15 ++++++++++++++- pyproject.toml | 2 +- 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 7180831..09301f1 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,8 @@ This is like [Invidious](https://invidious.io/) but only for [Ultimate Guitar](https://www.ultimate-guitar.com/). +**UPDATE 22.10.2023:** As ultimate-guitar.com started to block (some? my? server?) ip addresses it's now possible to send requests to UG over Tor (socks5 proxy listening on `localhost:9050`). This feature can be enabled when environment variable `FREETAR_ENABLE_TOR=1` is set. Supported since Freetar version 0.10.0. + ## Instances - https://freetar.de - https://freetar.habedieeh.re diff --git a/freetar/ug.py b/freetar/ug.py index f711bf2..1e411f8 100644 --- a/freetar/ug.py +++ b/freetar/ug.py @@ -3,6 +3,7 @@ from urllib.parse import quote, urlparse import json import re +import os from dataclasses import dataclass, field from .utils import FreetarError @@ -10,6 +11,12 @@ USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.3" +if os.environ.get("FREETAR_ENABLE_TOR", "") != "": + print("Enabling tor for requests to ultimate guitar") + PROXIES = {'https': 'socks5://127.0.0.1:9050'} +else: + PROXIES = None + @dataclass class SearchResult: @@ -102,7 +109,8 @@ def parse_chord(self, chord): def ug_search(value: str): try: resp = requests.get(f"https://www.ultimate-guitar.com/search.php?search_type=title&value={quote(value)}", - headers={'User-Agent': USER_AGENT}) + headers={'User-Agent': USER_AGENT}, + proxies=PROXIES) resp.raise_for_status() bs = BeautifulSoup(resp.text, 'html.parser') # data can be None @@ -182,7 +190,8 @@ def get_chords(s: SongDetail) -> SongDetail: def ug_tab(url_path: str): try: resp = requests.get("https://tabs.ultimate-guitar.com/tab/" + url_path, - headers={'User-Agent': USER_AGENT}) + headers={'User-Agent': USER_AGENT}, + proxies=PROXIES) resp.raise_for_status() bs = BeautifulSoup(resp.text, 'html.parser') data = bs.find("div", {"class": "js-store"}) diff --git a/poetry.lock b/poetry.lock index b11ac9a..2f7e1ca 100644 --- a/poetry.lock +++ b/poetry.lock @@ -470,6 +470,18 @@ files = [ {file = "pyrepl-0.9.0.tar.gz", hash = "sha256:292570f34b5502e871bbb966d639474f2b57fbfcd3373c2d6a2f3d56e681a775"}, ] +[[package]] +name = "pysocks" +version = "1.7.1" +description = "A Python SOCKS client module. See https://github.com/Anorov/PySocks for more information." +optional = false +python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*" +files = [ + {file = "PySocks-1.7.1-py27-none-any.whl", hash = "sha256:08e69f092cc6dbe92a0fdd16eeb9b9ffbc13cadfe5ca4c7bd92ffb078b293299"}, + {file = "PySocks-1.7.1-py3-none-any.whl", hash = "sha256:2725bd0a9925919b9b51739eea5f9e2bae91e83288108a9ad338b2e3a4435ee5"}, + {file = "PySocks-1.7.1.tar.gz", hash = "sha256:3f8804571ebe159c380ac6de37643bb4685970655d3bba243530d6558b799aa0"}, +] + [[package]] name = "rcssmin" version = "1.1.2" @@ -541,6 +553,7 @@ files = [ certifi = ">=2017.4.17" charset-normalizer = ">=2,<4" idna = ">=2.5,<4" +PySocks = {version = ">=1.5.6,<1.5.7 || >1.5.7", optional = true, markers = "extra == \"socks\""} urllib3 = ">=1.21.1,<3" [package.extras] @@ -770,4 +783,4 @@ test = ["big-O", "importlib-resources", "jaraco.functools", "jaraco.itertools", [metadata] lock-version = "2.0" python-versions = "^3.9" -content-hash = "af1a70767f275bcd4a0c55fe7d7b3df7379b2a3ab79cdad45c1b394565c7e4a1" +content-hash = "d875ad4ac2e79718f52925c1a79000e701eeb2ffce3d2949974166dc63bb4232" diff --git a/pyproject.toml b/pyproject.toml index 0b8104f..acbe493 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -10,7 +10,7 @@ homepage = "https://github.com/kmille/freetar" [tool.poetry.dependencies] python = "^3.9" -requests = "^2.28.2" +requests = {extras = ["socks"], version = "^2.32.3"} beautifulsoup4 = "^4.12.0" flask = "^2.2.3" waitress = "^2.1.2"