Ejemplo n.º 1
0
def run():
    start = time.time()
    program = request.json
    print "got program", program

    results = {}

    try:
        status, obj = compiler.compile(program)
        print "compiled in", time.time() - start, "secs"

        if "max_tracks" in program:
            max_tracks = program["max_tracks"]
        else:
            max_tracks = 40

        results["status"] = status

        if status == "ok":
            results["name"] = obj.name
            tids = pbl.get_tracks(obj, max_tracks)

            tracks = []
            results["tracks"] = tracks
            print
            for i, tid in enumerate(tids):
                print i, pbl.tlib.get_tn(tid)
                tracks.append(pbl.tlib.get_track(tid))
            print
        else:
            results["status"] = "error"
            results["message"] = status

    except pbl.PBLException as e:
        if debug_exceptions:
            raise
        results["status"] = "error"
        results["message"] = e.reason
        if e.component:
            cname = program["hsymbols"][e.component]
        else:
            cname = e.cname
        results["component"] = cname

    except Exception as e:
        if debug_exceptions:
            raise
        results["status"] = "error"
        results["message"] = str(e)

    results["time"] = time.time() - start
    print "compiled and executed in", time.time() - start, "secs"
    if app.trace:
        print json.dumps(results, indent=4)
    print "run", time.time() - start, results["status"]
    return jsonify(results)
Ejemplo n.º 2
0
    def run(self):
        print 'inventory'
        start = time.time()
        # program = request.json
        print cherrypy.request.headers
        cl = cherrypy.request.headers['Content-Length']
        #rawbody = cherrypy.request.body.read(int(cl))
        program = cherrypy.request.json

        print 'got program', program
        status, obj = compiler.compile(program)

        print 'compiled in', time.time() - start, 'secs'

        if 'max_tracks' in program:
            max_tracks = program['max_tracks']
        else:
            max_tracks = 40

        results = {'status': status}

        if status == 'ok':
            tracks = []
            tids = pbl.get_tracks(obj, max_tracks)
            print
            for i, tid in enumerate(tids):
                print i, pbl.tlib.get_tn(tid)
                tracks.append(pbl.tlib.get_track(tid))
            print
            results['tracks'] = tracks
            results['name'] = obj.name

        results['time'] = time.time() - start
        print 'compiled and executed in', time.time() - start, 'secs'
        if app.trace:
            print json.dumps(results, indent=4)
        print 'run', time.time() - start
        return results
Ejemplo n.º 3
0
    def run(self):
        print 'inventory'
        start = time.time()
        # program = request.json
        print cherrypy.request.headers
        cl = cherrypy.request.headers['Content-Length']
        #rawbody = cherrypy.request.body.read(int(cl))
        program = cherrypy.request.json

        print 'got program', program
        status, obj = compiler.compile(program)

        print 'compiled in', time.time() - start, 'secs'

        if 'max_tracks' in program:
            max_tracks = program['max_tracks']
        else:
            max_tracks = 40

        results = { 'status': status}

        if status == 'ok':
            tracks = []
            tids = pbl.get_tracks(obj, max_tracks)
            print
            for i, tid in enumerate(tids):
                print i, pbl.tlib.get_tn(tid)
                tracks.append(pbl.tlib.get_track(tid))
            print
            results['tracks'] = tracks
            results['name'] = obj.name

        results['time'] = time.time() - start
        print 'compiled and executed in', time.time() - start, 'secs'
        if app.trace:
            print json.dumps(results, indent=4)
        print 'run', time.time() - start
        return results
