Beispiel #1
0
    def __init__(self, host):
        self.host = host

        self.direct = Direct(self)
        self.server = None

        self.sp = None
        self.reconnect_time = None
        self.reconnect_timer = None

        self.ready_event = Event()
        self.messages = []
Beispiel #2
0
    def __init__(self, host):
        self.host = host

        self.direct = Direct(self)
        self.server = None

        self.sp = None
        self.reconnect_time = None
        self.reconnect_timer = None

        self.ready_event = Event()
        self.messages = []
Beispiel #3
0
            #             Direct(func6, bounds=np.array([[0,1]]*3), globalmin=GlobalMin(known=False, val=-3.86278)).run(file)
            # #             Direct(func6, bounds=np.array([[0,1]]*6), globalmin=GlobalMin(known=False, val=-3.32237)).run(file)
            #
            #             print('test Branin results:')
            # #             file.write('test Branin results:\n')
            #             Direct(func7, bounds=np.array([[-5,10],[0,15]]), globalmin=GlobalMin(known=True, val=0.397887)).run(file)
            #
            #             print('test Shekel results:')
            # #             file.write('test Shekel results:\n')
            # #             Direct(func8, bounds=np.array([[0,10]]*4), globalmin=GlobalMin(known=True, val=-10.5364)).run(file)
            # #             Direct(func8, bounds=np.array([[0,10]]*4), globalmin=GlobalMin(known=True, val=-10.4029)).run(file)
            #             Direct(func8, bounds=np.array([[0,10]]*4), globalmin=GlobalMin(known=True, val=-10.1532)).run(file)
            #
            #             print('test Shubert results:')
            # #             file.write('test Shubert results:\n')
            #             Direct(func9, bounds=np.array([[-10,10],[-10,10]]), globalmin=GlobalMin(known=True, val=-186.7309)).run(file)

            #             print('test Michalewics results:')
            #             file.write('test Michalewics results:\n')
            #             Direct(func10, bounds=np.array([[0,np.pi]]*2), globalmin=GlobalMin(known=True, val=-1.8013)).run(file)
            #             Direct(func10, bounds=np.array([[0,np.pi]]*5), globalmin=GlobalMin(known=True, val=-4.687658)).run(file)
            #             Direct(func10, bounds=np.array([[0,np.pi]]*10), globalmin=GlobalMin(known=True, val=-9.66015)).run(file)

            print('test Schwefel results:')
            #             file.write('test Schwefel results:\n')
            Direct(func11,
                   bounds=np.array([[-500, 500]] * 4),
                   globalmin=GlobalMin(known=True, val=0.)).run(file)
        file.close()
    except Exception:
        traceback.print_exc()
Beispiel #4
0
class SpotifyClient(object):
    def __init__(self, host):
        self.host = host

        self.direct = Direct(self)
        self.server = None

        self.sp = None
        self.reconnect_time = None
        self.reconnect_timer = None

        self.ready_event = Event()
        self.messages = []

    def start(self):
        if self.sp:
            self.sp.disconnect()
            self.sp = None

        self.sp = Spotify()

        self.ready_event = Event()
        self.messages = []

        self.sp.on('error', self.on_error)\
               .on('close', self.on_close)

        self.sp.login(self.host.username, self.host.password, self.on_login)

    def on_login(self):
        # Refresh server info
        self.host.refresh()

        # Release request hold
        self.ready_event.set()

    def on_error(self, message):
        self.messages.append((logging.ERROR, message))
        Log.Error(message)

    def on_close(self, code, reason=None):
        # Force re-authentication
        self.sp.authenticated = False

        # Reconnect
        self.connect()

    def connect(self):
        # Rate-limit re-connections
        if self.reconnect_time:
            span = time.time() - self.reconnect_time
            Log.Debug('Last reconnection attempt was %s seconds ago', span)

            # Delay next reconnection
            if span < 120:
                self.connect_delayed()
                return

        Log.Info('Attempting reconnection to Spotify...')

        self.reconnect_time = time.time()

        # Hold requests while we re-connect
        self.ready_event = Event()

        # Start connecting...
        self.sp.connect()

    def connect_delayed(self):
        self.reconnect_timer = Timer(180, self.connect)
        self.reconnect_timer.start()

        Log.Info('Reconnection will be attempted again in 180 seconds')

    @property
    def constructed(self):
        return self.sp and self.ready_event

    @property
    def ready(self):
        if not self.constructed:
            return False

        return self.ready_event.wait(10)

    def shutdown(self):
        self.sp.api.shutdown()
        self.sp = None

    #
    # Public methods
    #

    def search(self, query, query_type='all', max_results=50, offset=0):
        """ Execute a search

        :param query:          A query string.
        """
        return self.sp.search(query, query_type, max_results, offset)

    def artist_uris(self, artist):
        top_tracks = self.artist_top_tracks(artist)

        # Top Track URIs
        track_uris = []

        if top_tracks:
            track_uris = [tr.uri for tr in top_tracks.tracks if tr is not None]

        # Album URIs
        album_uris = [al.uri for al in artist.albums if al is not None]

        return track_uris, album_uris

    def artist_top_tracks(self, artist):
        for tt in artist.top_tracks:
            # TopTracks matches account region?
            if tt.country == self.sp.country:
                return tt

        # Unable to find TopTracks for account region
        return None

    #
    # Streaming
    #

    def track_url(self, track):
        if self.host.proxy_tracks and self.server:
            return self.server.get_track_url(str(track.uri),
                                             hostname=self.host.hostname)

        return function_path('play', uri=str(track.uri), ext='mp3')

    def stream_url(self, uri):
        if self.host.proxy_tracks and self.server:
            return self.server.get_track_url(str(uri),
                                             hostname=self.host.hostname)

        return self.direct.get(uri)

    def last_message(self):
        if not self.messages:
            return None, ''

        return self.messages[-1]
