import datetime

import bleach
import re

months = {
    1: u'jan',
    2: u'feb',
    3: u'mars',
    4: [u'apr\u00edl', u'apr'],
    5: u'ma\u00ed',
    6: u'j\u00fan\u00ed',
    7: u'j\u00fal\u00ed',
    8: u'\u00e1g\u00fast',
    9: u'sept',
    10: u'okt',
    11: u'n\u00f3v',
    12: u'des',
}


def decode_date_string(date_string=None):
    """
    Handles parsing abbreviated localised date strings into an ISO8601 compatible timestamp.

    Args:
        date_string (:obj: `string`, optional): unicode string with the extracted date

    Examples:
        >>> decode_date_string(None)
        
        >>> decode_date_string(u'27. apr\u00edl 2011')
        '2011-04-27'
    
        >>> decode_date_string(u'1. ma\u00ed.')
        '2019-05-01'
    
    """
    if date_string is None:
        # for when the input is None, re simply return
        return

    regex = re.compile(r'(?P<date>\d+). (?P<month>\w+)([. ]+)?(?P<year>\d+)?', re.UNICODE)
    match = regex.match(date_string)
    date = int(match.group('date'))
    month = translate_month(match.group('month'))
    year = match.group('year')
    # in some instances we don't have a specified year, it's assumed to be obvious in the context of the listing
    # just use the current year as a shortcut for now
    # TODO: make this work for dates that wrap over a year boundary
    if year is None:
        year = datetime.datetime.utcnow().year

    return '{}-{:02}-{:02}'.format(year, month, date)


def translate_month(month):
    """
    Translates the month string into an integer value 
    Args:
        month (unicode): month string parsed from the website listings.

    Returns:
        int: month index starting from 1
    
    Examples:
        >>> translate_month(u'jan')
        1
    """
    for key, values in months.items():
        if month in values:
            return key


def clean_html(input_html):
    allowed_tags = bleach.sanitizer.ALLOWED_TAGS + ['br', 'p']
    cleaner = bleach.sanitizer.Cleaner(tags=allowed_tags, strip=True, strip_comments=True)
    cleaned_lines = []

    if isinstance(input_html, (list, tuple)):
        for line in input_html:
            cleaned_lines.append(cleaner.clean(line).strip())
    elif isinstance(input_html, str):
        cleaned_lines.append(cleaner.clean(input_html).strip())

    return '<br>'.join(cleaned_lines)

@@ -53,17 +53,16 @@
 %0A    1: 
-u
 'jan',%0A 
@@ -67,17 +67,16 @@
 %0A    2: 
-u
 'feb',%0A 
@@ -81,17 +81,16 @@
 %0A    3: 
-u
 'mars',%0A
@@ -97,17 +97,16 @@
     4: %5B
-u
 'apr%5Cu00
@@ -111,17 +111,16 @@
 00edl', 
-u
 'apr'%5D,%0A
@@ -126,17 +126,16 @@
 %0A    5: 
-u
 'ma%5Cu00e
@@ -145,17 +145,16 @@
 %0A    6: 
-u
 'j%5Cu00fa
@@ -170,17 +170,16 @@
 %0A    7: 
-u
 'j%5Cu00fa
@@ -195,17 +195,16 @@
 %0A    8: 
-u
 '%5Cu00e1g
@@ -221,17 +221,16 @@
 %0A    9: 
-u
 'sept',%0A
@@ -237,17 +237,16 @@
     10: 
-u
 'okt',%0A 
@@ -252,17 +252,16 @@
     11: 
-u
 'n%5Cu00f3
@@ -272,17 +272,16 @@
     12: 
-u
 'des',%0A%7D
@@ -616,17 +616,16 @@
 _string(
-u
 '27. apr
@@ -696,17 +696,16 @@
 _string(
-u
 '1. ma%5Cu
@@ -1725,17 +1725,16 @@
 e_month(
-u
 'jan')%0A 

Update use of unicode to jive with py3