@@ -90,27 +90,47 @@ export default defineComponent({
90
90
}
91
91
return 0 ;
92
92
},
93
- async getInfo(stringData : string ) {
93
+ async getInfo(stringData : string , json : boolean ) {
94
94
try {
95
95
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
-
107
96
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
+ }
114
134
}
115
135
116
136
const urlChunks = urls .reduce ((acc , url , index ) => {
0 commit comments