def gainXp(arguments): errorCode = None username = arguments[0] reason = arguments[1] amount = arguments[2] #currentXP = executeSql("SELECT xp FROM `users` WHERE username = '******'", # username)[0]['xp'] currentXp = executeMDb('users', 'find', {'username': username}) newXp = currentXp + amount #print(currentXP[0]['xp']) #if executeSql("UPDATE users SET xp='{}' WHERE username='******'", # (newXP, username)) == (): if executeMDb('users', 'update', { 'username': username, '$set': { 'xp': newXp } }) != 1: log.writeLog(username, 'info', 'Gained {} xp. Reason: {}'.format(amount, reason)) errorCode = 1 else: errorCode = 0 return errorCode
def login(): if request.method == 'GET': return render_template('loginRegisterTemplate.html', formAction='login') # ''' # <form action='login' method='POST'> # <input type='text' name='username' id='username' placeholder='username'/> # <input type='password' name='password' id='password' placeholder='password'/> # <input type='submit' name='submit'/> # </form> username = request.form['username'] password = request.form['password'] errorCode = users.UsernameAndPasswordValidity(username, password) usernameValidCode = (errorCode == 1) if usernameValidCode: user = User() user.id = username login_user(user) username = flask_login.current_user.id log.writeLog(username, 'Log in.', 1) return render_template( 'simpleResponseTemplate.html', response='You were logged in as ' + flask_login.current_user.id, title='Logged in.' ) #'<p>Logged in as ' + flask_login.current_user.id + '</p>' log.writeLog(username, 'Log in.', 0) return render_template( 'simpleResponseTemplate.html', response='An error occurred while trying to log in.', title='Error while logging in')
def analyse_results(Ypred, Y, trackNames=trackNames, details=1): """ Analyse the results, several levels of details: 0: Print the score 1: Print the score and confusion matrix 2: Print the score, confusion matrix, and prediction for each track """ print("Labels: ", Y) print("Prediction:", Ypred) score = sum([1 if Ypred[i] == yi else 0 for i, yi in enumerate(Y)]) ratio = score / len(Y) writeLog("info", "Score: {:03f} ({}/{})".format(ratio, score, len(Y))) if details >= 1: cnf_matrix = confusion_matrix(Y, Ypred) np.set_printoptions(precision=2) # Plot non-normalized confusion matrix plt.figure() plot_confusion_matrix(cnf_matrix, classes=labels, title='Confusion matrix') plt.show() if details >= 2: for i, track in enumerate(trackNames): if Ypred[i] != Y[i]: res = "{} -> {} ({})".format(track, labels[Ypred[i]], labels[Y[i]]) res = "\033[91m{}\033[0m".format(res) else: res = "{} -> {}".format(track, labels[Y[i]]) print(res)
def create_data_from_files(): """Compute the data (features, labels,...) from audio files and save the results""" X = [] Y = [] trackNames = [] # Extract all the features for i, f in enumerate(folders): samples = glob.glob("../data/samples/{}/*.wav".format(f)) for s in samples: # es = file_actions.extract_sound_light(s, ratio=0.5, duration=10) # test light es = file_actions.extract_sound(s) esm = np.array(file_actions.convert_to_mono(es[0])[0]) d = {"label": i, "sound": esm, "params": es[1], "file": s} X.append(extract_features.extract_all_features(d)) Y.append(d["label"]) trackNames.append(file_name_to_track_name(d["file"])) # Make arrays fron the data X = np.array(X) Y = np.array(Y) flabels = extract_features.features_labels() # Save the data and returning it np.savetxt("../tmp/X.csv", X) np.savetxt("../tmp/Y.csv", Y) with open("../tmp/flabels.txt", "w") as f: f.write("\n".join(flabels)) with open("../tmp/trackNames.txt", "w") as f: f.write("\n".join(trackNames)) writeLog("info", "File extraction finished") return X, Y, flabels, trackNames
def display_params(soundParams): """Display some sound metadata""" nbch, ss, sf, nbs = soundParams[:4] writeLog("info", "Number of channels: {}".format(nbch), {"inFile": False}) writeLog("info", "Sample size in bytes: {}".format(ss), {"inFile": False}) writeLog("info", "Sampling frequency: {}".format(sf), {"inFile": False}) writeLog("info", "Number of samples: {}".format(nbs), {"inFile": False}) writeLog("info", "Duration : {:.1f}s".format(nbs / sf), {"inFile": False})
def convert_to_stereo(sound): """Converts a mono sound to stereo""" nbch = len(sound) if nbch == 2: return sound elif nbch == 1: return np.array([sound[0], sound[0]]) else: writeLog("error", "Not a mono nor stereo file")
def convert_to_mono(sound): """Converts a stereo sound to mono""" nbch = len(sound) if nbch == 1: return sound elif nbch == 2: return np.array([(sound[0] / 2 + sound[1] / 2)]).astype(sound.dtype) else: writeLog("error", "Not a mono nor stereo file")
def retInfo(self,v,retak): seavfile = time.strftime("%d%m%Y") if len(retak) != 0: print echo.fail(v + " Vulnerability connection : " + str(len(retak)) + "\r\n") for ra in retak: log.writeLog(str(seavfile),self.ackMod + " " + self.testWeb + v + " " + ra) print echo.high(ra) print "log save ok. path : " + log.getLogFile() + "/" + self.testWeb + ".log\r\n" raw_input(echo.high("Log Save Ok .. Press Enter to continue: "))
def printResults(labels, output): """Logs the results""" writeLog("debug", "output: {}".format(", ".join([str(x).zfill(2) for x in output]))) writeLog("debug", "labels: {}".format(", ".join([str(y).zfill(2) for y in labels]))) labels, output = fillIfNeeded(labels, output) confusionMatrix(labels, output) perfsByLabel(labels, output)
def timer_stop(category): """Stops the timer, log and returns the time in seconds""" try: dt = perf_counter() - time_data[category] writeLog("time", '{:.03f}s for "{}"'.format(dt, category), {"inFile": False}) return dt except KeyError: writeLog("warn", 'No time data for "{}"'.format(category)) return 0
def retweet(str, num=5): for tweet in tweepy.Cursor(api.search, str).items(num): try: if tweet.user.screen_name != user.screen_name: tweet.favorite() tweet.retweet() writeLog('Retweeted tweet by: ' + tweet.user.screen_name) except tweepy.TweepError as e: writeLog(e.reason) except StopInteration: break
def setShortFilePath(folder): if folder is None: # If the user just loaded the page there won't be a directory, so documents is default folder = "Documents" writeLog("Directory: " + folder) # Add the /static/media path to the full path shortPath = '/static/media/' + folder + '/' writeLog("Path: " + shortPath) return shortPath
def getTopLinkRatings(): try: username = flask_login.current_user.id except: username = '******' apiResponse = linkApi.executeApiAction('getTopLinkRatings') errorCode = apiResponse['errorCode'] log.writeLog(username, 'Get top links', errorCode) return jsonify(apiResponse)
def folder_mp3_to_wav(): """Convert all not already converted mp3 to wav""" for _, f in enumerate(os.listdir("../data/samplesMP3")): samplesMP3 = glob.glob("../data/samplesMP3/{}/*.mp3".format(f)) for s in samplesMP3: trackName = os.path.splitext(os.path.split(s)[1])[0] swav = "../data/samples/{}/{}.wav".format(f, trackName) if not os.path.isdir(os.path.split(swav)[0]): os.mkdir(os.path.split(swav)[0]) if not os.path.isfile(swav): to_wav(s, swav) writeLog("debug", "MP3 to WAV conversions finished.")
def getLinkRating(): try: username = flask_login.current_user.id except: username = '******' link = request.json['link'] apiResponse = linkApi.executeApiAction('getLinkRating', link) errorCode = apiResponse['errorCode'] log.writeLog(username, 'Get rating for {}'.format(link), errorCode) return jsonify(apiResponse)
def write_sound(sound, soundParams, soundFileLoc): """Write sound in wav file soundFileLoc, croping values (saturation)""" timer_start("Writing {}".format(soundFileLoc)) nbch, ss, _, nbs = soundParams[:4] if nbch == 1: rawSound = sound[0] elif nbch == 2: rawSound = sound.T.reshape((1, 2 * sound.shape[1]))[0, :] else: writeLog("error", "Not a mono nor stereo file") soundFile = wave.open(soundFileLoc, "w") soundFile.setparams(soundParams) # writing binary of nbch*nbs int, taken from rawSound soundFile.writeframes( wave.struct.pack("<" + str(nbch * nbs) + ("-bhiq"[ss]), *rawSound)) soundFile.close() timer_stop("Writing {}".format(soundFileLoc)) print("Fichier {} created!".format(soundFileLoc))
def addRating(): try: username = flask_login.current_user.id except: username = '******' #return request.form['link'] response = request.json link = response['link'] rating = response['rating'] apiResponse = linkApi.executeApiAction('addRating', (username, link, rating)) errorCode = apiResponse['errorCode'] log.writeLog(username, 'Add rating for {} ({}/10)'.format(link, rating), errorCode) #else: # log.writeLog(username, 'Calculating for {} failed.'.format(link),errorCode) return jsonify(apiResponse) #'ffactory'
def extract_sound(soundFileLoc): """Extract the wav file soundFileLoc into a numpy array, shape (nbch, nbs)""" timer_start("Extracting {}".format(soundFileLoc)) err_return = np.array([[]]), (0, 0, 0, 0, "NONE", "not compressed") if os.path.splitext(soundFileLoc)[1].lower() in (".mp3", ".wav", ".au"): try: audiofile = AudioSegment.from_file(soundFileLoc) except Exception: writeLog("error", "File not found or other I/O error. (DECODING FAILED)") return err_return if audiofile.sample_width == 2: data = np.fromstring(audiofile.raw_data, np.int16) elif audiofile.sample_width == 4: data = np.fromstring(audiofile.raw_data, np.int32) else: writeLog("error", "extract_sound(): sample_width is not 2 or 4") return err_return sf = audiofile.frame_rate x = [] for chn in range(audiofile.channels): x.append(data[chn::audiofile.channels]) x = np.array(x) else: writeLog("error", "readAudioFile(): Unknown file type!") return err_return timer_stop("Extracting {}".format(soundFileLoc)) # number of channels, sample size, sampling frequency, number of samples return x, (x.shape[0], audiofile.sample_width, sf, x.shape[1], "NONE", "not compressed")
def read_config(self): import log path = ('/'.join((__file__.split('/'))[:-1])) + '/server.config' if not check_perm(path, "r"): log.writeLog("Error reading Config File", 1, log.log_error) return config = configparser.ConfigParser() #config.sections() config.read(path) vals = [self._documentRoot, self._logFileName, self._maxConnections] for i, key in enumerate(keys): try: vals[i] = config['DEFAULT'][key] except: pass if vals[0] != '': self._documentRoot = vals[0] if vals[1] != '': self._logFileName = vals[1] if vals[2] != '': self._maxConnections = vals[2]
def getTheme(): writeLog('Getting theme') theme = request.cookies.get('theme') if theme is None: writeLog('Theme defaulted to blue') theme = 'blue' writeLog('theme cookie is ' + str(theme)) return theme
def setTheme(): theme = request.form['theme'] writeLog('Theme requested is ' + str(theme)) response = make_response(render_template('home.html', theme = theme)) writeLog('Response created') response.set_cookie('theme', theme) writeLog('Cookie set to ' + str(theme)) return response
def main(data, newData=""): log.writeLog(data, 'portForward', 1) portForwardObj = portForwardClass(data) portForwardObj.portForward() if newData != "": log.writeLog(newData, 'portForward-modify', 1) newData['action'] = 'add' newData['enable'] = '1' portForwardObj = portForwardClass(newData) portForwardObj.actionFun() log.writeLog(data, 'portForward', 2)
def setFilePath(folder): #Get the root of this python file root = os.path.dirname(os.path.abspath(__file__)) writeLog("Root: " + root) if folder is None: # If the user just loaded the page there won't be a directory, so documents is default folder = "Documents" writeLog("Directory: " + folder) # Add the /static/media path to the full path path = root + '/static/media/' + folder writeLog("Path: " + path) return path
def main(data): log.writeLog(data, 'upnp', 1) upnpObj = upnpClass(data) upnpObj.upnp() log.writeLog(data, 'upnp', 2)
auth.set_access_token(cfg['ACCESS_TOKEN'], cfg['ACCESS_TOKEN_SECRET']) api = tweepy.API(auth) parser = argparse.ArgumentParser() parser.add_argument('-u', '--user', dest='userName', help='The user_name of any authenticated tweeter user', default=cfg['DEFAULT_USER_NAME']) parser.add_argument('-c', '--count', dest='count', type=int, help='The no of last tweets on the user account', default=5) try: os.mkdir("users_log") except os.error: print 'The users_log directory is already present!' namespace=parser.parse_args() timeline = api.user_timeline(screen_name=namespace.userName, count=namespace.count) tweet_user=namespace.userName os.chdir("users_log") try: os.mkdir("%s" %tweet_user) except os.error: print ('The %s directory is already present!' %tweet_user) os.chdir("%s" %tweet_user) for tweet in timeline: tweet_time=str(tweet.created_at).replace(' ','-').replace(':','-') writeLog(tweet_time, tweet.text)
def main(data): log.writeLog(data, 'changeUserPwd', 1) changePwdObj = changePwdClass(data) changePwdObj.changeUserPwd() log.writeLog(data, 'changeUserPwd', 2)
def main(): log.writeLog("", 'diagnose', 1) diagnoseObj = diagnodeClass("") diagnoseObj.diagnose() log.writeLog("", 'diagnose', 2)
def main(data): log.writeLog(data, 'backupRestore', 1) backupObj = backupClass(data) backupObj.backupRestore() log.writeLog(data, 'backupRestore', 2)
from sklearn import linear_model from sklearn.metrics import confusion_matrix from globalStorage import GlobalStorage from log import initLog, writeLog import file_actions import extract_features # Reading the config file config = ConfigParser() cfile = os.path.join(os.getcwd(), "config.ini") config.read(cfile) # Logs initialization initLog(config) writeLog("info", "Program restarted") # Python version info sys.path.append(os.path.abspath(os.getcwd())) python_version = sys.version_info.major writeLog("debug", "Python version: {}".format(sys.version)) # Instanciationg the global storage gs = GlobalStorage() folders = os.listdir("../data/samples") writeLog("info", "Folders in ../data/samples: {}".format(folders)) file_actions.folder_mp3_to_wav() folders = os.listdir("../data/samples")
def to_mp3(ini, out, bitrate="320k"): """Converts the file to an mp3 file""" options = ["-threads", "auto", "-y", "-loglevel", "quiet"] call(["avconv", "-i", ini, "-c:a", "libmp3lame", "-ab", bitrate] + options + [out]) writeLog("debug", "File {} created!".format(out))
def to_wav(ini, out): """Converts the file to an wav file""" # options = ["-threads", "auto", "-y", "-loglevel", "quiet"] # call(["avconv", "-i", ini] + options + [out]) call(["mpg123", "-w", out, ini]) writeLog("debug", "File {} created!".format(out))
def main(data): log.writeLog(data, 'parentCtrl', 1) parentCtrlObj = parentCtrlClass(data) parentCtrlObj.parentCtrl() log.writeLog(data, 'parentCtrl', 2)