Ejemplo n.º 1
0
def download_file(filename):
    log.info(f'https://free-av-tools.com/trims/{filename}')
    extension = os.path.splitext(filename)[-1]
    if extension == ".m4a":
        return send_from_directory('trims', filename, mimetype="audio/mp4", as_attachment=True)
    else:
        return send_from_directory('trims', filename, as_attachment=True)
Ejemplo n.º 2
0
def trim_file():

    if request.form["request_type"] == "upload_complete":

        chosen_file = request.files["chosen_file"]
        # Make the filename safe
        filename_secure = secure_filename(chosen_file.filename)
        # Save the uploaded file to the uploads folder.
        chosen_file.save(os.path.join("uploads", filename_secure))
        return ''

    if request.form["request_type"] == "trim":

        filename = request.form["filename"]
        log_this(f'wants to trim: {filename}')
        uploaded_file_path = os.path.join("uploads", secure_filename(filename))
        start_time = request.form["start_time"]
        log.info(f'START TIME: {start_time}')
        end_time = request.form["end_time"]
        log.info(f'END TIME: {end_time}')
        ext = "." + filename.split(".")[-1]
        just_name = filename.split(".")[0]
        output_name = just_name + " [trimmed]" + ext
        output_file_path = os.path.join('trims', output_name)

        subprocess.run([
            'ffmpeg', '-y', '-i', uploaded_file_path, '-ss', start_time, '-to',
            end_time, '-map', '0', '-c', 'copy', output_file_path
        ],
                       shell=False)

        return output_file_path
Ejemplo n.º 3
0
def send_json_response(progress_file_path, video_id):

    for file in os.listdir(download_dir):

        # Delete the unnecessary files.
        if '.temp' in file or '.webp' in file:
            os.remove(os.path.join(download_dir, file))
        
        elif video_id in file:

            filesize = round((os.path.getsize(os.path.join(download_dir, file)) / 1_000_000), 2)
            log.info(f'{filesize} MB')

            user_ip = get_ip()
            # Query the database by IP.
            user = User.query.filter_by(ip=user_ip).first()

            if user:
                user.mb_downloaded += filesize
                db.session.commit()

            new_filename = file.replace('_', ' ').replace('#', '').replace(f'-{video_id}', '')
            os.replace(os.path.join(download_dir, file), os.path.join(download_dir, new_filename))

            with open("logs/downloads.txt", "a") as f:
                f.write(f'\n{new_filename}')

            return jsonify(download_path=os.path.join('downloads', new_filename), 
                           log_file=progress_file_path)
Ejemplo n.º 4
0
def delete_file(filepath):
    try:
        os.remove(filepath)
    except Exception as e:
        log.info(f'Unable to delete {filepath}:\n{e}')
    else:
        log.info(f'{Path(filepath).name} deleted.')
Ejemplo n.º 5
0
def send_file(filename):
    log.info(f'https://freeaudioconverter.net/conversions/{filename}')
    extension = os.path.splitext(filename)[-1]
    if extension == ".m4a":
        return send_from_directory('conversions', filename, mimetype="audio/mp4", as_attachment=True)
    else:
        return send_from_directory('conversions', filename, as_attachment=True)
Ejemplo n.º 6
0
def return_download_path():
    filename = [file for file in os.listdir(download_dir) if Path(file).suffix not in unwanted_filetypes and 
                Path(file).stem == session['filename_stem']][0]

    filesize = round((os.path.getsize(os.path.join(download_dir, filename)) / 1_000_000), 2)
    update_database(filesize)

    # Remove any hashtags or pecentage symbols as they cause an issue and make the filename more aesthetically pleasing.
    new_filename = filename.replace('#', '').replace('%', '').replace('_', ' ')

    try:
        # Rename the file.
        os.replace(os.path.join(download_dir, filename), os.path.join(download_dir, new_filename))
    except Exception as e:
        log.info(f'Unable to rename {filename} to {new_filename}:\n{e}')
        clean_up(Path(filename).stem)
    else:
        log.info(f'{new_filename} | {filesize} MB')
        clean_up(Path(new_filename).stem)

        # Update the list of videos downloaded.
        with open("logs/downloads.txt", "a") as f:
            f.write(f'\n{new_filename}')

        # Return the download link.
        return os.path.join('api', 'downloads', new_filename)
