CPJKU / partitura

A python package for handling modern staff notation of music
https://partitura.readthedocs.io
Apache License 2.0
227 stars 15 forks source link

Load scores from url with raw contents #354

Open manoskary opened 4 months ago

manoskary commented 4 months ago

Load score, performance MIDIs, or match files currently works by providing local paths. I suggest also supporting loading files directly from URLs such as raw content from GitHub. This could be easily done with no additional dependencies by using urllib. This is the two functions that we will need to validate the source of a input path and load the data:

from urllib.parse import urlparse
import urllib.request

def load_data_from_url(url: str):
    with urllib.request.urlopen(url) as response:
        data = response.read().decode()
    return data

def is_url(input):
    try:
        result = urlparse(input)
        return all([result.scheme, result.netloc])
    except ValueError:
        return False

This method could be useful for managing the Partitura test files, as well as scraping scores from repositories and online sources.