From: Mart Lubbers Date: Mon, 5 May 2014 11:44:11 +0000 (+0200) Subject: started with frontend X-Git-Url: https://git.martlubbers.net/?a=commitdiff_plain;h=4767b78927e677118e2d768ba5f7e8f326471c9d;p=bsc-thesis1415.git started with frontend --- diff --git a/program/hypfront/feed2html.py b/program/hypfront/feed2html.py new file mode 100644 index 0000000..436bdc1 --- /dev/null +++ b/program/hypfront/feed2html.py @@ -0,0 +1,278 @@ +#!/usr/bin/env python +import sys +import feedparser + +import getopt +import os +import re +import time + +DEFAULT_TEMPLATE = """\ + + + + " /> + <tpl:feed value="title" filter="xml" /> + + +
+

">

+
+ +
+

">

+
+

Posted by on

+
+
+
+
+ + +""" + +class Template: + """Generic template library. Initially implemented in bloglines2html.py""" + + re_parse = re.compile(r'|>(.*?))', + re.U | re.S) + re_parse_attr = re.compile(r'([a-z]+)\s*=\s*"([^"]*)"', re.U|re.I) + + # Default Date/Time format + datetime_format = '%Y-%m-%d %H:%M:%S' + + def __init__(self): + self.var = {} + + def format_datetime(self, data, format): + return time.strftime(format, data) + + def get_charset(self): + return 'utf-8' + + def parse(self, text): + return self.re_parse.sub(self.process_tag, text) + + def parse_attr(self, text): + return dict(self.re_parse_attr.findall(text.strip())) + + def process_tag(self, match): + tag = match.group(1) + attr = self.parse_attr(match.group(2)) + data = match.group(3) or u'' + + try: + handler = getattr(self, 'tag_%s' % tag) + except AttributeError: + return '' + else: + result = handler(attr, data) + if result is None: + result = '' + elif (isinstance(result, tuple) or + isinstance(result, time.struct_time)) and len(result) == 9: + result = self.format_datetime(result, + attr.get('format', self.datetime_format)) + + if 'filter' in attr: + result = do_filters(result, attr['filter']) + + if isinstance(result, unicode): + result = result.encode(self.get_charset()) + else: + result = str(result) + + return result + + def tag_charset(self, attr, data): + return self.get_charset() + + def tag_var(self, attr, data): + # Handle + val = self.var.get(attr.get('name'), attr.get('default', '')) + if isinstance(val, unicode): + val = val.encode(self.get_charset()) + return val + + +class RSSTemplate(Template): + """Template with tag handlers for feed data.""" + + re_range = re.compile(r'([\-\d]+)?(:([\-\d]+)?(:([\-\d]+)?)?)?') + + def __init__(self, feed): + Template.__init__(self) + self.feed = feed + self.content = None + self.contributors = None + self.enclosures = None + self.entries = None + + def get_charset(self): + return self.feed.encoding + + def get_loop(self, obj, objattr, attr, data): + def range_value(val): + if val is None: + return val + else: + return int(val) + + if obj: + items = obj.get(objattr) + if items: + result = [] + if 'select' in attr: + match = self.re_range.match(attr['select']) + if match: + # Only a single item is selected. + match = [range_value(x) + for x in list(match.group(1, 3, 5))] + items = items[slice(*match)] + + for item in items: + setattr(self, objattr, item) + result.append(self.parse(data)) + + setattr(self, objattr, None) + return ''.join(result) + + return '' + + def get_value(self, obj, attr, data): + if (obj is not None) and ('value' in attr): + key = attr['value'] + detail = attr.get('detail') + value = None + + # Check whether it is requesting a datetime field. + if (key+'_parsed') in obj: + return obj.get(key+'_parsed') + + # Check whether it is trying to get details. + if detail: + obj2 = attr.get('%s_detail' % key) + if obj2: + value = obj2.get(detail) + else: + value = obj.get(key) + + if isinstance(value, basestring): + return value + + return '' + + def tag_content(self, attr, data): + return self.get_value(self.content, attr, data) + + def tag_contents(self, attr, data): + return self.get_loop(self.entries, 'content', attr, data) + + def tag_contributor(self, attr, data): + return self.get_value(self.contributors, attr, data) + + def tag_contributors(self, attr, data): + return self.get_loop(self.entries, 'contributors', attr, data) + + def tag_enclosure(self, attr, data): + return self.get_value(self.enclosures, attr, data) + + def tag_enclosures(self, attr, data): + return self.get_loop(self.entries, 'enclosures', attr, data) + + def tag_entries(self, attr, data): + return self.get_loop(self.feed, 'entries', attr, data) + + def tag_entry(self, attr, data): + return self.get_value(self.entries, attr, data) + + def tag_feed(self, attr, data): + return self.get_value(self.feed.feed, attr, data) + + def tag_if_not(self, attr, data): + # Handle ... + try: + handler = getattr(self, 'tag_%s' % attr['tag']) + except (AttributeError, KeyError): + pass + else: + val = handler(attr, data) + match = attr.get('match') + + if ((not match) and (not val)) or (match and (val != match)): + return self.parse(data) + + return self.parse(data) + + def tag_if(self, attr, data): + # Handle ... + try: + handler = getattr(self, 'tag_%s' % attr['tag']) + except (AttributeError, KeyError): + pass + else: + val = handler(attr, data) + match = attr.get('match') + + if ((not match) and val) or (match and (val == match)): + return self.parse(data) + + return '' + + def tag_now(self, attr, data): + # Handle + return time.localtime() + + +def do_filters(data, filters): + for f in filters.split(','): + f = f.strip() + + # Try to find the filter using the global name space. + try: + f = globals()['filter_%s' % f] + except KeyError: + pass + else: + data = f(data) + + return data + + +def filter_basename(data): + """Return the basename of the filename.""" + idx = data.rfind('/') + if idx < 0: + idx = data.rfind('\\') + if idx < 0: + return data + + return data[idx+1:] + + +def filter_reducespace(data): + return re.sub(r'[\t\r\n\s]+', ' ', data).strip() + + +def filter_xml(data): + """Escape the XML entities.""" + data = data.replace('&', '&') + data = data.replace('<', '<') + data = data.replace('>', '>') + return data + + +def filter_xmlq(data): + """Escape the XML entities + quote, useful for XML attributes.""" + data = filter_xml(data) + data = data.replace('"', '"') + return data + + +def rss2html(inp): + # Parse the feed data and supply that to RSSTemplate object + template = RSSTemplate(inp) + + result = template.parse(DEFAULT_TEMPLATE) + + return result diff --git a/program/hypfront/feed2html.pyc b/program/hypfront/feed2html.pyc new file mode 100644 index 0000000..7c1e6ba Binary files /dev/null and b/program/hypfront/feed2html.pyc differ diff --git a/program/hypfront/hyper.py b/program/hypfront/hyper.py new file mode 100644 index 0000000..4405580 --- /dev/null +++ b/program/hypfront/hyper.py @@ -0,0 +1,40 @@ +#!/bin/env python +# -*- coding: utf-8 -*- + +import feedparser +import HTMLParser +import feed2html +from mod_python import apache + + +def req_pre(req): + req.log_error('handler') + req.content_type = 'text/html' + req.send_http_header() + req.write(""" + + HyperFrontend RSS feed input + + +""") + + +def req_post(req): + req.write(""" + + +""") + + +def handler(req): + req_pre(req) + args = dict(filter(lambda x: x[1], + map(lambda x: x.split('='), + req.__getattribute__('args').split('&')))) + if 'url' not in args and 'name' not in args: + req.write('Something went wrong, empty fields?
') + req.write('back') + else: + req.write(str(args)) + req_post(req) + return apache.OK diff --git a/program/hypfront/index.html b/program/hypfront/index.html new file mode 100644 index 0000000..db11dec --- /dev/null +++ b/program/hypfront/index.html @@ -0,0 +1,11 @@ + + +
+ + + +

RSS URL:

RSS Name:

+
+
+ + diff --git a/program/hypfront/install.sh b/program/hypfront/install.sh new file mode 100644 index 0000000..1cb3451 --- /dev/null +++ b/program/hypfront/install.sh @@ -0,0 +1,3 @@ +sudo rm -v /var/www/py/* +sudo cp -v ./*.py ./*.html /var/www/py/ +sudo chown -vR www-data:www-data /var/www/py diff --git a/program/hypfront/test.html b/program/hypfront/test.html new file mode 100644 index 0000000..4cdf35b --- /dev/null +++ b/program/hypfront/test.html @@ -0,0 +1,136 @@ + + + + + Nieuw in de voorverkoop Paradiso + + +
+

Nieuw in de voorverkoop Paradiso

+
+ +
+

donderdag 26 juni 2014 23:30 - Dondergrondse: Fuck Forever band night

+
+

Posted by on

+
+ +
+

donderdag 19 juni 2014 23:30 - Dondergrondse

+
+

Posted by on

+
+ +
+

zaterdag 7 juni 2014 20:00 - Kina Grannis - Locatie: Bitterzoet

+
+

Posted by on

+
+ +
+

vrijdag 3 oktober 2014 19:30 - Jay Brannan

+
+

Posted by on

+
+ +
+

donderdag 12 juni 2014 23:30 - Dondergrondse: hosted by The Daily Indie – Live: The Future’s Dust

+
+

Posted by on

+
+ +
+

woensdag 23 juli 2014 20:00 - Blitz Kids

+
+

Posted by on

+
+ +
+

donderdag 26 juni 2014 23:30 - Noodlanding!

+
Dansnacht, alternatieve hits
+

Posted by on

+
+ +
+

donderdag 19 juni 2014 23:30 - Noodlanding!

+
Dansnacht, alternatieve hits
+

Posted by on

+
+ +
+

maandag 15 september 2014 19:30 - Natural Child – 3VOOR12 presents - Locatie: Tolhuistuin (zaal)

+
aftrap officiële openingsweek van nieuwe zaal Tolhuistuin
+

Posted by on

+
+ +
+

zaterdag 5 juli 2014 22:00 - Ben Miller Band

+
+

Posted by on

+
+ +
+

donderdag 16 oktober 2014 20:30 - Zoot Woman – ADE - Locatie: Tolhuistuin (zaal)

+
+

Posted by on

+
+ +
+

woensdag 25 juni 2014 21:00 - Little Barrie - Locatie: Bitterzoet

+
+

Posted by on

+
+ +
+

woensdag 3 september 2014 19:30 - Earth Wind & Fire Experience feat. Al McKay

+
+

Posted by on

+
+ +
+

zondag 19 oktober 2014 19:00 - Di-rect

+
+

Posted by on

+
+ +
+

vrijdag 10 oktober 2014 19:30 - Royal Wood

+
+

Posted by on

+
+ +
+

zondag 1 juni 2014 20:00 - Dark Alley Cabaret

+
Amsterdam Burlesque Weekend
+

Posted by on

+
+ +
+

maandag 2 juni 2014 20:30 - The News – Reality Opera van Jacob TV door de Nederlandse Reisopera

+
+

Posted by on

+
+ +
+

zaterdag 24 mei 2014 21:00 - ‘Wij zijn 18’: filmpremiere, Palio Superspeed Donkey & afterparty

+
+

Posted by on

+
+ +
+

maandag 30 juni 2014 20:30 - Wiz Khalifa

+
+

Posted by on

+
+ +
+

woensdag 20 augustus 2014 21:00 - Dave Hause - Locatie: Bitterzoet

+
+

Posted by on

+
+ +
+ +
+ +