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

from flask import Flask, render_template, request, redirect, url_for
import os
import arrow
import config
import re
import requests
import twitter_rss

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.tpl')

@app.route('/user', methods=['POST'])
def handle_user():
    feed = request.form['user_data']
    path = request.path
    return redirect(url_for('feed_to_xml', feed=feed, path=path))

@app.route('/htag', methods=['POST'])
def handle_htag():
    feed = request.form['user_data']
    path = request.path
    return redirect(url_for('feed_to_xml', feed=feed, path=path))


@app.route('/<path>/<feed>.xml')
def feed_to_xml(feed, path):
    if check_cache_timeout(feed, path) is False:
        error = serve_from_cache(feed, path)
    else:
        try:
            if path == 'user':
                tweets = twitter_rss.UserTweetGetter(feed)
            elif path == 'htag':
                tweets = twitter_rss.HashtagTweetGetter(feed)
            error = tweets.to_rss().encode('utf-8')
        except AttributeError:
            err = 'User not found'
            error = render_template('index.tpl', err=err)
        write_data_to_file(tweets, feed, path)
        save_feed_for_updating(tweets, feed, path)
    return error

def check_cache_timeout(feed, path):
    xml_file = config.XML_DIR + path + '/' + feed + '.xml'
    time_now = arrow.utcnow()
    if os.path.isfile(xml_file):
        time_modified_file = arrow.get(os.path.getmtime(xml_file)).replace(minutes=+config.CACHE)
        print('{0} seconds left before next updating'.format((time_modified_file - time_now).seconds))
        result = time_now > time_modified_file
    else:
        result = True
    return result

def serve_from_cache(feed, path):
    error = ''
    try:
        with open(config.XML_DIR + path + '/' + feed + '.xml') as tweets:
            tweets = tweets.read()
            if not tweets:
                err = 'Cache is empty'
                error = render_template('index.tpl', err=err)
            else:
                print('Serving feed from cache')
                error = tweets
    except IOError:
        print('Could not write the feed cache')
    return error
           
def write_data_to_file(tweets, feed, path):
    err=None
    try:
        data = tweets.to_rss().encode('utf-8')
        print 'File does not exist: Creating feed...'
        with open(config.XML_DIR + path + '/' + feed + '.xml', 'w') as cache:
            cache.write(data)
        error = tweets.to_rss()
    except IOError:
        err = 'File could not be written'
    except AttributeError:
        err = 'User not found'
    error = render_template('index.tpl', err=err)
    return error

def save_feed_for_updating(tweets, feed, path):
    try:
        with open(config.XML_DIR + path + '/' + path + '.txt', 'a') as save:
            save.write(feed + '\n')
    except IOError:
        print 'Could not save ' + feed + ' in ' + path


@app.errorhandler(404)
def page_not_found(code):
    err = 'User not found'
    error = render_template('index.tpl', err=err)
    return error


if __name__ == "__main__":
    app.run(debug=True)
    # app.run(host='0.0.0.0')
    # app.run()