Ejemplo n.º 7
0
def send_json_response(download_type):
    global filename
    filename = [
        file for file in os.listdir(download_dir)
        if '.part' not in file and os.path.splitext(file)[0] == filename
    ][0]
    filesize = round(
        (os.path.getsize(os.path.join(download_dir, filename)) / 1_000_000), 2)
    log.info(f'{filename} | {filesize} MB')
    # Query the database by IP.
    user = User.query.filter_by(ip=get_ip()).first()
    # If the user has used the downloader before, update the database.
    if user:
        user.mb_downloaded += filesize
        db.session.commit()
    # Remove any hashtags or pecentage symbols as they cause an issue and make the filename more aesthetically pleasing.
    new_filename = filename.replace('#',
                                    '').replace(download_type, '.').replace(
                                        '%', '').replace('_', ' ')
    log.info(new_filename)
    os.replace(os.path.join(download_dir, filename),
               os.path.join(download_dir, new_filename))
    # Update the list of videos downloaded.
    with open("logs/downloads.txt", "a") as f:
        f.write(f'\n{new_filename}')

    return jsonify(download_path=os.path.join('downloads', new_filename),
                   log_file=session['progress_file_path'])
Ejemplo n.º 8
0
def trim_file():

    if request.form["request_type"] == "upload_complete":

        chosen_file = request.files["chosen_file"]
        # Make the filename safe
        filename_secure = secure_filename(chosen_file.filename)
        # Save the uploaded file to the uploads folder.
        chosen_file.save(os.path.join("uploads", filename_secure))
        return ''

    if request.form["request_type"] == "trim":

        filename = request.form["filename"]
        log_this(f'wants to trim: {filename}')
        uploaded_file_path = os.path.join("uploads", secure_filename(filename))
        start_time = request.form["start_time"]
        end_time = request.form["end_time"]
        ext = "." + filename.split(".")[-1]
        just_name = filename.split(".")[0]
        output_name = just_name + " [trimmed]" + ext

        log.info(f'ffmpeg -y -ss {start_time} -i "{uploaded_file_path}" '
                 f'-to {end_time} -map 0 -c copy "trims/{output_name}"')

        os.system(f'ffmpeg -y -ss {start_time} -i "{uploaded_file_path}" '
                  f'-to {end_time} -map 0 -c copy "trims/{output_name}"')

        return {
            "message":
            "File trimmed. The trimmed file will now start downloading.",
            "downloadFilePath": f'/trims/{output_name}'
        }
Ejemplo n.º 9
0
def download_file(filename):
    log.info(f'https://free-av-tools.com/trims/{filename}')
    mimetype_value = 'audio/mp4' if os.path.splitext(filename)[1] == ".m4a" else ''
    try:
        return send_from_directory('trims', filename, mimetype=mimetype_value, as_attachment=True)
    except Exception as error:
        log.error(f'Unable to send file. Error: \n{error}')
Ejemplo n.º 10
0
 def __mul__(self, b):
     a = self
     if len(b.shape) > len(a.shape):
         a = b
         b = self
     a = Factor(a, copy=True)
     b = Factor(b, copy=True)
     bVars = b.variables()
     bAxis = b.axes()
     log.info('Multiplying factors \n %s \n AND \n %s', a, b)
     #import pdb; pdb.set_trace()
     for axis, i in reversed(bAxis.items()):
         aVars = a.variables()
         aAxis = a.axes()
         if aAxis.has_key(axis):
             a = a.rollaxis(aAxis[axis])
         else:
             a = a.resize((b.shape[i],) + a.shape, [b.infoCopy()[i]] + a.infoCopy())
             for j in xrange(b.shape[i]):
                 a[j] = a[0]
         log.debug('Array after axis roll or resize %s', a)
     
     for index in np.ndindex(*b.shape):
         log.debug('Multiplying %s at index %s in \'a\' by %s', a[index], index, b[index])
         a[index] *= b[index]
     a._info[-1]['variables'] += bVars
     return a
