Atomstrom/models/feedinfo.py

73 lines
2.7 KiB
Python

# This file is part of Atomstrom
# Copyright (C) 2013 Ronald Schaten <ronald@schatenseite.de>
#
# Atomstrom 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 3 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, see <http://www.gnu.org/licenses/>.
from sqlalchemy import Column, Integer, ForeignKey, String, DateTime
from datetime import datetime, timedelta
from random import randint
from models import Base
class Feedinfo(Base):
__tablename__ = 'feedinfo'
id = Column(Integer, primary_key=True)
feed_id = Column(Integer, ForeignKey('feed.id'))
title = Column(String(255))
link = Column(String(255))
subtitle = Column(String(255))
author = Column(String(255))
publisher = Column(String(255))
status = Column(Integer)
version = Column(String(16))
encoding = Column(String(16))
bozo = Column(Integer)
lastfetched = Column(DateTime)
lastsuccessful = Column(DateTime)
nextfetch = Column(DateTime)
def __init__(self, parser):
self.update(parser)
def __repr__(self):
return "<Feedinfo('%s','%s','%s')>" % (self.title, self.subtitle, self.author)
def update(self, parser):
if parser.feed.has_key('title'):
self.title = parser.feed.get('title')
if parser.feed.has_key('link'):
self.link = parser.feed.get('link')
if parser.feed.has_key('subtitle'):
self.subtitle = parser.feed.get('subtitle')
if parser.feed.has_key('author'):
self.author = parser.feed.get('author')
if parser.feed.has_key('publisher'):
self.author = parser.feed.get('publisher')
self.status = parser.get('status')
self.version = parser.get('version')
self.encoding = parser.get('encoding')
self.bozo = parser.get('bozo')
self.lastfetched = datetime.now()
if parser.get('status') == 200 or parser.get('status') == 302:
self.lastsuccessful = datetime.now()
if self.feed:
waitminutes = randint(int(self.feed.frequency * .75), int(self.feed.frequency * 1.25))
else:
waitminutes = 0
self.nextfetch = (datetime.now() + timedelta(minutes=waitminutes))
# -*- coding: utf-8 -*-