Skip to content

Commit

Permalink
Merge pull request #214 from jshemas/new-tags
Browse files Browse the repository at this point in the history
adding extra og tags for music and video and JSON LD
  • Loading branch information
jshemas authored Mar 3, 2024
2 parents 497cfec + 2da007a commit ba270db
Show file tree
Hide file tree
Showing 26 changed files with 776 additions and 227 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Change Log

## 6.5.0

- Adding support for JSON LD
- Adding support for `og:image:alt`, `twitterAccount`, `fbAppId` and extra og tags for `music` and `video`
- Fixing jsdoc param name
- Updating dependencies

## 6.4.0

- Add character encoding detection and decoding logic using `iconv-lite`
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
[![Node.js CI](https://github.com/jshemas/openGraphScraper/workflows/Node.js%20CI/badge.svg?branch=master)](https://github.com/jshemas/openGraphScraper/actions?query=branch%3Amaster)
[![Known Vulnerabilities](https://snyk.io/test/github/jshemas/openGraphScraper/badge.svg)](https://snyk.io/test/github/jshemas/openGraphScraper)

A simple node module(with TypeScript declarations) for scraping Open Graph and Twitter Card info off a site.
A simple node module(with TypeScript declarations) for scraping Open Graph and Twitter Card and other metadata off a site.

Note: `open-graph-scraper` doesn't support browser usage at this time.
Note: `open-graph-scraper` doesn't support browser usage at this time but you can use `open-graph-scraper-lite` if you already have the `HTML` and can't use Node's [Fetch API](https://nodejs.org/dist/latest-v18.x/docs/api/globals.html#fetch).

## Installation

Expand Down
5 changes: 5 additions & 0 deletions dist/lib/extract.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ function extractMetaTags(body, options) {
// if onlyGetOpenGraphInfo isn't set, run the open graph fallbacks
if (!options.onlyGetOpenGraphInfo) {
ogObject = (0, fallback_1.default)(ogObject, options, $, body);
$('script').each((index, script) => {
if (script.attribs.type && script.attribs.type === 'application/ld+json') {
ogObject.jsonLD = JSON.parse($(script).text());
}
});
}
return ogObject;
}
Expand Down
85 changes: 85 additions & 0 deletions dist/lib/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ const fields = [
property: 'og:logo',
fieldName: 'ogLogo',
},
{
multiple: false,
property: 'og:website',
fieldName: 'ogWebsite',
},
{
multiple: true,
property: 'og:image',
Expand Down Expand Up @@ -162,11 +167,61 @@ const fields = [
property: 'og:video:secure_url',
fieldName: 'ogVideoSecureURL',
},
{
multiple: true,
property: 'og:movie',
fieldName: 'ogMovie',
},
{
multiple: true,
property: 'og:episode',
fieldName: 'ogEpisode',
},
{
multiple: true,
property: 'og:video:actor',
fieldName: 'ogVideoActor',
},
{
multiple: true,
property: 'og:video:actor:id',
fieldName: 'ogVideoActorId',
},
{
multiple: true,
property: 'og:video:actor:role',
fieldName: 'ogVideoActorRole',
},
{
multiple: true,
property: 'og:video:director',
fieldName: 'ogVideoDirector',
},
{
multiple: true,
property: 'og:video:writer',
fieldName: 'ogVideoWriter',
},
{
multiple: true,
property: 'og:video:duration',
fieldName: 'ogVideoDuration',
},
{
multiple: true,
property: 'og:video:release_date',
fieldName: 'ogVideoReleaseDate',
},
{
multiple: true,
property: 'og:video:Tag',
fieldName: 'ogVideoTag',
},
{
multiple: true,
property: 'og:video:series',
fieldName: 'ogVideoSeries',
},
{
multiple: true,
property: 'og:video:width',
Expand All @@ -182,6 +237,16 @@ const fields = [
property: 'og:video:type',
fieldName: 'ogVideoType',
},
{
multiple: true,
property: 'og:video:tv_show',
fieldName: 'ogVideoTvShow',
},
{
multiple: true,
property: 'og:video:other',
fieldName: 'ogVideoOther',
},
{
multiple: false,
property: 'twitter:card',
Expand Down Expand Up @@ -212,6 +277,11 @@ const fields = [
property: 'twitter:creator:id',
fieldName: 'twitterCreatorId',
},
{
multiple: false,
property: 'twitter:account',
fieldName: 'twitterAccount',
},
{
multiple: false,
property: 'twitter:title',
Expand Down Expand Up @@ -317,6 +387,11 @@ const fields = [
property: 'twitter:app:url:googleplay',
fieldName: 'twitterAppUrlGooglePlay',
},
{
multiple: true,
property: 'music:playlist',
fieldName: 'musicPlaylist',
},
{
multiple: true,
property: 'music:song',
Expand Down Expand Up @@ -352,6 +427,11 @@ const fields = [
property: 'music:duration',
fieldName: 'musicDuration',
},
{
multiple: true,
property: 'music:radio_station',
fieldName: 'musicRadioStation',
},
{
multiple: true,
property: 'music:creator',
Expand Down Expand Up @@ -857,5 +937,10 @@ const fields = [
property: 'al:web:should_fallback',
fieldName: 'alWebShouldFallback',
},
{
multiple: false,
property: 'fb:app_id',
fieldName: 'fbAppId',
},
];
exports.default = fields;
20 changes: 19 additions & 1 deletion dist/lib/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,8 @@ export type OgObjectInteral = {
error?: string;
errorDetails?: Error;
favicon?: string;
fbAppId?: string;
jsonLD?: object;
modifiedTime?: string;
musicAlbum?: string;
musicAlbumDisc?: string;
Expand All @@ -168,6 +170,8 @@ export type OgObjectInteral = {
musicCreator?: string;
musicDuration?: string;
musicMusician?: string;
musicPlaylist?: string;
musicRadioStation?: string;
musicReleaseDate?: string;
musicSong?: MusicSongObject[];
musicSongDisc?: string | string[] | null[];
Expand All @@ -189,17 +193,19 @@ export type OgObjectInteral = {
ogDate?: string;
ogDescription?: string;
ogDeterminer?: string;
ogEpisode?: string;
ogImage?: ImageObject[];
ogImageAlt?: string | string[] | null[];
ogImageHeight?: string | string[] | null[];
ogImageProperty?: string | string[] | null[];
ogImageSecureURL?: string | string[] | null[];
ogImageType?: string | string[] | null[];
ogImageURL?: string | string[] | null[];
ogImageWidth?: string | string[] | null[];
ogImageAlt?: string | string[] | null[];
ogLocale?: string;
ogLocaleAlternate?: string;
ogLogo?: string;
ogMovie?: string;
ogPriceAmount?: string;
ogPriceCurrency?: string;
ogProductAvailability?: string;
Expand All @@ -212,12 +218,23 @@ export type OgObjectInteral = {
ogType?: string;
ogUrl?: string;
ogVideo?: VideoObject[];
ogVideoActor?: string;
ogVideoActorId?: string;
ogVideoActorRole?: string;
ogVideoDirector?: string;
ogVideoDuration?: string;
ogVideoHeight?: string | string[] | null[];
ogVideoOther?: string;
ogVideoProperty?: string | string[] | null[];
ogVideoReleaseDate?: string;
ogVideoSecureURL?: string;
ogVideoSeries?: string;
ogVideoTag?: string;
ogVideoTvShow?: string;
ogVideoType?: string | string[] | null[];
ogVideoWidth?: string | string[] | null[];
ogVideoWriter?: string;
ogWebsite?: string;
placeLocationLatitude?: string;
placeLocationLongitude?: string;
profileFirstName?: string;
Expand All @@ -241,6 +258,7 @@ export type OgObjectInteral = {
restaurantVariationPriceAmount?: string;
restaurantVariationPriceCurrency?: string;
success?: boolean;
twitterAccount?: string;
twitterAppIdGooglePlay?: string;
twitterAppIdiPad?: string;
twitterAppIdiPhone?: string;
Expand Down
6 changes: 6 additions & 0 deletions lib/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ export default function extractMetaTags(body: string, options: OpenGraphScraperO
// if onlyGetOpenGraphInfo isn't set, run the open graph fallbacks
if (!options.onlyGetOpenGraphInfo) {
ogObject = fallback(ogObject, options, $, body);

$('script').each((index, script) => {
if (script.attribs.type && script.attribs.type === 'application/ld+json') {
ogObject.jsonLD = JSON.parse($(script).text());
}
});
}

return ogObject;
Expand Down
85 changes: 85 additions & 0 deletions lib/fields.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ const fields = [
property: 'og:logo',
fieldName: 'ogLogo',
},
{
multiple: false,
property: 'og:website',
fieldName: 'ogWebsite',
},
{
multiple: true,
property: 'og:image',
Expand Down Expand Up @@ -160,11 +165,61 @@ const fields = [
property: 'og:video:secure_url',
fieldName: 'ogVideoSecureURL',
},
{
multiple: true,
property: 'og:movie',
fieldName: 'ogMovie',
},
{
multiple: true,
property: 'og:episode',
fieldName: 'ogEpisode',
},
{
multiple: true,
property: 'og:video:actor',
fieldName: 'ogVideoActor',
},
{
multiple: true,
property: 'og:video:actor:id',
fieldName: 'ogVideoActorId',
},
{
multiple: true,
property: 'og:video:actor:role',
fieldName: 'ogVideoActorRole',
},
{
multiple: true,
property: 'og:video:director',
fieldName: 'ogVideoDirector',
},
{
multiple: true,
property: 'og:video:writer',
fieldName: 'ogVideoWriter',
},
{
multiple: true,
property: 'og:video:duration',
fieldName: 'ogVideoDuration',
},
{
multiple: true,
property: 'og:video:release_date',
fieldName: 'ogVideoReleaseDate',
},
{
multiple: true,
property: 'og:video:Tag',
fieldName: 'ogVideoTag',
},
{
multiple: true,
property: 'og:video:series',
fieldName: 'ogVideoSeries',
},
{
multiple: true,
property: 'og:video:width',
Expand All @@ -180,6 +235,16 @@ const fields = [
property: 'og:video:type',
fieldName: 'ogVideoType',
},
{
multiple: true,
property: 'og:video:tv_show',
fieldName: 'ogVideoTvShow',
},
{
multiple: true,
property: 'og:video:other',
fieldName: 'ogVideoOther',
},
{
multiple: false,
property: 'twitter:card',
Expand Down Expand Up @@ -210,6 +275,11 @@ const fields = [
property: 'twitter:creator:id',
fieldName: 'twitterCreatorId',
},
{
multiple: false,
property: 'twitter:account',
fieldName: 'twitterAccount',
},
{
multiple: false,
property: 'twitter:title',
Expand Down Expand Up @@ -315,6 +385,11 @@ const fields = [
property: 'twitter:app:url:googleplay',
fieldName: 'twitterAppUrlGooglePlay',
},
{
multiple: true,
property: 'music:playlist',
fieldName: 'musicPlaylist',
},
{
multiple: true,
property: 'music:song',
Expand Down Expand Up @@ -350,6 +425,11 @@ const fields = [
property: 'music:duration',
fieldName: 'musicDuration',
},
{
multiple: true,
property: 'music:radio_station',
fieldName: 'musicRadioStation',
},
{
multiple: true,
property: 'music:creator',
Expand Down Expand Up @@ -855,6 +935,11 @@ const fields = [
property: 'al:web:should_fallback',
fieldName: 'alWebShouldFallback',
},
{
multiple: false,
property: 'fb:app_id',
fieldName: 'fbAppId',
},
];

export default fields;
Loading

0 comments on commit ba270db

Please sign in to comment.