From 3480ef3ac1e15157984553fadc1f21ced52ab695 Mon Sep 17 00:00:00 2001 From: Matthew Hickson Date: Sat, 23 Nov 2024 16:39:07 -0500 Subject: [PATCH] installed a global opener for urlopen - works great! --- tuipod/models/podcast.py | 4 +--- tuipod/ui/podcast_app.py | 8 ++++++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/tuipod/models/podcast.py b/tuipod/models/podcast.py index 7aaea23..16d58a5 100644 --- a/tuipod/models/podcast.py +++ b/tuipod/models/podcast.py @@ -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) diff --git a/tuipod/ui/podcast_app.py b/tuipod/ui/podcast_app.py index 524e907..759e8fe 100644 --- a/tuipod/ui/podcast_app.py +++ b/tuipod/ui/podcast_app.py @@ -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 @@ -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")