#!/usr/bin/python

# Vote history converter, from the Filmweb format to the Criticker format.
# Usage: ./newfilmweb2criticker.py <filmweb-user-url>
#
# Tip: if you have the vote history HTML page stored locally, the file:// type URL will work too.
#
# (C)2009 Maciej Zok (on the basis filmweb2criticker.py by Lukasz Bolikowski)

import datetime
import re
import sys
import urllib

def fetch_votes_from_filmweb(filmwebUrl):
	filmwebUserId = urllib.urlopen(filmwebUrl).read()
	filmwebVotePage = 'http://www.filmweb.pl/splitString/user/'
	for line in filmwebUserId.split('\n'):
		line = line.strip()
		firstLine = re.match('[^>]*/UserFavoritePersons\?id=(\d+)[^>]*', line)
		if firstLine <> None:
			userId = firstLine.group(1).strip()
			filmwebVotePage += userId + '/filmVotes'
			continue
	filmwebVH = urllib.urlopen(filmwebVotePage).read()
	return filmwebVH

def parse_filmweb_votes(filmwebVoteHistory):
	cvh = '<recentrankings>\n'
	for line in filmwebVoteHistory.split('\\a'):
		line = line.strip()
		firstLine = re.match('(\d+)\\\c([^>]+)\\\c([^>]+)\\\c(\d{4})\\\c(\d)\\\c(\d{1,2})\\\c([^>]+)\\\c(\d{1,3}|[\\\e\d{1,3}]+)\\\c(\d{1,2}|[\\\e\d{1,2}]+)\\\c(\d{8}|)', line)
		if firstLine <> None:
			filmwebId = firstLine.group(1).strip()
			oTitle = firstLine.group(2).strip()
#			plTitle = firstLine.group(3).strip()
			year = firstLine.group(4).strip()
#			favourite = firstLine.group(5).strip()
			vote = firstLine.group(6).strip()
			img = firstLine.group(7).strip()
#			country = firstLine.group(8).strip()
#			genre = firstLine.group(9).strip()
			date = firstLine.group(10).strip()
			cvh += '  <film>\n'
			cvh += '    <filmid>' + filmwebId + '</filmid>\n'
			cvh += '    <filmname>' + oTitle + " " + year + '</filmname>\n'
			cvh += '    <img>http://gfx.filmweb.pl/po' + img + '</img>\n'
			cvh += '    <score>' + vote + '</score>\n'
			cvh += '    <quote/>\n'
			cvh += '    <reviewdate>' + date + '</reviewdate>\n'
			cvh += '    <tier>' + vote + '</tier>\n'
			cvh += '  </film>\n'
			continue
	cvh += '</recentrankings>\n'
	return cvh


if len(sys.argv) == 1:
	print "Usage %s <filmweb-vote-history-url>" % sys.argv[0]
	sys.exit(1)
filmwebUrl = sys.argv[1]
try:
	filmwebVoteHistory = fetch_votes_from_filmweb(filmwebUrl)
	critickerVoteHistory = parse_filmweb_votes(filmwebVoteHistory)
	print critickerVoteHistory,
except IOError:
	print "Could not get %s" % filmwebUrl
	sys.exit(2)


