Skip to content
This repository has been archived by the owner on Sep 28, 2022. It is now read-only.

Commit

Permalink
repair hs_wiki plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
firemark committed Jan 24, 2016
1 parent 9ff473c commit 3ac3bae
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 15 deletions.
53 changes: 40 additions & 13 deletions grazyna/plugins/hs_wiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@

import asyncio
import dateparser
import re

URL = 'https://wiki.hs-silesia.pl/wiki'
URL_MEETS = '%s/Planowane_spotkania' % URL
URL_CYCLIC = '%s/Cykliczne_obowiązki' % URL
TRASH_ID = "Wyw.C3.B3z_.C5.9Bmieci"
MEETS_ID = "Najbli.C5.BCsze_spotkania_tematyczne"
LIST_XPATH = '//span[starts-with(@id,$id)]/../following-sibling::ul/li'
PREV_MEETS_ID = "Odbyte_spotkania"
LIST_XPATH = '//span[starts-with(@id,$id)]/../following-sibling::ul[1]/li'

re_date_range = re.compile(r"(\d\d+?)\s*-\s*(\d\d?)")
re_every = re.compile(r"^[Kk]a[żz]d[yaą]")

@asyncio.coroutine
def get_html(url):
Expand All @@ -30,27 +35,39 @@ def get_html(url):
session.close()


def get_list_of_text_sorted_by_time(nodes, parse_date=None):
def get_list_of_text_sorted_by_time(nodes, parse_date=None, old_dates=False):
parse_date = parse_date or (lambda text: dateparser.parse(text))
now = datetime.now()
list_data = sorted(
((text, date) for text, date in (
(text.strip(), parse_date(text))
for text in (node.text for node in nodes) if text
) if date is not None and date > now),
key=lambda obj: obj[1] - now
(
(text, date) for text, date in (
(text.strip(), parse_date(text))
for text in (node.text for node in nodes) if text
)
if date is not None and (
(not old_dates and date > now) or
(old_dates and date < now)
)
),
key=lambda obj: (obj[1] - now) * (not old_dates * 2 - 1) # 0,1 -> 1,-1
)
return [text for text, date in list_data]


@register(cmd='next-meet')
def next_meet(bot, position: range_int(1)=1):
def meet_parse_date(text):
text = re_date_range.sub(r'\2', text)
raw_date, _, _ = text.partition('-')
raw_date = raw_date.strip()
raw_date = re_every.sub('', raw_date)
return dateparser.parse(raw_date)


@asyncio.coroutine
def show_meets(bot, position, label, old_dates):
html = yield from get_html(URL_MEETS)
nodes = html.xpath(LIST_XPATH, id=MEETS_ID)
nodes = html.xpath(LIST_XPATH, id=label)
list_data = get_list_of_text_sorted_by_time(
nodes,
lambda text: dateparser.parse(text.partition('-')[0].strip())
)
nodes, meet_parse_date, old_dates)
if not list_data:
return bot.reply('¬_¬ no meetings')
len_list = len(list_data)
Expand All @@ -62,6 +79,16 @@ def next_meet(bot, position: range_int(1)=1):
bot.reply(text)


@register(cmd='next-meet')
def next_meet(bot, position: range_int(1)=1):
yield from show_meets(bot, position, MEETS_ID, old_dates=False)


@register(cmd='prev-meet')
def prev_meet(bot, position: range_int(1)=1):
yield from show_meets(bot, position, PREV_MEETS_ID, old_dates=True)


@register(cmd='trash')
def trash(bot):
html = yield from get_html(URL_CYCLIC)
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@

setup(
name='grazyna',
version='0.5.5',
download_url='https://github.com/firemark/grazyna/tarball/0.5.5',
version='0.5.5.1',
download_url='https://github.com/firemark/grazyna/tarball/0.5.5.1',
description='Grazyna The irc bot',
long_description='Grazyna The irc bot',
classifiers=[],
Expand Down

0 comments on commit 3ac3bae

Please sign in to comment.