-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchURLs.js
75 lines (58 loc) · 3.52 KB
/
searchURLs.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
'use strict';
const puppeteer = require('puppeteer');
(async() => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
//Replace the page.goto parameter with the website and UNCOMMENT the below line
// await page.goto('WEBSITE HERE');
const DOMstring = await page.content();
const cheerio = require('cheerio');
// HTML <audio> Tag - https://www.w3schools.com/tags/tag_audio.asp
var audioTag = '<audio controls> <source src="horse.ogg" type="audio/ogg"> <source src="horse.mp3" type="audio/mpeg"> Your browser does not support the audio tag.</audio>';
// HTML <video> Tag - https://www.w3schools.com/tags/tag_video.asp
var videoTag = '<video width="320" height="240" controls> <source src="movie.mp4" type="video/mp4"> <source src="movie.ogg" type="video/ogg"> Your browser does not support the video tag.</video>';
var testCase1 = '<div class="sqs-audio-embed" data-url="https://static1.squarespace.com/static/50dc8cece4b03955129656eb/t/587bb6e5db29d69a1a276170/1484502978069/701_0231+%28online-audio-converter.com%29.mp3/original/701_0231+%28online-audio-converter.com%29.mp3">'
var testCase2 = '<div class="sqs-audio-embed" data-url="https://static1.squarespace.com/static/544b30c0e4b0023e70482546/t/58996bd944024399dae27e39/1486450657175/AF+Ep66.mp3/original/AF+Ep66.mp3">';
var testCase3 = '<div class="dropdown share-dropdown" id="downloadsButton"><button class="share-drop dropdown-toggle" data-toggle="dropdown"><i class="fa fa-download"></i><span class="hidden-xs">download</span></button><div class="dropdown-menu" role="menu" aria-labelledby="dLabel"><a class="btn" href="http://cdnapi.kaltura.com/p/537811/sp/53781100/playManifest/entryId/1_zaatpsw0/flavorId/1_vs6gl6x6/protocol/http/format/url/a.mp4" download="john-cleese-on-creativity-group-dynamics-and-celebrity-lq.mp4">SD video</a><a class="btn" href="http://cdnapi.kaltura.com/p/537811/sp/53781100/playManifest/entryId/1_zaatpsw0/flavorId/1_9ymnkm7b/protocol/http/format/url/a.mp3" download="john-cleese-on-creativity-group-dynamics-and-celebrity.mp3">audio</a></div></div>';
// When testing with your inputs change the cheerio.load parameters to DOMstring
const $ = cheerio.load(testCase1 + testCase2 + testCase3);
getAudioVideoURLs();
browser.close();
function getAudioVideoURLs() {
var audioSource = [], videoSource = [];
$('audio source, video source, .sqs-audio-embed, .share-dropdown > .dropdown-menu > .btn').each(function () {
var source = $(this).attr('src');
var isAudioSource = $(this).is('audio source');
var isSQSAudioEmbed = $(this).is('.sqs-audio-embed');
var isShareDropDownAnchorButton = $(this).is('.share-dropdown > .dropdown-menu > .btn');
var href = $(this).attr('href');
if (isAudioSource) {
audioSource.push(source);
}
else if (isSQSAudioEmbed) {
audioSource.push($(this).data('url'));
}
else if (isShareDropDownAnchorButton) {
var hrefIsAudio = href.indexOf("mp3") !== -1;
if (hrefIsAudio) {
audioSource.push(href);
}
else {
videoSource.push(href);
}
} else {
videoSource.push(source);
}
});
showLinksIfAvailable(audioSource, "Audio");
showLinksIfAvailable(videoSource, "Video");
}
function showLinksIfAvailable(source, type) {
if (source.length) {
console.log("\n" + type + " Links -");
source.forEach(function (link) {
console.log(link);
});
}
}
})();