Skip to content

Commit

Permalink
installed a global opener for urlopen - works great!
Browse files Browse the repository at this point in the history
  • Loading branch information
mwhickson committed Nov 23, 2024
1 parent d65ab0a commit 3480ef3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
4 changes: 1 addition & 3 deletions tuipod/models/podcast.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ def remove_episode(self, url: str) -> None:
break

def get_episode_list(self) -> []:
req = urllib.request.Request(self.url)
req.add_header("User-Agent", "Mozilla/9.9 (github.com/mwhickson/tuipod) Chrome/999.9.9.9 Gecko/99990101 Firefox/999 Safari/999.9")
with urllib.request.urlopen(req) as response:
with urllib.request.urlopen(self.url) as response:
result = response.read()

episodes = ET.fromstring(result)
Expand Down
8 changes: 8 additions & 0 deletions tuipod/ui/podcast_app.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import json

from urllib.request import build_opener, install_opener

from textual import on
from textual.app import App, ComposeResult
from textual.binding import Binding
Expand Down Expand Up @@ -42,6 +44,12 @@ def __init__(self):
self.current_podcast = None
self.current_episode = None

# REF: https://theorangeone.net/posts/urllib-useragent/#global-opener
# NOTE: _opener is not available for import, but it's not necessary (presumably within an app context)
opener = build_opener()
install_opener(opener)
opener.addheaders = [("User-Agent", "Mozilla/9.9 (github.com/mwhickson/tuipod) Chrome/999.9.9.9 Gecko/99990101 Firefox/999 Safari/999.9")]

def compose(self) -> ComposeResult:
yield Header(icon="#", show_clock=True, time_format="%I:%M %p")

Expand Down

0 comments on commit 3480ef3

Please sign in to comment.