]> git.r.bdr.sh - rbdr/netnewswire-to-apple-music/blob - netnewswire_to_apple_music.scpt
e7bf57c957178dafb0513803cc6485af12dbc5ce
[rbdr/netnewswire-to-apple-music] / netnewswire_to_apple_music.scpt
1 JsOsaDAS1.001.00bplist00Ñ\ 1\ 2Vscript_\11\10rconst internals = {
2 config: {
3 account: "iCloud",
4 musicFolder: "155 - music"
5 },
6 kURLParser: /https?:\/\/[^\s'"><]+/gi,
7
8 strategies: [
9 // youtube embed
10 {
11 kMatcher: /youtube.com\/embed\/([^?]+)/,
12 canHandle(url) {
13 return url.match(this.kMatcher);
14 },
15 handle(url) {
16 return 'youtube_embed:' + url.match(this.kMatcher)[1];
17 }
18 },
19 // youtube video
20 {
21 kMatcher: /youtube.com\/watch.*v=([^&]+)/,
22 canHandle(url) {
23 return url.match(this.kMatcher);
24 },
25 handle(url) {
26 return 'youtube_video:' + url.match(this.kMatcher)[1];
27 }
28 },
29 // youtube playlist
30 {
31 kMatcher: /youtube.com\/playlist.*list=([^&]+)/,
32 canHandle(url) {
33 return url.match(this.kMatcher);
34 },
35 handle(url) {
36 return 'youtube_playlist:' + url.match(this.kMatcher)[1];
37 }
38 },
39 // youtube unmatched
40 {
41 canHandle(url) {
42 return url.match(/youtube/);
43 },
44 handle(url) {
45 return '!!!!!!!!!!!!!!!!!:' + url
46 }
47 },
48 // bandcamp embedded track
49 {
50 kMatcher: /bandcamp.*EmbeddedPlayer.*track=([0-9]+)/,
51 canHandle(url) {
52 return url.match(this.kMatcher);
53 },
54 handle(url) {
55 return 'bc_embed_track:' + url.match(this.kMatcher)[1];
56 }
57 },
58 // bandcamp embedded album
59 {
60 kMatcher: /bandcamp.*EmbeddedPlayer.*album=([0-9]+)/,
61 canHandle(url) {
62 return url.match(this.kMatcher);
63 },
64 handle(url) {
65 return 'bc_embed_album:' + url.match(this.kMatcher)[1];
66 }
67 },
68 // bandcamp album
69 {
70 canHandle(url) {
71 return url.match(/https?:\/\/([^.]+)\.bandcamp\.com\/album/);
72 },
73 handle(url) {
74 return 'bc_album:' + url
75 }
76 },
77 // bandcamp album
78 {
79 canHandle(url) {
80 return url.match(/https?:\/\/([^.]+)\.bandcamp\.com\/track/);
81 },
82 handle(url) {
83 return 'bc_track:' + url
84 }
85 },
86 // bandcamp artist
87 {
88 kMatcher: /https?:\/\/([^.]+)\.bandcamp.com(?:\/music)?/,
89 canHandle(url) {
90 return url.match(this.kMatcher);
91 },
92 handle(url) {
93 return 'bc_artist:' + url.match(this.kMatcher)[1];
94 }
95 },
96 // bandcamp unmatched
97 {
98 canHandle(url) {
99 return url.match(/bandcamp\.com/);
100 },
101 handle(url) {
102 return '!!!!!!!!!!!!!!!!!:' + url
103 }
104 },
105 // soundcloud player
106 {
107 kMatcher: /soundcloud.com\/player.*tracks%2F([^&]+)/,
108 canHandle(url) {
109 return url.match(this.kMatcher);
110 },
111 handle(url) {
112 return 'sc_player: ' + url.match(this.kMatcher)[1]
113 }
114 },
115 // soundcloud track
116 {
117 kMatcher: /soundcloud.com\/([^\/]+)\/([^\/]+)/,
118 canHandle(url) {
119 return url.match(this.kMatcher);
120 },
121 handle(url) {
122 return 'soundcloud_track: ' + url.match(this.kMatcher)[2]
123 }
124 },
125 // soundcloud unmatched
126 {
127 canHandle(url) {
128 return url.match(/soundcloud/);
129 },
130 handle(url) {
131 return '!!!!!!!!!!!!!!!!!:' + url
132 }
133 },
134 // spotify embed
135 {
136 kMatcher: /spotify.com\/embed\/track\/([^?]+)/,
137 canHandle(url) {
138 return url.match(this.kMatcher);
139 },
140 handle(url) {
141 return 'sp_embed:' + url.match(this.kMatcher)[1];
142 }
143 },
144 // spotify unmatched
145 {
146 canHandle(url) {
147 return url.match(/spotify/);
148 },
149 handle(url) {
150 return '!!!!!!!!!!!!!!!!!:' + url
151 }
152 },
153 ],
154
155 extractURLsFromFeeds() {
156 const app = Application("NetNewsWire");
157 app.includeStandardAdditions = true;
158
159 let account = app.accounts.whose({name: internals.config.account})[0];
160 let musicFolder = account.folders.whose({name: internals.config.musicFolder})[0];
161 const feeds = musicFolder.webfeeds();
162
163 const urls = [];
164
165 for (const feed of feeds) {
166 const articles = feed.articles();
167
168 for (const article of articles) {
169 console.log(article.title())
170 const html = article.html();
171 const extractedURLs = html.match(internals.kURLParser) || [];
172 urls.push(...extractedURLs)
173 }
174 }
175
176 return urls;
177 },
178
179 extractSongsFromURLs(urls) {
180
181 let songs = [];
182
183 for (url of urls) {
184 for (const strategy of internals.strategies) {
185 if (strategy.canHandle(url)) {
186 songs.push(strategy.handle(url))
187 break;
188 }
189 }
190 }
191
192 return songs;
193 }
194 };
195
196 const run = function () {
197
198 const urls = internals.extractURLsFromFeeds();
199 const songs = internals.extractSongsFromURLs(urls);
200
201 return songs;
202 }
203
204 run();