Created
August 19, 2022 10:48
-
-
Save trumad/75f6c834f6acb3132b6a313dfa6c316d to your computer and use it in GitHub Desktop.
tampermonkey script to download photos from Flickr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Flickr Downloader | |
// @namespace https://github.com/f2face/flickr-dl.userscript | |
// @version 0.2.3 | |
// @description A userscript for downloading Flickr photos. | |
// @author f2face / trumad | |
// @match https://www.flickr.com/* | |
// @include http://www.flickr.com/* | |
// @include https://www.flickr.com/* | |
// @grant unsafeWindow | |
// @grant GM_download | |
// @require https://cdn.rawgit.com/uzairfarooq/arrive/v2.4.1/minified/arrive.min.js | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
// Flickr API endpoint | |
var api_endpoint = 'https://api.flickr.com/services/rest/?method=flickr.photos.getSizes&format=json&nojsoncallback=1'; | |
function downloadAll(){ | |
let buttons = document.querySelectorAll("[title*=Download]") | |
for (let i=0,j = buttons.length;i<j;i++){ | |
buttons[i].click() | |
} | |
} | |
function addDownloadButton(element) { | |
// Arrive | |
document.arrive(element, function(){ | |
var el = this; | |
var dlbar = createButton(); | |
var dlbtn = dlbar.getElementsByTagName('button')[0]; | |
var photo_url = unsafeWindow.location.href; | |
var interaction_bar = el.getElementsByClassName('interaction-bar'); | |
if (interaction_bar.length > 0) { | |
dlbar.style.position = 'absolute'; | |
dlbar.style.top = '-60px'; | |
dlbar.style.right = '10px'; | |
interaction_bar[0].appendChild(dlbar); | |
photo_url = el.getElementsByClassName('overlay')[0].getAttribute('href'); | |
} | |
else { | |
dlbar.style.marginBottom = '10px'; | |
dlbar.style.paddingBottom = '10px'; | |
dlbar.style.borderBottom = '1px solid #cfd6d9'; | |
el.getElementsByClassName('sub-photo-right-view')[0].prepend(dlbar); | |
} | |
// OnClick event | |
dlbtn.addEventListener('click', function(){ | |
var uri = photo_url; | |
var regex_patt = /\/(\d+)\//gi; | |
var photo_id = regex_patt.exec(uri)[1]; | |
var request_url = api_endpoint + '&api_key=' + getApiSiteKey() + '&photo_id=' + photo_id + '&csrf=' + getCsrfToken(); | |
dlbtn.disabled = true; | |
ajaxSend(request_url, function(data){ | |
data = JSON.parse(data); | |
download(data); | |
dlbtn.disabled = false; | |
}); | |
}); | |
}); | |
// Leave | |
document.leave(element, function(){ | |
document.unbindArrive(this); | |
}); | |
} | |
function createButton() { | |
var dlbar = document.createElement('div'); | |
dlbar.className = 'tool'; | |
dlbar.innerHTML = '<button style="min-width:0; padding:0 10px; z-index:100000;" title="Download">Download</button>'; | |
return dlbar; | |
} | |
function getApiSiteKey() { | |
return unsafeWindow.YUI_config.flickr.api.site_key; | |
} | |
function getCsrfToken() { | |
return unsafeWindow.YUI_config.flickr.csrf.token; | |
} | |
function basename(path) { | |
return path.split('/').slice(-1)[0]; | |
} | |
function ajaxSend(url, callback) { | |
var xhttp = new XMLHttpRequest(); | |
xhttp.onreadystatechange = function() { | |
if (this.readyState == 4 && this.status == 200) { | |
callback(this.responseText); | |
} | |
}; | |
xhttp.open("GET", url, true); | |
xhttp.send(); | |
} | |
function prepareImgLink(link) { | |
var img = link.split('.'); | |
return img.slice(0, -1).join('.') + '_d.' + img.slice(-1)[0]; | |
} | |
function isFirefox() { | |
return /Firefox\//i.test(navigator.userAgent); | |
} | |
async function download(data) { | |
if (!data.hasOwnProperty('sizes')) { | |
var error_msg = 'An error occured. Please refresh the page.'; | |
alert(data.hasOwnProperty('message') ? error_msg+"\r\nError: "+data.message : error_msg); | |
return false; | |
} | |
var largest_photo = data.sizes.size.slice(-1)[0]; | |
var img = prepareImgLink(largest_photo.source); | |
console.log(img); | |
await GM_download({ | |
saveAs: false, | |
url: img, | |
name: largest_photo.source, | |
}) | |
/* | |
var a = document.createElement('a'); | |
a.href = img; | |
if (isFirefox()) | |
a.dispatchEvent(new MouseEvent('click')); | |
else | |
a.click(); | |
*/ | |
} | |
// Add download button on photos grid | |
addDownloadButton('.photo-list-photo-interaction'); | |
// Add download button on single photo page | |
addDownloadButton('.photo-page-scrappy-view'); | |
document.querySelector('.download-album-icon').addEventListener('click',downloadAll); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This makes the "download all" button on flickr actually download all images on the page. Scroll down to make sure they're all loaded.