Ejemplo n.º 11
0
def send_json_response(progress_file_path, video_id):
    clean_downloads_foider()
    correct_file = [
        filename for filename in os.listdir(download_dir)
        if video_id in filename
    ][0]

    filesize = round(
        (os.path.getsize(os.path.join(download_dir, correct_file)) /
         1_000_000), 2)
    log.info(f'{filesize} MB')

    user_ip = get_ip()
    # Query the database by IP.
    user = User.query.filter_by(ip=user_ip).first()

    if user:
        user.mb_downloaded += filesize
        db.session.commit()

    new_filename = correct_file.replace('_', ' ').replace('#', '').replace(
        f'-{video_id}', '')
    log.info(new_filename)
    os.replace(os.path.join(download_dir, correct_file),
               os.path.join(download_dir, new_filename))

    with open("logs/downloads.txt", "a") as f:
        f.write(f'\n{new_filename}')

    return jsonify(download_path=os.path.join('downloads', new_filename),
                   log_file=progress_file_path)
Ejemplo n.º 12
0
    def reduce(self, **evidence):
        #Only possible to reduce by one node at the moment
        #TODO: Include reduced axes in result array
        var, val = evidence.popitem()
        info = self.infoCopy()
        i = self.axes()[var]
        j = list(info[i]['values']).index(val)
        log.info('Reducing by variable %s to value %s', var, val)
        ary = self[var:j]
        ary._info[-1]['variables'] = ary._info[-1]['variables'].replace(
            var, '')

        cAxes = [
            k for a, k in ary.axes().iteritems() if a not in ary.variables()
        ]
        cDims = tuple(ary.shape[l] for l in cAxes)
        log.debug('Conditioning axes:\n %s', list(cAxes))
        log.debug('Conditioning dimensions:\n %s', cDims)

        for d in np.ndindex(*cDims):
            index = tuple(
                slice(axisLength) if m not in cAxes else d[cAxes.index(m)]
                for m, axisLength in enumerate(ary.shape))
            log.debug('Summing array\n %s\n at index: %s', ary, index)
            ary[index] /= np.array(ary[index]).sum()
        return ary
Ejemplo n.º 13
0
    def __mul__(self, b):
        a = self
        if len(b.shape) > len(a.shape):
            a = b
            b = self
        a = Factor(a, copy=True)
        b = Factor(b, copy=True)
        bVars = b.variables()
        bAxis = b.axes()
        log.info('Multiplying factors \n %s \n AND \n %s', a, b)
        #import pdb; pdb.set_trace()
        for axis, i in reversed(bAxis.items()):
            aVars = a.variables()
            aAxis = a.axes()
            if aAxis.has_key(axis):
                a = a.rollaxis(aAxis[axis])
            else:
                a = a.resize((b.shape[i], ) + a.shape,
                             [b.infoCopy()[i]] + a.infoCopy())
                for j in xrange(b.shape[i]):
                    a[j] = a[0]
            log.debug('Array after axis roll or resize %s', a)

        for index in np.ndindex(*b.shape):
            log.debug('Multiplying %s at index %s in \'a\' by %s', a[index],
                      index, b[index])
            a[index] *= b[index]
        a._info[-1]['variables'] += bVars
        return a
Ejemplo n.º 14
0
def return_download_link(progress_file_path, video_id, download_type):
    for file in os.listdir(download_dir):
        if file.split(
                '.'
        )[-1] in relevant_extensions and video_id in file and download_type in file:

            log.info(f'DOWNLOADED "{file}"')
            filesize = round(
                (os.path.getsize(f'{download_dir}/{file}') / 1_000_000), 2)
            log.info(f'{filesize} MB')

            user_ip = get_ip()
            user = User.query.filter_by(ip=user_ip).first()

            if user:
                user.mb_downloaded += filesize
                db.session.commit()

            with open("logs/downloaded-files.txt", "a") as f:
                f.write(f'\n{file}')

            new_filename = file.replace('#', '').replace(f'-{video_id}', '')
            os.replace(f'{download_dir}/{file}',
                       f'{download_dir}/{new_filename}')

            return {
                'download_path': os.path.join('downloads', new_filename),
                'log_file': progress_file_path
            }
Ejemplo n.º 15
0
def send_file(filename):
    just_extension = filename.split('.')[-1]
    if just_extension == "m4a":
        log.info('Sending the file to the user.')
        return send_from_directory(f'{os.getcwd()}/conversions', filename, mimetype="audio/mp4", as_attachment=True)
    else:
        log.info('Sending the file to the user.')
        return send_from_directory(f'{os.getcwd()}/conversions', filename, as_attachment=True)
