Skip to content

Commit

Permalink
feat: add CRID as Identifikationsnummer
Browse files Browse the repository at this point in the history
  • Loading branch information
hairmare committed Dec 29, 2022
1 parent be31b8f commit df7b765
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 10 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
ConfigArgParse==1.5.3
pytz==2022.7
requests==2.28.1
rabe-cridlib==0.4.0
3 changes: 3 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ description-file = README.md

[tool:pytest]
addopts = --doctest-modules --cov=suisa_sendemeldung --pylint --cov-fail-under=100
filterwarnings =
ignore::DeprecationWarning:pylint
ignore::pytest.PytestRemovedIn8Warning:pytest_pylint
14 changes: 13 additions & 1 deletion suisa_sendemeldung/suisa_sendemeldung.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from os.path import basename, expanduser
from smtplib import SMTP

import cridlib
import pytz
from configargparse import ArgumentParser

from .acrclient import ACRClient
Expand Down Expand Up @@ -275,6 +277,7 @@ def get_csv(data):
"Komponist",
"ISRC",
"Label",
"Identifikationsnummer",
]
csv = StringIO()
csv.write("sep=,\n")
Expand Down Expand Up @@ -323,8 +326,17 @@ def get_csv(data):
isrc = ""
label = music.get("label")

# cridlib only supports timezone-aware datetime values, so we convert one
timestamp_utc = pytz.utc.localize(
datetime.strptime(metadata.get("timestamp_utc"), ACRClient.TS_FMT)
)
# we include the acrid in our CRID so we know about the data's provenience
# in case any questions about the data we delivered are asked
acrid = music.get("acrid")
local_id = cridlib.get(timestamp=timestamp_utc, fragment=f"acrid={acrid}")

csv_writer.writerow(
[ts_date, ts_time, duration, title, artist, composer, isrc, label]
[ts_date, ts_time, duration, title, artist, composer, isrc, label, local_id]
)
return csv.getvalue()

Expand Down
45 changes: 36 additions & 9 deletions tests/test_suisa_sendemeldung.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
"""Test the suisa_sendemeldung.suisa_sendemeldung module."""
from datetime import date, datetime
from datetime import date, datetime, timezone
from email.message import Message
from os.path import dirname
from unittest.mock import patch
from unittest.mock import call, patch

from configargparse import ArgumentParser
from freezegun import freeze_time
Expand Down Expand Up @@ -164,32 +164,41 @@ def test_merge_duplicates():
assert results[0]["metadata"]["played_duration"] == 20


def test_get_csv():
@patch("cridlib.get")
def test_get_csv(mock_cridlib_get):
"""Test get_csv."""
mock_cridlib_get.return_value = "crid://rabe.ch/v1/test"

# empty data
data = []
csv = suisa_sendemeldung.get_csv(data)
# pylint: disable=line-too-long
assert csv == (
"sep=,\n"
"Sendedatum,Sendezeit,Sendedauer,Titel,Künstler,Komponist,ISRC,Label\r\n"
"Sendedatum,Sendezeit,Sendedauer,Titel,Künstler,Komponist,ISRC,Label,Identifikationsnummer\r\n"
)
# pylint: enable=line-too-long
mock_cridlib_get.assert_not_called()

# bunch of data
mock_cridlib_get.reset_mock()
data = [
{
"metadata": {
"timestamp_local": "1993-03-01 13:12:00",
"timestamp_utc": "1993-03-01 13:12:00",
"played_duration": 60,
"music": [{"title": "Uhrenvergleich"}],
"music": [{"title": "Uhrenvergleich", "acrid": "a1"}],
}
},
{
"metadata": {
"timestamp_local": "1993-03-01 13:37:00",
"timestamp_utc": "1993-03-01 13:37:00",
"played_duration": 60,
"custom_files": [
{
"acrid": "a2",
"title": "Meme Dub",
"artist": "Da Gang",
"contributors": {
Expand All @@ -207,9 +216,11 @@ def test_get_csv():
{
"metadata": {
"timestamp_local": "1993-03-01 16:20:00",
"timestamp_utc": "1993-03-01 16:20:00",
"played_duration": 60,
"music": [
{
"acrid": "a3",
"title": "Bubbles",
"artists": [
{
Expand All @@ -230,12 +241,28 @@ def test_get_csv():
# pylint: disable=line-too-long
assert csv == (
"sep=,\n"
"Sendedatum,Sendezeit,Sendedauer,Titel,Künstler,Komponist,ISRC,Label\r\n"
"01/03/93,13:12:00,0:01:00,Uhrenvergleich,,,,\r\n"
"01/03/93,13:37:00,0:01:00,Meme Dub,Da Gang,Da Composah,id-from-well-published-isrc-database,\r\n"
'01/03/93,16:20:00,0:01:00,Bubbles,"Mary\'s Surprise Act, Climmy Jiff","Mary\'s Surprise Act, Climmy Jiff",important-globally-well-managed-id,Jane Records\r\n'
"Sendedatum,Sendezeit,Sendedauer,Titel,Künstler,Komponist,ISRC,Label,Identifikationsnummer\r\n"
"01/03/93,13:12:00,0:01:00,Uhrenvergleich,,,,,crid://rabe.ch/v1/test\r\n"
"01/03/93,13:37:00,0:01:00,Meme Dub,Da Gang,Da Composah,id-from-well-published-isrc-database,,crid://rabe.ch/v1/test\r\n"
'01/03/93,16:20:00,0:01:00,Bubbles,"Mary\'s Surprise Act, Climmy Jiff","Mary\'s Surprise Act, Climmy Jiff",important-globally-well-managed-id,Jane Records,crid://rabe.ch/v1/test\r\n'
)
# pylint: enable=line-too-long
mock_cridlib_get.assert_has_calls(
[
call(
timestamp=datetime(1993, 3, 1, 13, 12, tzinfo=timezone.utc),
fragment="acrid=a1",
),
call(
timestamp=datetime(1993, 3, 1, 13, 37, tzinfo=timezone.utc),
fragment="acrid=a2",
),
call(
timestamp=datetime(1993, 3, 1, 16, 20, tzinfo=timezone.utc),
fragment="acrid=a3",
),
]
)


def test_create_message():
Expand Down

0 comments on commit df7b765

Please sign in to comment.