Ejemplo n.º 4
0
    def execute_program(self, auth_code, pid, save_playlist):
        start = time.time()

        results = {}

        self.inc_global_counter("programs_executed")
        try:
            pbl.engine.clearEnvData()
            token = self.auth.get_fresh_token(auth_code)
            if not token:
                print 'WARNING: bad auth token', auth_code
                results['status'] = 'error'
                results['message'] = 'not authorized'
            else:
                delta = token['expires_at'] - time.time()
                print 'cur token expires in', delta, 'secs'
                user = token['user_id']
                program = self.get_program(user, pid)
                if not program:
                    return None
                pbl.engine.setEnv('spotify_auth_token', token['access_token'])
                pbl.engine.setEnv('spotify_user_id', token['user_id'])

                print 'executing', user, pid
                print '# executing', json.dumps(program, indent=4)
                status, obj = compiler.compile(program)
                print 'compiled in', time.time() - start, 'secs'

                if 'max_tracks' in program:
                    max_tracks = program['max_tracks']
                else:
                    max_tracks = 40

                results['status'] = status

                if status == 'ok':
                    results['name'] = obj.name
                    tids = pbl.get_tracks(obj, max_tracks)
                    results['tids'] = tids
                    self.inc_global_counter("tracks_generated", len(tids))

                    if save_playlist:
                        uri = self.get_info(pid, 'uri')
                        self.inc_global_counter("playlists_updated")
                        new_uri = plugs.save_to_playlist(
                            program['name'], uri, tids)
                        if uri != new_uri:
                            self.add_info(pid, 'uri', new_uri)
                        if new_uri:
                            results['uri'] = new_uri
                        else:
                            results['status'] = 'error'
                            results[
                                'message'] = "Can't save playlist to Spotify"
                else:
                    self.inc_global_counter("programs_execute_errors")
                    results['status'] = 'error'
                    results['message'] = status

        except pbl.PBLException as e:
            results['status'] = 'error'
            results['message'] = e.reason
            if e.component and e.component.name in program['hsymbols']:
                cname = program['hsymbols'][e.component.name]
            else:
                cname = e.cname
            results['component'] = cname
            print 'PBLException', json.dumps(results, indent=4)
            traceback.print_exc()
            if debug_exceptions:
                raise

        except Exception as e:
            results['status'] = 'error'
            results['message'] = str(e)
            print 'General Exception', json.dumps(results, indent=4)
            traceback.print_exc()
            if debug_exceptions:
                raise

        pbl.engine.clearEnvData()
        results['time'] = time.time() - start
        print 'compiled and executed in', time.time() - start, 'secs'

        self.add_stat(pid, 'last_run', time.time())
        if results['status'] == 'ok':
            self.inc_stat(pid, 'runs')
        else:
            self.inc_stat(pid, 'errors')

        print 'run', time.time() - start, results['status']
        return results
Ejemplo n.º 5
0
    def execute_program(self, auth_code, pid, save_playlist):
        start = time.time()

        results = { }

        self.inc_global_counter("programs_executed")
        try:
            pbl.engine.clearEnvData()
            token = self.auth.get_fresh_token(auth_code)
            if not token:
                print 'WARNING: bad auth token', auth_code
                results['status'] = 'error'
                results['message'] = 'not authorized'
            else:
                delta = token['expires_at'] - time.time()
                print 'cur token expires in', delta, 'secs'
                user = token['user_id']
                program = self.get_program(user, pid)
                if not program:
                    return None
                pbl.engine.setEnv('spotify_auth_token', token['access_token'])
                pbl.engine.setEnv('spotify_user_id', token['user_id'])

                print 'executing', user, pid
                print '# executing', json.dumps(program, indent=4)
                status, obj = compiler.compile(program)
                print 'compiled in', time.time() - start, 'secs'

                if 'max_tracks' in program:
                    max_tracks = program['max_tracks']
                else:
                    max_tracks = 40

                results['status'] = status

                if status == 'ok':
                    results['name'] = obj.name
                    tids = pbl.get_tracks(obj, max_tracks)
                    results['tids'] = tids
                    self.inc_global_counter("tracks_generated", len(tids))

                    if save_playlist:
                        uri = self.get_info(pid, 'uri')
                        self.inc_global_counter("playlists_updated")
                        new_uri = plugs.save_to_playlist(program['name'], uri, tids)
                        if uri != new_uri:
                            self.add_info(pid, 'uri', new_uri)
                        if new_uri:
                            results['uri'] = new_uri
                        else:
                            results['status'] = 'error'
                            results['message'] = "Can't save playlist to Spotify"
                else:
                    self.inc_global_counter("programs_execute_errors")
                    results['status'] = 'error'
                    results['message'] = status

        except pbl.PBLException as e:
            results['status'] = 'error'
            results['message'] = e.reason
            if e.component and e.component.name in program['hsymbols']:
                cname = program['hsymbols'][e.component.name]
            else:
                cname = e.cname
            results['component'] = cname
            print 'PBLException', json.dumps(results, indent=4)
            traceback.print_exc()
            if debug_exceptions:
                raise

        except Exception as e:
            results['status'] = 'error'
            results['message'] = str(e)
            print 'General Exception', json.dumps(results, indent=4)
            traceback.print_exc()
            if debug_exceptions:
                raise

        pbl.engine.clearEnvData()
        results['time'] = time.time() - start
        print 'compiled and executed in', time.time() - start, 'secs'

        self.add_stat(pid, 'last_run', time.time());
        if results['status'] == 'ok':
            self.inc_stat(pid, 'runs');
        else:
            self.inc_stat(pid, 'errors');

        print 'run', time.time() - start, results['status']
        return results