Ejemplo n.º 16
0
def run_ffmpeg(progress_filename, uploaded_file_path, params):
    log.info(f'Converter: {progress_filename}')
    progress_file_path = f'static/progress/"{progress_filename}".txt'
    log.info(params)
    os.system(
        f'ffmpeg -hide_banner -progress {progress_file_path} -y -i "{uploaded_file_path}" '
        f'-id3v2_version 3 -write_id3v1 true -metadata comment="freeaudioconverter.net" '
        f'-metadata encoded_by="freeaudioconverter.net" {params}')
Ejemplo n.º 17
0
def send_file(filename):
    time_now = datetime.now().strftime('%H:%M:%S')
    log.info(f'[{time_now}] https://free-av-tools.com/downloads/{filename}')
    mimetype_value = 'audio/mp4' if os.path.splitext(filename)[1] == ".m4a" else ''
    try:
        return send_from_directory(download_dir, filename, mimetype=mimetype_value, as_attachment=True)
    except Exception as error:
        log.error(f'Unable to send file. Error: \n{error}')
Ejemplo n.º 18
0
def clean_up(filename_stem):
    for file in os.listdir('downloads'):
        if filename_stem in file and Path(file).suffix != '':
            if (Path(file).suffix in ['.part', '.webp', '.ytdl'] or file.split('.')[-2] in ytdl_format_codes or 
                '.part' in file):
                try:
                    os.remove(os.path.join('downloads', file))
                except Exception as e:
                    log.info(f'[CLEAN UP] Unable to delete {file}:\n{e}')
Ejemplo n.º 19
0
def run_ffmpeg(progress_filename, uploaded_file_path, params):
    progress_file_path = f'static/progress/"{progress_filename}".txt'
    log.info(params)
    os.system(
        f'/usr/local/bin/ffmpeg -hide_banner -progress {progress_file_path} -y -i "{uploaded_file_path}" '
        f'-id3v2_version 3 -write_id3v1 true -metadata comment="freeaudioconverter.net" '
        f'-metadata encoded_by="freeaudioconverter.net" {params}')
    shutil.rmtree('static/progress')
    os.mkdir('static/progress')
Ejemplo n.º 20
0
def empty_folder(folder_path):
    for file in os.listdir(folder_path):
        try:
            os.remove(os.path.join(folder_path, file))
        except Exception as e:
            log.info(f'Unable to delete {folder_path}/{file}:\n{e}')

    log.info(f'{folder_path} folder emptied.')
        
Ejemplo n.º 21
0
async def watch_clients():
    log.info('Waiting for client orders...')
    writer = await NSQWriter(topic='client_orders').open()
    i = 0
    while True:
        await asyncio.sleep(random.randint(2, 6))
        order = f'Order {i}'
        log.info(f'Received client order: {order}')
        await writer.publish(order)
        i += 1
Ejemplo n.º 22
0
async def watch():
    log.info('Waiting for orders')
    reader = await NSQReader(topic='client_orders').open()
    await reader.subscribe()
    for waiter in reader.reader.wait_messages():
        message = await waiter
        log.info(f'Received order: {message.body}')
        await process_order(message)
        await message.fin()
    reader.close()
Ejemplo n.º 23
0
def download_file(filename):
    just_extension = filename.split('.')[-1]
    if just_extension == "m4a":
        log.info(f'https://freeaudioconverter.net/trims/{filename}')
        return send_from_directory('trims',
                                   filename,
                                   mimetype="audio/mp4",
                                   as_attachment=True)
    else:
        log.info(f'https://freeaudioconverter.net/trims/{filename}')
        return send_from_directory('trims', filename, as_attachment=True)
Ejemplo n.º 24
0
def delete_downloads():
    while True:
        sleep(60)
        if not is_downloading:
            for file in os.listdir('downloads'):
                if os.path.splitext(file) != '.part':
                    time_now = datetime.now().strftime('%H:%M:%S')
                    os.remove(os.path.join('downloads', file))
                    log.info(f'[{time_now}] Deleted downloads/{file}')
            for file in os.listdir('yt-progress'):
                os.remove(os.path.join('yt-progress', file))
Ejemplo n.º 25
0
def send_file(filename):
    log.info(f'https://freeaudioconverter.net/conversions/{filename}')
    mimetype_value = 'audio/mp4' if os.path.splitext(
        filename)[1] == ".m4a" else ''
    try:
        return send_from_directory('conversions',
                                   filename,
                                   mimetype=mimetype_value,
                                   as_attachment=True)
    except Exception as error:
        log.error(f'Unable to send file. Error: \n{error}')
