#!/usr/bin/env python
# coding: utf-8

import sys
import os.path
import cyclone.web
from twisted.python import log
from twisted.internet import reactor,defer

from feed import FeedManager

fm = FeedManager()

## notera hur både RSS och ATOM parsas 
fm.addFeed("http://svt.se/rss/nyheter/inrikes/")
fm.addFeed("http://www.svd.se/?service=rss&amp;type=senastenytt")
fm.addFeed("http://www.schneier.com/blog/index.xml")

# Twitters API kan användas med atom
fm.addFeed("http://search.twitter.com/search.atom?q=%23HTML5")


class IndexHandler(cyclone.web.RequestHandler):

    @defer.inlineCallbacks
    @cyclone.web.asynchronous
    def get(self):
        feed = fm.feeds[0]
        self.render("index.html",feeds=fm.feeds,images=fm.getImages())
        self.finish()





class AddFeedHandler(cyclone.web.RequestHandler):
    def get(self):
        # cast from unicode
        fm.addFeed(str(self.get_argument("url")))

class Application(cyclone.web.Application):
    def __init__(self):
        handlers = [
            (r"/", IndexHandler),
            (r"/add",AddFeedHandler)
            ]

        settings = {
            "static_path": "./static",
            "template_path": "./templates"
            }

        cyclone.web.Application.__init__(self, handlers, **settings)

if __name__ == "__main__":
    log.startLogging(sys.stdout)
    reactor.listenTCP(3042, Application())
    reactor.run()
