def main(configFile): global config # Reads and apply the config file read_config(configFile) logfile.start(config['logs_file']) # Connects to the server host = config['host'] username = config['username'] password = config['password'] if not password: file = open(config['password_file'], "r") password = file.read() file.close() server.connect(host, username, password) # Gets the list of files on the server folder and in the local folder localfiles = localdir.get_all_files(config['local_dir']) serverfiles = server.list_files() # If a file have the same name on the client and the server but with different # size, backups the file on the server and uploads the client's version for lName, lSize in localfiles: for sName, sSize in serverfiles: if lName == sName and lSize != sSize: log("Backing up " + sName + "...") print("Backing up " + sName + "...") server.backup_file(sName) serverfiles.remove((sName, sSize)) # Downloads every file that is on the server but not in the local folder for file in serverfiles: if not file in localfiles: log("Downloading " + file[0] + "...") print("Downloading " + file[0] + "...") filepath = join(config['local_dir'], file[0]) if is_in_local_dir(filepath): server.fetch_file(file[0], filepath) # Protection against directory traversal else: log("Detected directory traversal attempt, aborting", "W: ") raise Exception("Detected directory traversal attempt\n" \ + filepath + " is not in the local directory") # Uploads everyfile that is in the local folder but not on the server for file in localfiles: if not file in serverfiles: log("Uploading " + file[0] + "...") print("Uploading " + file[0] + "...") server.send_file(join(config['local_dir'], file[0])) # Exits server.disconnect() print("Server and local dir are now synchronised!")
def test_update_user_password(self): user = self.client.factory.create('hyd:User') user.username = "******" % (datetime.datetime.now()) user.password = "******" user.display_name = "Tom" new_user = self.client.service.add_user(user) old_client = self.client new_client = server.connect() self.client = new_client self.login(new_user.username, user.password) self.client.service.logout(user.username) self.client = old_client self.client.service.update_user_password(new_user.id, 'new_test_user_password') self.client = new_client self.assertRaises(suds.WebFault, self.login, user.username, user.password) self.login(user.username, 'new_test_user_password') self.client.service.logout(user.username) self.client = old_client
def stream_table(row): """ Updates stream db :param row: row to insert to stream db """ conn = connect() cur = conn.cursor() query = ''' INSERT INTO stream VALUES ( DEFAULT, %(stream_id)s, %(channel_id)s, %(url)s, %(language)s, %(scheduled)s, %(featured)s, %(mature)s, %(partner)s, %(sponsored)s, %(game)s, %(viewers)s, %(followers)s, %(total_views)s, %(video_count)s, %(team_count)s, current_timestamp )''' cur.execute(query, row) conn.commit() conn.close()
def table(row): """ checking and updating db with game info from giantbomb :param row: (dict) dict of twitch json object """ conn = connect() cur = conn.cursor() trialid = get.trialid() Giantbomb.check(row['name'], row['giantbombid']) row['trialid'] = trialid + 1 # adding values into snapshot table query = ''' INSERT INTO snapshot VALUES ( DEFAULT, %(name)s, %(giantbombid)s, %(trialid)s, %(rank)s, %(viewers)s, %(channels)s, current_timestamp ) ''' cur.execute(query, row) # updating game_name table: if no entry found add entry query = ''' DO $do$ BEGIN IF EXISTS(SELECT * FROM game_name WHERE name = %(name)s) THEN UPDATE game_name SET viewer_total = viewer_total + %(viewers)s, channel_total = channel_total + %(channels)s, rank_total = rank_total + %(rank)s, trials = trials + 1 WHERE name = %(name)s; ELSE INSERT INTO game_name VALUES ( DEFAULT, %(name)s, %(giantbombid)s, %(viewers)s, %(channels)s, %(rank)s, 1 ); END IF; END $do$ ''' cur.execute(query, row) conn.commit() conn.close()
def test_sharing_shared_network(self): #One client is for the 'root' user and must remain open so it #can be closed correctly in the tear down. old_client = self.client new_client = server.connect() self.client = new_client self.login("UserA", 'password') network_1 = self.create_network_with_data(new_proj=True) #share the whole project with user B, and allow them to share. self.client.service.share_project(network_1.project_id, ["UserB"], 'Y', 'Y') self.client.service.logout("UserA") self.login("UserB", 'password') self.assertRaises(suds.WebFault, self.client.service.share_project, network_1.project_id, ["UserC"], 'Y', 'Y') self.assertRaises(suds.WebFault, self.client.service.share_network, network_1.id, ["UserC"], 'Y', 'Y') self.client.service.logout("UserB") self.client = old_client
def main(): import os import time import tempoclock import loader import server ctl = server.connect(spew=True) ctl.sendMsg('/dumpOSC', 1) SYNTHDEF_PATH = os.path.join(os.path.expanduser('~'), '.pksampler', 'synthdefs') SYNTHDEFS = ( 'JASStereoSamplePlayer.scsyndef', 'JASSine.scsyndef', ) for fname in SYNTHDEFS: ctl.sendMsg('/d_load', os.path.join(SYNTHDEF_PATH, fname)) player = Player(ctl) ldr = loader.Loader(ctl) bid = ldr.load('/Users/patrick/.pksampler/clicks/click_1.wav') clock = tempoclock.TempoClock(140.0) beats = [clock.spb() * i for i in range(100)] now = time.time() + 1 freqs = [440, 550, 220, 200, 460] synth = Synth() synth.name = 'JASSine' for seconds in beats: abs = now + seconds freqs = freqs[1:] + freqs[:1] synth['freq'] = freqs[0] player.play(synth, abs, abs + 1)
def main(): import os import time import tempoclock import loader import server ctl = server.connect(spew=True) ctl.sendMsg("/dumpOSC", 1) SYNTHDEF_PATH = os.path.join(os.path.expanduser("~"), ".pksampler", "synthdefs") SYNTHDEFS = ("JASStereoSamplePlayer.scsyndef", "JASSine.scsyndef") for fname in SYNTHDEFS: ctl.sendMsg("/d_load", os.path.join(SYNTHDEF_PATH, fname)) player = Player(ctl) ldr = loader.Loader(ctl) bid = ldr.load("/Users/patrick/.pksampler/clicks/click_1.wav") clock = tempoclock.TempoClock(140.0) beats = [clock.spb() * i for i in range(100)] now = time.time() + 1 freqs = [440, 550, 220, 200, 460] synth = Synth() synth.name = "JASSine" for seconds in beats: abs = now + seconds freqs = freqs[1:] + freqs[:1] synth["freq"] = freqs[0] player.play(synth, abs, abs + 1)
def team_table(row): """ Check if row exists. If not, insert to team table :param row: row to insert """ if not row: return conn = connect() cur = conn.cursor() query = '''SELECT channelid, teamid FROM team WHERE channelid = %(channel_id)s AND teamid = %(team_id)s''' cur.execute(query, row) fetch = cur.fetchone() if fetch is None: query = ''' INSERT INTO team VALUES ( %(channel_id)s, %(team_id)s, %(team_name)s, current_timestamp )''' cur.execute(query, row) conn.commit() conn.close()
def video_table(row): """ Inserts row to video table :param row: row to insert """ if not row: return conn = connect() cur = conn.cursor() query = ''' INSERT INTO featured VALUES ( DEFAULT, %(channel_id)s, %(video_id)s, %(video_title)s, %(video_game)s, %(video_status)s, %(video_type)s, %(video_views)s, %(video_url)s, %(video_res)s, %(video_length)s, %(video_desc)s, current_timestamp )''' cur.execute(query, row) cur.commit() conn.close()
def test_share_project(self): #One client is for the 'root' user and must remain open so it #can be closed correctly in the tear down. old_client = self.client new_client = server.connect() self.client = new_client self.login("UserA", 'password') #create a project with two networks. network_1 = self.create_network_with_data(new_proj=True) network_2 = self.create_network_with_data(network_1.project_id) #Share a project which is read only with User B self.client.service.share_project(network_1.project_id, ["UserB"], 'Y', 'N') #share a network for editing with user C. THis should make #the project read accessible, but only one of the networks in the project. self.client.service.share_network(network_2.id, ["UserC"], 'N', 'N') self.client.service.logout("UserA") #User B should be able to see the project but not edit it or anything in it. self.login("UserB", 'password') userb_networks = self.client.service.get_networks(network_1.project_id) assert len(userb_networks.Network) == 2 userb_networks.Network[0].description = "Updated description" try: self.client.service.update_network(userb_networks.Network[0]) except Exception as e: assert e.fault.faultcode.find("HydraError") > 0 assert e.fault.faultstring.find("Permission denied.") >= 0 self.client.service.logout("UserB") #User C should be able to edit network 2 self.login("UserC", 'password') userc_networks = self.client.service.get_networks(network_2.project_id) assert len(userc_networks.Network) == 1 userc_networks.Network[0].description = "Updated description" updated_userc_net = self.client.service.update_network( userc_networks.Network[0]) assert updated_userc_net.description == "Updated description" #Now try to set the permission on network 2, so that user A, the creator #of the network, has their rights revolked. This should not succeed. self.assertRaises(suds.WebFault, self.client.service.set_project_permission, network_2.project_id, ["UserA"], 'N', 'N') self.client.service.logout("UserC") self.client = old_client
def __call__(self): try: output = connect(self.i) self.i = self.i + 1 return output except: print('no more data!') exit()
def connect_to_server(self): """Initiate connection to airborne server.""" server.setserver( ip=self.config.get('Network', 'ip'), role=gs.CONTROLLER, ip_controller=self.config.get('Network', 'ip_controller'), ) self.server = server.connect(self, MyImagesClient)
def test_share_project(self): #One client is for the 'root' user and must remain open so it #can be closed correctly in the tear down. old_client = self.client new_client = server.connect() self.client = new_client self.login("UserA", 'password') #create a project with two networks. network_1 = self.create_network_with_data(new_proj=True) network_2 = self.create_network_with_data(network_1.project_id) #Share a project which is read only with User B self.client.service.share_project(network_1.project_id, ["UserB"], 'Y', 'N') #share a network for editing with user C. THis should make #the project read accessible, but only one of the networks in the project. self.client.service.share_network(network_2.id, ["UserC"], 'N', 'N') self.client.service.logout("UserA") #User B should be able to see the project but not edit it or anything in it. self.login("UserB", 'password') userb_networks = self.client.service.get_networks(network_1.project_id) assert len(userb_networks.Network) == 2 userb_networks.Network[0].description = "Updated description" try: self.client.service.update_network(userb_networks.Network[0]) except Exception as e: assert e.fault.faultcode.find("HydraError") > 0 assert e.fault.faultstring.find("Permission denied.") >= 0 self.client.service.logout("UserB") #User C should be able to edit network 2 self.login("UserC", 'password') userc_networks = self.client.service.get_networks(network_2.project_id) assert len(userc_networks.Network) == 1 userc_networks.Network[0].description = "Updated description" updated_userc_net = self.client.service.update_network(userc_networks.Network[0]) assert updated_userc_net.description == "Updated description" #Now try to set the permission on network 2, so that user A, the creator #of the network, has their rights revolked. This should not succeed. self.assertRaises(suds.WebFault, self.client.service.set_project_permission,network_2.project_id, ["UserA"], 'N', 'N') self.client.service.logout("UserC") self.client = old_client
def connect_to_server(self): """Initiate connection to airborne server.""" server.setserver( ip=self.config.get('Network', 'ip'), #port=self.config.getint('Network', 'port'), role=self.config.get('Network', 'role'), ip_controller=self.config.get('Network', 'ip_controller'), ) self.server = server.connect(self, TargetsImagesClient)
def all_time(online=False): """ getting viewer distribution data and plotting :param online: (boolean) flag for plot.ly online plots :return: None """ conn = server.connect() cur = conn.cursor() cur.execute('''SELECT name, viewer_total FROM game_name''') rows = cur.fetchall() conn.close() total = sum(value for name, value in rows) new_rows = sorted([(name[:17], value) for name, value in rows if value > 0.005 * total], key=lambda x: x[1]) # + [('Other', sum(value for name, value in rows if value <= 0.01 * total))] label, values = zip(*new_rows) data = [ dict(y=label, x=values, type='bar', orientation='h', marker=dict(color='random')) ] layout = dict( title='Games By Viewers', titlefont=dict(size=48, ), margin=dict(l=100, pad=4), yaxis=dict( tickangle=-45, tickfont=dict(size=8), ), showlegend=False, ) fig = dict(data=data, layout=layout) plotly.plotly.image.save_as(fig, filename='static/img/plots/alltime', format='png') div = plotly.offline.plot(fig, output_type='div') with open('templates/plots/alltime.html', mode='w+') as f: f.write(div) if online: url = plotly.plotly.plot(fig, filename='All Time') print(plotly.tools.get_embed(url)) print 'created all time'
def test_share_network(self): #One client is for the 'root' user and must remain open so it #can be closed correctly in the tear down. old_client = self.client new_client = server.connect() self.client = new_client self.login("UserA", 'password') network_1 = self.create_network_with_data() network_2 = self.create_network_with_data() #Let User B view network 1, but not edit it (read_only is 'Y') self.client.service.share_network(network_1.id, ["UserB"], 'Y') #Let User C view and edit network 2; (read_only is 'N') self.client.service.share_network(network_2.id, ["UserC"], 'N', 'Y') self.client.service.logout("UserA") self.login("UserB", 'password') net1 = self.client.service.get_network(network_1.id) try: net2 = self.client.service.get_network(network_2.id) except Exception as e: print e net2 = None assert net1 is not None assert net2 is None self.client.service.logout("UserB") self.login("UserC", 'password') net2 = self.client.service.get_network(network_2.id) try: net1 = self.client.service.get_network(network_1.id) except Exception as e: print e net1 = None assert net1 is None assert net2 is not None #Now try to set the permission on network 2, so that user A, the creator #of the network, has their rights revolked. This should not succeed. self.assertRaises(suds.WebFault, self.client.service.set_network_permission, network_2.id, ["UserA"], 'N', 'N') self.client.service.logout("UserC") self.client = old_client
def test_share_network(self): #One client is for the 'root' user and must remain open so it #can be closed correctly in the tear down. old_client = self.client new_client = server.connect() self.client = new_client self.login("UserA", 'password') network_1 = self.create_network_with_data() network_2 = self.create_network_with_data() #Let User B view network 1, but not edit it (read_only is 'Y') self.client.service.share_network(network_1.id, ["UserB"], 'Y') #Let User C view and edit network 2; (read_only is 'N') self.client.service.share_network(network_2.id, ["UserC"], 'N', 'Y') self.client.service.logout("UserA") self.login("UserB", 'password') net1 = self.client.service.get_network(network_1.id) try: net2 = self.client.service.get_network(network_2.id) except Exception as e: print e net2 = None assert net1 is not None assert net2 is None self.client.service.logout("UserB") self.login("UserC", 'password') net2 = self.client.service.get_network(network_2.id) try: net1 = self.client.service.get_network(network_1.id) except Exception as e: print e net1 = None assert net1 is None assert net2 is not None #Now try to set the permission on network 2, so that user A, the creator #of the network, has their rights revolked. This should not succeed. self.assertRaises(suds.WebFault, self.client.service.set_network_permission,network_2.id, ["UserA"], 'N', 'N') self.client.service.logout("UserC") self.client = old_client
def connection_screen(players): #Enter the IP Address of your PC here ip = "192.168.0.3" port = 12456 screen = pygame.display.set_mode((display_width, display_height)) screen.fill(bg_color) pygame.display.set_caption('Connect all players') text_surface = display_text("All 4 connect to " + ip + " : " + str(port), (100, 0, 50), "small") screen.blit(text_surface, (100, 50)) text_surface = display_text( "After all players connected, press space to begin", (100, 0, 50), "small") screen.blit(text_surface, (100, 100)) pygame.display.update() server.connect(ip, port, players) while True: for event in pygame.event.get(): if event.type == pygame.QUIT: exit() if event.type == pygame.KEYDOWN: if event.key == pygame.K_SPACE: return
def set_ids(self): """ populates db_ids with giantbombids for searching :return: """ conn = connect() cur = conn.cursor() query = '''SELECT giantbombid FROM giantbomb''' cur.execute(query) fetch = cur.fetchall() fetch = [item[0] for item in fetch] self.db_ids = fetch conn.close() conn = connect() cur = conn.cursor() query = '''SELECT giantbombid, name FROM mismatch''' cur.execute(query) fetch = cur.fetchall() self.mismatch_ids = fetch conn.close()
def test(): conn = server.connect() try: os.remove("graph.dot") except: pass txn = transaction.transaction(tx_hash,conn) test=False txn.verify(True) test=open("graph.dot",'r').read() print(test) assert_equal(test,result)
def test(): conn = server.connect() try: os.remove("graph.dot") except: pass txn = transaction.transaction(tx_hash, conn) test = False txn.verify(True) test = open("graph.dot", 'r').read() print(test) assert_equal(test, result)
def add_db(self, api, name): """ db add with api call :param api: (str) api url :param name: (str) name of game :return: None """ fields = requests.get(api, params=dict( format='json', api_key=self.token ), headers=self.headers ).json() row = fields['results'] data = dict( name=name, giantbombid=row['id'], alias=row['aliases'], api=row['api_detail_url'], release=row['original_release_date'], deck=row['deck'] ) conn = connect() cur = conn.cursor() # looping through tablenames to populate tables for tablename in self.tablenames: self.add_resource(data['giantbombid'], tablename, row, cur) # final query to giantbomb table query = ''' INSERT INTO giantbomb VALUES ( %(giantbombid)s, %(name)s, %(alias)s, %(api)s, %(release)s, %(deck)s ) ''' cur.execute(query, data) conn.commit() conn.close()
def trialid(): """ fetches highest trial id number :return: (int) trial id """ conn = connect() cur = conn.cursor() cur.execute('''SELECT trialid FROM snapshot ORDER BY trialid DESC LIMIT 1''') trialid = cur.fetchone() if trialid: trialid = trialid[0] else: trialid = 1 conn.close() return trialid
def add_db_no_api(name, giantbombid): """db call for no api :param name: (str) name of game :param giantbombid: (int) id of game :return: None """ conn = connect() cur = conn.cursor() query = '''INSERT INTO giantbomb VALUES ( %(giantbombid)s, %(name)s ) ''' cur.execute(query, dict(giantbombid=giantbombid, name=name)) conn.commit() conn.close()
def test_unshare_project(self): #One client is for the 'root' user and must remain open so it #can be closed correctly in the tear down. old_client = self.client new_client = server.connect() self.client = new_client self.login("UserA", 'password') #create a project with two networks. network_1 = self.create_network_with_data(new_proj=True) self.create_network_with_data(project_id=network_1.project_id) #Share a project which is read only with User B self.client.service.share_project(network_1.project_id, ["UserB"], 'Y') self.client.service.logout("UserA") self.login("UserB", 'password') userb_networks = self.client.service.get_networks(network_1.project_id) assert len(userb_networks.Network) == 2 self.client.service.logout("UserB") #re-login as user A and un-share the project self.login("UserA", 'password') self.client.service.set_project_permission(network_1.project_id, ["UserB"], 'N', 'N') self.client.service.logout("UserA") #re-login as user B and try to access the formerly accessible project self.login("UserB", 'password') try: userb_networks = self.client.service.get_networks( network_1.project_id) except Exception as e: assert e.fault.faultcode.find("HydraError") > 0 assert e.fault.faultstring.find("Permission denied.") >= 0 self.client.service.logout("UserB") #reset the client to the 'root' client for a consistent tearDown self.client = old_client
def add_db(self, api, name): """ db add with api call :param api: (str) api url :param name: (str) name of game :return: None """ fields = requests.get(api, params=dict(format='json', api_key=self.token), headers=self.headers).json() row = fields['results'] data = dict(name=name, giantbombid=row['id'], alias=row['aliases'], api=row['api_detail_url'], release=row['original_release_date'], deck=row['deck']) conn = connect() cur = conn.cursor() # looping through tablenames to populate tables for tablename in self.tablenames: self.add_resource(data['giantbombid'], tablename, row, cur) # final query to giantbomb table query = ''' INSERT INTO giantbomb VALUES ( %(giantbombid)s, %(name)s, %(alias)s, %(api)s, %(release)s, %(deck)s ) ''' cur.execute(query, data) conn.commit() conn.close()
def test_unshare_project(self): #One client is for the 'root' user and must remain open so it #can be closed correctly in the tear down. old_client = self.client new_client = server.connect() self.client = new_client self.login("UserA", 'password') #create a project with two networks. network_1 = self.create_network_with_data(new_proj=True) self.create_network_with_data(project_id=network_1.project_id) #Share a project which is read only with User B self.client.service.share_project(network_1.project_id, ["UserB"], 'Y') self.client.service.logout("UserA") self.login("UserB", 'password') userb_networks = self.client.service.get_networks(network_1.project_id) assert len(userb_networks.Network) == 2 self.client.service.logout("UserB") #re-login as user A and un-share the project self.login("UserA", 'password') self.client.service.set_project_permission(network_1.project_id, ["UserB"], 'N', 'N') self.client.service.logout("UserA") #re-login as user B and try to access the formerly accessible project self.login("UserB", 'password') try: userb_networks = self.client.service.get_networks(network_1.project_id) except Exception as e: assert e.fault.faultcode.find("HydraError") > 0 assert e.fault.faultstring.find("Permission denied.") >= 0 self.client.service.logout("UserB") #reset the client to the 'root' client for a consistent tearDown self.client = old_client
def connect_method(connection_obj): ''' Establishing a tunnel along to the server, generating the payload format and then sending it to the client. ''' socket_obj, buffer_size, logfile = server.start_server() ip_address = server.connect(socket_obj, buffer_size, logfile) CRLF, SP = "\r\n", " " status_code = "204" + SP message = "HTTP/1.1 204 No Content" + CRLF server = "Server: " + ip_address + CRLF connection = "Connection: Keep-Alive" + CRLF date = sub_methods.current_date() + CRLF ''' Forming the actual payload/response message by attaching the header values. ''' payload = status_code + message + server + connection + date try: connection_obj.send(payload.encode()) except socket.error as e: print("Error: {}".format(e)) sys.exit(1)
def test_unshare_network(self): #One client is for the 'root' user and must remain open so it #can be closed correctly in the tear down. old_client = self.client new_client = server.connect() self.client = new_client self.login("UserA", 'password') network_1 = self.create_network_with_data() self.client.service.share_network(network_1.id, ["UserB"], 'Y', 'N') self.client.service.logout("UserA") self.login("UserB", 'password') net1 = self.client.service.get_network(network_1.id) assert net1 is not None self.client.service.logout("UserB") self.login("UserA", 'password') self.client.service.set_network_permission(network_1.id, ["UserB"], 'N', 'Y') self.client.service.logout("UserA") #re-login as user B and try to access the formerly accessible project self.login("UserB", 'password') try: self.client.service.get_network(network_1.id) except Exception as e: assert e.fault.faultcode.find("HydraError") > 0 assert e.fault.faultstring.find("Permission denied.") >= 0 self.client.service.logout("UserB") self.client = old_client
def drop_tables(): """KILL ALL THE TABLES""" conn = server.connect() cur = conn.cursor() cur.execute("DROP TABLE snapshot") cur.execute("DROP TABLE game_name") cur.execute("DROP TABLE ratingbomb") cur.execute("DROP TABLE rating") cur.execute("DROP TABLE franchisebomb") cur.execute("DROP TABLE franchise") cur.execute("DROP TABLE publisherbomb") cur.execute("DROP TABLE publisher") cur.execute("DROP TABLE platformbomb") cur.execute("DROP TABLE platform") cur.execute("DROP TABLE genrebomb") cur.execute("DROP TABLE genre") cur.execute("DROP TABLE themebomb") cur.execute("DROP TABLE theme") cur.execute("DROP TABLE mismatch") cur.execute("DROP TABLE giantbomb") conn.commit() conn.close()
def add_mismatch(self, name): """adding mismatch to table :param name: (int) name of game :return: giantbombid (int) """ giantbombid = self.search_name(name) conn = connect() cur = conn.cursor() if not giantbombid: print('no match found') query = '''SELECT giantbombid FROM giantbomb ORDER BY giantbombid DESC''' cur.execute(query) fetch = cur.fetchone() if fetch[0] < 1000000: giantbombid = 1000000 # setting id to high value to avoid mismatch else: giantbombid = fetch[0] + 1 # setting from highest value query = '''INSERT INTO mismatch VALUES ( DEFAULT, %(name)s, %(giantbombid)s ) ''' cur.execute(query, dict(name=name, giantbombid=giantbombid)) conn.commit() conn.close() return giantbombid
def make_tables(): """MAKE ALL THE TABLES""" conn = server.connect() cur = conn.cursor() # adding new tables cur.execute('''CREATE TABLE snapshot (ID SERIAL PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, GIANTBOMBID INT NOT NULL, TRIALID SERIAL NOT NULL, RANK INT NOT NULL, VIEWERS INT NOT NULL, CHANNELS INT NOT NULL, STAMP TIMESTAMP NOT NULL );''') cur.execute('''CREATE TABLE game_name (ID SERIAL PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, GIANTBOMBID INT NOT NULL, VIEWER_TOTAL INT NOT NULL, CHANNEL_TOTAL INT NOT NULL, RANK_TOTAL INT NOT NULL, TRIALS INT NOT NULL );''') cur.execute('''CREATE TABLE ratingbomb (RATINGBOMBID SERIAL PRIMARY KEY NOT NULL, RATINGID INT NOT NULL, GIANTBOMBID INT NOT NULL ); ''') cur.execute('''CREATE TABLE rating (RATINGID INT PRIMARY KEY NOT NULL, RATING TEXT NOT NULL ); ''') cur.execute('''CREATE TABLE franchisebomb (FRANCHISEBOMBID SERIAL PRIMARY KEY NOT NULL, FRANCHISEID INT NOT NULL, GIANTBOMBID INT NOT NULL ); ''') cur.execute('''CREATE TABLE franchise (FRANCHISEID INT PRIMARY KEY NOT NULL, FRANCHISE TEXT NOT NULL ); ''') cur.execute('''CREATE TABLE publisherbomb (PUBLISHERBOMBID SERIAL PRIMARY KEY NOT NULL, PUBLISHERID INT NOT NULL, GIANTBOMBID INT NOT NULL ); ''') cur.execute('''CREATE TABLE publisher (PUBLISHERID INT PRIMARY KEY NOT NULL, PUBLISHER TEXT NOT NULL ); ''') cur.execute('''CREATE TABLE platformbomb (PLATFORMID SERIAL PRIMARY KEY NOT NULL, PLATFORM INT NOT NULL, GIANTBOMBID INT NOT NULL ); ''') cur.execute('''CREATE TABLE platform (PLATFORMID INT PRIMARY KEY NOT NULL, PLATFORM TEXT NOT NULL ); ''') cur.execute('''CREATE TABLE genrebomb (GENREBOMBID SERIAL PRIMARY KEY NOT NULL, GENREID INT NOT NULL, GIANTBOMBID INT NOT NULL ); ''') cur.execute('''CREATE TABLE genre (GENREID INT PRIMARY KEY NOT NULL, GENRE TEXT NOT NULL ); ''') cur.execute('''CREATE TABLE themebomb (THEMEBOMBID SERIAL PRIMARY KEY NOT NULL, THEMEID INT NOT NULL, GIANTBOMBID INT NOT NULL ); ''') cur.execute('''CREATE TABLE theme (THEMEID INT PRIMARY KEY NOT NULL, THEME TEXT NOT NULL ); ''') cur.execute('''CREATE TABLE mismatch (MISMATCHID SERIAL PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, GIANTBOMBID INT NOT NULL ); ''') cur.execute('''CREATE TABLE giantbomb (GIANTBOMBID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, ALIAS TEXT NULL, API TEXT NULL, RELEASE TIMESTAMP NULL, DECK TEXT NULL ); ''') cur.execute('''CREATE TABLE mismatch (MISMATCHID SERIAL PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, GIANTBOMBID INT NOT NULL ); ''') cur.execute('''CREATE TABLE stream (DEFAULT SERIAL PRIMARY KEY NOT NULL, STREAMID BIGINT NOT NULL, CHANNELID INT NOT NULL, URL TEXT NOT NULL, LANGUAGE TEXT NULL, SCHEDULED BOOLEAN NOT NULL, FEATURED BOOLEAN NOT NULL, MATURE BOOLEAN NULL, PARTNER BOOLEAN NOT NULL, SPONSORED BOOLEAN NOT NULL, GAME TEXT NOT NULL, VIEWERS INT NOT NULL, FOLLOWERS INT NOT NULL, TOTALVIEWS INT NOT NULL, VIDEOS INT NOT NULL, TEAMS INT NULL, STAMP TIMESTAMP NOT NULL ); ''') cur.execute('''CREATE TABLE team (TEAMID BIGINT NOT NULL, CHANNELID BIGINT NOT NULL, TEAMNAME TEXT NULL, STAMP TIMESTAMP NOT NULL ); ''') conn.commit() conn.close()
def transaction_test(): conn = server.connect() for tx_hash in hashes: #each hash is its own test case yield assert_true, transaction.transaction(tx_hash, conn).verify()
def all_time(online=False): """ getting viewer distribution data and plotting :param online: (boolean) flag for plot.ly online plots :return: None """ conn = server.connect() cur = conn.cursor() cur.execute('''SELECT name, viewer_total FROM game_name''') rows = cur.fetchall() conn.close() total = sum(value for name, value in rows) new_rows = sorted([(name[:17], value) for name, value in rows if value > 0.005 * total], key=lambda x: x[1]) # + [('Other', sum(value for name, value in rows if value <= 0.01 * total))] label, values = zip(*new_rows) data = [dict( y=label, x=values, type='bar', orientation='h', marker=dict( color='random' ) )] layout = dict( title='Games By Viewers', titlefont=dict( size=48, ), margin=dict( l=100, pad=4 ), yaxis=dict( tickangle=-45, tickfont=dict( size=8 ), ), showlegend=False, ) fig = dict(data=data, layout=layout) plotly.plotly.image.save_as(fig, filename='static/img/plots/alltime', format='png') div = plotly.offline.plot(fig, output_type='div') with open('templates/plots/alltime.html', mode='w+') as f: f.write(div) if online: url = plotly.plotly.plot(fig, filename='All Time') print(plotly.tools.get_embed(url)) print 'created all time'
def time_series(online=False): """ getting time series data and plotting :param online: (boolean) flag for plot.ly online plots :return: None """ conn = server.connect() cur = conn.cursor() cur.execute(''' SELECT name, viewers, stamp FROM snapshot WHERE stamp >= '2016-04-02' ORDER BY name, stamp ASC ''') rows = cur.fetchall() conn.close() data = [] labels = [] viewers = [] timestamps = [] count = 0 for row in rows: if not labels: labels = row[0] if labels == row[0]: viewers.append(row[1]) timestamps.append(row[2]) else: count += 1 trace = plotly.graph_objs.Scatter( x=timestamps, y=viewers, name=labels, ) data.append(trace) labels = row[0] viewers = [row[1]] timestamps = [row[2]] layout = dict( title='Games - Time Series', titlefont=dict( size=48, ), showlegend=False ) fig = dict(data=data, layout=layout) div = plotly.offline.plot(fig, output_type='div') with open('templates/plots/timeseries.html', mode='w+') as f: f.write(div) plotly.plotly.image.save_as(fig, filename='static/img/plots/timeseries', format='png') if online: url = plotly.plotly.plot(fig, filename='Time Series') print(plotly.tools.get_embed(url))
#!/usr/bin/env python import db import sensor import server import move database = db.connect_db() cur = db.get_cursor(database) db.reset_table(cur) client=server.connect() while True: db.insert_data(database, cur, sensor.measure()) move.move(server.listen(client)) db.display_db(cur) db.close_db(database)
def create_plot(table_name, online=False): """ generating pie chart for table :param table_name: (str) name of table :param online: (boolean) flag for plot.ly online plots :return: None """ conn = server.connect() cur = conn.cursor() param = dict( name=table_name, namebomb=table_name + 'bomb', nameid=table_name + 'id' ) query = '''SELECT %(name)s.%(name)s, SUM(game_name.viewer_total) AS viewers FROM %(namebomb)s INNER JOIN game_name ON game_name.giantbombid = %(namebomb)s.giantbombid INNER JOIN %(name)s ON %(namebomb)s.%(nameid)s = %(name)s.%(nameid)s''' % param if table_name == 'rating': query += ''' WHERE rating.rating LIKE 'ESRB%' GROUP BY rating.rating ORDER BY rating.rating ASC; ''' elif table_name =='platform': query = ''' SELECT platformgroup.platformgroup, SUM(game_name.viewer_total) AS viewers FROM platformbomb INNER JOIN game_name ON game_name.giantbombid = platformbomb.giantbombid INNER JOIN platform ON platformbomb.platformid = platform.platformid INNER JOIN platformgroup ON platformgroup.platformid = platform.platformid GROUP BY platformgroup.platformgroup ORDER BY viewers DESC; ''' else: query += ''' GROUP BY %(name)s.%(name)s ORDER BY viewers DESC; ''' % param cur.execute(query) rows = cur.fetchall() conn.close() if table_name == 'genre': total = sum(value for name, value in rows) rows = [(name, value) for name, value in rows if value > 0.01 * total] + [('Other', sum(value for name, value in rows if value <= 0.01 * total))] label, values = zip(*sorted(rows, key=lambda x: -x[1])) data = [dict( y=values, x=label, marker=dict(color=['#1F77B4', '#FF7F0E', '#2CA02C', '#D62728', '#103D5D', '#9467BD', '#17BECF', '#E377C2', '#FFB574', '#57A9E2']), type='bar', )] layout = dict( title=table_name.title() + ' By Viewers', titlefont=dict( size=48, ), showlegend=False ) fig = dict(data=data, layout=layout) plotly.plotly.image.save_as(fig, filename='static/img/plots/' + table_name, format='png') div = plotly.offline.plot(fig, output_type='div') with open('templates/plots/' + table_name + '.html', mode='w+') as f: f.write(div) if online: url = plotly.plotly.plot(fig, filename=table_name) print(plotly.tools.get_embed(url)) print 'created ' + table_name
def connect(self): server = connect() con_vm = server return con_vm
def create_stream_model(stream_ids, name): """ creates plot and a list of stream objects with identifiers :param name: (string) name of game :param stream_ids: (list) list of stream id api keys :return: (list) list of dict of stream objects """ starscream = dill.load(open('application/models/' + CLEANNAME.sub('', name) + '.dill', mode='rb')) print 'loaded model' conn = server.connect() cur = conn.cursor() query = '''SELECT stamp, viewers FROM snapshot WHERE name = %(name)s AND stamp >= '2016-04-07' ORDER BY stamp ASC ''' cur.execute(query, dict(name=name)) fetch = cur.fetchall() conn.close() print 'loaded data' times, actual = zip(*fetch) predicted = starscream.predict(dict(times=times)) data = [ dict( x=times, y=predicted, type='scatter', name='Machine', stream=dict(token=stream_ids[0]) ), dict( x=times, y=actual, type='scatter', mode='markers', name=name, stream=dict(token=stream_ids[1]) ), dict( x=times, y=0 if actual == 0 else abs((predicted - actual)/actual)*100, type='scatter', name='% Error', stream=dict(token=stream_ids[2]) ), ] stream = [ dict( stream_obj=plotly.plotly.Stream(stream_ids[0]), name='Machine', stream_id=stream_ids[0] ), dict( stream_obj=plotly.plotly.Stream(stream_ids[1]), name='Viewers', stream_id=stream_ids[1] ), dict( stream_obj=plotly.plotly.Stream(stream_ids[2]), name='% Error', stream_id=stream_ids[2] ) ] layout = dict(title=name) fig = dict(data=data, layout=layout) print 'loaded plot' url = plotly.plotly.plot(fig, filename='model', auto_open=False) return plotly.tools.get_embed(url), stream
def test_hide_data(self): """ Test for the hiding of data. Create a network with some data. Hide the timeseries created, check if another user can see it. Share the time series with one users. Check if they can see it but a third user can't. """ #One client is for the 'root' user and must remain open so it #can be closed correctly in the tear down. old_client = self.client new_client = server.connect() self.client = new_client self.login("UserA", 'password') network_1 = self.create_network_with_data() #Let User B view network 1, but not edit it (read_only is 'Y') self.client.service.share_network(network_1.id, ["UserB", "UserC"], 'Y') scenario = network_1.scenarios.Scenario[0] data = [x.value for x in scenario.resourcescenarios.ResourceScenario] data_to_hide = data[-1].id self.client.service.hide_dataset(data_to_hide, ["UserB"], 'Y', 'Y', 'Y') self.client.service.logout("UserA") self.login("UserB", 'password') netA = self.client.service.get_network(network_1.id) scenario = netA.scenarios.Scenario[0] data = [x.value for x in scenario.resourcescenarios.ResourceScenario] for d in data: if d.id == data_to_hide: assert d.hidden == 'Y' assert d.value is not None else: #The rest of the data is unhidden, so should be there. assert d.hidden == 'N' assert d.value is not None #Check user B can see the dataset self.client.service.logout("UserB") self.login("UserC", 'password') #Check user C cannot see the dataset netB = self.client.service.get_network(network_1.id) scenario = netB.scenarios.Scenario[0] data = [x.value for x in scenario.resourcescenarios.ResourceScenario] for d in data: if d.id == data_to_hide: assert d.hidden == 'Y' assert d.value is None else: #The rest of the data is unhidden, so should be there. assert d.hidden == 'N' assert d.value is not None self.client.service.logout("UserC") self.client = old_client
def test_edit_hidden_data(self): """ test_edit_hidden_data Test for the case where one user hides data and another user sets the data to something else. User A Creates a network with some data User A Hides the timeseries created. User A shares network with User B Check user B cannot see timeseries value User B sets value of timeseries to something else. Save network. Attribute now should have a new, unhidden dataset assigned to that attribute. """ #One client is for the 'root' user and must remain open so it #can be closed correctly in the tear down. old_client = self.client new_client = server.connect() self.client = new_client self.login("UserA", 'password') network_1 = self.create_network_with_data() #Let User B view network 1, but not edit it (read_only is 'Y') self.client.service.share_network(network_1.id, ["UserB"], 'N') scenario = network_1.scenarios.Scenario[0] data = [x for x in scenario.resourcescenarios.ResourceScenario] for d in data: if d.value.type == 'timeseries': attr_to_be_changed = d.resource_attr_id data_to_hide = d.value.id break self.client.service.hide_dataset(data_to_hide, [], 'Y', 'Y', 'Y') self.client.service.logout("UserA") self.login("UserB", 'password') netA = self.client.service.get_network(network_1.id) scenario = netA.scenarios.Scenario[0] #Find the hidden piece of data and replace it with another #to simulate a case of two people working on one attribute #where one cannot see the value of it. for d in scenario.resourcescenarios.ResourceScenario: if d.resource_attr_id == attr_to_be_changed: #THis piece of data is indeed the hidden one. assert d.value.hidden == 'Y' t1 = datetime.datetime.now() t2 = t1+datetime.timedelta(hours=1) ts_val = {0: {t1: [11, 21, 31, 41, 51], t2: [12, 22, 32, 42, 52]}} #Reassign the value of the dataset to something new. d.value.value = json.dumps(ts_val) else: #The rest of the data is unhidden, so should be there. assert d.value.hidden == 'N' assert d.value.value is not None updated_net = self.client.service.update_network(netA) updated_net = self.client.service.get_network(updated_net.id) scenario = updated_net.scenarios.Scenario[0] #After updating the network, check that the new dataset #has been applied for d in scenario.resourcescenarios.ResourceScenario: if d.resource_attr_id == attr_to_be_changed: assert d.value.hidden == 'N' assert d.value.id != data_to_hide #Now validate that the dataset was not overwritten, but replaced #by getting the old dataset and ensuring user B can still not see it. hidden_dataset = self.client.service.get_dataset(data_to_hide) assert hidden_dataset.hidden == 'Y' assert hidden_dataset.value == None self.client.service.logout("UserB") self.client = old_client
key = cryptic.get_key() config = ConfigParser() config.read('config.ini') email = config.get("Email", "Email") emailPass = config.get("Email", "Password") host = config.get("MySQL", "Host") sql_user = config.get("MySQL", "User") password = config.get("MySQL", "Password") database = config.get("MySQL", "Database") try: db = server.connect(host, sql_user, password, database) c = db.cursor() except Exception as e: logger.exception(f"Exception {e} caught, exiting...") exit() MASTERPASS = "" USERPASS = "" empty = False valid = False with open("data/save.pickle", "r+b") as f: if os.path.getsize("data/save.pickle") == 0: empty = True f.close()
def setup(): server.connect()