Ejemplo n.º 26
0
def send_file(filename):
    log.info(f'{datetime.now().strftime("[%H:%M:%S]")} {filename}')
    mimetype_value = 'audio/mp4' if os.path.splitext(
        filename)[1] == '.m4a' else ''
    try:
        return send_from_directory('conversions',
                                   filename,
                                   mimetype=mimetype_value,
                                   as_attachment=True)
    finally:
        delete_file(os.path.join('conversions', filename))
Ejemplo n.º 27
0
def run_opus(progress_filename, uploaded_file_path, opus_encoding_type,
             opus_vorbis_slider, opus_cbr_bitrate, output_path):
    if opus_encoding_type == "opus_vbr":
        run_ffmpeg(
            progress_filename, uploaded_file_path,
            f'-c:a libopus -b:a {opus_vorbis_slider}k {output_path}.opus')
    else:  # CBR
        log.info("vbr off")
        run_ffmpeg(
            progress_filename, uploaded_file_path,
            f'-c:a libopus -vbr off -b:a {opus_cbr_bitrate}k {output_path}.opus'
        )
Ejemplo n.º 28
0
def run_youtube_dl(download_type, args):
    download_start_time = time()
    try:
        # Using subprocess.run() this way allows the stdout to be written to a file.
        with open(session['progress_file_path'], 'w') as f:
            subprocess.run(args, stdout=f)
    except Exception as error:
        log.error(f'Unable to download video: \n{error}')

    download_complete_time = time()
    log.info(
        f'{download_type} was chosen. Download took: {round((download_complete_time - download_start_time), 1)}s'
    )
Ejemplo n.º 29
0
def download_file(filename):
    just_extension = filename.split('.')[-1]
    if just_extension == "m4a":
        log.info(f'[M4A] SENDING {filename}')
        return send_from_directory(f'{os.getcwd()}/trims',
                                   filename,
                                   mimetype="audio/mp4",
                                   as_attachment=True)
    else:
        log.info(f'SENDING {filename}')
        return send_from_directory(f'{os.getcwd()}/trims',
                                   filename,
                                   as_attachment=True)
Ejemplo n.º 30
0
def send_file(filename):
    if os.path.splitext(filename)[-1] == ".m4a":
        try:
            return send_from_directory(download_dir, filename, mimetype="audio/mp4", as_attachment=True)
        finally:
            log.info(f'"{filename}" sent.')
            os.remove(os.path.join(download_dir, filename))  
    else:
        try:
            return send_from_directory(download_dir, filename, as_attachment=True)
        finally:
            log.info(f'"{filename}" sent.')
            os.remove(os.path.join(download_dir, filename))
Ejemplo n.º 31
0
def update_database():
    # Use the get_ip function imported from loggers.py
    user_ip = get_ip()
    # Query the database by IP.
    user = User.query.filter_by(ip=user_ip).first()
    if user:
        x = 'times' if user.times_used_yt_downloader == 1 else 'times'
        log.info(f'This user has used the downloader {user.times_used_yt_downloader} {x} before.')
        user.times_used_yt_downloader += 1
        db.session.commit()
    else:
        new_user = User(ip=user_ip, times_used_yt_downloader=1, mb_downloaded=0)
        db.session.add(new_user)
        db.session.commit()
Ejemplo n.º 32
0
    def reduce(self, **evidence):
        #Only possible to reduce by one node at the moment
        #TODO: Include reduced axes in result array
        var, val = evidence.popitem()
        info = self.infoCopy()
        i = self.axes()[var]
        j = list(info[i]['values']).index(val)
        log.info('Reducing by variable %s to value %s', var, val)
        ary = self[var: j] 
        ary._info[-1]['variables']  = ary._info[-1]['variables'].replace(var, '')

        cAxes = [ k for a,k in ary.axes().iteritems() if a not in ary.variables() ]
        cDims = tuple( ary.shape[l] for l in cAxes )
        log.debug('Conditioning axes:\n %s', list(cAxes))
        log.debug('Conditioning dimensions:\n %s', cDims)

        for d in np.ndindex(*cDims):
            index = tuple( slice(axisLength) if m not in cAxes else d[cAxes.index(m)] for m, axisLength in enumerate(ary.shape)  )
            log.debug('Summing array\n %s\n at index: %s', ary, index)
            ary[index] /= np.array(ary[index]).sum()
        return ary