Repository navigation
Expand file tree
/
Copy pathpullShows.js
More file actions
113 lines (89 loc) · 3.4 KB
/
Copy pathpullShows.js
File metadata and controls
113 lines (89 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
require('dotenv').config();
let axios = require('axios');
const prisma = require('./prisma/prismaClient.js');
const getOmrToken = require('./getOmrToken');
let startDate = new Date();
startDate = startDate.toLocaleDateString();
startDate = startDate.replaceAll("/","-")
let endDate = new Date();
endDate.setDate (endDate.getDate() + 7);
endDate = endDate.toLocaleDateString();
endDate = endDate.replaceAll("/","-")
const url = `https://www.ohmyrockness.com/api/shows.json?daterange%5Bfrom%5D=${startDate}&daterange%5Buntil%5D=${endDate}&index=true&per=500®ioned=1`
const pullShows = async function() {
try {
console.log("getting omr token and fetching shows");
const result = await getOmrToken();
if (!result || !result.apiData || !result.apiData.success) {
console.error("Failed to get shows data:", result?.apiData?.error || "No data returned");
return;
}
const shows = result.apiData.data;
console.log(`Received ${shows.length} shows from API`);
console.log("Starting to process shows...");
for (const show of shows) {
try {
console.log(`Processing show ${show.id} - ${show.venue?.name}`);
console.log(`Raw starts_at: ${show.starts_at}`);
console.log(`Parsed date: ${new Date(show.starts_at)}`);
console.log(`Parsed date ISO: ${new Date(show.starts_at).toISOString()}`);
const result = await prisma.$transaction(async (prisma) => {
console.log("adding show: " + show.id);
const newShow = await prisma.show.upsert({
where: { omr_id: show.id },
update: {
ticket_url: show.tickets_url,
venue_name: show.venue.name,
latlong: `[${show.venue.latitude},${show.venue.longitude}]`,
date: new Date(show.starts_at),
},
create: {
omr_id: show.id,
ticket_url: show.tickets_url,
venue_name: show.venue.name,
latlong: `[${show.venue.latitude},${show.venue.longitude}]`,
date: new Date(show.starts_at),
},
});
console.log("successfully added show: " + show.id);
console.log("show obj: " + newShow.id, newShow.omr_id);
const bands = show.cached_bands;
for (const band of bands) {
const newBand = await prisma.band.upsert({
where: { name: band.name },
update: {},
create: { name: band.name },
});
console.log("band added: " + newBand.id, newBand.name);
// Add relationship in the join table BandShow
await prisma.bandShow.upsert({
where: {
bandId_showId: {
bandId: newBand.id,
showId: newShow.id,
},
},
update: {},
create: {
bandId: newBand.id,
showId: newShow.id,
},
});
}
});
console.log('Transaction committed for show:', show.id);
} catch (err) {
console.error('Transaction failed for show:', show.id, 'Error:', err.message);
continue
// Rethrow or handle as needed
}
}
} catch (err) {
console.error("Error:", err, err.response ? err.response.data : err.message);
}
};
module.exports = pullShows
// let getShows = ()=>{
// pullShows()
// }
// getShows()