Пример #1
0
def get_objects(imurl, request):

    r = requests.get(imurl, stream=True)
    if r.status_code == 200:
        r.raw.decode_content = True

        with open("tmp.webp", 'wb') as f:
            shutil.copyfileobj(r.raw, f)

        dwebp(input_image="tmp.webp", output_image="tmp.png", option="-o")

        boxes, classes = get_prediction("tmp.png")
        classes = list(set(classes))
        con = mysql.connector.connect(autocommit=True,
                                      host=os.getenv('MYSQL_SERVER'),
                                      user=os.getenv('MYSQL_USER'),
                                      passwd=os.getenv('MYSQL_PASS'),
                                      auth_plugin="mysql_native_password",
                                      database=os.getenv('MYSQL_DATABASE'))
        cur = con.cursor()
        query = "update user_data set last_access=current_timestamp() where uname='{}'".format(
            request.form['user'])
        cur.execute(query)
        query = "update user_data set objects=objects + {} where uname='{}'".format(
            len(classes), request.form['user'])
        cur.execute(query)
        con.close()
        return jsonify(objects=classes)
Пример #2
0
        finalPath = "assets/image" + str(x) + "." + type
        path = "output/" + finalPath

        print("Known file type. Writing " + path)

        if type == "webp":
            path = "output/tmp/" + str(x) + ".webp"
            print("Converting webp to png")

            with open(path, "wb") as newFile:
                encodedString = f[1].encode()
                newFile.write(base64.decodebytes(encodedString))

            finalPath = "assets/image" + str(x) + ".png"
            dwebp(path, "output/assets/image" + str(x) + ".png", "-o")
            print("Converted file, saved to " + "output/assets/image" +
                  str(x) + ".png")
        else:
            with open(path, "wb") as newFile:
                encodedString = f[1].encode()
                newFile.write(base64.decodebytes(encodedString))

        text = text.replace(f[0] + f[1], finalPath)

print("Writing HTML")

with open("output/output.html", "w") as newFile:
    newFile.write(text)

print("All done!")
Пример #3
0
# pass input_image(.jpeg,.pnp .....) path ,
# output_image(give path where to save and image file name with .webp
# file type extension)
# print(cwebp(
#     f"{str(Path(__file__).parent.parent)}{sep}extras{sep}python_logo.jpg",
#     f"{str(Path(__file__).parent.parent)}{sep}extras{sep}python_logo.webp",
#     "-q 80",logging="-v",bin_path="/home/sky/Desktop/webp/lib/libwebp_linux/bin/cwebp"))

# ******* dwebp ************ #

# pass input_image(.webp image) path ,output_image(.jpeg,.pnp .....)
print(
    dwebp(
        f"{str(Path(__file__).parent.parent)}{sep}extras{sep}python_logo.webp",
        f"{str(Path(__file__).parent.parent)}{sep}extras{sep}python_logo.jpg",
        "-o",
        logging="-v",
        bin_path="/home/sky/Desktop/webp/lib/libwebp_linux/bin/dwebp"))

# ***************** gif2webp *********************** #

# pass input_image(.gif) path ,output_image(give path where to save and
# image file name with .webp file type extension)
# print(gifwebp(
#     f"{str(Path(__file__).parent.parent)}{sep}extras{sep}linux_logo.gif",
#     f"{str(Path(__file__).parent.parent)}{sep}extras{sep}linux_logo.webp",
#     "-q 80"))

# *************** webpmux ********************* #

