Skip to content
Snippets Groups Projects
Select Git revision
  • 8262eb5abbc0f7951a76c4d112e5fee91ee9a038
  • main default protected
2 results

mapping1.ts

Blame
  • user avatar
    Andreas Hubel authored
    8262eb5a
    History
    mapping1.ts 2.59 KiB
    import { Client, cacheExchange, fetchExchange, gql } from '@urql/core';
    import { Response } from './types';
    
    const audiothekApi = new Client({
      url: 'https://api.ardaudiothek.de/graphql',
      exchanges: [cacheExchange, fetchExchange],
    });
    
    const query = gql`
      query Item {
        item(id: "14278953") {
          show {
            coreId
            externalId
            title
            url
            publisher: publicationService {
              coreId
              dvbServiceId
              title
              url
              organizationName
              organization {
                name
                url
              }
            }
            coreDocument
          }
          assetId
          url: sharingUrl
          title
          published: publishDate
          updated: core(key: "modified")
          imagesList {
            url
            title
          }
          coreDocument
          audioList {
            title
            audioCodec
            href
          }
        }
      }
    `;
    
    async function fetchData() {
      const result = await audiothekApi.query<Response>(query, {}).toPromise();
      if (result.error) {
        throw result.error;
      }
      // console.log(JSON.stringify(result.data, null, 2));
      return result.data;
    }
    
    function transformToActivityPub(response: Response) {
      const item = response.item;
      return {
        "@context": "https://www.w3.org/ns/activitystreams",
        "id": item.assetId,
        "type": "PodcastEpisode",
        "published": item.published,
        "updated": item.updated,
        "attributedTo": item.show.coreId,
        "externalId": item.url,
        "title": item.title,
        "description": {
          "type": "Note",
          "mediaType": "text/markdown",
          "content": item.coreDocument
        },
        "image": item.imagesList.length > 0 ? {
          "type": "Image",
          "url": item.imagesList[0].url
        } : undefined,
        "audio": item.audioList.map(audio => ({
          "type": "Audio",
          "name": audio.title,
          "mediaType": "audio/mpeg",
          "url": audio.href
        })),
        "partOf": {
          "type": "PodcastSeries",
          "id": item.show.coreId,
          "externalId": item.show.externalId,
          "name": item.show.title,
          "attributedTo": item.show.publisher.coreId,
          "publisher": {
            "type": "BroadcastService",
            "name": item.show.publisher.title,
            "url": item.show.publisher.url,
            "organization": {
              "type": "Organization",
              "name": item.show.publisher.organization.name,
              "url": item.show.publisher.organization.url
            }
          }
        }
      };
    }
    
    async function main() {
      const response = await fetchData();
    
      if (response) {
        const activityPubEpisode = transformToActivityPub(response);
        console.log(JSON.stringify(activityPubEpisode, null, 2));
      }
    }
    
    main();