Skip to content

Commit

Permalink
Add script to generate license summary
Browse files Browse the repository at this point in the history
  • Loading branch information
jbruechert committed Feb 17, 2024
1 parent 0d00eff commit 7a78946
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 5 deletions.
6 changes: 5 additions & 1 deletion feeds/de-berlin.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@
{
"name": "vbb",
"type": "http",
"url": "https://www.vbb.de/fileadmin/user_upload/VBB/Dokumente/API-Datensaetze/gtfs-mastscharf/GTFS.zip"
"url": "https://www.vbb.de/fileadmin/user_upload/VBB/Dokumente/API-Datensaetze/gtfs-mastscharf/GTFS.zip",
"license": {
"spdx-identifier": "CC-BY-4.0",
"url": "https://www.vbb.de/vbb-services/api-open-data/datensaetze/"
}
}
]
}
15 changes: 12 additions & 3 deletions feeds/lv.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,27 @@
{
"name": "rigas-satiksme",
"type": "http",
"url": "https://gtfs.pro/files/uran/improved-gtfs-satiksme.zip"
"url": "https://gtfs.pro/files/uran/improved-gtfs-satiksme.zip",
"license": {
"spdx-identifier": "CC0-1.0"
}
},
{
"name": "rigas-saraksti",
"type": "http",
"url": "https://gtfs.pro/files/uran/improved-gtfs-saraksti.zip"
"url": "https://gtfs.pro/files/uran/improved-gtfs-saraksti.zip",
"license": {
"spdx-identifier": "CC0-1.0"
}
},
{
"name": "valsts-sia-autotransporta",
"type": "http",
"url": "https://www.atd.lv/sites/default/files/GTFS/gtfs-latvia-lv.zip",
"fix": true
"fix": true,
"license": {
"spdx-identifier": "CC0-1.0"
}
}
]
}
67 changes: 67 additions & 0 deletions src/generate-attribution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
#!/usr/bin/env python3
# SPDX-FileCopyrightText: 2024 Jonah Brüchert <[email protected]>
#
# SPDX-License-Identifier: AGPL-3.0-or-later

import json
import csv
import io
import transitland

from pathlib import Path
from metadata import *
from zipfile import ZipFile


if __name__ == "__main__":
feed_dir = Path("feeds/")

transitland_atlas = transitland.Atlas.load(Path("transitland-atlas/"))

attributions = []

for feed in feed_dir.glob("*.json"):
parsed = {}
with open(feed, "r") as f:
parsed = json.load(f)

region = Region(parsed)
for source in region.sources:
if (type(source) == TransitlandSource):
source = transitland_atlas.source_by_id(source)

attribution = {}

if source.license:
if source.license.spdx_identifier:
attribution["spdx_identifier"] \
= source.license.spdx_identifier
if source.license.url:
attribution["url"] = source.license.url

attribution["copyright_holders"] = []

metadata_filename = feed.name
region_name = metadata_filename[:metadata_filename.rfind('.')]

feed_path = Path(f"out/{region_name}_{source.name}.gtfs.zip")

attribution["filename"] = feed_path.name

if not feed_path.exists():
print(f"Info: {feed_path} does not exist, skipping…")
continue

with ZipFile(feed_path) as z:
with z.open("agency.txt", "r") as a:
with io.TextIOWrapper(a) as at:
agencyreader = \
csv.DictReader(at, delimiter=',', quotechar='"')
for row in agencyreader:
attribution["copyright_holders"] \
.append(row["agency_name"])

attributions.append(attribution)

with open("out/license.json", "w") as outfile:
json.dump(attributions, outfile, indent=4, ensure_ascii=False)
15 changes: 14 additions & 1 deletion src/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# SPDX-License-Identifier: AGPL-3.0-or-later

from typing import List
from typing import List, Optional


class Maintainer:
Expand All @@ -14,12 +14,25 @@ def __init__(self, parsed: dict):
self.email = parsed["email"]


class License:
spdx_identifier: Optional[str] = None
url: Optional[str] = None


class Source:
name: str
fix: bool = False
license: License

def __init__(self, parsed: dict = None):
self.license = License()
if parsed:
if "license" in parsed:
if "spdx-identifier" in parsed["license"]:
self.license.spdx_identifier = parsed["license"]["spdx-identifier"]
if "url" in parsed["license"]:
self.license.url = parsed["license"]["url"]

self.name = parsed["name"]
if "fix" in parsed:
self.fix = bool(parsed["fix"])
Expand Down
9 changes: 9 additions & 0 deletions src/transitland.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,14 @@ def load(path: Path):
def source_by_id(self, source: TransitlandSource) -> HttpSource:
feed = self.by_id[source.transitland_atlas_id]
http_source = HttpSource()
http_source.name = source.name
http_source.url = feed["urls"]["static_current"]

if "license" in feed:
http_source.license = License()
if "spdx_identifier" in feed["license"]:
http_source.license.spdx_identifier = feed["license"]["spdx_identifier"]
if "url" in feed["license"]:
http_source.license.url = feed["license"]["url"]

return http_source

0 comments on commit 7a78946

Please sign in to comment.