# %%%%%%%%%%%%%%%%%% Add ICC profile,XMP metadata and EXIF metadata
Пример #4
0
def main(album_url=None, normal_url=None):
    if not album_url:
        album_url = input(
            'Apple Music URL (https://music.apple.com/xxx) for the album: ')
    if normal_url and normal_url.lower() == 'n':
        normal_url = album_url
    do_manual = normal_url and normal_url.lower() == 'x'
    if not normal_url or do_manual:
        normal_url = album_url
        album_deluxe_ask = input('Is this album a deluxe version (y/[n])? ')
        if album_deluxe_ask.lower() == 'y':
            normal_url = input(
                'Apple Music URL (https://music.apple.com/xxx) for the NON-DELUXE album version: '
            )
        if album_deluxe_ask.lower().startswith('http'):
            normal_url = album_deluxe_ask

    if not os.path.exists(get_relative_path('cache', 'artwork')):
        if not os.path.exists(get_relative_path('cache')):
            os.mkdir(get_relative_path('cache'))
        os.mkdir(get_relative_path('cache', 'artwork'))
    if not os.path.exists(get_relative_path('cache', 'itunes_path.txt')):
        itunes_path = input(
            'What is the path of your system\'s iTunes folder (e.g. D:\Music\iTunes)? '
        )
        f = open(get_relative_path('cache', 'itunes_path.txt'), 'w+')
        f.write(itunes_path)
        f.close()
    else:
        f = open(get_relative_path('cache', 'itunes_path.txt'), 'r')
        itunes_path = f.read()
        f.close()
    itunes_add_path = os.path.join(itunes_path, 'iTunes Media',
                                   'Automatically Add to iTunes')

    exists_count = 0
    while os.path.exists(
            get_relative_path('cache', 'TEMP-{}'.format(exists_count))):
        exists_count += 1
    downloads_path = get_relative_path('cache', 'TEMP-{}'.format(exists_count))
    os.mkdir(downloads_path)
    atexit.register(shutil.rmtree, downloads_path)

    album_res = requests.get(normal_url).text
    schema_start_string = '<script name="schema:music-album" type="application/ld+json">'
    schema_start_index = album_res.index(schema_start_string)
    album_schema = json.loads(
        album_res[schema_start_index + len(schema_start_string):album_res.
                  index('</script>', schema_start_index)])

    album_name = get_titlecase(album_schema['name'])
    normal_album_track_count = len(album_schema['tracks'])
    album_year = int(album_schema['datePublished'][:4])
    album_artist = get_titlecase(album_schema['byArtist']['name'])
    album_artist_current = get_titlecase(album_schema['byArtist']['name'],
                                         True)
    album_genre = album_schema['genre'][0].replace('&amp;', '&')

    webp_artwork_path = get_relative_path(
        'cache', 'artwork', '{}-{}-{}.webp'.format(
            album_artist.replace(' ', '_'), album_name.replace(' ', '_'),
            album_year).replace('?', '').replace('"', '').replace('*', ''))
    album_artwork_path = get_relative_path(
        'cache', 'artwork', '{}-{}-{}.png'.format(
            album_artist.replace(' ', '_'), album_name.replace(' ', '_'),
            album_year).replace('?', '').replace('"', '').replace('*', ''))
    if not os.path.exists(album_artwork_path):
        artwork_search_str = album_res[:album_res.index(' 1000w')]
        artwork_search_str = artwork_search_str[artwork_search_str.
                                                rindex('https://'):]
        album_artwork_url = artwork_search_str.strip()
        print(album_artwork_url)
        r = requests.get(album_artwork_url, stream=True)
        print(webp_artwork_path)
        if r.status_code == 200:
            with open(webp_artwork_path, 'wb+') as f:
                r.raw.decode_content = True
                shutil.copyfileobj(r.raw, f)
            dwebp(webp_artwork_path, album_artwork_path, '-o')
            os.remove(webp_artwork_path)

    if normal_url != album_url:
        deluxe_album_res = requests.get(album_url).text
        schema_start_index = deluxe_album_res.index(schema_start_string)
        album_schema = json.loads(
            deluxe_album_res[schema_start_index +
                             len(schema_start_string):deluxe_album_res.
                             index('</script>', schema_start_index)])

    deluxe_album_track_count = len(album_schema['tracks'])
    deluxe_album_name = get_titlecase(album_schema['name'])
    album_tracks = [
        get_titlecase(track['name']).replace('F*****g', 'F*****g')
        for track in album_schema['tracks']
    ]
    is_deluxe_list = [False] * normal_album_track_count + [True] * (
        deluxe_album_track_count - normal_album_track_count)

    print(album_name, album_artist, album_genre, deluxe_album_track_count,
          album_year, album_tracks)
    print()

    thread_list = []
    global url_pending
    url_pending = [None] * len(album_tracks)
    for idx, track in enumerate(album_tracks):
        # print('Downloading "{}" (track {}/{})...'.format(track, idx+1, album_track_count))
        current_thread = Thread(
            target=download_song,
            args=(track, idx + 1, is_deluxe_list[idx], album_artist,
                  album_artist_current, album_name, deluxe_album_name,
                  album_genre, album_year, album_artwork_path, downloads_path,
                  do_manual))
        current_thread.start()
        # download_song(track, idx+1, album_artist, album_artist_current, album_name, album_genre, album_year, album_artwork_path, downloads_path)
        thread_list.append(current_thread)
    while None in url_pending:
        sleep(1)
    for idx, thread in enumerate(thread_list):
        if not url_pending[idx]:
            thread.join()
    for idx, thread in enumerate(thread_list):
        if url_pending[idx]:
            global pending_thread_song
            pending_thread_song = album_tracks[idx]
            thread.join()

    print()

    track_filenames = [
        '{} {}.mp3'.format(
            str(idx + 1).zfill(2),
            track.replace(':', '').replace('?', '').replace('!', '').replace(
                '"', '').replace('*', ''))
        for idx, track in enumerate(album_tracks)
    ]
    for track_filename in track_filenames:
        try:
            os.replace(os.path.join(downloads_path, track_filename),
                       os.path.join(itunes_add_path, track_filename))
            print('{} is complete!'.format(track_filename))
        except:
            print('{} was NOT downloaded.'.format(track_filename))