Created
September 30, 2020 15:29
-
-
Save malibayram/78137920acb3509279aa0c64b1154273 to your computer and use it in GitHub Desktop.
5 saatte bir çalışarak kanala yeni eklenen videoların bilgilerini firestore kaydetme işlemi
This file contains hidden or 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 { db, admin } = require('../admin'); | |
const functions = require('firebase-functions'); | |
const https = require('https'); | |
const runtimeOpts = { | |
timeoutSeconds: 540, // firebase max of 9 minutes | |
} | |
const apiKey = "AIzaS**************************QX0c"; | |
const channelId = "UCrVR30q-Seo82gBhmAe_1uQ"; | |
const yeniVideoKontrolEt = functions.runWith(runtimeOpts) | |
.pubsub.schedule('every 5 hours').onRun(async (context) => { | |
console.log(context.timestamp); | |
console.log('yeniVideoKontrolEt : every 5 hours'); | |
const videolar = await db.collection('youtube_videolari').get(); | |
let videoIDS = []; | |
videolar.forEach((v) => { | |
const videoId = v.id; | |
videoIDS.push(videoId); | |
}); | |
const searchUrl = `https://www.googleapis.com/youtube/v3/search?key=${apiKey}&channelId=${channelId}&part=id&order=date&maxResults=4`; | |
const searchRequest = https.get(searchUrl, (res2) => { | |
res2.on('data', (d2) => { | |
console.log('data: ' + d2); | |
try { | |
const sonuc = JSON.parse(d2.toString()); | |
if (sonuc.items) | |
sonuc.items.forEach((s) => { | |
if (!videoIDS.includes(s.id.videoId)) { | |
db.collection('youtube_videolari').doc(s.id.videoId).set({ | |
id: s.id.videoId, | |
ekleyen: 'cloud_functions', | |
cloud: 'cloud_functions', | |
timestamp: admin.firestore.FieldValue.serverTimestamp(), | |
onay: false, | |
}); | |
} | |
}); | |
} catch (e) { | |
console.log('hata: ' + e); | |
} | |
}); | |
}); | |
searchRequest.on('error', (e2) => { | |
console.log("searchRequest got error " + e2); | |
}); | |
}); | |
module.exports = yeniVideoKontrolEt; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment