-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse_youtube_json.js
114 lines (100 loc) · 4.27 KB
/
parse_youtube_json.js
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
114
let settings = input.config({
title: "Extract and Format JSON Data",
description: `This script will parse the JSON contents from the specified fields and extract the values into separate fields.`,
items: [
input.config.table("table", { label: "Table" }),
input.config.field("statisticsField", {
parentTable: "table",
label: "Statistics JSON field",
}),
input.config.field("snippetField", {
parentTable: "table",
label: "Snippet JSON field",
}),
input.config.field("viewCountField", {
parentTable: "table",
label: "Field to store viewCount",
}),
input.config.field("likeCountField", {
parentTable: "table",
label: "Field to store likeCount",
}),
input.config.field("favoriteCountField", {
parentTable: "table",
label: "Field to store favoriteCount",
}),
input.config.field("commentCountField", {
parentTable: "table",
label: "Field to store commentCount",
}),
input.config.field("defaultAudioLanguageField", {
parentTable: "table",
label: "Field to store defaultAudioLanguage",
}),
],
});
async function extractAndFormatJSON() {
let { table, statisticsField, snippetField, viewCountField, likeCountField, favoriteCountField, commentCountField, defaultAudioLanguageField } = settings;
let query = await table.selectRecordsAsync();
let outputData = [];
// Loop through each record
for (let record of query.records) {
// Parse the JSON fields
let statistics = record.getCellValue(statisticsField);
let snippet = record.getCellValue(snippetField);
let viewCount, likeCount, favoriteCount, commentCount, defaultAudioLanguage;
if (statistics) {
try {
let statisticsJson = JSON.parse(statistics);
// Extract values from statistics
viewCount = statisticsJson.viewCount ? parseInt(statisticsJson.viewCount, 10) : null;
likeCount = statisticsJson.likeCount ? parseInt(statisticsJson.likeCount, 10) : null;
favoriteCount = statisticsJson.favoriteCount ? parseInt(statisticsJson.favoriteCount, 10) : null;
commentCount = statisticsJson.commentCount ? parseInt(statisticsJson.commentCount, 10) : null;
// Update the record with the extracted and formatted values
await table.updateRecordAsync(record.id, {
[viewCountField.id]: viewCount,
[likeCountField.id]: likeCount,
[favoriteCountField.id]: favoriteCount,
[commentCountField.id]: commentCount
});
} catch (e) {
console.log(`Error parsing statistics JSON for record ${record.id}: ${e}`);
}
}
if (snippet) {
try {
let snippetJson = JSON.parse(snippet);
// Extract default audio language
defaultAudioLanguage = snippetJson.defaultAudioLanguage ? snippetJson.defaultAudioLanguage : "";
// Update the record with the extracted value
await table.updateRecordAsync(record.id, {
[defaultAudioLanguageField.id]: defaultAudioLanguage
});
} catch (e) {
console.log(`Error parsing snippet JSON for record ${record.id}: ${e}`);
}
}
// Add data to output table
outputData.push({
RecordID: record.id,
ViewCount: viewCount,
LikeCount: likeCount,
FavoriteCount: favoriteCount,
CommentCount: commentCount,
DefaultAudioLanguage: defaultAudioLanguage
});
// Periodically update the output to show progress
if (outputData.length % 10 === 0) {
output.clear();
output.markdown(`Processed ${outputData.length} records so far...`);
output.table(outputData);
}
}
// Final output
output.clear();
output.markdown(`Processed all records.`);
output.table(outputData);
output.text('Update complete.');
}
await extractAndFormatJSON();