|
1 | | -import { createClient } from "https://esm.sh/@supabase/supabase-js@2"; |
2 | | -import { getRecentTracks } from "./lastfm.ts"; |
3 | | -import { Row, TableListened } from "./table.ts"; |
| 1 | +import * as Sentry from "https://deno.land/x/sentry/index.mjs"; |
| 2 | +import { scrobbles } from "./scrobbles.ts"; |
4 | 3 | import { env } from "./env.ts"; |
5 | 4 |
|
6 | | -Deno.serve(async () => { |
7 | | - const size = 50; |
8 | | - const supabaseClient = createClient(env.SUPABASE_URL, env.SUPABASE_ANON_KEY); |
9 | | - const table = new TableListened(supabaseClient); |
10 | | - const startFrom: number | null = await table.getLastListenedDate(); |
11 | | - let totalPages = 1; |
12 | | - let total = 0; |
13 | | - let page = 1; |
14 | | - |
15 | | - if (startFrom) { |
16 | | - console.log( |
17 | | - "Starting from last listened date:", |
18 | | - new Date(startFrom * 1000).toLocaleString(), |
19 | | - ); |
20 | | - } else { |
21 | | - console.log("Database is empty, starting from the last page"); |
22 | | - } |
| 5 | +Sentry.init({ |
| 6 | + dsn: Deno.env.get("SENTRY_DSN")!, |
| 7 | + defaultIntegrations: false, |
| 8 | + tracesSampleRate: 1.0, |
| 9 | + // deno-lint-ignore ban-ts-comment |
| 10 | + // @ts-ignore |
| 11 | + profilesSampleRate: 1.0, |
| 12 | +}); |
23 | 13 |
|
24 | | - const fmInitial = await getRecentTracks( |
25 | | - env.LASTFM_API_KEY, |
26 | | - env.LASTFM_USERNAME, |
27 | | - 1, |
28 | | - size, |
29 | | - startFrom, |
30 | | - ); |
31 | | - if (fmInitial === null) { |
32 | | - console.error("Fail - No tracks returned from initial api request."); |
33 | | - return new Response("error", { headers: { "Content-Type": "text/plain" } }); |
34 | | - } |
35 | | - const paginationInitial = fmInitial.recenttracks["@attr"]; |
36 | | - totalPages = parseInt(paginationInitial.totalPages); |
37 | | - total = parseInt(paginationInitial.total); |
38 | | - page = totalPages; |
| 14 | +// Set region and execution_id as custom tags |
| 15 | +Sentry.setTag("region", Deno.env.get("SB_REGION")); |
| 16 | +Sentry.setTag("execution_id", Deno.env.get("SB_EXECUTION_ID")); |
| 17 | +Sentry.setTag("url", env.SUPABASE_URL); |
39 | 18 |
|
40 | | - if (startFrom) { |
41 | | - console.log( |
42 | | - `Found ${total} new tracks to save! Starting from page ${totalPages}.`, |
43 | | - ); |
44 | | - } else { |
45 | | - console.log( |
46 | | - `Found ${total} tracks in total. Starting from page ${totalPages}.`, |
47 | | - ); |
| 19 | +Deno.serve(async () => { |
| 20 | + try { |
| 21 | + const result = await scrobbles(env); |
| 22 | + return new Response(result, { |
| 23 | + status: 200, |
| 24 | + headers: { "Content-Type": "text/plain" }, |
| 25 | + }); |
| 26 | + } catch (e) { |
| 27 | + Sentry.captureException(e); |
| 28 | + return new Response("error occured, please check sentry/supabase logs", { |
| 29 | + status: 500, |
| 30 | + headers: { "Content-Type": "text/plain" }, |
| 31 | + }); |
48 | 32 | } |
49 | | - |
50 | | - let processedPages = 0; |
51 | | - do { |
52 | | - if (processedPages === 10) { |
53 | | - console.log( |
54 | | - `Already inserted ${ |
55 | | - processedPages * size |
56 | | - } items to db. Stopped. See ya in next cron.`, |
57 | | - ); |
58 | | - break; |
59 | | - } |
60 | | - |
61 | | - const fm = await getRecentTracks( |
62 | | - env.LASTFM_API_KEY, |
63 | | - env.LASTFM_USERNAME, |
64 | | - page, |
65 | | - size, |
66 | | - startFrom, |
67 | | - ); |
68 | | - if (fm === null) { |
69 | | - console.error("Fail - No tracks returned from api request."); |
70 | | - break; |
71 | | - } |
72 | | - const data = fm.recenttracks; |
73 | | - const tracks = data.track; |
74 | | - |
75 | | - if (total === 0 || tracks.length === 0) { |
76 | | - break; |
77 | | - } |
78 | | - |
79 | | - console.log(`Fetching page ${page}/${totalPages}`); |
80 | | - |
81 | | - const toInsert: Row[] = tracks |
82 | | - .filter((track) => !(track["@attr"] && track["@attr"].nowplaying)) |
83 | | - .map((track) => ({ |
84 | | - created_at: new Date().toISOString(), |
85 | | - listened_at: new Date(track.date!.uts * 1000).toISOString(), |
86 | | - artist_name: track.artist.name, |
87 | | - track_name: track.name, |
88 | | - album_name: track.album["#text"], |
89 | | - lastfm_data: track, |
90 | | - })); |
91 | | - |
92 | | - const { error, message } = await table.save(toInsert); |
93 | | - if (error) { |
94 | | - break; |
95 | | - } |
96 | | - |
97 | | - if (message) { |
98 | | - console.log(message); |
99 | | - } |
100 | | - |
101 | | - processedPages++; |
102 | | - page--; |
103 | | - } while (page >= 1); |
104 | | - |
105 | | - return new Response("ok1", { headers: { "Content-Type": "text/plain" } }); |
106 | 33 | }); |
0 commit comments