Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use external metadata api #268

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions suisa_sendemeldung/acrclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytz
from acrclient import Client
from requests.exceptions import HTTPError
from tqdm import tqdm


Expand Down Expand Up @@ -112,3 +113,22 @@ def get_interval_data(
data.remove(entry)

return data

# FIXME: naming of the function is debatable
# FIXME: servers sometimes responds with 500 while it works a few minutes later
def get_external_metadata(self, acrid):
params = {"acr_id": acrid, "include_works": 1}
response = {}
try:
response = self.json(path="/api/external-metadata/tracks", params=params)
except HTTPError as e:
print(f"1: {e.request.url}")
try:
response = self.json(
path="/api/external-metadata/tracks", params=params
)
except HTTPError as e:
print(f"2: {e.request.url}")
pass

return response
29 changes: 25 additions & 4 deletions suisa_sendemeldung/suisa_sendemeldung.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,10 @@ def funge_release_date(release_date: str = ""):

if len(release_date) == 10:
# we can make it look like what suisa has in their examples if it's the right length
return datetime.strptime(release_date, "%Y-%m-%d").strftime("%Y%m%d")
try:
return datetime.strptime(release_date, "%Y-%m-%d").strftime("%Y%m%d")
except ValueError:
return ""
# we discard other records since there is no way to convert records like a plain
# year into dd/mm/yyyy properly without further guidance from whomever ingests
# the data, in some cases this means we discard data that only contain a year
Expand Down Expand Up @@ -439,6 +442,25 @@ def get_isrc(music):
return isrc


def get_composer(acrid):
composers = []
external_metadata = client.get_external_metadata(acrid=acrid)
for data in external_metadata.get("data", []):
for work in data.get("works", []):
for contributor in work.get("contributors", []):
if "Composer" in contributor.get("roles", []):
c = contributor.get("name")
c = f"{c} (Composer)"
if c not in composers:
composers.append(c)
elif "Writer" in contributor.get("roles", []):
w = contributor.get("name")
w = f"{w} (Writer)"
if w not in composers:
composers.append(w)
return ", ".join(composers)


# all local vars are required, eight are already used for the csv entries
# pylint: disable-msg=too-many-locals
def get_csv(data, station_name=""):
Expand Down Expand Up @@ -501,9 +523,7 @@ def get_csv(data, station_name=""):

artist = get_artist(music)

composer = ", ".join(music.get("contributors", {}).get("composers", ""))
if not composer:
composer = artist
composer = get_composer(music["acrid"])

isrc = get_isrc(music)
label = music.get("label")
Expand Down Expand Up @@ -729,6 +749,7 @@ def main(): # pragma: no cover
start_date, end_date = parse_date(args)
filename = parse_filename(args, start_date)

global client
client = ACRClient(bearer_token=args.bearer_token)
data = client.get_interval_data(
args.project_id, args.stream_id, start_date, end_date, timezone=args.timezone
Expand Down