from datetime import datetime

from bottle import abort, default_app, response, route, run
from rss2producer import RSS2Feed
import dataset

from settings import TABLE, PAGES


# copy pasted from scraper
LIMIT = 50


slug_to_name = {slug: name for slug, pk, name in PAGES}
slug_to_id = {slug: pk for slug, pk, name in PAGES}


@route('/')
def index():
    out = []
    out.append('<li><a href="{}/">{}</a></li>'.format('all', 'All'))
    for slug, pk, name in PAGES:
        out.append('<li><a href="{}/">{}</a></li>'.format(slug, name))
    return u''.join(out)


@route('/<slug>/')
def feed_detail(slug):
    if slug != 'all' and slug not in slug_to_name:
        abort(404, 'No results for that Board or Commission')
    db = dataset.connect()  # uses DATABASE_URL
    # table = db.load_table(TABLE)  # don't allow dataset to create the table
    if slug == 'all':
        feed_info = dict(
            title='Boards and Commissions',
            link='http://www.austintexas.gov/cityclerk/boards_commissions/',
            description='Feed of Boards and Commissions activity',
        )
    else:
        feed_info = dict(
            title=slug_to_name[slug],
            link=('http://www.austintexas.gov/cityclerk/boards_commissions/'
                'meetings/{}_1.htm'.format(slug_to_id[slug])),
            description='Feed of {} activity'.format(slug_to_name[slug]),
        )
    feed = RSS2Feed(**feed_info)
    filter_kwargs = {}
    if slug != 'all':
        filter_kwargs['bandc'] = slug
    # results = table.find(_limit=LIMIT, order_by='-date', **filter_kwargs)
    # HACK for 'order_by' not working
    sql = 'SELECT * from {} ORDER BY date DESC LIMIT {}'.format(
        TABLE,
        LIMIT,
    )
    results = db.query(sql)
    import re
    for row in results:
        title = row['title'] or row['type']
        if slug == 'all':
            title = '[{}] {}'.format(row['bandc'], title)
        text = row['text'].strip().encode('ascii', 'ignore')[:600].strip()
        text = re.sub(r'\s+', ' ', text)[:500]
        if text.startswith('no text'):
            text = '{}: {}'.format(row['type'], text)
        feed.append_item(
            title=title,
            link=row['url'],
            description=text if text else row['type'],
            # convert date into datetime
            pub_date=datetime.combine(row['date'], datetime.min.time()),
        )
    response.content_type = 'application/rss+xml'
    return feed.get_xml()


app = default_app()


if __name__ == '__main__':
    run(host='localhost', port=8000, reloader=True)

@@ -1419,16 +1419,18 @@
 nfo)%0A   
+ #
  filter_
@@ -1441,16 +1441,35 @@
 gs = %7B%7D%0A
+    where_sql = ''%0A
     if s
@@ -1489,16 +1489,72 @@
 %0A       
+ where_sql = %22WHERE bandc = '%7B%7D'%22.format(slug)%0A        #
  filter_
@@ -1717,16 +1717,19 @@
  from %7B%7D
+ %7B%7D
  ORDER B
@@ -1773,16 +1773,35 @@
  TABLE,%0A
+        where_sql,%0A
         

fix slug wasn't used to filter down