<commit_before>#!/usr/bin/python

# Python program that emulates ChromeCast device

from twisted.internet import reactor
from twisted.internet.protocol import DatagramProtocol
import tornado.ioloop
import tornado.web
import tornado.websocket
import threading
import string
import argparse
import signal
import logging
from textwrap import dedent
import shlex, subprocess

yt_status = dict(state="stopped", link="")
cc_status = dict(state="stopped", link="")
pm_status = dict(state="stopped", link="")
gm_status = dict(state="stopped", link="")
gcsa_status = dict(state="stopped", link="")
gcp_status = dict(state="stopped", link="")

friendlyName = "Mopidy"
user_agent ="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) CrKey/30.0.1573.2 Safari/537.36"
chrome = "/opt/google/chrome/chrome"

class SSDP(DatagramProtocol):
    SSDP_ADDR = '172.16.58.3'
    SSDP_PORT = 1900
    MS = """HTTP/1.1 200 OK\r
LOCATION: http://$ip:8008/ssdp/device-desc.xml\r
CACHE-CONTROL: max-age=1800\r
CONFIGID.UPNP.ORG: 7337\r
BOOTID.UPNP.ORG: 7337\r
USN: uuid:3e1cc7c0-f4f3-11e2-b778-0800200c9a66::urn:dial-multiscreen-org:service:dial:1\r
ST: urn:dial-multiscreen-org:service:dial:1\r
\r
"""

    def __init__(self, iface):
        self.iface = iface
        self.transport = reactor.listenMulticast(
            self.SSDP_PORT, self, listenMultiple=True)
        self.transport.setLoopbackMode(1)
        self.transport.joinGroup(self.SSDP_ADDR, interface=iface)

    def stop(self):
        self.transport.leaveGroup(self.SSDP_ADDR, interface=self.iface)
        self.transport.stopListening()

    def datagramReceived(self, datagram, address):
        if "urn:dial-multiscreen-org:service:dial:1" in datagram and "M-SEARCH" in datagram:
            data =string.Template(dedent(self.MS)).substitute(ip=self.iface)
            self.transport.write(data, address)

class LEAP(tornado.web.RequestHandler):

    service = None
    ip = None
    url = "$query"

    def prepare(self):
        self.ip = self.request.host

    def _response(self, data):
        self.set_header("Content-Type", "application/xml; charset=UTF-8")
        self.set_header("Cache-control", "no-cache, must-revalidate, no-store")
        self.set_header("Etag", "")
        self.finish(self._toXML(data))

    def post(self):
        self.set_status(201, "Created")
        self.set_header("Location", self._getLocation(self.__class__.__name__))
        self.launch(self.request.body)

    def _getLocation(self, app):
        return "http://%s/apps/%s/run" % (self.ip, app )

    def launch(self, data):
        appurl = string.Template(self.url).substitute(query=data)
        command_line ="""%s --app="%s" --user-agent="%s"  """  % (chrome, appurl, user_agent)
        print(command_line)
        args = shlex.split(command_line)
        self.pid = subprocess.Popen(args)

    def destroy(self, pid):
        pid.terminate()

    def _toXML(self, data):<commit_msg>Close #7<commit_after>#!/usr/bin/python

# Python program that emulates ChromeCast device

from twisted.internet import reactor
from twisted.internet.protocol import DatagramProtocol
import tornado.ioloop
import tornado.web
import tornado.websocket
import threading
import string
import argparse
import signal
import logging
from textwrap import dedent
import shlex, subprocess

yt_status = dict(state="stopped", link="")
cc_status = dict(state="stopped", link="")
pm_status = dict(state="stopped", link="")
gm_status = dict(state="stopped", link="")
gcsa_status = dict(state="stopped", link="")
gcp_status = dict(state="stopped", link="")

friendlyName = "Mopidy"
user_agent ="Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) CrKey/30.0.1573.2 Safari/537.36"
chrome = "/opt/google/chrome/chrome"

class SSDP(DatagramProtocol):
    SSDP_ADDR = '172.16.58.3'
    SSDP_PORT = 1900
    MS = """HTTP/1.1 200 OK\r
LOCATION: http://$ip:8008/ssdp/device-desc.xml\r
CACHE-CONTROL: max-age=1800\r
CONFIGID.UPNP.ORG: 7337\r
BOOTID.UPNP.ORG: 7337\r
USN: uuid:3e1cc7c0-f4f3-11e2-b778-0800200c9a66::urn:dial-multiscreen-org:service:dial:1\r
ST: urn:dial-multiscreen-org:service:dial:1\r
\r
"""

    def __init__(self, iface):
        self.iface = iface
        self.transport = reactor.listenMulticast(
            self.SSDP_PORT, self, listenMultiple=True)
        self.transport.setLoopbackMode(1)
        self.transport.joinGroup(self.SSDP_ADDR, interface=iface)

    def stop(self):
        self.transport.leaveGroup(self.SSDP_ADDR, interface=self.iface)
        self.transport.stopListening()

    def datagramReceived(self, datagram, address):
        if "urn:dial-multiscreen-org:service:dial:1" in datagram and "M-SEARCH" in datagram:
            data =string.Template(dedent(self.MS)).substitute(ip=self.iface)
            self.transport.write(data, address)

class LEAP(tornado.web.RequestHandler):

    service = None
    ip = None
    url = "$query"

    def prepare(self):
        self.ip = self.request.host

    def _response(self, data):
        self.set_header("Content-Type", "application/xml; charset=UTF-8")
        self.set_header("Cache-control", "no-cache, must-revalidate, no-store")
        self.set_header("Etag", "")
        self.finish(self._toXML(data))

    def post(self):
        self.set_status(201)
        self.set_header("Location", self._getLocation(self.__class__.__name__))
        self.launch(self.request.body)

    def _getLocation(self, app):
        return "http://%s/apps/%s/run" % (self.ip, app )

    def launch(self, data):
        appurl = string.Template(self.url).substitute(query=data)
        command_line ="""%s --app="%s" --user-agent="%s"  """  % (chrome, appurl, user_agent)
        print(command_line)
        args = shlex.split(command_line)
        self.pid = subprocess.Popen(args)

    def destroy(self, pid):
        pid.terminate()

    def _toXML(self, data):
{'fraction_non_alphanumeric': 0.14110846650799047, 'fraction_numerical': 0.030431825909554573, 'mean_word_length': 3.6359338061465722, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1380736', 'n_tokens_mistral': 2176, 'n_tokens_neox': 2027, 'n_words': 379}