mirror of
https://github.com/oddluck/limnoria-plugins.git
synced 2025-04-26 04:51:09 -05:00
SpiffyTitles: more cleanup
This commit is contained in:
parent
7925f57697
commit
b0afb198ad
@ -243,8 +243,7 @@ conf.registerChannelValue(
|
|||||||
SpiffyTitles.default,
|
SpiffyTitles.default,
|
||||||
"fileTemplate",
|
"fileTemplate",
|
||||||
registry.String(
|
registry.String(
|
||||||
"{% if type %}[{{type}}] {% endif %}"
|
"{% if type %}[{{type}}] {% endif %}" "{% if size %}({{size}}){% endif %}",
|
||||||
"{% if size %}({{size}}){% endif %}",
|
|
||||||
_("""Template used for default title responses"""),
|
_("""Template used for default title responses"""),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
@ -40,11 +40,11 @@ import random
|
|||||||
import time
|
import time
|
||||||
import json
|
import json
|
||||||
import unicodedata
|
import unicodedata
|
||||||
|
import datetime
|
||||||
from urllib.parse import urlparse, parse_qsl
|
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 pendulum
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from supybot.i18n import PluginInternationalization
|
from supybot.i18n import PluginInternationalization
|
||||||
@ -327,7 +327,7 @@ class SpiffyTitles(callbacks.Plugin):
|
|||||||
title = self.get_formatted_title(title, channel)
|
title = self.get_formatted_title(title, channel)
|
||||||
# Update link cache
|
# Update link cache
|
||||||
log.debug("SpiffyTitles: caching %s" % (url))
|
log.debug("SpiffyTitles: caching %s" % (url))
|
||||||
now = pendulum.now()
|
now = datetime.datetime.now()
|
||||||
if channel not in self.link_cache:
|
if channel not in self.link_cache:
|
||||||
self.link_cache[channel] = []
|
self.link_cache[channel] = []
|
||||||
self.link_cache[channel].append(
|
self.link_cache[channel].append(
|
||||||
@ -360,7 +360,7 @@ class SpiffyTitles(callbacks.Plugin):
|
|||||||
if len(self.link_cache) == 0:
|
if len(self.link_cache) == 0:
|
||||||
return
|
return
|
||||||
cached_link = None
|
cached_link = None
|
||||||
now = pendulum.now()
|
now = datetime.datetime.now()
|
||||||
stale = False
|
stale = False
|
||||||
seconds = 0
|
seconds = 0
|
||||||
if channel in self.link_cache:
|
if channel in self.link_cache:
|
||||||
@ -855,8 +855,7 @@ class SpiffyTitles(callbacks.Plugin):
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
duration = "LIVE"
|
duration = "LIVE"
|
||||||
published = snippet["publishedAt"]
|
published = snippet["publishedAt"].split("T")[0]
|
||||||
published = self.get_published_date(published)
|
|
||||||
timestamp = self.get_timestamp_from_youtube_url(url)
|
timestamp = self.get_timestamp_from_youtube_url(url)
|
||||||
yt_logo = self.get_youtube_logo(channel)
|
yt_logo = self.get_youtube_logo(channel)
|
||||||
compiled_template = yt_template.render(
|
compiled_template = yt_template.render(
|
||||||
@ -898,12 +897,6 @@ class SpiffyTitles(callbacks.Plugin):
|
|||||||
log.debug("SpiffyTitles: falling back to default handler")
|
log.debug("SpiffyTitles: falling back to default handler")
|
||||||
return self.handler_default(url, channel)
|
return self.handler_default(url, channel)
|
||||||
|
|
||||||
def get_published_date(self, date):
|
|
||||||
date = pendulum.parse(date, strict=False)
|
|
||||||
date = pendulum.datetime(date.year, date.month, date.day)
|
|
||||||
date = date.to_date_string()
|
|
||||||
return date
|
|
||||||
|
|
||||||
def get_duration_from_seconds(self, duration_seconds):
|
def get_duration_from_seconds(self, duration_seconds):
|
||||||
m, s = divmod(duration_seconds, 60)
|
m, s = divmod(duration_seconds, 60)
|
||||||
h, m = divmod(m, 60)
|
h, m = divmod(m, 60)
|
||||||
@ -927,8 +920,27 @@ class SpiffyTitles(callbacks.Plugin):
|
|||||||
4 minutes and 41 seconds. This method returns the total seconds
|
4 minutes and 41 seconds. This method returns the total seconds
|
||||||
so that the duration can be parsed as usual.
|
so that the duration can be parsed as usual.
|
||||||
"""
|
"""
|
||||||
duration = pendulum.parse(input)
|
regex = re.compile(
|
||||||
return duration.total_seconds()
|
"""
|
||||||
|
(?P<sign> -?) P
|
||||||
|
(?:(?P<years> \d+) Y)?
|
||||||
|
(?:(?P<months> \d+) M)?
|
||||||
|
(?:(?P<days> \d+) D)?
|
||||||
|
(?: T
|
||||||
|
(?:(?P<hours> \d+) H)?
|
||||||
|
(?:(?P<minutes>\d+) M)?
|
||||||
|
(?:(?P<seconds>\d+) S)?
|
||||||
|
)?
|
||||||
|
""",
|
||||||
|
re.VERBOSE,
|
||||||
|
)
|
||||||
|
duration = regex.match(input).groupdict(0)
|
||||||
|
delta = datetime.timedelta(
|
||||||
|
hours=int(duration["hours"]),
|
||||||
|
minutes=int(duration["minutes"]),
|
||||||
|
seconds=int(duration["seconds"]),
|
||||||
|
)
|
||||||
|
return delta.total_seconds()
|
||||||
|
|
||||||
def get_timestamp_from_youtube_url(self, url):
|
def get_timestamp_from_youtube_url(self, url):
|
||||||
"""
|
"""
|
||||||
@ -1209,12 +1221,16 @@ class SpiffyTitles(callbacks.Plugin):
|
|||||||
"""
|
"""
|
||||||
Return relative time delta between now and s (dt string).
|
Return relative time delta between now and s (dt string).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
try: # timeline's created_at Tue May 08 10:58:49 +0000 2012
|
try: # timeline's created_at Tue May 08 10:58:49 +0000 2012
|
||||||
ddate = pendulum.parse(s)
|
ddate = time.strptime(s, "%Y-%m-%dT%H:%M:%SZ")[:-2]
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return s
|
try: # search's created_at Thu, 06 Oct 2011 19:41:12 +0000
|
||||||
|
ddate = time.strptime(s, "%a, %d %b %Y %H:%M:%S +0000")[:-2]
|
||||||
|
except ValueError:
|
||||||
|
return s
|
||||||
# do the math
|
# do the math
|
||||||
d = pendulum.now() - ddate
|
d = datetime.datetime.now() - datetime.datetime(*ddate, tzinfo=None)
|
||||||
# now parse and return.
|
# now parse and return.
|
||||||
if d.days:
|
if d.days:
|
||||||
rel_time = "{:1d}d ago".format(abs(d.days))
|
rel_time = "{:1d}d ago".format(abs(d.days))
|
||||||
@ -1479,8 +1495,8 @@ class SpiffyTitles(callbacks.Plugin):
|
|||||||
% (request.status_code, request.content.decode()[:200])
|
% (request.status_code, request.content.decode()[:200])
|
||||||
)
|
)
|
||||||
if data:
|
if data:
|
||||||
today = pendulum.now().date()
|
today = datetime.datetime.now().date()
|
||||||
created = pendulum.from_timestamp(data["created_utc"]).date()
|
created = datetime.datetime.fromtimestamp(data["created_utc"]).date()
|
||||||
age_days = (today - created).days
|
age_days = (today - created).days
|
||||||
if age_days == 0:
|
if age_days == 0:
|
||||||
age = "today"
|
age = "today"
|
||||||
@ -1577,9 +1593,7 @@ class SpiffyTitles(callbacks.Plugin):
|
|||||||
if client_id and self.is_valid_imgur_id(album_id):
|
if client_id and self.is_valid_imgur_id(album_id):
|
||||||
log.debug("SpiffyTitles: found imgur album id %s" % (album_id))
|
log.debug("SpiffyTitles: found imgur album id %s" % (album_id))
|
||||||
try:
|
try:
|
||||||
header = {
|
header = {"Authorization": "Client-ID {0}".format(client_id)}
|
||||||
"Authorization": "Client-ID {0}".format(client_id)
|
|
||||||
}
|
|
||||||
api_url = "https://api.imgur.com/3/album/{0}".format(album_id)
|
api_url = "https://api.imgur.com/3/album/{0}".format(album_id)
|
||||||
request = requests.get(api_url, headers=header, timeout=10)
|
request = requests.get(api_url, headers=header, timeout=10)
|
||||||
request.raise_for_status()
|
request.raise_for_status()
|
||||||
@ -1642,9 +1656,7 @@ class SpiffyTitles(callbacks.Plugin):
|
|||||||
if client_id and self.is_valid_imgur_id(image_id):
|
if client_id and self.is_valid_imgur_id(image_id):
|
||||||
log.debug("SpiffyTitles: found image id %s" % (image_id))
|
log.debug("SpiffyTitles: found image id %s" % (image_id))
|
||||||
try:
|
try:
|
||||||
headers = {
|
headers = {"Authorization": "Client-ID {0}".format(client_id)}
|
||||||
"Authorization": "Client-ID {0}".format(client_id)
|
|
||||||
}
|
|
||||||
api_url = "https://api.imgur.com/3/image/{0}".format(image_id)
|
api_url = "https://api.imgur.com/3/image/{0}".format(image_id)
|
||||||
request = requests.get(api_url, headers=headers, timeout=10)
|
request = requests.get(api_url, headers=headers, timeout=10)
|
||||||
request.raise_for_status()
|
request.raise_for_status()
|
||||||
@ -1711,6 +1723,7 @@ class SpiffyTitles(callbacks.Plugin):
|
|||||||
irc.reply(title)
|
irc.reply(title)
|
||||||
else:
|
else:
|
||||||
irc.reply(error_message + " {}".format(err))
|
irc.reply(error_message + " {}".format(err))
|
||||||
|
|
||||||
t = wrap(t, ["text"])
|
t = wrap(t, ["text"])
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,4 +1,3 @@
|
|||||||
requests
|
requests
|
||||||
beautifulsoup4
|
beautifulsoup4
|
||||||
jinja2
|
jinja2
|
||||||
pendulum
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user