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

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import os
import sys
import codecs

from slugify import slugify
from time import strptime, strftime

from html2text import html2text

class MarkdownPipeline(object):
    """Scrapy pipeline to save reviews as markdown document"""

    def parse_datetime(self, str_datetime):
        """Parse date string in russian"""
        dictionary = {u"января": 'Jan', u"февраля": 'Feb', u"марта": 'Mar',
            u"апреля": 'Apr', u"мая": 'May', u"июня": 'Jun', u"июля": 'Jul',
            u"августа": 'Aug', u"сентября": 'Sep', u"октября": 'Oct',
            u"ноября": 'Nov', u"декабря": 'Dec'}

        for russian, english in dictionary.items():
            str_datetime = str_datetime.replace(russian, english)

        return strptime(str_datetime, '%d %b %Y %H:%M')

    def fix_typography(self, s):
        """Fix typographic symbols"""
        s = s.replace(u'\x97', u'\u2014') # Fix dashes
        s = s.replace(u'\x85', u'\u2026') # Fix ellipsis
        return s

    def process_item(self, item, spider):
        """Process and save review item"""
        settings = spider.settings
        if not os.path.exists(settings['MARKDOWN_OUTPUT']):
            os.mkdir(settings['MARKDOWN_OUTPUT'])

        file_name = strftime('%Y-%m-%d-', self.parse_datetime(item['review_datetime'][0])) + slugify(item['movie_title'][0]) + '.md'

        try:
            output_file = codecs.open(settings['MARKDOWN_OUTPUT'] + '/' + file_name, 'w', 'utf-8')
        except IOError:
            print 'Error opening target file: %s' % output_file
            sys.exit(1)

        if len(item['review_title']) > 0:
            title = item['review_title'][0]
        else:
            title = item['movie_title'][0]

        title = self.fix_typography(title)

        output_file.write("%s\n" % title)
        output_file.write("%s\n\n" % ('=' * len(title)))

        output_file.write("* **User Id:** %s\n" % item['user_id'])
        output_file.write("* **Movie Title:** %s\n" % item['movie_title'][0])
        output_file.write("* **Movie Original Title:** %s\n" % item['movie_original_title'][0])
        output_file.write("* **Movie Link:** [{0}]({0})\n".format(item['movie_link'][0]))
        output_file.write("* **Review Date:** %s\n" % item['review_datetime'][0])
        output_file.write("* **Review Grade:** %s\n" % item['review_grade'][0])
        output_file.write("* **Review Link:** [{0}]({0})\n".format(item['review_link']))

        output_file.write("\n")

        review_text = html2text(item['review_text'])
        review_text = self.fix_typography(review_text)
        output_file.write(review_text)

        output_file.close()

        return item
