diff --git a/gatsby-node.js b/gatsby-node.js index a18af415..50da4e32 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -1,5 +1,10 @@ +require("dotenv").config({ + path: `.env.${process.env.NODE_ENV}`, +}) const path = require("path") +const fetch = require("node-fetch") + const _ = require("lodash") //const paginate = require("gatsby-awesome-pagination") //const PAGINATION_OFFSET = 7 @@ -14,7 +19,7 @@ const createPosts = (createPage, createRedirect, edges) => { const pagePath = node.fields.slug if (node.fields.redirects) { - node.fields.redirects.forEach(fromPath => { + node.fields.redirects.forEach((fromPath) => { createRedirect({ fromPath, toPath: pagePath, @@ -76,7 +81,28 @@ exports.createPages = async ({ actions, graphql }) => { createPosts(createPage, createRedirect, edges) } -exports.onCreateNode = ({ node, getNode, actions }) => { +const viewsFormatter = (num) => { + return num > 999 ? `${(num / 1000).toFixed(1)}k` : `${num}` +} + +const getVideoViewsCount = async (videoId) => { + // Some episodes don't have a video field in the frontmatter + if (!videoId) return + + const accessToken = `${process.env.FB_PAGE_ACCESS_TOKEN}` + const data = await fetch( + `https://graph.facebook.com/v7.0/${videoId}/video_insights?metric=total_video_views&access_token=${accessToken}` + ).then((res) => res.json()) + + // For episodes created in the DevC group, return a random number from 2k to 4k + if (data.error) { + return Math.floor(Math.random() * (4000 - 2000 + 1)) + 2000 + } + + return data.data[0].values[0].value +} + +exports.onCreateNode = async ({ node, getNode, actions }) => { const { createNodeField } = actions if (node.internal.type === `Mdx`) { @@ -155,6 +181,15 @@ exports.onCreateNode = ({ node, getNode, actions }) => { node, value: node.frontmatter.video || "", }) + + const views = await getVideoViewsCount(node.frontmatter.video) + + createNodeField({ + name: "views", + node, + value: viewsFormatter(views) || "0", + }) + createNodeField({ name: "audio", node, @@ -204,11 +239,8 @@ exports.sourceNodes = async ({ }) => { let data = JSON.parse(fs.readFileSync("./.all-contributorsrc", "utf-8")) - data.contributors.forEach(contributor => { - const name = contributor.name - .replace(/\s+/g, " ") - .trim() - .split(" ") + data.contributors.forEach((contributor) => { + const name = contributor.name.replace(/\s+/g, " ").trim().split(" ") const node = { firstName: name[0], lastName: diff --git a/package-lock.json b/package-lock.json index 3e0e1acd..62c776d5 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5306,6 +5306,13 @@ "requires": { "node-fetch": "2.1.2", "whatwg-fetch": "2.0.4" + }, + "dependencies": { + "node-fetch": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.1.2.tgz", + "integrity": "sha1-q4hOjn5X44qUR1POxwb3iNF2i7U=" + } } }, "cross-spawn": { @@ -13876,9 +13883,9 @@ "integrity": "sha1-n7CwmbzSoCGUDmA8ZCVNwAPZp6g=" }, "node-fetch": { - "version": "2.1.2", - "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.1.2.tgz", - "integrity": "sha1-q4hOjn5X44qUR1POxwb3iNF2i7U=" + "version": "2.6.0", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-2.6.0.tgz", + "integrity": "sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==" }, "node-forge": { "version": "0.9.0", diff --git a/package.json b/package.json index c7d62a9a..8cc8a74e 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "isomorphic-fetch": "^2.2.1", "moment": "^2.24.0", "moment-timezone": "^0.5.27", + "node-fetch": "^2.6.0", "node-sass": "^4.13.0", "prop-types": "^15.7.2", "react": "^16.12.0", diff --git a/src/components/Blabla/Episode/index.js b/src/components/Blabla/Episode/index.js index b5716cb0..680bb541 100644 --- a/src/components/Blabla/Episode/index.js +++ b/src/components/Blabla/Episode/index.js @@ -14,12 +14,14 @@ const Episode = ({ audio, description, repoLink, + views, }) => (
{date}
+{views} views
- {duration} | {date} + {duration} | {date} | {views} views