Skip to content

Instantly share code, notes, and snippets.

@moeiscool
Last active May 1, 2024 12:30
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save moeiscool/fe7d027c9a9711be28f73c9b653aa5c7 to your computer and use it in GitHub Desktop.
Save moeiscool/fe7d027c9a9711be28f73c9b653aa5c7 to your computer and use it in GitHub Desktop.
Amcrest Camera Mass (Bulk) Video Exporter with Web Browser Console
/// How it works :
/// This script will download videos from your Amcrest Camera automatically (automate cumbersome task of individually clicking download on each video)
/// Limitation is that it will stop downloading when it reaches the end of the month.
/// HOW TO USE :
// 1. Login to Amcrest camera and click Playback tab.
// 2. Select Day of the Month to begin downloading from.
// 3. Open the Web Browser Console. (CTRL + SHIFT + C on Mac)
// 4. Paste this entire script in the execution field and run it.
// 5. You will see the files start downloading.
// Your browser may warn about "Multiple Files Download", Allow it to do this.
function waitSync(waitTime){
return new Promise((resolve,reject) => {
setTimeout(function(){
resolve()
},waitTime)
})
}
function downloadFile(fileLink){
return new Promise((resolve,reject) => {
fetch(fileLink)
.then(resp => resp.blob())
.then(blob => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.style.display = 'none';
a.href = url;
// the filename you want
var fileParts = fileLink.split('/')
var filename = `Amcrest-${fileParts[fileParts.length - 5]}_${fileParts[fileParts.length - 4]}-${fileParts[fileParts.length - 3]}_${fileParts[fileParts.length - 1].replace('[M][0@0][0]','')}`
a.download = filename;
document.body.appendChild(a);
a.click();
window.URL.revokeObjectURL(url);
console.log('your file has downloaded!',fileLink); // or you know, something with better UX...
setTimeout(function(){
resolve(true)
},1000)
})
.catch(() => {
resolve(false)
});
})
}
async function downloadFilesInlist(){
var thelist = []
await waitSync(1500)
jQuery('[data-action="download"]').each(function(n,downloadButton){
var el = jQuery(downloadButton)
el.click()
var downloadLink = location.origin + el.attr('href')
thelist.push(downloadLink)
})
for (let i = 0; i < thelist.length; i++){
var downloadLink = thelist[i]
var hasDownloaded = await downloadFile(downloadLink)
console.log('hasDownloaded',hasDownloaded)
}
}
async function downloadFilesInPages(){
async function doPageLoop(){
await waitSync(500)
var pageParts = jQuery('.u-page-info:visible').text().split('/')
var n = 0;
pageParts.forEach(function(v){
pageParts[n] = v.trim()
n++;
});
var currentPage = pageParts[0]
var pageMax = pageParts[1]
console.log(`Doing Page ${currentPage} / ${pageMax}...`)
await downloadFilesInlist()
console.log(`Done Page ${currentPage} / ${pageMax}!`)
if(currentPage !== pageMax){
jQuery('.u-pagination .u-input:visible').val(parseInt(currentPage) + 1)
await waitSync(100)
jQuery('[data-action="goto"]:visible').click()
await waitSync(1500)
await doPageLoop()
}
}
await doPageLoop()
console.log('done!')
await waitSync(2000)
}
function doSelectedMark(){
return new Promise((resolve,reject) => {
jQuery('[btn-for="onToRecord"]:visible').click()
downloadFilesInPages().then(() => {
// go back to calendar
setTimeout(function(){
jQuery('[btn-for="onToCalendar"]:visible').click()
resolve()
},2000)
})
})
}
var currentlySelectedMark = null
function loadCalenderMarks(){
var allMarkEls = jQuery('.u-calendar-mark:visible')
var allMarks = []
allMarkEls.each(function(n,v){
var el = jQuery(v)
var isSelected = el.hasClass('u-calendar-select')
var theKey = el.attr('data-value');
if(isSelected)currentlySelectedMark = `${theKey}`
allMarks.push(theKey)
})
return allMarks
}
function selectNextMark(){
var allMarks = loadCalenderMarks()
var indexOfNow = allMarks.indexOf(currentlySelectedMark)
var nextMark = allMarks[indexOfNow + 1]
console.log(currentlySelectedMark)
console.log(nextMark)
if(nextMark){
jQuery(`.u-calendar-mark[data-value="${nextMark}"]`).click()
return true
}
return false
}
function marksChecks(){
loadCalenderMarks()
async function aLoop(){
console.log('currentlySelectedMark',currentlySelectedMark)
await doSelectedMark()
var nextFound = selectNextMark()
if(nextFound)await aLoop()
}
aLoop()
}
marksChecks()
@xenotropic
Copy link

In firefox don't forget to turn off "Always ask where to save files" first! (and thanks for the script!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment