-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9632a3f
commit 9213bf0
Showing
9 changed files
with
376 additions
and
243 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ logs | |
npm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
.vscode | ||
|
||
# Runtime data | ||
pids | ||
|
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
const youtube = require('./youtube'); | ||
const firebase = require("./firebase"); | ||
module.exports.firebase = firebase; | ||
const youtube = require("./youtube"); | ||
module.exports.youtube = youtube; | ||
const firebase = require('./firebase'); | ||
module.exports.firebase = firebase; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,135 +1,131 @@ | ||
// methods to call youtube's api | ||
const moment = require("moment"); | ||
var request = require("request-promise"); | ||
const cache = require("../../cache"); | ||
const firebase = require("../firebase"); | ||
const logger = require("../logger"); | ||
module.exports = YOUTUBE_CRON => { | ||
var request = require("request-promise"); | ||
const cache = require("../../cache"); | ||
const firebase = require("../firebase"); | ||
const logger = require("../logger"); | ||
const { cronRunnerData } = require("../firebase"); | ||
let YOUTUBE_API_KEY; | ||
cronRunnerData(`cron/runners/${YOUTUBE_CRON}`, value => { | ||
YOUTUBE_API_KEY = value.key; | ||
console.log("using new key", YOUTUBE_API_KEY); | ||
}); | ||
|
||
const infoConstructor = item => { | ||
const { id, snippet, statistics } = item; | ||
const { title, customUrl, description, thumbnails } = snippet; | ||
const smallImage = thumbnails.default.url; | ||
const mediumImage = thumbnails.medium.url; | ||
const largeImage = thumbnails.high.url; | ||
const { viewCount, subscriberCount, videoCount } = statistics; | ||
const infoConstructor = item => { | ||
const { id, snippet, statistics, brandingSettings } = item; | ||
const { title, customUrl = null, description = null, thumbnails } = snippet; | ||
const { channel, image } = brandingSettings; | ||
const { | ||
keywords = null, | ||
featuredChannelsUrls = null, | ||
profileColor = null, | ||
unsubscribedTrailer = null | ||
} = channel; | ||
const smallImage = thumbnails.default.url; | ||
const mediumImage = thumbnails.medium.url; | ||
const largeImage = thumbnails.high.url; | ||
const { viewCount, subscriberCount, videoCount } = statistics; | ||
|
||
return { | ||
id, | ||
title, | ||
url: "https://youtube.com/" + customUrl, | ||
description, | ||
smallImage, | ||
mediumImage, | ||
largeImage, | ||
viewCount, | ||
subscriberCount, | ||
viewCount, | ||
videoCount | ||
return { | ||
id, | ||
title, | ||
url: "https://youtube.com/channel/" + id, | ||
description, | ||
smallImage, | ||
mediumImage, | ||
largeImage, | ||
viewCount, | ||
subscriberCount, | ||
viewCount, | ||
videoCount, | ||
image, | ||
keywords, | ||
featuredChannelsUrls, | ||
profileColor, | ||
unsubscribedTrailer, | ||
customUrl | ||
}; | ||
}; | ||
}; | ||
|
||
const videoConstructor = item => { | ||
const { id, snippet } = item; | ||
const { videoId } = id; | ||
const { | ||
title, | ||
channelTitle, | ||
liveBroadcastContent, | ||
publishedAt, | ||
thumbnails | ||
} = snippet; | ||
const { high } = thumbnails; | ||
const youtubeVideoURLBase = "https://youtu.be/"; | ||
const videoUrl = youtubeVideoURLBase + videoId; | ||
const { url } = high; | ||
const thumbnailUrl = url; | ||
const videoConstructor = item => { | ||
const { id, snippet } = item; | ||
const { videoId } = id; | ||
const { | ||
channelId, | ||
title, | ||
description, | ||
channelTitle, | ||
liveBroadcastContent, | ||
publishedAt, | ||
thumbnails | ||
} = snippet; | ||
const { high } = thumbnails; | ||
const youtubeVideoURLBase = "https://youtu.be/"; | ||
const videoUrl = youtubeVideoURLBase + videoId; | ||
const { url } = high; | ||
const thumbnailUrl = url; | ||
|
||
return { | ||
videoId, | ||
title, | ||
channelTitle, | ||
liveBroadcastContent, | ||
videoUrl, | ||
thumbnailUrl, | ||
publishedAt | ||
return { | ||
videoId, | ||
title, | ||
channelTitle, | ||
liveBroadcastContent, | ||
videoUrl, | ||
thumbnailUrl, | ||
publishedAt, | ||
description, | ||
channelId | ||
}; | ||
}; | ||
}; | ||
|
||
const getChannelVideos = async () => { | ||
cache.timesHitYoutube += 1; | ||
const YOUTUBE_API_KEY = process.env.youtube_api_key; | ||
const YOUTUBE_CHANNEL_ID = process.env.YOUTUBE_CHANNEL_ID; | ||
let result; | ||
try { | ||
result = await request({ | ||
url: `https://www.googleapis.com/youtube/v3/search?key=${YOUTUBE_API_KEY}&channelId=${YOUTUBE_CHANNEL_ID}&part=snippet&type=video&order=date&maxResults=5`, | ||
method: "GET", | ||
json: true | ||
}); | ||
return result; | ||
} catch (error) { | ||
logger.error(error); | ||
return error; | ||
} | ||
}; | ||
const getLiveVideos = async () => { | ||
cache.timesHitYoutube += 1; | ||
const YOUTUBE_API_KEY = process.env.youtube_api_key; | ||
const YOUTUBE_CHANNEL_ID = process.env.YOUTUBE_CHANNEL_ID; | ||
let result; | ||
try { | ||
result = await request({ | ||
url: `https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=${YOUTUBE_CHANNEL_ID}&type=video&eventType=live&key=${YOUTUBE_API_KEY}`, | ||
method: "GET", | ||
json: true | ||
}); | ||
return result; | ||
} catch (error) { | ||
logger.error(error); | ||
return error; | ||
} | ||
}; | ||
const getBySearch = async query => { | ||
query = `${query}&key=${YOUTUBE_API_KEY}`; | ||
let result; | ||
try { | ||
result = await request({ | ||
url: `https://www.googleapis.com/youtube/v3/search${query}`, | ||
method: "GET", | ||
json: true | ||
}); | ||
return result; | ||
} catch (error) { | ||
logger.error(error); | ||
return error; | ||
} | ||
}; | ||
|
||
const getChannelDetails = async () => { | ||
cache.timesHitYoutube += 1; | ||
const YOUTUBE_API_KEY = process.env.youtube_api_key; | ||
const YOUTUBE_CHANNEL_ID = process.env.YOUTUBE_CHANNEL_ID; | ||
let result; | ||
try { | ||
result = await request({ | ||
url: `https://www.googleapis.com/youtube/v3/channels?part=snippet%2CcontentDetails%2Cstatistics&id=${YOUTUBE_CHANNEL_ID}&key=${YOUTUBE_API_KEY}`, | ||
method: "GET", | ||
json: true | ||
}); | ||
const getByChannels = async query => { | ||
query = `${query}&key=${YOUTUBE_API_KEY}`; | ||
let result; | ||
try { | ||
result = await request({ | ||
url: `https://www.googleapis.com/youtube/v3/channels${query}`, | ||
method: "GET", | ||
json: true | ||
}); | ||
|
||
return result; | ||
} catch (error) { | ||
logger.error(error); | ||
return error; | ||
} | ||
}; | ||
return result; | ||
} catch (error) { | ||
logger.error(error); | ||
return error; | ||
} | ||
}; | ||
|
||
const getChannelDetailsAndUpdateDB = async () => { | ||
const value = await getChannelDetails(); | ||
const { items } = value; | ||
if (items && items.length === 1) { | ||
firebase.sendInfoToDB(infoConstructor(items[0])); | ||
} else { | ||
logger.warn("No items in channel search"); | ||
} | ||
}; | ||
const getChannelDetailsAndUpdateDB = async ({ query, ...rest }) => { | ||
const { items = [] } = await getByChannels(query); | ||
items.forEach(item => { | ||
firebase.sendInfoToDB({ ...infoConstructor(item), ...rest }); | ||
}); | ||
}; | ||
|
||
const updateDBwithVideos = async ({ items }) => { | ||
if (items && items.length > 0) { | ||
const getVideosAndUpdateDB = async ({ query, ...rest }) => { | ||
const { items = [] } = await getBySearch(query); | ||
items.forEach(item => { | ||
firebase.sendVideoToDB(videoConstructor(item)); | ||
firebase.sendVideoToDB({ ...videoConstructor(item), ...rest }); | ||
}); | ||
} else { | ||
logger.warn("No items in search"); | ||
} | ||
}; | ||
}; | ||
|
||
module.exports.updateDBwithVideos = updateDBwithVideos; | ||
module.exports.getChannelDetailsAndUpdateDB = getChannelDetailsAndUpdateDB; | ||
module.exports.getChannelVideos = getChannelVideos; | ||
module.exports.getLiveVideos = getLiveVideos; | ||
return { | ||
getChannelDetailsAndUpdateDB, | ||
getVideosAndUpdateDB | ||
}; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
module.exports = { | ||
"videos": {}, | ||
"timesHitYoutube": 0 | ||
}; | ||
videos: {} | ||
}; |
Oops, something went wrong.