root / atomisator / entries.py

Revision 102:917b0f7d3576, 3.6 kB (checked in by Tarek Ziad?? <tarek@…>, 15 months ago)

fixes

Line 
1#!/usr/bin/python
2# -*- coding: UTF-8 -*-
3#
4# Copyright (c) 2007 Tarek Ziadé
5#
6# Authors:
7#   Tarek Ziadé <tarek@ziade.org>
8#
9# This program is free software; you can redistribute it and/or
10# modify it under the terms of the GNU General Public License
11# as published by the Free Software Foundation; either version 2
12# of the License, or (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program; if not, write to the Free Software
21# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22# $Id: tables.py 1518 2007-05-21 12:35:44Z rage $
23""" Feed mapping
24"""
25from datetime import datetime
26from time import strftime
27from sqlalchemy import *
28from sqlalchemy.exceptions import *
29
30_connector = None
31TIME_FMT = '%Y-%m-%d %H:%M:%S'
32
33class Entries(object):
34
35    def __init__(self, sqluri):
36        self._mapper = self._getEntriesMapper(sqluri)
37        try:
38            self._mapper.create()
39        except SQLError:
40            pass # already there
41
42    def getConnector(self):
43        return _connector
44
45    def _getEntriesMapper(self, sqluri):
46        global _connector
47        _connector = create_engine(sqluri)
48        _metadata = BoundMetaData(_connector)
49
50        return Table('entries', _metadata,
51                     Column('id', Integer, primary_key=True),
52                     Column('url', String(200)),
53                     Column('title', String(300)),
54                     Column('content', String(500)),
55                     Column('date', DateTime),
56                     Column('creation_date', DateTime))
57
58    def insert_entry(self, url, title, content, date=strftime(TIME_FMT),
59                     creation_date=strftime(TIME_FMT)):
60        """inserts entry"""
61        inserter = self._mapper.insert()
62        inserter.execute(url=url, title=title, content=content, date=date,
63                         creation_date=creation_date)
64
65    def insert_entries(self, entries):
66        """insert entries"""
67        for entry in entries:
68            if 'url' in entry:
69                url = entry['url']
70            else:
71                url = entry['link']
72
73            if 'creation_date' not in entry:
74                entry['creation_date'] = strftime(TIME_FMT)
75
76            content = entry['content']
77
78            if 'date' in entry.keys():
79                entry_date = entry['date']
80            else:
81                entry_date = strftime(TIME_FMT)
82
83            cuts = ('GMT', '+')
84
85            for cut in cuts:
86                if entry_date.find(cut) != -1:
87                    entry_date = entry_date[:entry_date.find(cut)].strip()
88
89            if isinstance(entry_date, basestring):
90                fmts = ('%c', '%Y-%m-%dT%H:%M:%S+00:00',
91                        '%a, %d %b %Y %H:%M:%S')
92                converted = False
93                for fmt in fmts:
94                    try:
95                        entry_date = datetime.strptime(entry_date, fmt)
96                        entry_date = strftime(TIME_FMT, entry_date.timetuple())
97                    except ValueError:
98                        pass
99                    else:
100                        converted = True
101                        break
102
103            self.insert_entry(url, entry['title'], content,
104                              entry_date, entry['creation_date'])
105
106
107    def select_entries(self, *args):
108        """select entries"""
109        return self._mapper.select().execute(*args).fetchall()
110
Note: See TracBrowser for help on using the browser.