Beispiel #5
0
class SpotifyClient(object):
    def __init__(self, host):
        self.host = host

        self.direct = Direct(self)
        self.server = None

        self.sp = None
        self.reconnect_time = None
        self.reconnect_timer = None

        self.ready_event = Event()
        self.messages = []

    def start(self):
        if self.sp:
            self.sp.disconnect()
            self.sp = None

        self.sp = Spotify()

        self.ready_event = Event()
        self.messages = []

        self.sp.on('error', self.on_error)\
               .on('close', self.on_close)

        self.sp.login(self.host.username, self.host.password, self.on_login)

    def on_login(self):
        # Refresh server info
        self.host.refresh()

        # Release request hold
        self.ready_event.set()

    def on_error(self, message):
        self.messages.append((logging.ERROR, message))
        Log.Error(message)

    def on_close(self, code, reason=None):
        # Force re-authentication
        self.sp.authenticated = False

        # Reconnect
        self.connect()

    def connect(self):
        # Rate-limit re-connections
        if self.reconnect_time:
            span = time.time() - self.reconnect_time
            Log.Debug('Last reconnection attempt was %s seconds ago', span)

            # Delay next reconnection
            if span < 120:
                self.connect_delayed()
                return

        Log.Info('Attempting reconnection to Spotify...')

        self.reconnect_time = time.time()

        # Hold requests while we re-connect
        self.ready_event = Event()

        # Start connecting...
        self.sp.connect()

    def connect_delayed(self):
        self.reconnect_timer = Timer(180, self.connect)
        self.reconnect_timer.start()

        Log.Info('Reconnection will be attempted again in 180 seconds')

    @property
    def constructed(self):
        return self.sp and self.ready_event

    @property
    def ready(self):
        if not self.constructed:
            return False

        return self.ready_event.wait(10)

    def shutdown(self):
        self.sp.api.shutdown()
        self.sp = None

    #
    # Public methods
    #

    def search(self, query, query_type='all', max_results=50, offset=0):
        """ Execute a search

        :param query:          A query string.
        """
        return self.sp.search(query, query_type, max_results, offset)

    def artist_uris(self, artist):
        top_tracks = self.artist_top_tracks(artist)

        # Top Track URIs
        track_uris = []

        if top_tracks:
            track_uris = [tr.uri for tr in top_tracks.tracks if tr is not None]

        # Album URIs
        album_uris = [al.uri for al in artist.albums if al is not None]

        return track_uris, album_uris

    def artist_top_tracks(self, artist):
        for tt in artist.top_tracks:
            # TopTracks matches account region?
            if tt.country == self.sp.country:
                return tt

        # Unable to find TopTracks for account region
        return None

    #
    # Streaming
    #

    def track_url(self, track):
        if self.host.proxy_tracks and self.server:
            return self.server.get_track_url(str(track.uri), hostname=self.host.hostname)

        return function_path('play', uri=str(track.uri), ext='mp3')

    def stream_url(self, uri):
        if self.host.proxy_tracks and self.server:
            return self.server.get_track_url(str(uri), hostname=self.host.hostname)

        return self.direct.get(uri)

    def last_message(self):
        if not self.messages:
            return None, ''

        return self.messages[-1]
Beispiel #6
0
from events import EventCreate
import pymongo
import os
from pymongo import MongoClient
from flask import request, jsonify, json, render_template, redirect
from mailhandler import Mailhandler

client = MongoClient('localhost', 27017)
app = Flask(__name__)

app.secret_key = os.urandom(24)
first = First()
alma = Alumni()
stud = Student()
teach = Teacher()
di = Direct()


@app.route('/crash')
def main():
    raise Exception()


@app.route('/')
def index():
    return render_template('index.html')


@app.route('/homelog')
def homelog():
    return render_template('login.html')