Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add views count for episodes #169

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions gatsby-node.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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}`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use Intl.NumberFormat for a generic logic for number formating (please mention me if you need a hand)

}

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}`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have URL as a const at the top of file gives more readability

).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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Returning N/A is more relevant to me

}

return data.data[0].values[0].value
}

exports.onCreateNode = async ({ node, getNode, actions }) => {
const { createNodeField } = actions

if (node.internal.type === `Mdx`) {
Expand Down Expand Up @@ -155,6 +181,15 @@ exports.onCreateNode = ({ node, getNode, actions }) => {
node,
value: node.frontmatter.video || "",
})

const views = await getVideoViewsCount("738019970309937")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the id here is hardcoded


createNodeField({
name: "views",
node,
value: viewsFormatter(views) || "0",
})

createNodeField({
name: "audio",
node,
Expand Down Expand Up @@ -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:
Expand Down
13 changes: 10 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down