Exemple #1
0
    def infopage(self) -> None:
        t = INFO_PAGE_TCLASS()
        t.admin = ""

        if get_server("tivo_mak", "") and get_server("togo_path", ""):
            t.togo = "<br>Pull from TiVos:<br>"
        else:
            t.togo = ""

        for section, settings in getShares():
            plugin_type = settings.get("type")
            if plugin_type == "settings":
                t.admin += (
                    '<a href="/TiVoConnect?Command=Settings&amp;'
                    + "Container="
                    + quote(section)
                    + '">Settings</a><br>'
                )
            elif plugin_type == "togo" and t.togo:
                for tsn in pytivo.config.TIVOS:
                    if tsn and "address" in pytivo.config.TIVOS[tsn]:
                        t.togo += (
                            '<a href="/TiVoConnect?'
                            + "Command=NPL&amp;Container="
                            + quote(section)
                            + "&amp;TiVo="
                            + pytivo.config.TIVOS[tsn]["address"]
                            + '">'
                            + pytivo.config.TIVOS[tsn]["name"]
                            + "</a><br>"
                        )

        self.send_html(str(t))
Exemple #2
0
def setup(
    config: Optional[str] = None,
    extraconf: Optional[str] = None,
    in_service: bool = False,
) -> TivoHTTPServer:
    config_init(config=config, extraconf=extraconf)
    init_logging()
    sys.excepthook = exceptionLogger

    port = getPort()

    httpd = TivoHTTPServer(("", int(port)), TivoHTTPHandler)
    LOGGER.info("Last modified: " + last_date())
    LOGGER.info("Python: " + platform.python_version())
    LOGGER.info("System: " + platform.platform())

    for section, settings in getShares():
        httpd.add_container(section, settings)

    b = Beacon()
    b.add_service(b"TiVoMediaServer:%d/http" % int(port))
    b.start()
    if "listen" in getBeaconAddresses():
        b.listen()

    httpd.set_beacon(b)
    httpd.set_service_status(in_service)

    LOGGER.info("pyTivo is ready.")
    return httpd
Exemple #3
0
    def root_container(self) -> None:
        tsn = self.headers.get("TiVo_TCD_ID", "")
        tsnshares = getShares(tsn)
        tsncontainers = []
        for section, settings in tsnshares:
            try:
                mime = GetPlugin(settings["type"]).CONTENT_TYPE
                if mime.split("/")[1] in ("tivo-videos", "tivo-music", "tivo-photos"):
                    settings["content_type"] = mime
                    tsncontainers.append((section, settings))
            except Exception as msg:
                LOGGER.error(section + " - " + str(msg), exc_info=True)

        t = ROOT_CONTAINER_TCLASS()

        if self.server.beacon is None:
            raise Exception("No self.server.beacon")

        if self.server.beacon.bd:
            t.renamed = self.server.beacon.bd.renamed
        else:
            t.renamed = {}
        t.containers = tsncontainers
        t.hostname = socket.gethostname()
        t.escape = escape
        t.quote = quote
        self.send_xml(str(t))
Exemple #4
0
 def do_command(self, query: Query, command: str, target: str, tsn: str) -> bool:
     for name, container in getShares(tsn):
         if target == name:
             plugin = GetPlugin(container["type"])
             if hasattr(plugin, command):
                 self.cname = name
                 self.container = container
                 method = getattr(plugin, command)
                 method(self, query)
                 return True
             else:
                 break
     return False
Exemple #5
0
 def __init__(self) -> None:
     """ Announce our shares via Zeroconf. """
     self.share_names: List[str] = []
     self.share_info: List[zeroconf.ServiceInfo] = []
     self.rz = zeroconf.Zeroconf()
     self.renamed: Dict[str, str] = {}
     old_titles = self.scan()
     address = socket.inet_aton(get_ip())
     port = int(getPort())
     LOGGER.info("Announcing shares...")
     for section, settings in getShares():
         try:
             ct = GetPlugin(settings["type"]).CONTENT_TYPE
         except:
             continue
         if ct.startswith("x-container/"):
             if "video" in ct:
                 platform = PLATFORM_VIDEO
             else:
                 platform = PLATFORM_MAIN
             LOGGER.info("Registering: %s" % section)
             self.share_names.append(section)
             desc = {
                 "path": SHARE_TEMPLATE % quote(section),
                 "platform": platform,
                 "protocol": "http",
                 "tsn": "{%s}" % uuid.uuid4(),
             }
             tt = ct.split("/")[1]
             title = section
             count = 1
             while title in old_titles:
                 count += 1
                 title = "%s [%d]" % (section, count)
                 self.renamed[section] = title
             info = zeroconf.ServiceInfo(
                 "_%s._tcp.local." % tt,
                 "%s._%s._tcp.local." % (title, tt),
                 address,
                 port,
                 0,
                 0,
                 desc,
             )
             self.rz.register_service(info)
             self.share_info.append(info)
Exemple #6
0
 def ToGo(self, handler: "TivoHTTPHandler", query: Query) -> None:
     togo_path = get_server("togo_path", "")
     for name, data in getShares():
         if togo_path == name:
             togo_path = str(data.get("path"))
     if togo_path:
         tivoIP = query["TiVo"][0]
         tsn = tivos_by_ip(tivoIP)
         tivo_mak = get_tsn("tivo_mak", tsn)
         urls = query.get("Url", [])
         decode = "decode" in query
         save = "save" in query
         ts_format = "ts_format" in query
         for theurl in urls:
             STATUS[theurl] = {
                 "running": False,
                 "error": "",
                 "rate": "",
                 "queued": True,
                 "size": 0,
                 "finished": False,
                 "decode": decode,
                 "save": save,
                 "ts_format": ts_format,
             }
             if tivoIP in QUEUE:
                 QUEUE[tivoIP].append(theurl)
             else:
                 QUEUE[tivoIP] = [theurl]
                 _thread.start_new_thread(
                     ToGo.process_queue,
                     (self, tivoIP, tivo_mak, togo_path))
             LOGGER.info('[%s] Queued "%s" for transfer to %s' %
                         (time.strftime("%d/%b/%Y %H:%M:%S"),
                          unquote(theurl), togo_path))
         urlstring = "<br>".join([unquote(x) for x in urls])
         message = TRANS_QUEUE % (urlstring, togo_path)
     else:
         message = MISSING
     handler.redir(message, 5)
Exemple #7
0
    def __init__(self) -> None:
        self.UDPSock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        self.UDPSock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
        self.services: List[bytes] = []
        self.bd: Optional[ZCBroadcast]

        self.platform = PLATFORM_VIDEO
        for section, settings in getShares():
            try:
                ct = GetPlugin(settings["type"]).CONTENT_TYPE
            except:
                continue
            if ct in ("x-container/tivo-music", "x-container/tivo-photos"):
                self.platform = PLATFORM_MAIN
                break

        if get_zc():
            try:
                self.bd = ZCBroadcast()
            except:
                LOGGER.error("Zeroconf failure", exc_info=True)
                self.bd = None
        else:
            self.bd = None
Exemple #8
0
 def reset(self) -> None:
     self.containers.clear()
     for section, settings in getShares():
         self.add_container(section, settings)