1

Watch for new listenings

by
Published Sep 29, 2022

GET /api/v1/history/listenings

Scriptยท trigger funkwhale Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 6 days ago
1
//native
2

3
import {
4
  getState,
5
  setState,
6
} from "windmill-client@1";
7

8
/**
9
 * **IMPORTANT:** This does not return listenings on the first run.
10
 *
11
 * You can read more about the Listenings History API at
12
 * https://docs.funkwhale.audio/swagger/#/User%20activity/get_api_v1_history_listenings
13
 */
14
type Funkwhale = {
15
  baseUrl: string;
16
  token: string;
17
};
18
export async function main(
19
  auth: Funkwhale,
20
  query?: string,
21
  scope = "all",
22
  page_size = 10,
23
) {
24
  const previousState = await getState();
25
  const url = new URL("/api/v1/history/listenings", auth.baseUrl);
26
  const params = { query, scope, page_size };
27
  Object.entries(params).forEach(([key, value]) => {
28
    value && url.searchParams.append(key, encodeURIComponent(value));
29
  });
30
  const response = await fetch(url, {
31
    headers: {
32
      Authorization: `Bearer ${auth.token}`,
33
    },
34
  });
35

36
  const data = await response.json();
37
  data.results.reverse();
38

39
  const newListenings = [];
40
  let latestId;
41
  for (const listening of data.results) {
42
    if (latestId === undefined || listening.id > latestId) {
43
      latestId = listening.id;
44
    }
45
    if (
46
      previousState?.latestId !== undefined &&
47
      listening.id <= previousState.latestId
48
    ) {
49
      continue;
50
    }
51
    newListenings.push(listening);
52
  }
53

54
  if (latestId !== undefined) {
55
    await setState({
56
      latestId,
57
    });
58
  }
59
  return newListenings;
60
}
61

Other submissions
  • Submitted by jaller94 Deno
    Created 398 days ago
    1
    import {
    2
      getState,
    3
      setState,
    4
    } from "https://deno.land/x/[email protected]/mod.ts";
    5
    
    
    6
    /**
    7
     * **IMPORTANT:** This does not return listenings on the first run.
    8
     *
    9
     * You can read more about the Listenings History API at
    10
     * https://docs.funkwhale.audio/swagger/#/User%20activity/get_api_v1_history_listenings
    11
     */
    12
    type Funkwhale = {
    13
      baseUrl: string;
    14
      token: string;
    15
    };
    16
    export async function main(
    17
      auth: Funkwhale,
    18
      query?: string,
    19
      scope = "all",
    20
      page_size = 10,
    21
    ) {
    22
      const previousState = await getState();
    23
      const url = new URL("/api/v1/history/listenings", auth.baseUrl);
    24
      const params = { query, scope, page_size };
    25
      Object.entries(params).forEach(([key, value]) => {
    26
        value && url.searchParams.append(key, encodeURIComponent(value));
    27
      });
    28
      const response = await fetch(url, {
    29
        headers: {
    30
          Authorization: `Bearer ${auth.token}`,
    31
        },
    32
      });
    33
    
    
    34
      const data = await response.json();
    35
      data.results.reverse();
    36
    
    
    37
      const newListenings = [];
    38
      let latestId;
    39
      for (const listening of data.results) {
    40
        if (latestId === undefined || listening.id > latestId) {
    41
          latestId = listening.id;
    42
        }
    43
        if (
    44
          previousState?.latestId !== undefined &&
    45
          listening.id <= previousState.latestId
    46
        ) {
    47
          continue;
    48
        }
    49
        newListenings.push(listening);
    50
      }
    51
    
    
    52
      if (latestId !== undefined) {
    53
        await setState({
    54
          latestId,
    55
        });
    56
      }
    57
      return newListenings;
    58
    }
    59