-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch-history-exporter-for-amazon-prime-video.js
170 lines (140 loc) · 4.63 KB
/
watch-history-exporter-for-amazon-prime-video.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
(() => {
// Delimiters for the CSV file
const DELIMITER = {
STRING: '"',
FIELD: ',',
RECORD: '\n',
};
// Column names for the CSV file
const COLUMN_NAME = {
DATE_WATCHED: 'Date Watched',
TYPE: 'Type',
TITLE: 'Title',
EPISODE_TITLE: 'Episode',
};
// Values for the TYPE column in the CSV file
const MEDIA_TYPE_NAME = {
SERIES: 'Series',
MOVIE: 'Movie',
};
// Print an informational message to the console
const log = (msg, showPrefix = true, startGroup = false) => {
const logFunc = startGroup ? console.group : console.info;
const prefixArray = showPrefix
? [
'%c[Watch History Exporter for Amazon Prime]',
'color:#1399FF;background:#00050d;font-weight:bold;',
]
: [];
logFunc(...prefixArray, msg);
};
// Add a movie or episode to the array
const addItem = (watchHistoryArray, dateWatched, title, episodeTitle) => {
const formattedDateWatched = new Date(dateWatched)
.toISOString()
.split('T')[0];
const mediaType = episodeTitle
? MEDIA_TYPE_NAME.SERIES
: MEDIA_TYPE_NAME.MOVIE;
const formattedTitle = `${DELIMITER.STRING}${title}${DELIMITER.STRING}`;
const formattedEpisodeTitle = episodeTitle
? `${DELIMITER.STRING}${episodeTitle}${DELIMITER.STRING}`
: '';
watchHistoryArray.push([
formattedDateWatched,
mediaType,
formattedTitle,
formattedEpisodeTitle,
]);
return watchHistoryArray;
};
// Parse the watch history and return an array of arrays
const parseWatchHistory = () => {
log('Parsing watch history... Items found:', true, true);
// Initialize an empty array to store the watch history
const watchHistoryArray = [];
// Select all list items within the watch history
const dateSections = document.querySelectorAll(
'div[data-automation-id=activity-history-items] > ul > li',
);
// Loop over date sections
for (const dateSection of dateSections) {
const mediaSections = dateSection.querySelectorAll('ul > li');
const dateWatched = dateSection.querySelector(
'[data-automation-id^="wh-date"]',
).textContent;
log(dateWatched, false, true);
// Loop over media watched for each date
for (const mediaSection of mediaSections) {
const episodesWatchedCheckbox =
mediaSection.querySelector('[type="checkbox"]');
const title = mediaSection.querySelector('img').alt;
// If the 'Episodes watched' checkbox exists, it's a series
// Otherwise, it's a movie
if (episodesWatchedCheckbox) {
log(`[${MEDIA_TYPE_NAME.SERIES}] ${title}`, false, true);
// Click the 'Episodes watched' checkbox if it exists to get the episode information
if (!episodesWatchedCheckbox.checked) {
// A click event is required to load the episode information (checking from DOM doesn't work)
episodesWatchedCheckbox.click();
}
const episodeSections = mediaSection.querySelectorAll(
'[data-automation-id^=wh-episode] > div > p',
);
// Loop over episodes watched for each series
for (const episodeSection of episodeSections) {
const episodeTitle = episodeSection?.textContent?.trim();
log(episodeTitle, false);
addItem(watchHistoryArray, dateWatched, title, episodeTitle);
}
console.groupEnd();
} else {
log(`[${MEDIA_TYPE_NAME.MOVIE}] ${title}`, false);
addItem(watchHistoryArray, dateWatched, title);
}
}
console.groupEnd();
}
console.groupEnd();
return watchHistoryArray;
};
// Force lazy loading of the watch history by scrolling to the bottom of the page
const forceLoadWatchHistory = async () => {
log('Loading watch history...');
return new Promise((resolve) => {
const autoScrollInterval = setInterval(() => {
if (
!document.querySelector(
'div[data-automation-id=activity-history-items] > div > noscript',
)
) {
clearInterval(autoScrollInterval);
resolve();
}
window.scrollTo(0, document.body.scrollHeight);
}, 500);
});
};
// Download the watch history as a CSV file
const downloadCsv = (inputArray) => {
log('Saving CSV file...', true, true);
log(
'If you are not prompted to save a file, make sure "Pop-ups and redirects" and "Automatic downloads" are enabled for www.primevideo.com in your browser.',
false,
);
console.groupEnd();
const csvData = [Object.values(COLUMN_NAME), ...inputArray]
.map((item) => item.join(DELIMITER.FIELD))
.join(DELIMITER.RECORD);
const csvDataUrl = `data:text/csv;charset=utf-8,${encodeURIComponent(csvData)}`;
window.open(csvDataUrl);
};
// Entry point
const main = async () => {
log('Script started');
await forceLoadWatchHistory();
downloadCsv(parseWatchHistory());
log('Script finished');
};
return main();
})();