Created
April 26, 2019 08:02
-
-
Save StefKors/5bdf3c4bdd2628daf49db2503cfc6ad8 to your computer and use it in GitHub Desktop.
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
const router = require('express').Router() | |
// FLICKR | |
const FLICKR_API = require('../../settings/flickr.js') | |
const Flickr = require('flickr-sdk') | |
const flickr = new Flickr(FLICKR_API.key) | |
// FIREBASE | |
const admin = require('firebase-admin') | |
const serviceAccount = require('./../../settings/progressbar-cache-firebase-adminsdk.json') | |
// init firebase app | |
if (!admin.apps.length) { | |
admin.initializeApp({ | |
credential: admin.credential.cert(serviceAccount), | |
databaseURL: 'https://progressbar-cache.firebaseio.com' | |
}) | |
} | |
// setup the correct collection to reference | |
var db = admin.database() | |
var ref = db.ref('cache') | |
// init obj for collecting of data | |
var overviewObj = [] | |
function safeSet(val) { | |
if (val == undefined) { | |
return null | |
} else { | |
return val | |
} | |
} | |
var readRef = db.ref("cache/airtable-albums"); | |
var Array_Albums = []; | |
// readRef.on("value", function(snapshot) { | |
// // console.log(snapshot.val()); | |
// for (let i = 0; i < snapshot.val().length; i++) { | |
// const id = snapshot.val()[i].fields.Number; | |
// // Array_Albums.push(id) | |
// setTimeout(() => { | |
// callAPI(id); | |
// console.count('Flickr -> Firebase ' + id); | |
// }, 1000 * 60 * 60 * (i)); // 1 hour | |
// } | |
// }, function (errorObject) { | |
// console.log("The albums read failed: " + errorObject.code); | |
// }); | |
var S03_ID = '72157697093742854' | |
var pageNo = 1 | |
function callAPI(id) { | |
flickr.photosets | |
.getPhotos({ | |
photoset_id: id, | |
user_id: FLICKR_API.id, | |
page: pageNo | |
}) | |
.then(function(res) { | |
var flickrRef = ref.child('flickr_v2') | |
let array = res.body.photoset.photo | |
let size = 'b' // b=big m=medium s=small t=thumb | |
array.forEach(photo => { | |
photo.url = `https://farm${photo.farm}.staticflickr.com/${ photo.server}/${photo.id}_${photo.secret}_${size}.jpg` | |
flickr.tags | |
.getListPhoto({ | |
photo_id: photo.id | |
}) | |
.then(function(res) { | |
photo.tags = res.body.photo.tags.tag | |
// ------------------------------- | |
overviewObj.push(photo) | |
// ------------------------------- | |
}) | |
.catch(function(err) { | |
console.log('flickr.tags.getListphoto error', err) | |
}) | |
}) | |
// https://stackoverflow.com/questions/46408326/android-firebase-save-new-data-without-overwriting-old-data | |
setTimeout(function(){ | |
if (overviewObj) { | |
console.log(overviewObj.length) | |
flickrRef.push().set(overviewObj) | |
} | |
}, 40000); // wait 20 sec | |
}) | |
.catch(function(err) { | |
console.error('flickr photoset error', err) | |
}) | |
} | |
// callAPI() | |
// call every ?120? minutes to update | |
// callAPI() | |
setInterval(() => { | |
// console.count('Flickr -> Firebase'); | |
// callAPI() | |
}, 1000 * 60 * 120); | |
router.get('/', function(req, res) { | |
res.send('API route Flickr') | |
}) | |
module.exports = router |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment