# -*- coding: utf-8 -*-
# ------------------------------------------------------------
# Controlador para RSS
# ------------------------------------------------------------
import random
import re
import threading
from controller import Controller
from controller import Platformtools
from core.item import Item
class rss(Controller):
pattern = re.compile("^/rss")
data = {}
def __init__(self, handler=None, ):
super(rss, self).__init__(handler)
self.platformtools = platformtools(self)
def extract_item(self, path):
if path == "/rss" or path == "/rss/":
item = Item(channel="channelselector", action="mainlist")
else:
item = Item().fromurl(path.replace("/rss/", ""))
return item
def run(self, path):
item = self.extract_item(path)
from platformcode import launcher
launcher.run(item)
def set_data(self, data):
self.data = data
def get_data(self, id):
if "id" in self.data and self.data["id"] == id:
data = self.data["result"]
else:
data = None
return data
def send_data(self, data, headers={}, response=200):
headers.setdefault("content-type", "application/rss+xml")
headers.setdefault("connection", "close")
self.handler.send_response(response)
for header in headers:
self.handler.send_header(header, headers[header])
self.handler.end_headers()
self.handler.wfile.write(data)
class platformtools(Platformtools):
def __init__(self, controller):
self.controller = controller
self.handler = controller.handler
def create_rss(self, itemlist):
resp = '\n'
resp += '\n'
resp += '\n'
resp += 'http://' + self.controller.host + '/rss\n'
resp += 'MenĂº Principal\n'
for item in itemlist:
resp += '- \n'
resp += '' + item.title + '\n'
resp += '' + item.thumbnail + '\n'
resp += '' + self.controller.host + '/rss/' + item.tourl() + '\n'
resp += '
\n\n'
resp += '\n'
resp += '\n'
return resp
def render_items(self, itemlist, parentitem):
new_itemlist = []
for item in itemlist:
# if item.action == "search": continue
# if item.channel=="search": continue
# if item.channel=="setting": continue
# if item.channel=="help": continue
new_itemlist.append(item)
response = self.create_rss(new_itemlist)
self.controller.send_data(response)
def dialog_select(self, heading, list):
ID = "%032x" % (random.getrandbits(128))
response = '\n'
response += '\n'
response += '\n'
response += '/rss\n'
response += '' + heading + '\n'
for option in list:
response += '- \n'
response += '' + option + '\n'
response += '%s\n'
response += 'http://' + self.controller.host + '/data/' + threading.current_thread().name + '/' + ID + '/' + str(
list.index(option)) + '\n'
response += '
\n\n'
response += '\n'
response += '\n'
self.controller.send_data(response)
self.handler.server.shutdown_request(self.handler.request)
while not self.controller.get_data(ID):
continue
return int(self.controller.get_data(ID))
def dialog_ok(self, heading, line1, line2="", line3=""):
text = line1
if line2: text += "\n" + line2
if line3: text += "\n" + line3
ID = "%032x" % (random.getrandbits(128))
response = '\n'
response += '\n'
response += '\n'
response += '/rss\n'
response += '' + heading + '\n'
response += '- \n'
response += '' + text + '\n'
response += '%s\n'
response += '\n'
response += '
\n\n'
response += '- \n'
response += 'Si\n'
response += '%s\n'
response += 'http://' + self.controller.host + '/data/' + threading.current_thread().name + '/' + ID + '/1\n'
response += '
\n\n'
response += '- \n'
response += '
\n'
response += '\n'
self.controller.send_data(response)
self.handler.server.shutdown_request(self.handler.request)
while not self.controller.get_data(ID):
continue
def dialog_yesno(self, heading, line1, line2="", line3=""):
text = line1
if line2: text += "\n" + line2
if line3: text += "\n" + line3
ID = "%032x" % (random.getrandbits(128))
response = '\n'
response += '\n'
response += '\n'
response += '/rss\n'
response += '' + heading + '\n'
response += '- \n'
response += '' + text + '\n'
response += '%s\n'
response += '\n'
response += '
\n\n'
response += '- \n'
response += 'Si\n'
response += '%s\n'
response += 'http://' + self.controller.host + '/data/' + threading.current_thread().name + '/' + ID + '/1\n'
response += '
\n\n'
response += '- \n'
response += 'No\n'
response += '%s\n'
response += 'http://' + self.controller.host + '/data/' + threading.current_thread().name + '/' + ID + '/0\n'
response += '
\n\n'
response += '\n'
response += '\n'
self.controller.send_data(response)
self.handler.server.shutdown_request(self.handler.request)
while not self.controller.get_data(ID):
continue
return bool(int(self.controller.get_data(ID)))
def dialog_notification(self, heading, message, icon=0, time=5000, sound=True):
# No disponible por ahora, muestra un dialog_ok
self.dialog_ok(heading, message)
def play_video(self, item):
response = '\n'
response += '\n'
response += '\n'
response += '/rss\n'
response += '' + item.title + '\n'
response += '- \n'
response += '' + item.title + '\n'
response += '%s\n'
response += '' + item.video_url + '\n'
response += '
\n\n'
response += '\n'
response += '\n'
self.controller.send_data(response)