#!/usr/bin/python

import BaseHTTPServer
import SocketServer
import base64

import os
import time

class PageFeederRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_404(self):
        self.send_response(404, "Not Found")
        self.end_headers()
        self.wfile.write("%s is bogus" % self.path)

    def do_GET(self):
        if self.path == "/next":
            self.send_response(200, "OK")
            self.end_headers()
            self.wfile.write(self.next_url())
            return
        self.do_404()

    def do_POST(self):
        # print "PATH:", self.path
        if self.path == "/push_url":
            return self.do_POST_push_url()
        self.do_404()

    def next_url(self):
        if not self.server.urls:
            return "javascript:alert('nothing left')"
        return self.server.urls.pop(0)

    # test with:
    # echo http://www.thok.org/ | curl -d@- http://localhost:3382/url_save_all
    # curl -H'url: http://www.thok.org/bloggery' -d http://localhost:3382/push_url
    # curl http://localhost:3383/next
    def do_POST_push_url(self):
        # get it from the content, or a header?
        newurl = self.headers.getheader("url")
        # later, maybe, get the description
        self.send_response(200, "OK")
        self.end_headers()
        self.server.urls.append(newurl)

    def log_request(self, code=None, size=None):
        # this is just to make the default per-requst logs shut up
        pass
        

# the original just uses list append/pop(0).
# now, persist it.

class PersistList:
    def __init__(self, path):
        self.path = path
        if not os.path.isdir(self.path):
            os.mkdir(self.path)

    def pop(self, n):
        fetch = os.listdir(self.path)
        fetch.sort()
        retname = fetch.pop(n)
        retpath = os.path.join(self.path, retname)
        retval = open(retpath,"r").read()
        os.unlink(retpath)
        return retval

    def append(self, v):
        open(os.path.join(self.path, 'PL%s' % time.time()), "w").write(v)

    def __nonzero__(self):
        # we don't actually do any filtering...
        return len(os.listdir(self.path)) > 0

class ThreadingHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
    def setup_feed(self):
        #self.urls = []
        self.urls = PersistList(os.path.expanduser("~/.page_feeder_stashdir"))

import sys,os
if __name__ == "__main__":
    server = ThreadingHTTPServer(("localhost", 3383), PageFeederRequestHandler)
    server.setup_feed()
    server.serve_forever()