#!/usr/bin/python
# -*- coding: UTF-8 -*-
#
# Copyright (c) 2007 Tarek Ziadé
#
# Authors:
#   Tarek Ziadé <tarek@ziade.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
# $Id: tables.py 1518 2007-05-21 12:35:44Z rage $
""" Feed mapping
"""
from datetime import datetime
from time import strftime
from sqlalchemy import *
from sqlalchemy.exceptions import *

_connector = None
TIME_FMT = '%Y-%m-%d %H:%M:%S'

class Entries(object):

    def __init__(self, sqluri):
        self._mapper = self._getEntriesMapper(sqluri)
        try:
            self._mapper.create()
        except SQLError:
            pass # already there

    def getConnector(self):
        return _connector

    def _getEntriesMapper(self, sqluri):
        global _connector
        _connector = create_engine(sqluri)
        _metadata = BoundMetaData(_connector)

        return Table('entries', _metadata,
                     Column('id', Integer, primary_key=True),
                     Column('url', String(200)),
                     Column('title', String(300)),
                     Column('content', String(500)),
                     Column('date', DateTime),
                     Column('creation_date', DateTime))

    def insert_entry(self, url, title, content, date=strftime(TIME_FMT),
                     creation_date=strftime(TIME_FMT)):
        """inserts entry"""
        inserter = self._mapper.insert()
        inserter.execute(url=url, title=title, content=content, date=date,
                         creation_date=creation_date)

    def insert_entries(self, entries):
        """insert entries"""
        for entry in entries:
            if 'url' in entry:
                url = entry['url']
            else:
                url = entry['link']

            if 'creation_date' not in entry:
                entry['creation_date'] = strftime(TIME_FMT)

            content = entry['content']

            if 'date' in entry.keys():
                entry_date = entry['date']
            else:
                entry_date = strftime(TIME_FMT)

            cuts = ('GMT', '+')

            for cut in cuts:
                if entry_date.find(cut) != -1:
                    entry_date = entry_date[:entry_date.find(cut)].strip()

            if isinstance(entry_date, basestring):
                fmts = ('%c', '%Y-%m-%dT%H:%M:%S+00:00',
                        '%a, %d %b %Y %H:%M:%S')
                converted = False
                for fmt in fmts:
                    try:
                        entry_date = datetime.strptime(entry_date, fmt)
                        entry_date = strftime(TIME_FMT, entry_date.timetuple())
                    except ValueError:
                        pass
                    else:
                        converted = True
                        break

            self.insert_entry(url, entry['title'], content,
                              entry_date, entry['creation_date'])


    def select_entries(self, *args):
        """select entries"""
        return self._mapper.select().execute(*args).fetchall()


