Compare commits
No commits in common. "77e51c8229ce74969f26c953f21d67247b478204" and "7cbdafe88c6c186b8a273870dd5cbd3708ce3bce" have entirely different histories.
77e51c8229
...
7cbdafe88c
2 changed files with 4 additions and 23 deletions
|
@ -6,24 +6,11 @@ from markdownify import markdownify
|
||||||
import re
|
import re
|
||||||
from bs4 import BeautifulSoup
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
from hrochobot.utils.ksp_utils import ksp_feed, strip_id, KSP_URL
|
from hrochobot.utils.ksp_utils import ksp_feed
|
||||||
import hrochobot.utils.data as data
|
import hrochobot.utils.data as data
|
||||||
|
|
||||||
NEWS_JSON = "news"
|
NEWS_JSON = "news"
|
||||||
|
|
||||||
async def get_news_ids():
|
|
||||||
feed = await ksp_feed()
|
|
||||||
return list(map(lambda e: e.id, feed.entries))
|
|
||||||
|
|
||||||
async def autocomplete_news_ids(ctx):
|
|
||||||
value = ctx.value.lower()
|
|
||||||
options = []
|
|
||||||
for id_ in map(strip_id, await get_news_ids()):
|
|
||||||
lid = id_.lower()
|
|
||||||
if lid.startswith(value) or lid.split("_", 1)[1].startswith(value):
|
|
||||||
options.append(id_)
|
|
||||||
return options
|
|
||||||
|
|
||||||
def guess_color(title):
|
def guess_color(title):
|
||||||
"""
|
"""
|
||||||
Automagically guess color of given post.
|
Automagically guess color of given post.
|
||||||
|
@ -56,7 +43,7 @@ def format_entry(entry, author=None):
|
||||||
if author:
|
if author:
|
||||||
embed.set_author(name=author)
|
embed.set_author(name=author)
|
||||||
|
|
||||||
embed.set_thumbnail(url=f"{KSP_URL}/img/hippo_head.png")
|
embed.set_thumbnail(url="https://ksp.mff.cuni.cz/img/hippo_head.png")
|
||||||
|
|
||||||
date = datetime.fromisoformat(entry.published)
|
date = datetime.fromisoformat(entry.published)
|
||||||
embed.set_footer(text=date.strftime("%-d. %-m. %Y"))
|
embed.set_footer(text=date.strftime("%-d. %-m. %Y"))
|
||||||
|
@ -70,7 +57,7 @@ async def post_news(bot, guild, entry_id):
|
||||||
|
|
||||||
channel = get(guild.channels, id=news_json["news_channel"])
|
channel = get(guild.channels, id=news_json["news_channel"])
|
||||||
feed = await ksp_feed()
|
feed = await ksp_feed()
|
||||||
entries_with_id = list(filter(lambda e: strip_id(e.id) == entry_id, feed.entries))
|
entries_with_id = list(filter(lambda e: e.id == f"https://ksp.mff.cuni.cz/news_{entry_id}", feed.entries))
|
||||||
if len(entries_with_id) == 0:
|
if len(entries_with_id) == 0:
|
||||||
return f"Entry with id ``{entry_id}`` not found."
|
return f"Entry with id ``{entry_id}`` not found."
|
||||||
|
|
||||||
|
@ -105,7 +92,7 @@ class News(commands.Cog):
|
||||||
return await ctx.respond(f"News channel set to {channel.mention}.", ephemeral=True)
|
return await ctx.respond(f"News channel set to {channel.mention}.", ephemeral=True)
|
||||||
|
|
||||||
@news.command(description="Synchronize news feed.")
|
@news.command(description="Synchronize news feed.")
|
||||||
@discord.option("id", str, description="Id of entry to send.", autocomplete=autocomplete_news_ids)
|
@discord.option("id", str, description="Id of entry to send.")
|
||||||
async def post_news(self, ctx, id: int):
|
async def post_news(self, ctx, id: int):
|
||||||
err = await post_news(self.bot, ctx.guild, id)
|
err = await post_news(self.bot, ctx.guild, id)
|
||||||
if err:
|
if err:
|
||||||
|
|
|
@ -37,9 +37,3 @@ def active_deadlines() -> List[Tuple[str, datetime]]:
|
||||||
|
|
||||||
async def ksp_feed() -> feedparser.util.FeedParserDict:
|
async def ksp_feed() -> feedparser.util.FeedParserDict:
|
||||||
return feedparser.parse(f"{KSP_URL}/ksp.feed")
|
return feedparser.parse(f"{KSP_URL}/ksp.feed")
|
||||||
|
|
||||||
def strip_id(id_):
|
|
||||||
text = f"{KSP_URL}/"
|
|
||||||
if id_.startswith(text):
|
|
||||||
id_ = id_[len(text):]
|
|
||||||
return id_
|
|
||||||
|
|
Loading…
Reference in a new issue