UNPKG

open-graph-scraper-ts

Version:

Node.js scraper module for Open Graph and Twitter Card info

2 lines (1 loc) 31.9 kB
import{load as ae}from"cheerio";import b from"chardet";function _(e){if(!(typeof e=="string"||e instanceof String)){let t=typeof e;throw e===null?t="null":t==="object"&&(t=e.constructor.name),new TypeError(`Expected a string but received a ${t}`)}}function k(e={},r){for(let t in r)typeof e[t]>"u"&&(e[t]=r[t]);return e}var z={require_tld:!0,allow_underscores:!1,allow_trailing_dot:!1,allow_numeric_tld:!1,allow_wildcard:!1,ignore_max_length:!1};function B(e,r){_(e),r=k(r,z),r.allow_trailing_dot&&e[e.length-1]==="."&&(e=e.substring(0,e.length-1)),r.allow_wildcard===!0&&e.indexOf("*.")===0&&(e=e.substring(2));let t=e.split("."),a=t[t.length-1];return r.require_tld&&(t.length<2||!r.allow_numeric_tld&&!/^([a-z\u00A1-\u00A8\u00AA-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]{2,}|xn[a-z0-9-]{2,})$/i.test(a)||/\s/.test(a))||!r.allow_numeric_tld&&/^\d+$/.test(a)?!1:t.every(o=>!(o.length>63&&!r.ignore_max_length||!/^[a-z_\u00a1-\uffff0-9-]+$/i.test(o)||/[\uff01-\uff5e]/.test(o)||/^-|-$/.test(o)||!r.allow_underscores&&/_/.test(o)))}var V="(?:[0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])",u=`(${V}[.]){3}${V}`,J=new RegExp(`^${u}$`),m="(?:[0-9a-fA-F]{1,4})",Q=new RegExp(`^((?:${m}:){7}(?:${m}|:)|(?:${m}:){6}(?:${u}|:${m}|:)|(?:${m}:){5}(?::${u}|(:${m}){1,2}|:)|(?:${m}:){4}(?:(:${m}){0,1}:${u}|(:${m}){1,3}|:)|(?:${m}:){3}(?:(:${m}){0,2}:${u}|(:${m}){1,4}|:)|(?:${m}:){2}(?:(:${m}){0,3}:${u}|(:${m}){1,5}|:)|(?:${m}:){1}(?:(:${m}){0,4}:${u}|(:${m}){1,6}|:)|(?::((?::${m}){0,5}:${u}|(?::${m}){1,7}|:)))(%[0-9a-zA-Z-.:]{1,})?$`);function c(e,r=""){return _(e),r=String(r),r?r==="4"?J.test(e):r==="6"?Q.test(e):!1:c(e,4)||c(e,6)}var Z={protocols:["http","https","ftp"],require_tld:!0,require_protocol:!1,require_host:!0,require_port:!1,require_valid_protocol:!0,allow_underscores:!1,allow_trailing_dot:!1,allow_protocol_relative_urls:!1,allow_fragments:!0,allow_query_components:!0,validate_length:!0},K=/^\[([^\]]+)\](?::([0-9]+))?$/;function X(e){return Object.prototype.toString.call(e)==="[object RegExp]"}function R(e,r){for(let t=0;t<r.length;t+=1){let a=r[t];if(e===a||X(a)&&a.test(e))return!0}return!1}function S(e,r){if(_(e),!e||/[\s<>]/.test(e)||e.indexOf("mailto:")===0||(r=k(r,Z),r.validate_length&&e.length>=2083)||!r.allow_fragments&&e.includes("#")||!r.allow_query_components&&(e.includes("?")||e.includes("&")))return!1;let t,a,o,s,i,l,f;if(l=e.split("#"),e=l.shift(),l=e.split("?"),e=l.shift(),l=e.split("://"),l.length>1){if(t=l.shift().toLowerCase(),r.require_valid_protocol&&r.protocols.indexOf(t)===-1)return!1}else{if(r.require_protocol)return!1;if(e.slice(0,2)==="//"){if(!r.allow_protocol_relative_urls)return!1;l[0]=e.slice(2)}}if(e=l.join("://"),e==="")return!1;if(l=e.split("/"),e=l.shift(),e===""&&!r.require_host)return!0;if(l=e.split("@"),l.length>1){if(r.disallow_auth||l[0]===""||(a=l.shift(),a.indexOf(":")>=0&&a.split(":").length>2))return!1;let[F,q]=a.split(":");if(F===""&&q==="")return!1}let p=l.join("@");i=null,f=null;let I=p.match(K);if(I?(o="",f=I[1],i=I[2]||null):(l=p.split(":"),o=l.shift(),l.length&&(i=l.join(":"))),i!==null&&i.length>0){if(s=parseInt(i,10),!/^[0-9]+$/.test(i)||s<=0||s>65535)return!1}else if(r.require_port)return!1;return r.host_whitelist?R(o,r.host_whitelist):o===""&&!r.require_host?!0:!(!c(o)&&!B(o,r)&&(!f||!c(f,6))||(o=o||f,r.host_blacklist&&R(o,r.host_blacklist)))}var g={allow_fragments:!0,allow_protocol_relative_urls:!1,allow_query_components:!0,allow_trailing_dot:!1,allow_underscores:!1,protocols:["http","https"],require_host:!0,require_port:!1,require_protocol:!1,require_tld:!0,require_valid_protocol:!0,validate_length:!0};function P(e,r){return typeof e=="string"&&e.length>0&&S(e,r)}var Y=e=>/^(f|ht)tps?:\/\//i.test(e)?e:`http://${e}`;function E(e,r){return{url:P(e,r)?Y(e):null}}function y(e){let r=e.split(".").pop()??"";return[r]=r.split("?"),r}function A(e){return["apng","bmp","gif","ico","cur","jpg","jpeg","jfif","pjpeg","pjp","png","svg","tif","tiff","webp"].includes(e)}function L(e){let r=[".doc",".docx",".xls",".xlsx",".ppt",".pptx",".3gp",".avi",".mov",".mp4",".m4v",".m4a",".mp3",".mkv",".ogv",".ogm",".ogg",".oga",".webm",".wav",".bmp",".gif",".jpg",".jpeg",".png",".webp",".zip",".rar",".tar",".tar.gz",".tgz",".tar.bz2",".tbz2",".txt",".pdf"],t=y(e);return r.some(a=>`.${t}`.includes(a))}function T(e){return Object.entries(e).forEach(([r,t])=>{t&&typeof t=="object"?T(t):t===void 0&&delete e[r]}),e}function C(e){return{options:{onlyGetOpenGraphInfo:!1,...e}}}function D(e){if(!Array.isArray(e))return!1;let r=!0;return e.forEach(t=>{typeof t=="object"?((!("fieldName"in t)||typeof t.fieldName!="string")&&(r=!1),(!("multiple"in t)||typeof t.multiple!="boolean")&&(r=!1),(!("property"in t)||typeof t.property!="string")&&(r=!1)):r=!1}),r}function W(e){return e.replace(/\\x([0-9a-f]{2})/ig,(r,t)=>{let a=parseInt(t,16);return a===34?'\\"':String.fromCharCode(a)})}var n=(e,r,t)=>t(e).attr(r)&&(t(e).attr(r)?.length??0)>0;function $(e,r,t,a){let o=s=>r.onlyGetOpenGraphInfo?r.onlyGetOpenGraphInfo===!0?!1:!r.onlyGetOpenGraphInfo.includes(s):!0;if(!e.ogTitle&&o("title")&&(t("title").text()&&t("title").text().length>0?e.ogTitle=t("title").first().text():t('head > meta[name="title"]').attr("content")&&(t('head > meta[name="title"]').attr("content")?.length??0)>0?e.ogTitle=t('head > meta[name="title"]').attr("content"):t(".post-title").text()&&t(".post-title").text().length>0?e.ogTitle=t(".post-title").text():t(".entry-title").text()&&t(".entry-title").text().length>0?e.ogTitle=t(".entry-title").text():t('h1[class*="title" i] a').text()&&t('h1[class*="title" i] a').text().length>0?e.ogTitle=t('h1[class*="title" i] a').text():t('h1[class*="title" i]').text()&&t('h1[class*="title" i]').text().length>0&&(e.ogTitle=t('h1[class*="title" i]').text())),!e.ogDescription&&o("description")&&(n('head > meta[name="description"]',"content",t)?e.ogDescription=t('head > meta[name="description"]').attr("content"):n('head > meta[itemprop="description"]',"content",t)?e.ogDescription=t('head > meta[itemprop="description"]').attr("content"):t("#description").text()&&t("#description").text().length>0&&(e.ogDescription=t("#description").text())),!e.ogImage&&o("image")?(e.ogImage=[],t("img").map((s,i)=>{let l=t(i).attr("src")??"";if(!l)return!1;let f=y(l);if(!P(l,r.urlValidatorSettings??g)||!A(f))return!1;let p={url:l,type:f};return t(i).attr("width")&&Number(t(i).attr("width"))&&(p.width=Number(t(i).attr("width"))),t(i).attr("height")&&Number(t(i).attr("height"))&&(p.height=Number(t(i).attr("height"))),e.ogImage?.push(p),!1}),e.ogImage=e.ogImage.filter(s=>s.url!==void 0&&s.url!=="").filter((s,i)=>i<10),e.ogImage.length===0&&delete e.ogImage):e.ogImage&&e.ogImage.map(s=>{if(s.url&&!s.type){let i=y(s.url);A(i)&&(s.type=i)}return!1}),!e.ogAudioURL&&!e.ogAudioSecureURL&&o("audioUrl")){let s=t("audio").attr("src")??"",i=t("audio > source").attr("src")??"";if(n("audio","src",t)){s.startsWith("https")?e.ogAudioSecureURL=s:e.ogAudioURL=s;let l=t("audio").attr("type")??"";!e.ogAudioType&&n("audio","type",t)&&(e.ogAudioType=l)}else if(n("audio > source","src",t)){i.startsWith("https")?e.ogAudioSecureURL=i:e.ogAudioURL=i;let l=t("audio > source").attr("type")??"";!e.ogAudioType&&n("audio > source","type",t)&&(e.ogAudioType=l)}}if(!e.ogLocale&&o("locale")&&(n("html","lang",t)?e.ogLocale=t("html").attr("lang"):n('head > meta[itemprop="inLanguage"]',"content",t)&&(e.ogLocale=t('head > meta[itemprop="inLanguage"]').attr("content"))),!e.ogLogo&&o("logo")&&(n('meta[itemprop="logo"]',"content",t)?e.ogLogo=t('meta[itemprop="logo"]').attr("content"):n('img[itemprop="logo"]',"src",t)&&(e.ogLogo=t('img[itemprop="logo"]').attr("src"))),!e.ogUrl&&o("url")&&(n('link[rel="canonical"]',"href",t)?e.ogUrl=t('link[rel="canonical"]').attr("href"):n('link[rel="alternate"][hreflang="x-default"]',"href",t)&&(e.ogUrl=t('link[rel="alternate"][hreflang="x-default"]').attr("href"))),!e.ogDate&&o("date")&&(n('head > meta[name="date"]',"content",t)?e.ogDate=t('head > meta[name="date"]').attr("content"):n('[itemprop*="datemodified" i]',"content",t)?e.ogDate=t('[itemprop*="datemodified" i]').attr("content"):n('[itemprop="datepublished" i]',"content",t)?e.ogDate=t('[itemprop="datepublished" i]').attr("content"):n('[itemprop*="date" i]',"content",t)?e.ogDate=t('[itemprop*="date" i]').attr("content"):n('time[itemprop*="date" i]',"datetime",t)?e.ogDate=t('time[itemprop*="date" i]').attr("datetime"):n("time[datetime]","datetime",t)&&(e.ogDate=t("time[datetime]").attr("datetime"))),!e.favicon&&o("favicon")&&(n('link[rel="shortcut icon"]',"href",t)?e.favicon=t('link[rel="shortcut icon"]').attr("href"):n('link[rel="icon"]',"href",t)?e.favicon=t('link[rel="icon"]').attr("href"):n('link[rel="mask-icon"]',"href",t)?e.favicon=t('link[rel="mask-icon"]').attr("href"):n('link[rel="apple-touch-icon"]',"href",t)?e.favicon=t('link[rel="apple-touch-icon"]').attr("href"):n('link[type="image/png"]',"href",t)?e.favicon=t('link[type="image/png"]').attr("href"):n('link[type="image/ico"]',"href",t)?e.favicon=t('link[type="image/ico"]').attr("href"):n('link[type="image/x-icon"]',"href",t)?e.favicon=t('link[type="image/x-icon"]').attr("href"):n('head > meta[property*="appIcon"]',"content",t)&&(e.favicon=t('head > meta[property*="appIcon"]').attr("content"))),n("meta","charset",t))e.charset=t("meta").attr("charset");else if(n('head > meta[name="charset"]',"content",t))e.charset=t('head > meta[name="charset"]').attr("content");else if(n('head > meta[http-equiv="content-type"]',"content",t)){let s=t('head > meta[http-equiv="content-type"]').attr("content")??"",i=/charset=([^()<>@,;:"/[\]?.=\s]*)/i;if(i.test(s)){let l=i.exec(s);l?.[1]&&(e.charset=l[1]||"utf-8")}}else if(a){let i=new TextEncoder().encode(a);e.charset=b.detect(i)??""}return e}var M=$;var O=[{multiple:!1,property:"og:title",fieldName:"ogTitle"},{multiple:!1,property:"og:type",fieldName:"ogType"},{multiple:!1,property:"og:logo",fieldName:"ogLogo"},{multiple:!1,property:"og:website",fieldName:"ogWebsite"},{multiple:!0,property:"og:image",fieldName:"ogImageProperty"},{multiple:!0,property:"og:image:url",fieldName:"ogImageURL"},{multiple:!0,property:"og:image:alt",fieldName:"ogImageAlt"},{multiple:!0,property:"og:image:secure_url",fieldName:"ogImageSecureURL"},{multiple:!0,property:"og:image:width",fieldName:"ogImageWidth"},{multiple:!0,property:"og:image:height",fieldName:"ogImageHeight"},{multiple:!0,property:"og:image:type",fieldName:"ogImageType"},{multiple:!1,property:"og:url",fieldName:"ogUrl"},{multiple:!1,property:"og:audio",fieldName:"ogAudio"},{multiple:!1,property:"og:audio:url",fieldName:"ogAudioURL"},{multiple:!1,property:"og:audio:secure_url",fieldName:"ogAudioSecureURL"},{multiple:!1,property:"og:audio:type",fieldName:"ogAudioType"},{multiple:!1,property:"og:description",fieldName:"ogDescription"},{multiple:!1,property:"og:determiner",fieldName:"ogDeterminer"},{multiple:!1,property:"og:locale",fieldName:"ogLocale"},{multiple:!1,property:"og:locale:alternate",fieldName:"ogLocaleAlternate"},{multiple:!1,property:"og:site_name",fieldName:"ogSiteName"},{multiple:!1,property:"og:product:retailer_item_id",fieldName:"ogProductRetailerItemId"},{multiple:!1,property:"og:product:price:amount",fieldName:"ogProductPriceAmount"},{multiple:!1,property:"og:product:price:currency",fieldName:"ogProductPriceCurrency"},{multiple:!1,property:"og:product:availability",fieldName:"ogProductAvailability"},{multiple:!1,property:"og:product:condition",fieldName:"ogProductCondition"},{multiple:!1,property:"og:price:amount",fieldName:"ogPriceAmount"},{multiple:!1,property:"og:price:currency",fieldName:"ogPriceCurrency"},{multiple:!1,property:"og:availability",fieldName:"ogAvailability"},{multiple:!0,property:"og:video",fieldName:"ogVideoProperty"},{multiple:!0,property:"og:video:url",fieldName:"ogVideoProperty"},{multiple:!1,property:"og:video:secure_url",fieldName:"ogVideoSecureURL"},{multiple:!1,property:"og:movie",fieldName:"ogMovie"},{multiple:!1,property:"og:episode",fieldName:"ogEpisode"},{multiple:!1,property:"og:video:actor",fieldName:"ogVideoActor"},{multiple:!1,property:"og:video:actor:id",fieldName:"ogVideoActorId"},{multiple:!1,property:"og:video:actor:role",fieldName:"ogVideoActorRole"},{multiple:!1,property:"og:video:director",fieldName:"ogVideoDirector"},{multiple:!1,property:"og:video:writer",fieldName:"ogVideoWriter"},{multiple:!1,property:"og:video:duration",fieldName:"ogVideoDuration"},{multiple:!1,property:"og:video:release_date",fieldName:"ogVideoReleaseDate"},{multiple:!1,property:"og:video:Tag",fieldName:"ogVideoTag"},{multiple:!1,property:"og:video:series",fieldName:"ogVideoSeries"},{multiple:!0,property:"og:video:width",fieldName:"ogVideoWidth"},{multiple:!0,property:"og:video:height",fieldName:"ogVideoHeight"},{multiple:!0,property:"og:video:type",fieldName:"ogVideoType"},{multiple:!1,property:"og:video:tv_show",fieldName:"ogVideoTvShow"},{multiple:!1,property:"og:video:other",fieldName:"ogVideoOther"},{multiple:!1,property:"twitter:card",fieldName:"twitterCard"},{multiple:!1,property:"twitter:url",fieldName:"twitterUrl"},{multiple:!1,property:"twitter:site",fieldName:"twitterSite"},{multiple:!1,property:"twitter:site:id",fieldName:"twitterSiteId"},{multiple:!1,property:"twitter:creator",fieldName:"twitterCreator"},{multiple:!1,property:"twitter:creator:id",fieldName:"twitterCreatorId"},{multiple:!1,property:"twitter:account",fieldName:"twitterAccount"},{multiple:!1,property:"twitter:title",fieldName:"twitterTitle"},{multiple:!1,property:"twitter:description",fieldName:"twitterDescription"},{multiple:!0,property:"twitter:image",fieldName:"twitterImageProperty"},{multiple:!0,property:"twitter:image:height",fieldName:"twitterImageHeight"},{multiple:!0,property:"twitter:image:width",fieldName:"twitterImageWidth"},{multiple:!0,property:"twitter:image:src",fieldName:"twitterImageSrc"},{multiple:!0,property:"twitter:image:alt",fieldName:"twitterImageAlt"},{multiple:!0,property:"twitter:player",fieldName:"twitterPlayerProperty"},{multiple:!0,property:"twitter:player:width",fieldName:"twitterPlayerWidth"},{multiple:!0,property:"twitter:player:height",fieldName:"twitterPlayerHeight"},{multiple:!0,property:"twitter:player:stream",fieldName:"twitterPlayerStream"},{multiple:!1,property:"twitter:player:stream:content_type",fieldName:"twitterPlayerStreamContentType"},{multiple:!1,property:"twitter:app:name:iphone",fieldName:"twitterAppNameiPhone"},{multiple:!1,property:"twitter:app:id:iphone",fieldName:"twitterAppIdiPhone"},{multiple:!1,property:"twitter:app:url:iphone",fieldName:"twitterAppUrliPhone"},{multiple:!1,property:"twitter:app:name:ipad",fieldName:"twitterAppNameiPad"},{multiple:!1,property:"twitter:app:id:ipad",fieldName:"twitterAppIdiPad"},{multiple:!1,property:"twitter:app:url:ipad",fieldName:"twitterAppUrliPad"},{multiple:!1,property:"twitter:app:name:googleplay",fieldName:"twitterAppNameGooglePlay"},{multiple:!1,property:"twitter:app:id:googleplay",fieldName:"twitterAppIdGooglePlay"},{multiple:!1,property:"twitter:app:url:googleplay",fieldName:"twitterAppUrlGooglePlay"},{multiple:!1,property:"music:playlist",fieldName:"musicPlaylist"},{multiple:!0,property:"music:song",fieldName:"musicSongProperty"},{multiple:!0,property:"music:song:disc",fieldName:"musicSongDisc"},{multiple:!0,property:"music:song:track",fieldName:"musicSongTrack"},{multiple:!0,property:"music:song:url",fieldName:"musicSongUrl"},{multiple:!1,property:"music:musician",fieldName:"musicMusician"},{multiple:!1,property:"music:release_date",fieldName:"musicReleaseDate"},{multiple:!1,property:"music:duration",fieldName:"musicDuration"},{multiple:!1,property:"music:radio_station",fieldName:"musicRadioStation"},{multiple:!1,property:"music:creator",fieldName:"musicCreator"},{multiple:!1,property:"music:album",fieldName:"musicAlbum"},{multiple:!1,property:"music:album:disc",fieldName:"musicAlbumDisc"},{multiple:!1,property:"music:album:track",fieldName:"musicAlbumTrack"},{multiple:!1,property:"music:album:url",fieldName:"musicAlbumUrl"},{multiple:!1,property:"article:published_date",fieldName:"articlePublishedDate"},{multiple:!1,property:"article:published_time",fieldName:"articlePublishedTime"},{multiple:!1,property:"article:modified_date",fieldName:"articleModifiedDate"},{multiple:!1,property:"article:modified_time",fieldName:"articleModifiedTime"},{multiple:!1,property:"article:expiration_time",fieldName:"articleExpirationTime"},{multiple:!1,property:"article:author",fieldName:"articleAuthor"},{multiple:!1,property:"article:section",fieldName:"articleSection"},{multiple:!1,property:"article:tag",fieldName:"articleTag"},{multiple:!1,property:"article:publisher",fieldName:"articlePublisher"},{multiple:!1,property:"og:article:published_time",fieldName:"ogArticlePublishedTime"},{multiple:!1,property:"og:article:modified_time",fieldName:"ogArticleModifiedTime"},{multiple:!1,property:"og:article:expiration_time",fieldName:"ogArticleExpirationTime"},{multiple:!1,property:"og:article:author",fieldName:"ogArticleAuthor"},{multiple:!1,property:"og:article:section",fieldName:"ogArticleSection"},{multiple:!1,property:"og:article:tag",fieldName:"ogArticleTag"},{multiple:!1,property:"og:article:publisher",fieldName:"ogArticlePublisher"},{multiple:!1,property:"books:book",fieldName:"booksBook"},{multiple:!1,property:"book:author",fieldName:"bookAuthor"},{multiple:!1,property:"book:isbn",fieldName:"bookIsbn"},{multiple:!1,property:"book:release_date",fieldName:"bookReleaseDate"},{multiple:!1,property:"book:canonical_name",fieldName:"bookCanonicalName"},{multiple:!1,property:"book:tag",fieldName:"bookTag"},{multiple:!1,property:"books:rating:value",fieldName:"booksRatingValue"},{multiple:!1,property:"books:rating:scale",fieldName:"booksRatingScale"},{multiple:!1,property:"profile:first_name",fieldName:"profileFirstName"},{multiple:!1,property:"profile:last_name",fieldName:"profileLastName"},{multiple:!1,property:"profile:username",fieldName:"profileUsername"},{multiple:!1,property:"profile:gender",fieldName:"profileGender"},{multiple:!1,property:"business:contact_data:street_address",fieldName:"businessContactDataStreetAddress"},{multiple:!1,property:"business:contact_data:locality",fieldName:"businessContactDataLocality"},{multiple:!1,property:"business:contact_data:region",fieldName:"businessContactDataRegion"},{multiple:!1,property:"business:contact_data:postal_code",fieldName:"businessContactDataPostalCode"},{multiple:!1,property:"business:contact_data:country_name",fieldName:"businessContactDataCountryName"},{multiple:!1,property:"restaurant:menu",fieldName:"restaurantMenu"},{multiple:!1,property:"restaurant:restaurant",fieldName:"restaurantRestaurant"},{multiple:!1,property:"restaurant:section",fieldName:"restaurantSection"},{multiple:!1,property:"restaurant:variation:price:amount",fieldName:"restaurantVariationPriceAmount"},{multiple:!1,property:"restaurant:variation:price:currency",fieldName:"restaurantVariationPriceCurrency"},{multiple:!1,property:"restaurant:contact_info:website",fieldName:"restaurantContactInfoWebsite"},{multiple:!1,property:"restaurant:contact_info:street_address",fieldName:"restaurantContactInfoStreetAddress"},{multiple:!1,property:"restaurant:contact_info:locality",fieldName:"restaurantContactInfoLocality"},{multiple:!1,property:"restaurant:contact_info:region",fieldName:"restaurantContactInfoRegion"},{multiple:!1,property:"restaurant:contact_info:postal_code",fieldName:"restaurantContactInfoPostalCode"},{multiple:!1,property:"restaurant:contact_info:country_name",fieldName:"restaurantContactInfoCountryName"},{multiple:!1,property:"restaurant:contact_info:email",fieldName:"restaurantContactInfoEmail"},{multiple:!1,property:"restaurant:contact_info:phone_number",fieldName:"restaurantContactInfoPhoneNumber"},{multiple:!1,property:"place:location:latitude",fieldName:"placeLocationLatitude"},{multiple:!1,property:"place:location:longitude",fieldName:"placeLocationLongitude"},{multiple:!1,property:"og:date",fieldName:"ogDate"},{multiple:!1,property:"author",fieldName:"author"},{multiple:!1,property:"updated_time",fieldName:"updatedTime"},{multiple:!1,property:"modified_time",fieldName:"modifiedTime"},{multiple:!1,property:"published_time",fieldName:"publishedTime"},{multiple:!1,property:"release_date",fieldName:"releaseDate"},{multiple:!1,property:"dc.source",fieldName:"dcSource"},{multiple:!1,property:"dc.subject",fieldName:"dcSubject"},{multiple:!1,property:"dc.title",fieldName:"dcTitle"},{multiple:!1,property:"dc.type",fieldName:"dcType"},{multiple:!1,property:"dc.creator",fieldName:"dcCreator"},{multiple:!1,property:"dc.coverage",fieldName:"dcCoverage"},{multiple:!1,property:"dc.language",fieldName:"dcLanguage"},{multiple:!1,property:"dc.contributor",fieldName:"dcContributor"},{multiple:!1,property:"dc.date",fieldName:"dcDate"},{multiple:!1,property:"dc.date.issued",fieldName:"dcDateIssued"},{multiple:!1,property:"dc.date.created",fieldName:"dcDateCreated"},{multiple:!1,property:"dc.description",fieldName:"dcDescription"},{multiple:!1,property:"dc.identifier",fieldName:"dcIdentifier"},{multiple:!1,property:"dc.publisher",fieldName:"dcPublisher"},{multiple:!1,property:"dc.rights",fieldName:"dcRights"},{multiple:!1,property:"dc.relation",fieldName:"dcRelation"},{multiple:!1,property:"dc.format.media",fieldName:"dcFormatMedia"},{multiple:!1,property:"dc.format.size",fieldName:"dcFormatSize"},{multiple:!1,property:"al:ios:url",fieldName:"alIosUrl"},{multiple:!1,property:"al:ios:app_store_id",fieldName:"alIosAppStoreId"},{multiple:!1,property:"al:ios:app_name",fieldName:"alIosAppName"},{multiple:!1,property:"al:iphone:url",fieldName:"alIphoneUrl"},{multiple:!1,property:"al:iphone:app_store_id",fieldName:"alIphoneAppStoreId"},{multiple:!1,property:"al:iphone:app_name",fieldName:"alIphoneAppName"},{multiple:!1,property:"al:ipad:url",fieldName:"alIpadUrl"},{multiple:!1,property:"al:ipad:app_store_id",fieldName:"alIpadAppStoreId"},{multiple:!1,property:"al:ipad:app_name",fieldName:"alIpadAppName"},{multiple:!1,property:"al:android:url",fieldName:"alAndroidUrl"},{multiple:!1,property:"al:android:package",fieldName:"alAndroidPackage"},{multiple:!1,property:"al:android:class",fieldName:"alAndroidClass"},{multiple:!1,property:"al:android:app_name",fieldName:"alAndroidAppName"},{multiple:!1,property:"al:windows_phone:url",fieldName:"alWindowsPhoneUrl"},{multiple:!1,property:"al:windows_phone:app_id",fieldName:"alWindowsPhoneAppId"},{multiple:!1,property:"al:windows_phone:app_name",fieldName:"alWindowsPhoneAppName"},{multiple:!1,property:"al:windows:url",fieldName:"alWindowsUrl"},{multiple:!1,property:"al:windows:app_id",fieldName:"alWindowsAppId"},{multiple:!1,property:"al:windows:app_name",fieldName:"alWindowsAppName"},{multiple:!1,property:"al:windows_universal:url",fieldName:"alWindowsUniversalUrl"},{multiple:!1,property:"al:windows_universal:app_id",fieldName:"alWindowsUniversalAppId"},{multiple:!1,property:"al:windows_universal:app_name",fieldName:"alWindowsUniversalAppName"},{multiple:!1,property:"al:web:url",fieldName:"alWebUrl"},{multiple:!1,property:"al:web:should_fallback",fieldName:"alWebShouldFallback"},{multiple:!1,property:"fb:app_id",fieldName:"fbAppId"}],h=O;var j=e=>({alt:e[3],height:e[2],url:e[0],width:e[1]}),ee=e=>({height:e[2],stream:e[3],url:e[0],width:e[1]}),te=e=>({disc:e[2],track:e[1],url:e[0]}),G=e=>({height:e[2],type:e[3],url:e[0],width:e[1],alt:e[4]}),w=(e,r)=>{if(!(e.url&&r.url))return 0;let a=e.url.match(/\.(\w{2,5})$/)?.[1].toLowerCase()??null,s=r.url.match(/\.(\w{2,5})$/)?.[1].toLowerCase()??null;return a==="gif"&&s!=="gif"?-1:a!=="gif"&&s==="gif"?1:Math.max(r.width??0,r.height??0)-Math.max(e.width??0,e.height??0)},re=(e,r)=>e.track&&r.track?(e.disc??0)>(r.disc??0)?1:(e.disc??0)<(r.disc??0)?-1:e.track-r.track:0,d=(e,...r)=>e===void 0?[]:e.map((t,a)=>[t,...r.map(o=>o[a])]);function ie(e){(e.ogImageSecureURL??e.ogImageURL??e.ogImageProperty??e.ogImageWidth??e.ogImageHeight??e.ogImageType??e.ogImageAlt)&&(e.ogImageSecureURL=e.ogImageSecureURL?e.ogImageSecureURL:[],e.ogImageURL=e.ogImageURL?e.ogImageURL:[],e.ogImageProperty=e.ogImageProperty?e.ogImageProperty:[],e.ogImageProperty=e.ogImageSecureURL.length!==0?e.ogImageSecureURL:e.ogImageProperty,e.ogImageProperty=e.ogImageProperty.length!==0?e.ogImageProperty:e.ogImageURL,e.ogImageWidth=e.ogImageWidth?e.ogImageWidth:[],e.ogImageHeight=e.ogImageHeight?e.ogImageHeight:[],e.ogImageType=e.ogImageType?e.ogImageType:[],e.ogImageAlt=e.ogImageAlt?e.ogImageAlt:[]);let r=d(e.ogImageProperty,e.ogImageWidth,e.ogImageHeight,e.ogImageType,e.ogImageAlt).map(G).filter(i=>i.url!==void 0&&i.url!=="").filter((i,l)=>l<10).sort(w);(e.ogVideoProperty??e.ogVideoWidth??e.ogVideoHeight??e.ogVideoType)&&(e.ogVideoProperty=e.ogVideoProperty?e.ogVideoProperty:[],e.ogVideoWidth=e.ogVideoWidth?e.ogVideoWidth:[],e.ogVideoHeight=e.ogVideoHeight?e.ogVideoHeight:[],e.ogVideoType=e.ogVideoType?e.ogVideoType:[]);let t=d(e.ogVideoProperty,e.ogVideoWidth,e.ogVideoHeight,e.ogVideoType).map(G).filter(i=>i.url!==void 0&&i.url!=="").filter((i,l)=>l<10).sort(w);(e.twitterImageSrc??e.twitterImageProperty??e.twitterImageWidth??e.twitterImageHeight??e.twitterImageAlt)&&(e.twitterImageSrc=e.twitterImageSrc?e.twitterImageSrc:[],e.twitterImageProperty=e.twitterImageProperty?e.twitterImageProperty:e.twitterImageSrc,e.twitterImageWidth=e.twitterImageWidth?e.twitterImageWidth:[],e.twitterImageHeight=e.twitterImageHeight?e.twitterImageHeight:[],e.twitterImageAlt=e.twitterImageAlt?e.twitterImageAlt:[]);let a=d(e.twitterImageProperty,e.twitterImageWidth,e.twitterImageHeight,e.twitterImageAlt).map(j).filter(i=>i.url!==void 0&&i.url!=="").filter((i,l)=>l<10).sort(w);(e.twitterPlayerProperty??e.twitterPlayerWidth??e.twitterPlayerHeight??e.twitterPlayerStream)&&(e.twitterPlayerProperty=e.twitterPlayerProperty?e.twitterPlayerProperty:[],e.twitterPlayerWidth=e.twitterPlayerWidth?e.twitterPlayerWidth:[],e.twitterPlayerHeight=e.twitterPlayerHeight?e.twitterPlayerHeight:[],e.twitterPlayerStream=e.twitterPlayerStream?e.twitterPlayerStream:[]);let o=d(e.twitterPlayerProperty,e.twitterPlayerWidth,e.twitterPlayerHeight,e.twitterPlayerStream).map(ee).filter(i=>i.url!==void 0&&i.url!=="").filter((i,l)=>l<10).sort(w);(e.musicSongProperty??e.musicSongTrack??e.musicSongDisc??e.musicSongUrl)&&(e.musicSongUrl=e.musicSongUrl?e.musicSongUrl:[],e.musicSongProperty=e.musicSongProperty?e.musicSongProperty:e.musicSongUrl,e.musicSongTrack=e.musicSongTrack?e.musicSongTrack:[],e.musicSongDisc=e.musicSongDisc?e.musicSongDisc:[]);let s=d(e.musicSongProperty,e.musicSongTrack,e.musicSongDisc).map(te).filter(i=>i.url!==void 0&&i.url!=="").filter((i,l)=>l<10).sort(re);return h.filter(i=>i.multiple&&i.fieldName?.match("(ogImage|ogVideo|twitter|musicSong).*")).forEach(i=>{delete e[i.fieldName]}),r.length&&(e.ogImage=r),t.length&&(e.ogVideo=t),a.length&&(e.twitterImage=a),o.length&&(e.twitterPlayer=o),s.length&&(e.musicSong=s),e=T(e),e}var H=ie;function N(e,r){let t={success:!0},a=ae(e),o=h;return a("meta").each((s,i)=>{if(!i.attribs||!i.attribs.property&&!i.attribs.name)return;let l=i.attribs.property||i.attribs.name,f=i.attribs.content||i.attribs.value;o.forEach(p=>{p&&l.toLowerCase()===p.property.toLowerCase()&&(p.fieldName==="musicSongDisc"||p.fieldName==="musicSongProperty"||p.fieldName==="musicSongTrack"||p.fieldName==="musicSongUrl"||p.fieldName==="ogImageAlt"||p.fieldName==="ogImageHeight"||p.fieldName==="ogImageProperty"||p.fieldName==="ogImageSecureURL"||p.fieldName==="ogImageType"||p.fieldName==="ogImageURL"||p.fieldName==="ogImageWidth"||p.fieldName==="ogVideoHeight"||p.fieldName==="ogVideoProperty"||p.fieldName==="ogVideoType"||p.fieldName==="ogVideoWidth"||p.fieldName==="twitterImageAlt"||p.fieldName==="twitterImageHeight"||p.fieldName==="twitterImageProperty"||p.fieldName==="twitterImageSrc"||p.fieldName==="twitterImageWidth"||p.fieldName==="twitterPlayerHeight"||p.fieldName==="twitterPlayerProperty"||p.fieldName==="twitterPlayerStream"||p.fieldName==="twitterPlayerWidth"?t[p.fieldName]?t[p.fieldName]?.push(f):t[p.fieldName]=[f]:t[p.fieldName]=f)}),r.customMetaTags&&(r.customMetaTags.forEach(p=>{t.customMetaTags||(t.customMetaTags={}),p&&l.toLowerCase()===p.property.toLowerCase()&&(p.multiple?t.customMetaTags[p.fieldName]?Array.isArray(t.customMetaTags[p.fieldName])&&(t.customMetaTags[p.fieldName]=[...t.customMetaTags[p.fieldName],f]):t.customMetaTags[p.fieldName]=[f]:t.customMetaTags[p.fieldName]=f)}),t.customMetaTags&&Object.keys(t.customMetaTags).length===0&&delete t.customMetaTags)}),t=H(t),(!r.onlyGetOpenGraphInfo||Array.isArray(r.onlyGetOpenGraphInfo))&&(t=M(t,r,a,e),a("script").each((s,i)=>{if(i.attribs.type&&i.attribs.type==="application/ld+json"){t.jsonLD||(t.jsonLD=[]);let l=a(i).text();l&&(l=l.replace(/(\r\n|\n|\r)/gm,""),l=W(l),t.jsonLD.push(JSON.parse(l)))}})),t}import{fetch as le}from"undici";import{decode as oe}from"iconv-lite";import{load as pe}from"cheerio";import se from"chardet";var x=(e,r,t)=>t(e).attr(r)&&(t(e).attr(r)?.length??0)>0;function ne(e,r,t){if(x("meta","charset",t))return t("meta").attr("charset");if(x('head > meta[name="charset"]',"content",t))return t('head > meta[name="charset"]').attr("content");if(x('head > meta[http-equiv="content-type"]',"content",t)){let a=t('head > meta[http-equiv="content-type"]').attr("content")??"",o=/charset=([^()<>@,;:"/[\]?.=\s]*)/i;if(o.test(a)){let s=o.exec(a);if(s?.[1])return s[1]}}return e?se.detect(Buffer.from(r)):"utf-8"}async function v(e){let r,t;try{let a=/^[\u0000-\u00ff]{0,}$/,o=e.url??"";a.test(o)||(o=encodeURI(o)),t=await le(o??"",{signal:AbortSignal.timeout((e.timeout??10)*1e3),...e.fetchOptions,headers:{Origin:o??"",Accept:"text/html",...e.fetchOptions?.headers}});let s=await t.arrayBuffer(),i=Buffer.from(s).toString("utf-8"),l=ne(i,s,pe(i))??"utf-8";if(l.toLowerCase()==="utf-8"?r=i:r=oe(Buffer.from(s),l),t?.headers?.get("content-type")&&!t.headers.get("content-type")?.includes("text/"))throw new Error("Page must return a header content-type with text/");if(t?.status&&(t.status.toString().startsWith("4")||t.status.toString().startsWith("5")))switch(t.status){case 400:throw new Error("400 Bad Request");case 401:throw new Error("401 Unauthorized");case 403:throw new Error("403 Forbidden");case 404:throw new Error("404 Not Found");case 408:throw new Error("408 Request Timeout");case 410:throw new Error("410 Gone");case 500:throw new Error("500 Internal Server Error");case 502:throw new Error("502 Bad Gateway");case 503:throw new Error("503 Service Unavailable");case 504:throw new Error("504 Gateway Timeout");default:throw new Error("Server has returned a 400/500 error code")}if(r===void 0||r==="")throw new Error("Page not found")}catch(a){throw a instanceof Error&&a.message==="fetch failed"?a.cause:a}return{body:r,response:t}}async function U(e){let{options:r}=C(e);if(r.html&&r.url)throw new Error("Must specify either `url` or `html`, not both");if(!D(r.customMetaTags??[]))throw new Error("Invalid Custom Meta Tags");if(r.html){let a=N(r.html,r);return a.success=!0,{ogObject:a,response:{body:r.html},html:r.html}}let t=E(r.url??"",r.urlValidatorSettings??g);if(!t.url)throw new Error("Invalid URL");if(r.url=t.url,L(r.url))throw new Error("Must scrape an HTML page");if(r?.blacklist?.some(a=>r.url?.includes(a)))throw new Error("Host name has been black listed");try{let{body:a,response:o}=await v(r),s=N(a,r);return s.requestUrl=r.url,{ogObject:s,response:o,html:a}}catch(a){throw a&&(a.code==="ENOTFOUND"||a.code==="EHOSTUNREACH"||a.code==="ENETUNREACH")?new Error("Page not found"):a&&a.name==="AbortError"?new Error("The operation was aborted due to timeout"):a instanceof Error?a:new Error("Page not found")}}async function me(e){let r;try{r=await U(e)}catch(a){let o=a;throw{error:!0,result:{success:!1,requestUrl:e.url,error:o.message,errorDetails:o},response:void 0,html:void 0}}return{error:!1,result:r.ogObject,response:r.response,html:r.html}}var Ge=me;export{Ge as default};