From 91d084b26338d492a084e925e862572b11994146 Mon Sep 17 00:00:00 2001 From: zakariasaad Date: Sat, 1 Aug 2020 23:14:26 +0100 Subject: [PATCH 1/3] add: query fb graph api + create node field --- gatsby-node.js | 46 +++++++++++++++++++++++++++++++++++++++------- package-lock.json | 13 ++++++++++--- package.json | 1 + 3 files changed, 50 insertions(+), 10 deletions(-) diff --git a/gatsby-node.js b/gatsby-node.js index a18af415..4c74170d 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("738019970309937") + + 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", From 093086496dcb87b6350a331fdb7959595ca12db4 Mon Sep 17 00:00:00 2001 From: zakariasaad Date: Sat, 1 Aug 2020 23:20:25 +0100 Subject: [PATCH 2/3] add: views count on episodes --- src/components/Blabla/Episode/index.js | 2 ++ src/components/Blabla/EpisodeItem/index.js | 4 ++-- src/components/Blabla/EpisodesMenu/index.js | 1 + src/pages/blablas.js | 1 + src/templates/blabla.js | 1 + 5 files changed, 7 insertions(+), 2 deletions(-) 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

{title}

diff --git a/src/components/Blabla/EpisodeItem/index.js b/src/components/Blabla/EpisodeItem/index.js index 99a4f528..f7ed40f6 100644 --- a/src/components/Blabla/EpisodeItem/index.js +++ b/src/components/Blabla/EpisodeItem/index.js @@ -4,7 +4,7 @@ import PlayIcon from "assets/play.svg" import "./index.scss" -export default ({ title, date, slug, duration, active }) => ( +export default ({ title, date, slug, duration, active, views }) => ( (

{title}

- {duration} | {date} + {duration} | {date} | {views} views

diff --git a/src/components/Blabla/EpisodesMenu/index.js b/src/components/Blabla/EpisodesMenu/index.js index 13e06d5f..b6d6879a 100644 --- a/src/components/Blabla/EpisodesMenu/index.js +++ b/src/components/Blabla/EpisodesMenu/index.js @@ -22,6 +22,7 @@ export default ({ selectedEpisode }) => ( slug date(formatString: "MMMM DD, YYYY") duration + views } } } diff --git a/src/pages/blablas.js b/src/pages/blablas.js index 4c215c55..d9012958 100644 --- a/src/pages/blablas.js +++ b/src/pages/blablas.js @@ -39,6 +39,7 @@ export const pageQuery = graphql` video repoLink audio + views } body } diff --git a/src/templates/blabla.js b/src/templates/blabla.js index 5334f35d..760f1123 100644 --- a/src/templates/blabla.js +++ b/src/templates/blabla.js @@ -38,6 +38,7 @@ export const pageQuery = graphql` repoLink audio tags + views } body } From 48e6db60222ffd99800f14d9a2f4498a40b2f598 Mon Sep 17 00:00:00 2001 From: zakariasaad Date: Sat, 1 Aug 2020 23:36:41 +0100 Subject: [PATCH 3/3] remove: test video id --- gatsby-node.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gatsby-node.js b/gatsby-node.js index 4c74170d..50da4e32 100644 --- a/gatsby-node.js +++ b/gatsby-node.js @@ -182,7 +182,7 @@ exports.onCreateNode = async ({ node, getNode, actions }) => { value: node.frontmatter.video || "", }) - const views = await getVideoViewsCount("738019970309937") + const views = await getVideoViewsCount(node.frontmatter.video) createNodeField({ name: "views",