Update plugin.py

Added so when you post a user link, it displays the result probably and not getting `HTTPError: 403 Client Error: Blocked for url`
This commit is contained in:
klapvogn 2024-08-11 12:29:30 +02:00 committed by GitHub
parent b174eeaf31
commit d34389d19e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -40,6 +40,9 @@ from urllib.parse import urlparse, parse_qsl
from bs4 import BeautifulSoup from bs4 import BeautifulSoup
from jinja2 import Template from jinja2 import Template
import requests import requests
import praw
import urllib
try: try:
from supybot.i18n import PluginInternationalization from supybot.i18n import PluginInternationalization
@ -73,6 +76,7 @@ class SpiffyTitles(callbacks.Plugin):
client_secret=self.registryValue("reddit.clientsecret"), client_secret=self.registryValue("reddit.clientsecret"),
user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0' user_agent='Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0'
) )
proxy = str(conf.supybot.protocols.http.proxy) proxy = str(conf.supybot.protocols.http.proxy)
if proxy: if proxy:
match = re.match(r"https?:\/\/", proxy, re.IGNORECASE) match = re.match(r"https?:\/\/", proxy, re.IGNORECASE)
@ -81,6 +85,8 @@ class SpiffyTitles(callbacks.Plugin):
self.proxies["http"] = proxy self.proxies["http"] = proxy
self.proxies["https"] = proxy self.proxies["https"] = proxy
def add_handlers(self): def add_handlers(self):
""" """
Adds all handlers Adds all handlers
@ -247,13 +253,22 @@ class SpiffyTitles(callbacks.Plugin):
""" """
if self.registryValue("default.enabled", channel): if self.registryValue("default.enabled", channel):
log.debug("SpiffyTitles: calling default handler for %s" % (url)) log.debug("SpiffyTitles: calling default handler for %s" % (url))
# Extract the domain from the URL
parsed_url = urllib.parse.urlparse(url)
domain = parsed_url.netloc
# Format the domain with the title
default_template = Template( default_template = Template(
self.registryValue("default.template", channel=channel) self.registryValue("default.template", channel=channel)
) )
(title, is_redirect) = self.get_source_by_url(url, channel) (title, is_redirect) = self.get_source_by_url(url, channel)
if title: if title:
# Adding the domain part to the title template
formatted_domain = format(_(' (at %s)'), domain)
title_template = default_template.render( title_template = default_template.render(
title=title, redirect=is_redirect title=title + formatted_domain, # Append the domain to the title
redirect=is_redirect
) )
return title_template return title_template
else: else:
@ -417,7 +432,7 @@ class SpiffyTitles(callbacks.Plugin):
Retrieves value of <title> tag from HTML Retrieves value of <title> tag from HTML
""" """
title = None title = None
soup = BeautifulSoup(html) soup = BeautifulSoup(html, "html.parser")
if soup: if soup:
try: try:
title = soup.title.string.strip() title = soup.title.string.strip()
@ -1326,10 +1341,10 @@ class SpiffyTitles(callbacks.Plugin):
if response: if response:
imdb_template = Template(self.registryValue("imdb.template")) imdb_template = Template(self.registryValue("imdb.template"))
meta = None meta = None
tomato = None tomatometer = None
for rating in response["Ratings"]: for rating in response["Ratings"]:
if rating["Source"] == "Rotten Tomatoes": if rating["Source"] == "Rotten Tomatoes":
tomato = rating["Value"] tomatometer = rating["Value"]
if rating["Source"] == "Metacritic": if rating["Source"] == "Metacritic":
meta = "{0}%".format(rating["Value"].split("/")[0]) meta = "{0}%".format(rating["Value"].split("/")[0])
template_vars = { template_vars = {
@ -1339,8 +1354,8 @@ class SpiffyTitles(callbacks.Plugin):
"director": response.get("Director"), "director": response.get("Director"),
"plot": response.get("Plot"), "plot": response.get("Plot"),
"imdb_id": response.get("imdbID"), "imdb_id": response.get("imdbID"),
"imdb_rating": response.get("imdbRating"), "rating": response.get("imdbRating"),
"tomatoMeter": tomato, "tomato": tomatometer,
"metascore": meta, "metascore": meta,
"released": response.get("Released"), "released": response.get("Released"),
"genre": response.get("Genre"), "genre": response.get("Genre"),
@ -1461,7 +1476,7 @@ class SpiffyTitles(callbacks.Plugin):
reddit_handler_enabled = self.registryValue("reddit.enabled", channel=channel) reddit_handler_enabled = self.registryValue("reddit.enabled", channel=channel)
if not reddit_handler_enabled: if not reddit_handler_enabled:
return self.handler_default(url, channel) return self.handler_default(url, channel)
self.log.debug("SpiffyTitles: calling reddit handler for %s" % (url)) self.log.debug("SpiffyTitles: calling reddit handler for %s" % (url))
patterns = { patterns = {
"thread": { "thread": {
@ -1478,8 +1493,8 @@ class SpiffyTitles(callbacks.Plugin):
), ),
}, },
"user": { "user": {
"pattern": r"^/u(?:ser)?/(?P<user>[^/]+)/?$", "pattern": r"^/r(?:ser)?/(?P<user>[^/]+)/?$",
"url": "https://www.reddit.com/user/{user}/about.json", # No URL needed since PRAW handles this internally
}, },
} }
info = urlparse(url) info = urlparse(url)
@ -1494,9 +1509,9 @@ class SpiffyTitles(callbacks.Plugin):
if not match: if not match:
self.log.debug("SpiffyTitles: no title found.") self.log.debug("SpiffyTitles: no title found.")
return self.handler_default(url, channel) return self.handler_default(url, channel)
self.log.debug("SpiffyTitles: requesting %s" % (url)) self.log.debug("SpiffyTitles: requesting %s" % (url))
extract = "" extract = ""
try: try:
if link_type == "thread": if link_type == "thread":
@ -1517,7 +1532,15 @@ class SpiffyTitles(callbacks.Plugin):
data["title"] = submission.title data["title"] = submission.title
elif link_type == "user": elif link_type == "user":
user = self.reddit.redditor(link_info["user"]) user = self.reddit.redditor(link_info["user"])
data = vars(user) data = {
"id": user.id,
"name": user.name,
"link_karma": user.link_karma,
"comment_karma": user.comment_karma,
"created_utc": user.created_utc,
"is_mod": user.is_mod,
"is_gold": user.is_gold
}
else: else:
return self.handler_default(url, channel) return self.handler_default(url, channel)
except KeyError as e: except KeyError as e:
@ -1526,7 +1549,7 @@ class SpiffyTitles(callbacks.Plugin):
except Exception as e: except Exception as e:
self.log.error(f"SpiffyTitles: Reddit Error: {e}") self.log.error(f"SpiffyTitles: Reddit Error: {e}")
return self.handler_default(url, channel) return self.handler_default(url, channel)
if data: if data:
today = datetime.datetime.now().date() today = datetime.datetime.now().date()
created = datetime.datetime.fromtimestamp(data.get("created_utc", 0)).date() created = datetime.datetime.fromtimestamp(data.get("created_utc", 0)).date()
@ -1567,8 +1590,8 @@ class SpiffyTitles(callbacks.Plugin):
"comments": "{:,}".format(data.get("num_comments", 0)), "comments": "{:,}".format(data.get("num_comments", 0)),
"created": created.strftime("%Y-%m-%d"), "created": created.strftime("%Y-%m-%d"),
"age": age, "age": age,
"link_karma": "{:,}".format(data.get("link_karma", 0)), "link_karma": "{:,}".format(data.get("link_karma", 0)), # User-specific
"comment_karma": "{:,}".format(data.get("comment_karma", 0)), "comment_karma": "{:,}".format(data.get("comment_karma", 0)), # User-specific
"extract": "%%extract%%", "extract": "%%extract%%",
} }
reply = reddit_template.render(template_vars) reply = reddit_template.render(template_vars)
@ -1586,7 +1609,7 @@ class SpiffyTitles(callbacks.Plugin):
else: else:
self.log.debug("SpiffyTitles: falling back to default handler") self.log.debug("SpiffyTitles: falling back to default handler")
return self.handler_default(url, channel) return self.handler_default(url, channel)
def is_valid_imgur_id(self, input): def is_valid_imgur_id(self, input):
""" """
Tests if input matches the typical imgur id, which seems to be alphanumeric. Tests if input matches the typical imgur id, which seems to be alphanumeric.
@ -1776,7 +1799,7 @@ class SpiffyTitles(callbacks.Plugin):
return title return title
else: else:
return self.handler_default(url, channel) return self.handler_default(url, channel)
def t(self, irc, msg, args, query): def t(self, irc, msg, args, query):
""" """
Retrieves title for a URL on demand Retrieves title for a URL on demand