"""
$Id$
$HeadURL$
"""

from __future__ import unicode_literals, print_function, division
import requests
from BeautifulSoup import BeautifulSoup
import datetime
import time

baseurl = "http://www.verkkoposti.com/e3/TrackinternetServlet?lang=fi&LOTUS_hae=Hae&LOTUS_side=1&LOTUS_trackId=%s&LOTUS_hae=Hae"


def command_posti(bot, user, channel, args):
    """Get the latest status for a package"""
    result = getstatus(bot, args, count=1)

    for line in result:
        return bot.say(channel, line.encode("UTF-8"))


def getstatus(bot, code, count=None):
    """Parse the package status page"""
    url = baseurl % code
    r = bot.get_url(url)
    bs = BeautifulSoup(r.content)
    if not bs:
        return

    res = []

    statuslist = bs.find("div", {'class': "result_up"}).find("table", {'width': '500'}).findAll("p", {'class': 'resulttext'})
    for status in statuslist:
        date, statustext, location = status.contents
        statustext = statustext.string
        date = time.strptime(date, "%d.%m.%Y, klo %H:%M&nbsp;")
        location = location[6:].strip()

        dt = datetime.datetime(*date[0:6])
        now = datetime.datetime.now()
        age = now - dt

        agestr = []

        if age.days > 0:
            agestr.append("%dd" % age.days)

        secs = age.seconds
        hours, minutes, seconds = secs // 3600, secs // 60 % 60, secs % 60

        if hours > 0:
            agestr.append("%dh" % hours)
        if minutes > 0:
            agestr.append("%dm" % minutes)

        res.append("%s - %s - %s" % (" ".join(agestr) + " ago", statustext, location))

    if count:
        return res[:count]
    else:
        return res

@@ -168,16 +168,28 @@
 t time%0A%0A
+lang = 'en'%0A
 baseurl 
@@ -190,17 +190,17 @@
 seurl = 
-%22
+'
 http://w
@@ -247,18 +247,18 @@
 et?lang=
-fi
+%25s
 &LOTUS_h
@@ -307,17 +307,17 @@
 _hae=Hae
-%22
+'
 %0A%0A%0Adef c
@@ -623,21 +623,29 @@
 aseurl %25
+ (lang,
  code
+)
 %0A    r =
@@ -989,16 +989,45 @@
 .string%0A
+        if lang == 'fi':%0A    
         
@@ -1081,16 +1081,175 @@
 &nbsp;%22)
+  # finnish%0A        elif lang == 'en':%0A            date = time.strptime(date, %22%25d.%25m.%25Y, %25H:%25M&nbsp;%22)  # english%0A        else:%0A            date = time.time()%0A
 %0A       
@@ -1708,24 +1708,174 @@
 %25 minutes)%0A%0A
+        if lang == 'fi':%0A            res.append(%22%25s - %25s - %25s%22 %25 (%22 %22.join(agestr) + %22 sitten%22, statustext, location))%0A        elif lang == 'en':%0A    
         res.
@@ -1950,16 +1950,17 @@
 tion))%0A%0A
+%0A
     if c

Make language configurable