Skip to content

Commit c8ab8fd

Browse files
committed
watch-history.html
1 parent aa6bef7 commit c8ab8fd

File tree

2 files changed

+39
-19
lines changed

2 files changed

+39
-19
lines changed

src/components/FileInputButton.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export default defineComponent({
4545
const reader = new FileReader();
4646
reader.onload = (e: ProgressEvent<FileReader>) => {
4747
const { result } = e.target as FileReader;
48-
if (result) this.$emit('load', String(result));
48+
if (result) this.$emit('load', String(result), file.name.endsWith('.json'));
4949
};
5050
reader.readAsText(file);
5151
}

src/components/Load.vue

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -90,27 +90,47 @@ export default defineComponent({
9090
}
9191
return 0;
9292
},
93-
async getInfo(stringData: string) {
93+
async getInfo(stringData: string, json: boolean) {
9494
try {
9595
const beginningOfYear = new Date(new Date().getFullYear(), 0, 1);
96-
const entries: Array<IEntry> = JSON.parse(stringData).filter((entry: IEntry) => {
97-
const date = new Date(entry.time);
98-
return date > beginningOfYear;
99-
});
100-
const chunks = entries.reduce((acc, entry, index) => {
101-
const chunkIndex = Math.floor(index / 50);
102-
if (!acc[chunkIndex]) acc[chunkIndex] = [];
103-
acc[chunkIndex].push(entry);
104-
return acc;
105-
}, [] as Array<IEntry[]>);
106-
10796
const urls: string[] = [];
108-
for (const chunk of chunks) {
109-
const entryIDs = chunk.map((content) => content.titleUrl?.split('=')[1]);
110-
const url = `https://youtube.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&id=${entryIDs.join(
111-
encodeURIComponent(','),
112-
)}&key=${this.apikey}`;
113-
urls.push(url);
97+
98+
if (json) {
99+
const entries: Array<IEntry> = JSON.parse(stringData).filter((entry: IEntry) => {
100+
const date = new Date(entry.time);
101+
return date > beginningOfYear;
102+
});
103+
const chunks = entries.reduce((acc, entry, index) => {
104+
const chunkIndex = Math.floor(index / 50);
105+
if (!acc[chunkIndex]) acc[chunkIndex] = [];
106+
acc[chunkIndex].push(entry);
107+
return acc;
108+
}, [] as Array<IEntry[]>);
109+
for (const chunk of chunks) {
110+
const entryIDs = chunk.map((content) => content.titleUrl?.split('=')[1]);
111+
const url = `https://youtube.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&id=${entryIDs.join(
112+
encodeURIComponent(','),
113+
)}&key=${this.apikey}`;
114+
urls.push(url);
115+
}
116+
} else {
117+
const regexp = /<a href="https:\/\/www\.youtube\.com\/watch\?v=(.+?)".+?<\/a><br>.+?<\/a><br>(.+?GMT)<\/div>/g;
118+
let matches: [string, Date][] = [...stringData.matchAll(regexp)]
119+
.map((match) => [match[1], new Date(match[2])]);
120+
matches = matches.filter(([, date]) => date >= beginningOfYear);
121+
const chunks = matches.reduce((acc, entry, index) => {
122+
const chunkIndex = Math.floor(index / 50);
123+
if (!acc[chunkIndex]) acc[chunkIndex] = [];
124+
acc[chunkIndex].push(entry);
125+
return acc;
126+
}, [] as Array<[string, Date][]>);
127+
for (const chunk of chunks) {
128+
const entryIDs = chunk.map((content) => content[0]);
129+
const url = `https://youtube.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics&id=${entryIDs.join(
130+
encodeURIComponent(','),
131+
)}&key=${this.apikey}`;
132+
urls.push(url);
133+
}
114134
}
115135
116136
const urlChunks = urls.reduce((acc, url, index) => {

0 commit comments

Comments
 (0)