Skip to content

Commit

Permalink
Enable Tor support
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
kmille committed Oct 22, 2024
1 parent ad09c6f commit f0ab6a3
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 11 additions & 2 deletions freetar/ug.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,20 @@
from urllib.parse import quote, urlparse
import json
import re
import os

from dataclasses import dataclass, field
from .utils import FreetarError


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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"})
Expand Down
15 changes: 14 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit f0ab6a3

Please sign in to comment.