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

from datetime import date
from datetime import timedelta
from optparse import OptionParser
from feedcircuit.feedcircuit import Feed
from feedcircuit.feedcircuit import Item
import locale
import os
import sys

try:
    import json
except ImportError:
    import simplejson as json


#locale.setlocale(locale.LC_ALL, '')
report_progress = True

context = None

def json_callback(item):
    json.dump((item, context), sys.stdout)
    sys.stdout.write("\n")
    sys.stdout.flush()


def handle_stdin():
    global context
    try:
        work_items = json.load(sys.stdin)
        if work_items:
            for kwargs, ctx in work_items:
                context = ctx
                kwargs["callback"] = json_callback
                feed = Feed(**kwargs)
                feed.update()
    except BaseException:
        pass #we were interrupted


def update_callback(item):
    if report_progress and item[0]:
        print item[0], item[1]

if len(sys.argv) > 1 and sys.argv[1] == "-":
    handle_stdin()
    sys.exit()

parser = OptionParser("usage: %prog [options] url1 url2 ...")
parser.add_option("-p", "--path", help = "path to store feed", dest = "path", default = "")
parser.add_option("-s", "--single-page", help = "transform single html page", \
    action = "store_true", dest = "single", default = False)
parser.add_option("-c", "--cache", help = "cache items", \
    action = "store_true", dest = "cache", default = False)
parser.add_option("-i", "--inline", help = "inline items", \
    action = "store_true", dest = "inline", default = False)
parser.add_option("-q", "--quiet", help = "do not report any progress to the stdout", \
    action = "store_false", dest = "report_progress", default = True)
parser.add_option("-d", "--days-to-keep", help = "days to keep news", \
    type = "int", dest = "days_to_keep")
parser.add_option("-a", "--allow-scripts", help = "do not remove scripts from pages", \
    action = "store_true", dest = "allow_scripts", default = False)
parser.add_option("-n", "--new-window", help = "open articles in a new window", \
    action = "store_true", dest = "new_window", default = False)
parser.add_option("--include", help = "regexp for urls to include to the feed", dest = "include")
parser.add_option("--exclude", help = "regexp for urls to exxlude from the feed", dest = "exclude")
parser.add_option("-r", "--reformatter-url", help = "reformatting service url", dest = "reformatter_url")

parser.add_option("--ignore-print-version", help = "ignore link to the printable version", \
    action = "store_true", dest = "ignore_print_version", default = False)
parser.add_option("--print-version-keyword", help = "link to the printable version", dest = "print_version_keyword")
parser.add_option("--ignore-single-page-version", help = "ignore link to the single page version", \
    action = "store_true", dest = "ignore_single_page_version", default = False)
parser.add_option("--single-page-version-keyword", help = "link to the single page version", dest = "single_page_version_keyword")
parser.add_option("--next-page-keyword", help = "link to the next page", dest = "next_page_keyword")
parser.add_option("--content-tag", help = "html element which contains article text", dest = "content_tag")
parser.add_option("--ignore-images", help = "strip images from the feed", \
    action = "store_true", dest = "ignore_images", default = False)
(options, args) = parser.parse_args()

if len(args) == 0:
    print "please specify at least one feed url"
else:
    report_progress = options.report_progress

    kwargs = {
        "path": options.path,
        "cache_path": ".cache",
        "callback": update_callback,
        "allow_scripts": options.allow_scripts,
        "reformatter_url": options.reformatter_url
        }

    if options.ignore_print_version:
        kwargs["print_version_keywords"] = None
    elif options.print_version_keyword:
        kwargs["print_version_keywords"] = [options.print_version_keyword.decode(locale.getpreferredencoding())]

    if options.ignore_single_page_version:
        kwargs["single_page_keywords"] = None
    elif options.single_page_version_keyword:
        kwargs["single_page_keywords"] = [options.single_page_version_keyword.decode(locale.getpreferredencoding())]

    if options.next_page_keyword:
        kwargs["next_page_keywords"] = [options.next_page_keyword.decode(locale.getpreferredencoding())]

    kwargs["ignore_images"] = options.ignore_images

    if options.content_tag:
        ct = {}
        parts = options.content_tag.split(" ")
        if len(parts):
            ct["name"] = parts[0]
            ct["attrs"] = {}
            for part in parts[1:]:
                name_val = part.split("=")
                if len(name_val) == 2:
                    ct["attrs"][name_val[0]] = \
                        name_val[1].replace('"', "").replace("&quot;", '"')
            kwargs["content_kwargs"] = ct
        

    if options.single:
        for arg in args:
            item = Item(arg, **kwargs)
            item.process()
            item.save(filename = item.title + ".html")
    else:
        kwargs["inline_items"] = options.inline
        kwargs["cache_items"] = options.cache
        kwargs["include"] = options.include
        kwargs["exclude"] = options.exclude
        kwargs["new_window"] = options.new_window
        if options.days_to_keep:
            kwargs["days_to_keep"] = options.days_to_keep
        for arg in args:
            feed = Feed(arg, **kwargs)
            feed.update()

