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 all commits
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(node.frontmatter.video)

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
2 changes: 2 additions & 0 deletions src/components/Blabla/Episode/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ const Episode = ({
audio,
description,
repoLink,
views,
}) => (
<div className="episode">
<Player video={video} audio={audio} />
<div className="info">
<div className="title">
<p>{date}</p>
<p>{views} views</p>
<h1> {title} </h1>
<Actions title={title} shareUrl={`https://geeksblabla.com/${slug}`} />
</div>
Expand Down
4 changes: 2 additions & 2 deletions src/components/Blabla/EpisodeItem/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) => (
<Link
to={`/${slug}`}
activeClassName="episode-item active"
Expand All @@ -17,7 +17,7 @@ export default ({ title, date, slug, duration, active }) => (
<div>
<h2>{title}</h2>
<p>
Copy link
Contributor

Choose a reason for hiding this comment

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

Small padding will be nice here

{duration} | {date}
{duration} | {date} | {views} views
</p>
</div>
</Link>
Expand Down
1 change: 1 addition & 0 deletions src/components/Blabla/EpisodesMenu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default ({ selectedEpisode }) => (
slug
date(formatString: "MMMM DD, YYYY")
duration
views
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/pages/blablas.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const pageQuery = graphql`
video
repoLink
audio
views
}
body
}
Expand Down
1 change: 1 addition & 0 deletions src/templates/blabla.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export const pageQuery = graphql`
repoLink
audio
tags
views
}
body
}
Expand Down