Skip to main content

A python library for reading osz2 files

Project description

osz2.py

Python Version GitHub License GitHub Actions Workflow Status

osz2.py is a Python library for reading osz2 files. It's a direct port of the existing Osz2Decryptor project by xxCherry and osz2-go by me. The Python port itself was done by @ascenttree; all credit goes to them. I took part in code refactoring and optimizing the performance using Numba and NumPy, such that the encryption only takes ~0.150 seconds instead of 25 seconds.

This project won't provide beatmap parsing support. You will have to implement that by yourself, if you decide to use this library for implementing the beatmap submission system.

Installation

pip install osz2

Or install from source:

git clone https://github.com/Lekuruu/osz2.py
cd osz2.py
pip install -e .

Usage

This repository provides a command-line interface for easy testing:

python -m osz2 <input.osz2> <output_directory>

But that's not all!
Here is an example of how to use osz2.py as a library:

from osz2 import Osz2Package, MetadataType

# Parse package from file
package = Osz2Package.from_file("beatmap.osz2")

# Access metadata
print("Title:", package.metadata.get(MetadataType.Title))
print("Artist:", package.metadata.get(MetadataType.Artist))
print("Creator:", package.metadata.get(MetadataType.Creator))
print("Difficulty:", package.metadata.get(MetadataType.Difficulty))

# Access files
for file in package.files:
    print(f"File: {file.filename}, Size: {len(file.content)} bytes")

# Extract specific files
for file in package.files:
    if not file.filename.endswith(".osu"):
        continue

    with open(file.filename, "wb") as f:
        f.write(file.content)

# Create a regular .osz package
with open("beatmap.osz", "wb") as f:
    f.write(package.create_osz_package())

Metadata-only Mode

If you only need to read metadata without extracting files, you can use the metadata_only parameter:

# Only parse metadata
package = Osz2Package.from_file("beatmap.osz2", metadata_only=True)

# Access metadata
print("Title:", package.metadata.get(MetadataType.Title))
print("BeatmapSet ID:", package.metadata.get(MetadataType.BeatmapSetID))

Alternative Constructors

# From file path
package = Osz2Package.from_file("beatmap.osz2")

# From bytes
with open("beatmap.osz2", "rb") as f:
    data = f.read()
    package = Osz2Package.from_bytes(data)

# From an io.BufferedReader-like object, e.g. a file stream
with open("beatmap.osz2", "rb") as f:
    package = Osz2Package(f)

Applying a patch

When developing an implementation of the beatmap submission system, this could come in handy:

# Assuming you have a source osz2 file and a patch file
osz2_file = b"..."
patch_file = b"..."

updated_osz2 = osz2.apply_bsdiff_patch(osz2_file, patch_file)
osz2 = Osz2Package.from_bytes(updated_osz2)

Using osu!stream .osf2 files

I have not tested this, but in theory this should work by passing in KeyType.OSF2 when initializing the osz2 package:

osf2 = Osz2Package.from_file("beatmap.osf2", key_type=KeyType.OSF2)

You can also specify this when using the command-line interface:

python -m osz2 <input.osz2> <output_directory> --key-type osf2

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

osz2-1.0.6.tar.gz (13.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

osz2-1.0.6-py3-none-any.whl (15.2 kB view details)

Uploaded Python 3

File details

Details for the file osz2-1.0.6.tar.gz.

File metadata

  • Download URL: osz2-1.0.6.tar.gz
  • Upload date:
  • Size: 13.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.23

File hashes

Hashes for osz2-1.0.6.tar.gz
Algorithm Hash digest
SHA256 4b978031a006bb58fceac5b3ef942d87a9d888a2d0da281984fbb7150f7a3b43
MD5 a6a8e3bec5d7c5e9cdd032107d1af79f
BLAKE2b-256 4e579ce5eb445922b817e14d1333ebe4f882c22047fd315c4af2bf3a24b99480

See more details on using hashes here.

File details

Details for the file osz2-1.0.6-py3-none-any.whl.

File metadata

  • Download URL: osz2-1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 15.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.23

File hashes

Hashes for osz2-1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 f4aff4680b07654075ba6021a9f8cb1888d11f78b3e273482b99109ef87e79e5
MD5 bbba34b196133707e8d96ae5ef1b9f6f
BLAKE2b-256 1d7efbdbf3890d6b86b57242b5657ffa23c4ab8920700e9dcfc1f6e3ab636d54

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page