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

Slightly refactor context retrieval #26

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/events/fetch-context/eventbrite.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const fetch = require('node-fetch')
const logger = require('@architect/shared/logger')

function name () {
return 'Eventbrite'
}

function isEventbriteUrl (url) {
return ((url.indexOf('https://eventbrite.com') > -1) ||
(url.indexOf('https://www.eventbrite.com') > -1) ||
Expand Down Expand Up @@ -29,6 +33,7 @@ async function fetchContext (url) {
}

module.exports = {
name,
isEventbriteUrl,
fetchContext
}
6 changes: 5 additions & 1 deletion src/events/fetch-context/granary.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const fetch = require('node-fetch')
const logger = require('@architect/shared/logger')

function name () {
return 'Granary'
}

function getGranaryUrl (url) {
const granaryBaseUrl = 'https://granary.io/'
const safeUrl = encodeURIComponent(url)
Expand All @@ -26,4 +30,4 @@ async function fetchContext (url) {
return mf2.items[0].properties
}

module.exports = { fetchContext }
module.exports = { name, fetchContext }
28 changes: 14 additions & 14 deletions src/events/fetch-context/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,35 @@ const granary = require('./granary')
const meetup = require('./meetup')
const openGraph = require('./open-graph')

async function getContext (url) {
// for specific sites, use custom parsing
async function getHandler (url) {
if (meetup.isMeetupUrl(url)) {
const properties = await meetup.fetchContext(url)
if (properties) {
return properties
}
return meetup
} else if (eventbrite.isEventbriteUrl(url)) {
const properties = await eventbrite.fetchContext(url)
if (properties) {
return properties
}
return eventbrite
} else {
return granary
}
// otherwise fallback to Granary, and then OpenGraph
const properties = await granary.fetchContext(url)
}

async function getContext (handler, url) {
// if our fetching fails, fallback to OpenGraph
const properties = await handler.fetchContext(url)
if (properties) {
logger.info(`Context fetched ${url} using ${handler.name()}`, JSON.stringify(properties))
return properties
}

logger.info(`Context fetching ${url} using fallback ${openGraph.name()}`, JSON.stringify(properties))
return await openGraph.fetchContext(url)
}

exports.handler = async function subscribe (event) {
const data = await arc.tables()
const { url } = JSON.parse(event.Records[0].Sns.Message)
const properties = await getContext(url)
const handler = await getHandler(url)
const properties = await getContext(handler, url)
await data.contexts.put({
url,
properties
})
logger.info(`Context fetched ${url}`, JSON.stringify(properties))
}
5 changes: 5 additions & 0 deletions src/events/fetch-context/meetup.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const fetch = require('node-fetch')
const logger = require('@architect/shared/logger')

function name () {
return 'Meetup'
}

function isMeetupUrl (url) {
return ((url.indexOf('https://meetup.com') > -1) ||
(url.indexOf('https://www.meetup.com') > -1))
Expand All @@ -27,6 +31,7 @@ async function fetchContext (url) {
}

module.exports = {
name,
isMeetupUrl,
fetchContext
}
6 changes: 5 additions & 1 deletion src/events/fetch-context/open-graph.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
const ogs = require('open-graph-scraper')
const logger = require('@architect/shared/logger')

function name () {
return 'OpenGraph'
}

function setName (result, properties) {
if (result.ogTitle) {
properties.name = [result.ogTitle]
Expand Down Expand Up @@ -61,4 +65,4 @@ async function fetchContext (url) {
return properties
}

module.exports = { fetchContext }
module.exports = { name, fetchContext }