Skip to content

Commit

Permalink
Merge pull request #243 from jshemas/aug-9
Browse files Browse the repository at this point in the history
august clean up
  • Loading branch information
jshemas authored Aug 17, 2024
2 parents 80b1800 + 4eda428 commit 5d9874d
Show file tree
Hide file tree
Showing 19 changed files with 104 additions and 68 deletions.
10 changes: 8 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Change Log

## 6.8.0

- Updating how `onlyGetOpenGraphInfo` works. By default it is `false` but now it accepts an array of properties for which no fallback should be used.
- Updating how you get types `import { SuccessResult } from 'open-graph-scraper/types';`. See readme for details.
- Updating dependencies

## 6.7.2

- Adding `types` to the npm export. You can now use `import { SuccessResult } from 'open-graph-scraper/types/lib/types';`
Expand Down Expand Up @@ -100,7 +106,7 @@
- Modified the `url` property in `OpenGraphScraperOptions` to be an optional property since you don't need this when using just `html`
- `Type` can optional in `ImageObject` since type is not set it it's invalid
- Take all of the `customMetaTags` out of base of `ogObject` and store them into `ogObject.customMetaTags`
- The interal meta properties can be string arrays
- The internal meta properties can be string arrays
- Updating Dependencies

## 6.1.0
Expand All @@ -119,7 +125,7 @@
- Replace `GOT` with [fetch](https://nodejs.org/docs/latest-v18.x/api/globals.html#fetch)!
- Only supporting `node18` or higher going forward
- Updated how options work. `Fetch` and `OGS` options no longer being mixed together, users can now set [fetch options](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options) using `options.fetchOptions`
- Remove any ogImages/ogVideos/twitterImages/twitterPlayers/musicSongs resultes that have no url
- Remove any ogImages/ogVideos/twitterImages/twitterPlayers/musicSongs results that have no url
- The `downloadLimit` option has been removed in favor of just using timeouts.
- Limit ogImages/ogVideos/twitterImages/twitterPlayers/musicSongs to 10 items
- Adding html to the `SuccessResult` of `OGS`
Expand Down
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,31 @@ Check the return for a ```success``` flag. If success is set to true, then the u

Note: `open-graph-scraper` uses the [Fetch API](https://nodejs.org/dist/latest-v18.x/docs/api/globals.html#fetch) for requests and most of [Fetch's options](https://developer.mozilla.org/en-US/docs/Web/API/fetch#options) should work as `open-graph-scraper`'s `fetchOptions` options.

## Types And Import Example

```javascript
// example of how to get types
import type { SuccessResult } from 'open-graph-scraper/types';
const example: SuccessResult = {
result: { ogTitle: 'this is a title' },
error: false,
response: {},
html: '<html></html>'
}

// import example
import ogs from 'open-graph-scraper';
const options = { url: 'http://ogp.me/' };
ogs(options)
.then((data) => {
const { error, html, result, response } = data;
console.log('error:', error); // This returns true or false. True if there was an error. The error itself is inside the result object.
console.log('html:', html); // This contains the HTML of page
console.log('result:', result); // This contains all of the Open Graph results
console.log('response:', response); // This contains response from the Fetch API
});
```

## Custom Meta Tag Example

```javascript
Expand Down Expand Up @@ -123,7 +148,7 @@ The request header is set to [undici](https://github.com/nodejs/undici) by defau

```javascript
const ogs = require("open-graph-scraper");
const userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/112.0.0.0 Safari/537.36';
const userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36';
ogs({ url: 'https://www.wikipedia.org/', fetchOptions: { headers: { 'user-agent': userAgent } } })
.then((data) => {
const { error, html, result, response } = data;
Expand Down
4 changes: 2 additions & 2 deletions lib/extract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import fields from './fields';
import mediaSetup from './media';
import { unescapeScriptText } from './utils';

import type { OgObjectInteral, OpenGraphScraperOptions } from './types';
import type { OgObjectInternal, OpenGraphScraperOptions } from './types';

/**
* extract all of the meta tags needed for ogs
Expand All @@ -16,7 +16,7 @@ import type { OgObjectInteral, OpenGraphScraperOptions } from './types';
*
*/
export default function extractMetaTags(body: string, options: OpenGraphScraperOptions) {
let ogObject: OgObjectInteral = { success: true };
let ogObject: OgObjectInternal = { success: true };
const $ = load(body);
const metaFields = fields;

Expand Down
4 changes: 2 additions & 2 deletions lib/fallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
isUrlValid,
} from './utils';
import type {
OpenGraphScraperOptions, ImageObject, OgObjectInteral, OnlyGetOpenGraphInfoItem,
OpenGraphScraperOptions, ImageObject, OgObjectInternal, OnlyGetOpenGraphInfoItem,
} from './types';

const doesElementExist = (selector:string, attribute:string, $: CheerioAPI) => (
Expand All @@ -24,7 +24,7 @@ const doesElementExist = (selector:string, attribute:string, $: CheerioAPI) => (
* @return {object} object with ogs results with updated fallback values
*
*/
export function fallback(ogObject: OgObjectInteral, options: OpenGraphScraperOptions, $: CheerioAPI, body: string) {
export function fallback(ogObject: OgObjectInternal, options: OpenGraphScraperOptions, $: CheerioAPI, body: string) {
const shouldFallback = (key: OnlyGetOpenGraphInfoItem): boolean => {
if (!options.onlyGetOpenGraphInfo) {
return true;
Expand Down
4 changes: 2 additions & 2 deletions lib/fields.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { OgObjectInteral } from './types';
import type { OgObjectInternal } from './types';

type Fields = {
multiple: boolean;
property: string;
fieldName: keyof OgObjectInteral;
fieldName: keyof OgObjectInternal;
}[];

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/media.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { removeNestedUndefinedValues } from './utils';
import type {
ImageObject,
MusicSongObject,
OgObjectInteral,
OgObjectInternal,
TwitterImageObject,
TwitterPlayerObject,
VideoObject,
Expand Down Expand Up @@ -84,7 +84,7 @@ const zip = (array: any, ...args: any) => {
* @return {object} object with ogs results with updated media values
*
*/
export function mediaSetup(ogObject: OgObjectInteral) {
export function mediaSetup(ogObject: OgObjectInternal) {
// sets ogImage property/width/height/type to empty array if one these exists
if (
ogObject.ogImageSecureURL
Expand Down
4 changes: 2 additions & 2 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ export interface MusicSongObject {
url: string;
}

export interface OgObjectInteral {
export interface OgObjectInternal {
alAndroidAppName?: string;
alAndroidClass?: string;
alAndroidPackage?: string;
Expand Down Expand Up @@ -319,7 +319,7 @@ export interface OgObjectInteral {

// Omit values from mediaMapperProperties
export type OgObject = Omit<
OgObjectInteral,
OgObjectInternal,
'musicSongDisc' |
'musicSongProperty' |
'musicSongTrack' |
Expand Down
8 changes: 4 additions & 4 deletions lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import isUrl from './isUrl';
import type {
CustomMetaTags,
OgObjectInteral,
OgObjectInternal,
OpenGraphScraperOptions,
ValidatorSettings,
} from './types';
Expand Down Expand Up @@ -93,13 +93,13 @@ export function isThisANonHTMLUrl(url: string): boolean {
}

/**
* Find and delete nested undefs
* Find and delete nested undefineds
*
* @param {object} object - object to be cleaned
* @return {object} object without nested undefs
* @return {object} object without nested undefineds
*
*/
export function removeNestedUndefinedValues(object: Record<string, any>): OgObjectInteral {
export function removeNestedUndefinedValues(object: Record<string, any>): OgObjectInternal {
Object.entries(object).forEach(([key, value]) => {
if (value && typeof value === 'object') removeNestedUndefinedValues(value);
else if (value === undefined) delete object[key];
Expand Down
44 changes: 22 additions & 22 deletions package-lock.json

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

14 changes: 7 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "open-graph-scraper",
"description": "Node.js scraper module for Open Graph and Twitter Card info",
"version": "6.7.2",
"version": "6.8.0",
"license": "MIT",
"main": "./dist/cjs/index.js",
"types": "./types/index.d.ts",
Expand All @@ -11,7 +11,7 @@
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
},
"./types/lib/types": "./types/lib/types.d.ts"
"./types": "./types/lib/types.d.ts"
},
"scripts": {
"build:cjs": "tsc --project tsconfig.build.json --module commonjs --outDir dist/cjs/",
Expand Down Expand Up @@ -39,7 +39,7 @@
"chardet": "^2.0.0",
"cheerio": "^1.0.0-rc.12",
"iconv-lite": "^0.6.3",
"undici": "^6.19.5"
"undici": "^6.19.7"
},
"files": [
"/dist",
Expand All @@ -48,9 +48,9 @@
"CHANGELOG.md"
],
"devDependencies": {
"@snyk/protect": "^1.1292.2",
"@snyk/protect": "^1.1292.4",
"@types/mocha": "^10.0.7",
"@types/node": "^18.19.42",
"@types/node": "^18.19.44",
"@typescript-eslint/eslint-plugin": "^7.18.0",
"@typescript-eslint/parser": "^7.18.0",
"chai": "^4.5.0",
Expand All @@ -59,8 +59,8 @@
"eslint-config-airbnb-typescript": "^18.0.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-mocha": "^10.5.0",
"eslint-plugin-promise": "^7.0.0",
"mocha": "^10.6.0",
"eslint-plugin-promise": "^7.1.0",
"mocha": "^10.7.3",
"nyc": "^17.0.0",
"sinon": "^18.0.0",
"ts-mocha": "^10.0.0",
Expand Down
9 changes: 6 additions & 3 deletions tests/integration/video.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { expect } from 'chai';

import ogs from '../../index';

describe('video', function () {
// TODO: youtube is blocking requests from github, will need to find a way around this
describe.skip('video', function () {
it('Test Youtube Video - Should Return correct Open Graph Info', function () {
return ogs({ url: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ' }).then(function ({ error, result, response }) {
const userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36';
return ogs({ url: 'https://www.youtube.com/watch?v=dQw4w9WgXcQ', fetchOptions: { headers: { 'user-agent': userAgent } } }).then(function ({ error, result, response }) {
console.log('error:', error);
console.log('result:', result);
expect(error).to.be.eql(false);
Expand Down Expand Up @@ -111,7 +113,8 @@ describe('video', function () {
});

it('Test Youtube Video with bad escape sequence - Should Return correct Open Graph Info', function () {
return ogs({ url: 'https://www.youtube.com/watch?v=nFbKMg4E3JM' }).then(function ({ error, result, response }) {
const userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36';
return ogs({ url: 'https://www.youtube.com/watch?v=nFbKMg4E3JM', fetchOptions: { headers: { 'user-agent': userAgent } } }).then(function ({ error, result, response }) {
console.log('error:', error);
console.log('result:', result);
expect(error).to.be.eql(false);
Expand Down
3 changes: 2 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import type { ErrorResult, OpenGraphScraperOptions, SuccessResult } from './lib/
* for scraping Open Graph and Twitter Card info off a website.
*
* @param {object} options - The options used by Open Graph Scraper
* @param {boolean} [options.onlyGetOpenGraphInfo] - Only fetch open graph info and don't fall back on anything else.
* @param {boolean|string[]} [options.onlyGetOpenGraphInfo] - Only fetch open graph info and don't fall back on
* anything else.
* @param {object} [options.customMetaTags] - Here you can define custom meta tags you want to scrape.
* @param {object} [options.fetchOptions] - Sets the options used by fetch for the http requests
* @param {object} [options.urlValidatorSettings] - Sets the options used by validator.js for testing the URL
Expand Down
4 changes: 2 additions & 2 deletions types/lib/extract.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { OgObjectInteral, OpenGraphScraperOptions } from './types';
import type { OgObjectInternal, OpenGraphScraperOptions } from './types';
/**
* extract all of the meta tags needed for ogs
*
Expand All @@ -7,4 +7,4 @@ import type { OgObjectInteral, OpenGraphScraperOptions } from './types';
* @return {object} object with ogs results
*
*/
export default function extractMetaTags(body: string, options: OpenGraphScraperOptions): OgObjectInteral;
export default function extractMetaTags(body: string, options: OpenGraphScraperOptions): OgObjectInternal;
4 changes: 2 additions & 2 deletions types/lib/fallback.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { CheerioAPI } from 'cheerio';
import type { OpenGraphScraperOptions, OgObjectInteral } from './types';
import type { OpenGraphScraperOptions, OgObjectInternal } from './types';
/**
* ogs fallbacks
*
Expand All @@ -9,5 +9,5 @@ import type { OpenGraphScraperOptions, OgObjectInteral } from './types';
* @return {object} object with ogs results with updated fallback values
*
*/
export declare function fallback(ogObject: OgObjectInteral, options: OpenGraphScraperOptions, $: CheerioAPI, body: string): OgObjectInteral;
export declare function fallback(ogObject: OgObjectInternal, options: OpenGraphScraperOptions, $: CheerioAPI, body: string): OgObjectInternal;
export default fallback;
Loading

0 comments on commit 5d9874d

Please sign in to comment.