# -*- coding: utf-8 -*-

import lxml.html
import re


class Vastavirta(object):
    def __init__(self):
        self.data = None

        self.url = "http://vastavirta.net/fi/"
        self.name = "Vastavirta"
        self.city = "Tampere"
        self.country = "Finland"
        self.parseddata = None
        self.monthmap = {   \
                "Tam" : 1,  \
                "Hel" : 2,  \
                "Maa" : 3,  \
                "Huh" : 4,  \
                "Tou" : 5,  \
                "Kes" : 6,  \
                "Hei" : 7,  \
                "Elo" : 8,  \
                "Syy" : 9,  \
                "Lok" : 10, \
                "Mar" : 11, \
                "Jou" : 12 }

        # Parsing patterns
        self.monetarypattern = re.compile("[0-9.,]+ e")

    def getVenueName(self):
        return self.name

    def getCity(self):
        return self.city

    def getCountry(self):
        return self.country

    def eventSQLentity(self):
        return { u"name" : self.name, \
                 u"city" : self.city, \
                 u"country" : self.country }

    def parsePrice(self, tag):
        parsedprice = tag.xpath('./*/div[@class="event-details"]/*/text()')
        prices = " ".join(parsedprice).replace(u"€", u"e")
        regexprice = re.findall(self.monetarypattern, prices)

        if regexprice:
            return u"%s €" % (" ".join(regexprice).replace(" e", ""))
        else:
            return u"0 €"

    def parseDate(self, tag):
        day = tag.xpath('./*/div[@class="start-date"]/div[@class="event-day"]/text()')
        m = tag.xpath('./*/div[@class="start-date"]/div[@class="event-month"]/text()')
        year = tag.xpath('./*/div[@class="start-date"]/div[@class="event-year"]/text()')

        day = " ".join(day)
        month = " ".join(m)
        year = " ".join(year)

        if day is "" or month is "" or year is "":
            return ""

        month = self.monthmap[month]
        return "%.4d-%.2d-%.2d" % (int(year), int(month), int(day))

    def parseEvent(self, event):
        date = self.parseDate(event)
        etitle = " ".join(event.xpath('./*/div[@class="event-title"]/*/text()'))
        prices = self.parsePrice(event)

        return { u"venue" : self.getVenueName(), \
                 u"date" : date,                 \
                 u"name" : etitle,               \
                 u"price" : prices }

    def parseEvents(self, data):
        doc = lxml.html.fromstring(data).getroottree().getroot()
        tags = doc.xpath('//div[@class="event-list"]/ul' \
                       + '[@class="event-list-view"]'    \
                       + '/li[@class="event "]')
        tmp = u""

        for event in reversed(tags):
            parsed = self.parseEvent(event)

            if parsed["date"] == "":
                tmp += ", " + parsed["name"]
            else:
                parsed ["name"] += tmp
                yield parsed
                tmp = u""

if __name__ == '__main__':
    import requests

    v = Vastavirta()
    r = requests.get(v.url)

    #with open("venues/vastavirta.html") as f:
    #    r = f.read()

    for event in v.parseEvents(r.text):
        print event.values()


@@ -691,92 +691,8 @@
  %7D%0A%0A
-        # Parsing patterns%0A        self.monetarypattern = re.compile(%22%5B0-9.,%5D+ e%22)%0A%0A
     
@@ -1059,33 +1059,32 @@
  = tag.xpath('./
-*
 /div%5B@class=%22eve
@@ -1149,113 +1149,27 @@
 ice)
-.replace(u%22%E2%82%AC%22, u%22e%22)%0A        regexprice = re.findall(self.monetarypattern, prices)%0A%0A        if regexprice
+%0A%0A        if prices
 :%0A  
@@ -1193,53 +1193,19 @@
 u%22%25s
- %E2%82%AC
 %22 %25 (
-%22 %22.join(regexprice).replace(%22 e%22, %22%22)
+prices
 )%0A  
@@ -1242,10 +1242,8 @@
  u%220
- %E2%82%AC
 %22%0A%0A 
@@ -2966,26 +2966,89 @@
     
-pr
+for k, v 
 in
-t
  event.
-values()
+iteritems():%0A            print %22%25-10s: %25s%22 %25 (k, v)%0A        print
 %0A%0A

Fix monetary parsing and put printing in line with other plugins.