Edits history of script submission #161 for ' Watch for new listenings (funkwhale)'

  • deno
    import {
      getState,
      setState,
    } from "https://deno.land/x/[email protected]/mod.ts";
    
    /**
     * **IMPORTANT:** This does not return listenings on the first run.
     *
     * You can read more about the Listenings History API at
     * https://docs.funkwhale.audio/swagger/#/User%20activity/get_api_v1_history_listenings
     */
    type Funkwhale = {
      baseUrl: string;
      token: string;
    };
    export async function main(
      auth: Funkwhale,
      query?: string,
      scope = "all",
      page_size = 10,
    ) {
      const previousState = await getState();
      const url = new URL("/api/v1/history/listenings", auth.baseUrl);
      const params = { query, scope, page_size };
      Object.entries(params).forEach(([key, value]) => {
        value && url.searchParams.append(key, encodeURIComponent(value));
      });
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
        },
      });
    
      const data = await response.json();
      data.results.reverse();
    
      const newListenings = [];
      let latestId;
      for (const listening of data.results) {
        if (latestId === undefined || listening.id > latestId) {
          latestId = listening.id;
        }
        if (
          previousState?.latestId !== undefined &&
          listening.id <= previousState.latestId
        ) {
          continue;
        }
        newListenings.push(listening);
      }
    
      if (latestId !== undefined) {
        await setState({
          latestId,
        });
      }
      return newListenings;
    }
    

    Submitted by hugo697 539 days ago

  • deno
    import {
      getState,
      setState,
    } from "https://deno.land/x/[email protected]/mod.ts";
    
    /**
     * **IMPORTANT:** This does not return listenings on the first run.
     *
     * You can read more about the Listenings History API at
     * https://docs.funkwhale.audio/swagger/#/User%20activity/get_api_v1_history_listenings
     */
    type Funkwhale = {
      baseUrl: string;
      token: string;
    };
    export async function main(
      auth: Funkwhale,
      query?: string,
      scope = "all",
      page_size = 10,
    ) {
      const previousState = await getState();
      const url = new URL("/api/v1/history/listenings", auth.baseUrl);
      const params = { query, scope, page_size };
      Object.entries(params).forEach(([key, value]) => {
        value && url.searchParams.append(key, encodeURIComponent(value));
      });
      const response = await fetch(url, {
        headers: {
          Authorization: `Bearer ${auth.token}`,
        },
      });
    
      const data = await response.json();
      data.results.reverse();
    
      const newListenings = [];
      let latestId;
      for (const listening of data.results) {
        if (latestId === undefined || listening.id > latestId) {
          latestId = listening.id;
        }
        if (
          previousState?.latestId !== undefined &&
          listening.id <= previousState.latestId
        ) {
          continue;
        }
        newListenings.push(listening);
      }
    
      if (latestId !== undefined) {
        await setState({
          latestId,
        });
      }
      return newListenings;
    }
    

    Submitted by admin 1172 days ago

  • deno
    import { getState, setState } from "https://deno.land/x/[email protected]/mod.ts";
    
    /** 
     * **IMPORTANT:** This does not return listenings on the first run.
     * 
     * You can read more about the Listenings History API at 
     * https://docs.funkwhale.audio/swagger/#/User%20activity/get_api_v1_history_listenings
     */
    type Funkwhale = {
      baseUrl: string;
      token: string;
    };
    export async function main(
      auth: Funkwhale,
      query?: string,
      scope = "all",
      page_size = 10
    ) {
      const previousState = await getState();
      const url = new URL('/api/v1/history/listenings', auth.baseUrl);
      const params = { query, scope, page_size }
      Object.entries(params).forEach(([key, value]) => {
        value && url.searchParams.append(key, encodeURIComponent(value));
      })
      const response = await fetch(url, {
        headers: {
          "Authorization": `Bearer ${auth.token}`,
        },
      });
    
      const data = await response.json();
      data.results.reverse();
    
      const newListenings = [];
      let latestId;
      for(const listening of data.results) {
        if (latestId === undefined || listening.id > latestId) {
          latestId = listening.id;
        }
        if (previousState?.latestId !== undefined && listening.id <= previousState.latestId) {
          continue;
        }
        newListenings.push(listening);
      }
    
      if (latestId !== undefined) {
        await setState({
          latestId,
        });
      }
      return newListenings;
    }
    

    Submitted by admin 1175 days ago

  • deno
    import { Resource, getState, setState } from "https://deno.land/x/[email protected]/mod.ts";
    
    /** 
     * **IMPORTANT:** This does not return listenings on the first run.
     * 
     * You can read more about the Listenings History API at 
     * https://docs.funkwhale.audio/swagger/#/User%20activity/get_api_v1_history_listenings
     */
    export async function main(
      auth: Resource<'funkwhale'>,
      query?: string,
      scope = "all",
      page_size = 10
    ) {
      const previousState = await getState();
      const url = new URL('/api/v1/history/listenings', auth.baseUrl);
      const params = { query, scope, page_size }
      Object.entries(params).forEach(([key, value]) => {
        value && url.searchParams.append(key, encodeURIComponent(value));
      })
      const response = await fetch(url, {
        headers: {
          "Authorization": `Bearer ${auth.token}`,
        },
      });
    
      const data = await response.json();
      data.results.reverse();
    
      const newListenings = [];
      let latestId;
      for(const listening of data.results) {
        if (latestId === undefined || listening.id > latestId) {
          latestId = listening.id;
        }
        if (previousState?.latestId !== undefined && listening.id <= previousState.latestId) {
          continue;
        }
        newListenings.push(listening);
      }
    
      if (latestId !== undefined) {
        await setState({
          latestId,
        });
      }
      return newListenings;
    }
    

    Submitted by adam186 1303 days ago

  • deno
    import { Resource, getState, setState } from "https://deno.land/x/[email protected]/mod.ts";
    
    /** 
     * **IMPORTANT:** This does not return listenings on the first run.
     * 
     * You can read more about the Listenings History API at 
     * https://docs.funkwhale.audio/swagger/#/User%20activity/get_api_v1_history_listenings
     */
    export async function main(
      auth: Resource<'funkwhale'>,
      query?: string,
      scope = "all",
      page_size = 10
    ) {
      const previousState = await getState();
      const url = new URL('/api/v1/history/listenings', auth.baseUrl);
      const params = { query, scope, page_size }
      Object.entries(params).forEach(([key, value]) => {
        value && url.searchParams.append(key, encodeURIComponent(value));
      })
      const response = await fetch(url, {
        headers: {
          "Authorization": `Bearer ${auth.token}`,
        },
      });
    
      const data = await response.json();
      data.results.reverse();
    
      const newListenings = [];
      let latestId;
      for(const listening of data.results) {
        if (latestId === undefined || listening.id > latestId) {
          latestId = listening.id;
        }
        if (previousState?.latestId !== undefined && listening.id <= previousState.latestId) {
          continue;
        }
        newListenings.push(listening);
      }
    
      if (latestId !== undefined) {
        await setState({
          latestId,
        });
      }
      return newListenings;
    }
    

    Submitted by adam186 1338 days ago

  • deno
    import { Resource, getState, setState } from "https://deno.land/x/[email protected]/mod.ts";
    
    /** 
     * **IMPORTANT:** This does not return listenings on the first run.
     * 
     * You can read more about the Listenings History API at 
     * https://docs.funkwhale.audio/swagger/#/User%20activity/get_api_v1_history_listenings
     */
    export async function main(
      auth: Resource<'funkwhale'>,
      query?: string,
      scope = "all",
      page_size = 10
    ) {
      const previousState = await getState();
      const url = new URL('/api/v1/history/listenings', auth.baseUrl);
      const params = { query, scope, page_size }
      Object.entries(params).forEach(([key, value]) => {
        value && url.searchParams.append(key, encodeURIComponent(value));
      })
      const response = await fetch(url, {
        headers: {
          "Authorization": `Bearer ${auth.token}`,
        },
      });
    
      const data = await response.json();
      data.results.reverse();
    
      const newListenings = [];
      let latestId;
      for(const listening of data.results) {
        if (latestId === undefined || listening.id > latestId) {
          latestId = listening.id;
        }
        if (previousState?.latestId !== undefined && listening.id <= previousState.latestId) {
          continue;
        }
        newListenings.push(listening);
      }
    
      if (latestId !== undefined) {
        await setState({
          latestId,
        });
      }
      return newListenings;
    }
    

    Submitted by adam186 1409 days ago

  • deno
    import * as wmill from "https://deno.land/x/[email protected]/mod.ts"
    
    // Watches for new listenings. Does not return listenings on the first run.
    //
    // At max, this will return 10 new listenings.
    // If your schedule doesn't run often, you might miss some.
    
    export async function main(baseUrl: string, token: string, query?: string, scope="all") {
      const previousState = await wmill.getInternalState();
      // https://docs.funkwhale.audio/swagger/#/User%20activity/get_api_v1_history_listenings
      // Pagination is set to 10 so that the returned JSON object isn't to large for Windmill.
      let url = `${baseUrl}/api/v1/history/listenings?page_size=10}`;
      url = query ? `${url}&query=${encodeURIComponent(query)}` : url;
      url = scope ? `${url}&scope=${encodeURIComponent(scope)}` : url;
      const response = await fetch(url, {
        headers: {
          "Authorization": `Bearer ${token}`,
        },
      });
    
      const data = await response.json();
    
      data.results.reverse();
    
      const newListenings = [];
      let latestId;
      for(const listening of data.results) {
        if (latestId === undefined || listening.id > latestId) {
          latestId = listening.id;
        }
        if (previousState?.latestId !== undefined && listening.id <= previousState.latestId) {
          continue;
        }
        newListenings.push(listening);
      }
    
      if (latestId !== undefined) {
        await wmill.setInternalState({
          latestId,
        });
      }
      return newListenings;
    }

    Submitted by jaller94 1489 days ago

  • deno
    import * as wmill from "https://deno.land/x/[email protected]/mod.ts"
    
    export async function main(baseUrl: string, token: string) {
      const previousState = await wmill.getInternalState();
      const url = `${baseUrl}/api/v1/history/listenings?page_size=10`;
      const response = await fetch(url, {
        headers: {
          'Authorization': `Bearer ${token}`,
        },
      });
    
      const data = await response.json();
    
      data.results.reverse();
    
      const newListenings = [];
      let latestId;
      for(const listening of data.results) {
        if (latestId === undefined || listening.id > latestId) {
          latestId = listening.id;
        }
        if (previousState?.latestId !== undefined && listening.id <= previousState.latestId) {
          continue;
        }
        newListenings.push(listening);
      }
    
      if (latestId !== undefined) {
        await wmill.setInternalState({
          latestId,
        });
      }
      return newListenings;
    }

    Submitted by jaller94 1489 days ago