Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

yt-search

Package Overview
Dependencies
Maintainers
1
Versions
72
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

yt-search - npm Package Compare versions

Comparing version 2.2.0 to 2.2.1

2

dist/yt-search.min.js

@@ -1,1 +0,1 @@

!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).ytSearch=e()}}(function(){return function l(r,u,o){function s(t,e){if(!u[t]){if(!r[t]){var i="function"==typeof require&&require;if(!e&&i)return i(t,!0);if(d)return d(t,!0);var n=new Error("Cannot find module '"+t+"'");throw n.code="MODULE_NOT_FOUND",n}var a=u[t]={exports:{}};r[t][0].call(a.exports,function(e){return s(r[t][1][e]||e)},a,a.exports,l,r,u,o)}return u[t].exports}for(var d="function"==typeof require&&require,e=0;e<o.length;e++)s(o[e]);return s}({1:[function(s,e,t){"use strict";var l="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};var G=s("cheerio"),d=s("dasu");s("async.parallellimit");d.follow=!1,d.debug=!1;var W=s("jsonpath"),c="Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html) (yt-search; https://www.npmjs.com/package/yt-search)",h=s("url"),i={};Object.keys(process.env).forEach(function(e){var t=process.env[e];if("0"==t||"false"==t||!t)return i[e]=!1;i[e]=t});var m=i.debug;function X(){m&&console.log.apply(this,arguments)}var n,y=s("querystring"),g=s("human-time"),Q={YT:"https://youtube.com",SEARCH_MOBILE:"https://m.youtube.com/results",SEARCH_DESKTOP:"https://www.youtube.com/results"};function a(e,t){if(!t)return new Promise(function(i,n){a(e,function(e,t){if(e)return n(e);i(t)})});var i=void 0;if((i="string"==typeof e?{query:e}:e).userAgent&&(c=i.userAgent),i.search=i.query||i.search,i.original_search=i.search,i.videoId)return function(e,a){X("fn: getVideoMetaData");var t=void 0;"string"==typeof e&&(t=e);"object"===(void 0===e?"undefined":l(e))&&(t=e.videoId);var i="https://www.youtube.com/watch?hl=en&gl=US&v="+t,n=h.parse(i);n.headers={"user-agent":c,accept:"text/html","accept-encoding":"gzip","accept-language":"en-US"},n.headers["user-agent"]="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Safari/605.1.15",d.req(n,function(e,t,i){if(e)a(e);else{if(200!==t.status)return a("http status: "+t.status);if(m){var n=s("fs");s("path");n.writeFileSync("dasu.response",t.responseText,"utf8")}try{!function(e,t){X("_parseVideoInitialData");var i=e.match(/ytInitialData.*=\s*({.*});/);if(!i[1])return t("could not find inital data in the html document");var n=e.match(/ytInitialPlayerResponse.*=\s*({.*});/);if(!n[1])return t("could not find inital player data in the html document");var a=JSON.parse(i[1]),l=JSON.parse(n[1]),r=W.value(a,"$..currentVideoEndpoint..videoId");if(!r)return t("video unavailable");if("ERROR"===W.value(l,"$..status")||"Video unavailable"===W.value(l,"$..reason"))return t("video unavailable");var u=W.value(a,"$..videoPrimaryInfoRenderer..title..text")||W.value(a,"$..videoPrimaryInfoRenderer..title..simpleText")||W.value(a,"$..videoPrimaryRenderer..title..text")||W.value(a,"$..videoPrimaryRenderer..title..simpleText")||W.value(a,"$..title..text")||W.value(a,"$..title..simpleText"),o=W.query(l,"$..description..text").join("")||W.query(l,"$..description..simpleText").join("")||W.query(a,"$..description..text").join("")||W.query(a,"$..description..simpleText").join(""),s=W.value(a,"$..owner..title..text")||W.value(a,"$..owner..title..simpleText"),d=W.value(a,"$..owner..navigationEndpoint..url")||W.value(a,"$..owner..title..url"),c="https://i.ytimg.com/vi/"+r+"/hqdefault.jpg",v=te(I(1e3*Number(W.value(l,"$..videoDetails..lengthSeconds")))),p=W.value(a,"$..sentimentBar..tooltip").split(/[,.]/).join("").split(/\D+/),f=(Number(p[0]),Number(p[1]),W.value(a,"$..dateText")),h={title:u,description:o,url:Q.YT+"/watch?v="+r,videoId:r,seconds:Number(v.seconds),timestamp:v.timestamp,duration:v,views:Number(W.value(l,"$..videoDetails..viewCount")),genre:(W.value(l,"$..category")||"").toLowerCase(),uploadDate:W.value(l,"$..uploadDate"),ago:g(new Date(f)),image:c,thumbnail:c,author:{name:s,url:Q.YT+d}};t(null,h)}(i,a)}catch(e){a(e)}}})}(i.videoId,t);if(i.listId)return function(e,a){X("fn: getPlaylistMetaData");var t=void 0;"string"==typeof e&&(t=e);"object"===(void 0===e?"undefined":l(e))&&(t=e.listId||e.playlistId);var i="https://www.youtube.com/playlist?hl=en&gl=US&list="+t,n=h.parse(i);n.headers={"user-agent":c,accept:"text/html","accept-encoding":"gzip","accept-language":"en-US"},d.req(n,function(e,t,i){if(e)a(e);else{if(200!==t.status)return a("http status: "+t.status);if(m){var n=s("fs");s("path");n.writeFileSync("dasu.response",t.responseText,"utf8")}try{!function(e,t){X("fn: parsePlaylistBody");var i=e.match(/ytInitialData.*=\s*({.*});/)[1];if(!i)throw new Error("failed to parse ytInitialData json data");var n=JSON.parse(i),a=W.value(n,"$..microformat..urlCanonical").split("=")[1],l=W.value(n,"$..sidebar.playlistSidebarRenderer.items[0]..stats[1].simpleText").match(/\d+/),r=W.query(n,"$..contents..tabs[0]..contents[0]..contents[0]..contents")[0],u=[];r.forEach(function(e){var t=e,i={title:W.value(t,"$..title..simpleText"),videoId:W.value(t,"$..videoId"),listId:a,thumbnail:W.value(t,"$..thumbnail..thumbnails[0]..url").split("?")[0],author:{name:W.value(t,"$..shortBylineText..runs[0]..text"),url:"https://youtube.com"+W.value(t,"$..shortBylineText..runs[0]..url")}};u.push(i)});var o={title:W.value(n,"$..microformat..title"),listId:a,url:"https://youtube.com/playlist?list="+a,views:Number(l),date:v(W.value(n,"$..sidebar.playlistSidebarRenderer.items[0]..stats[2]..simpleText")),image:u[0].thumbnail,thumbnail:u[0].thumbnail,videos:u,author:{name:W.value(n,"$..videoOwner..title..runs[0]..text"),url:"https://youtube.com"+W.value(n,"$..videoOwner..navigationEndpoint..url")}};t&&t(null,o)}(i,a)}catch(e){a(e)}}})}(i.listId,t);if(!i.search)return t(Error("yt-search: no query given"));!function v(p,f){var e=y.escape(p.search).split(/\s+/),t=p.hl||"en",i=p.gl||"US",n=p.category||"",a=Number(p.pageStart)||1,l=Number(p.pageEnd)||Number(p.pages)||1;a<=0&&(a=1)<=l&&(l+=1),Number.isNaN(l)&&f("error: pageEnd must be a number"),p.pageStart=a,p.pageEnd=l,p.currentPage=p.currentPage||a;var r="?";-1===(r+="search_query="+e.join("+")).indexOf("&hl=")&&(r+="&hl="+t),-1===r.indexOf("&gl=")&&(r+="&gl="+i),n&&(r+="&category="+n),p.sp&&(r+="&sp="+p.sp);var u=Q.SEARCH_DESKTOP+r,o=h.parse(u);o.headers={"user-agent":c,accept:"text/html","accept-encoding":"gzip","accept-language":"en-US"},X(o),X("getting results: "+p.currentPage),d.req(o,function(e,t,i){if(e)f(e);else{if(200!==t.status)return f("http status: "+t.status);if(m){var n=s("fs");s("path"),n.writeFileSync("dasu.response",t.responseText,"utf8")}try{w(i,function(e,t){if(e)return f(e);var i=t,n=i.filter(x),a=i.filter($),l=i.filter(b),r=i.filter(T);p._data=p._data||{},p._data.videos=p._data.videos||[],p._data.playlists=p._data.playlists||[],p._data.channels=p._data.channels||[],p._data.live=p._data.live||[],n.forEach(function(e){p._data.videos.push(e)}),a.forEach(function(e){p._data.playlists.push(e)}),l.forEach(function(e){p._data.channels.push(e)}),r.forEach(function(e){p._data.live.push(e)}),p.currentPage++;var u=p.currentPage<=p.pageEnd;if(u&&t._sp)p.sp=t._sp,setTimeout(function(){v(p,f)},2500);else{var o=p._data.videos.filter(x),s=p._data.playlists.filter($),d=p._data.channels.filter(b),c=p._data.live.filter(T);f(null,{videos:o,live:c,playlists:s,lists:s,accounts:d,channels:d})}})}catch(e){f(e)}}})}(i,t)}function x(e,t,i){if("video"!==e.type)return!1;var n=e.videoId;return i.findIndex(function(e){return n===e.videoId})===t}function $(e,t,i){if("list"!==e.type)return!1;var n=e.listId;return i.findIndex(function(e){return n===e.listId})===t}function b(e,t,i){if("channel"!==e.type)return!1;var n=e.url;return i.findIndex(function(e){return n===e.url})===t}function T(e,t,i){if("live"!==e.type)return!1;var n=e.videoId;return i.findIndex(function(e){return n===e.videoId})===t}function Z(e){var t=void 0;return"string"!=typeof e?e.length?Z(t=e[0]):void 0:0===(t=(t=(t=(t=e).split("?")[0]).split("/default.jpg").join("/hqdefault.jpg")).split("/default.jpeg").join("/hqdefault.jpeg")).indexOf("//")?"https://"+t.slice(2):t.split("http://").join("https://")}function ee(e){var t=e.split(/\s+/).filter(function(e){return e.match(/\d/)})[0].toLowerCase(),i=Number(t.replace(/\D/g,""));return 0<=t.indexOf("m")?1e6*i:0<=t.indexOf("k")?1e3*i:i}function w(e,t){var n=/{.*}/,i=G.load(e),a=i("div#initial-data").html()||"";if(!(a=n.exec(a)||""))for(var l=i("script"),r=0;r<l.length;r++){i(l[r]).html().split("\n").forEach(function(e){for(var t=void 0;0<=(t=e.indexOf("ytInitialData"));){e=e.slice(t+"ytInitialData".length);var i=n.exec(e);i&&i.length>a.length&&(a=i)}})}if(!a)return t("could not find inital data in the html document");var u=[],o=[],s=JSON.parse(a[0]),d=W.query(s,"$..itemSectionRenderer..contents.*");X("items.length: "+d.length);for(var c=0;c<d.length;c++){var v=d[c],p=void 0,f="unknown",h=v.compactPlaylistRenderer||v.playlistRenderer,m=v.compactChannelRenderer||v.channelRenderer,y=v.compactVideoRenderer||v.videoRenderer,g=h&&W.value(v,"$..playlistId"),x=m&&W.value(v,"$..channelId"),$=y&&W.value(v,"$..videoId");$&&(f="video"),x&&(f="channel"),g&&(f="list"),0<=W.query(v,"$..viewCountText..text").join("").indexOf("watching")&&(f="live");try{switch(f){case"video":var b=Z(W.value(v,"$..thumbnail..url"))||Z(W.value(v,"$..thumbnails..url"))||Z(W.value(v,"$..thumbnails")),T=W.value(v,"$..title..text")||W.value(v,"$..title..simpleText"),w=W.value(v,"$..shortBylineText..text")||W.value(v,"$..longBylineText..text"),I=W.value(v,"$..shortBylineText..url")||W.value(v,"$..longBylineText..url"),S=W.value(v,"$..publishedTimeText..text")||W.value(v,"$..publishedTimeText..simpleText"),j=W.value(v,"$..viewCountText..text")||W.value(v,"$..viewCountText..simpleText")||"0",_=Number(j.split(/\s+/)[0].split(/[,.]/).join("").trim()),q=te(W.value(v,"$..lengthText..text")||W.value(v,"$..lengthText..simpleText")||"0:00"),E=W.query(v,"$..description..text").join("")||W.query(v,"$..descriptionSnippet..text").join("");p={type:"video",videoId:$,url:Q.YT+"/watch?v="+$,title:T.trim(),description:E,image:b,thumbnail:b,seconds:Number(q.seconds),timestamp:q.timestamp,duration:q,ago:S,views:Number(_),author:{name:w,url:Q.YT+I}};break;case"list":var D=Z(W.value(v,"$..thumbnail..url"))||Z(W.value(v,"$..thumbnails..url"))||Z(W.value(v,"$..thumbnails")),N=W.value(v,"$..title..text")||W.value(v,"$..title..simpleText"),C=W.value(v,"$..shortBylineText..text")||W.value(v,"$..longBylineText..text")||W.value(v,"$..shortBylineText..simpleText")||W.value(v,"$..longBylineText..simpleTextn")||"YouTube",O=W.value(v,"$..shortBylineText..url")||W.value(v,"$..longBylineText..url")||"",B=W.value(v,"$..videoCountShortText..text")||W.value(v,"$..videoCountText..text")||W.value(v,"$..videoCountShortText..simpleText")||W.value(v,"$..videoCountText..simpleText")||W.value(v,"$..thumbnailText..text")||W.value(v,"$..thumbnailText..simpleText");p={type:"list",listId:g,url:Q.YT+"/playlist?list="+g,title:N.trim(),image:D,thumbnail:D,videoCount:B,author:{name:C,url:Q.YT+O}};break;case"channel":var R=Z(W.value(v,"$..thumbnail..url"))||Z(W.value(v,"$..thumbnails..url"))||Z(W.value(v,"$..thumbnails")),P=W.value(v,"$..title..text")||W.value(v,"$..title..simpleText")||W.value(v,"$..displayName..text"),M=W.value(v,"$..shortBylineText..text")||W.value(v,"$..longBylineText..text")||W.value(v,"$..displayName..text")||W.value(v,"$..displayName..simpleText"),Y=W.value(v,"$..videoCountText..text")||W.value(v,"$..videoCountText..simpleText")||"0",k=W.value(v,"$..subscriberCountText..text")||W.value(v,"$..subscriberCountText..simpleText");"string"==typeof k&&(k=k.split(/\s+/).filter(function(e){return e.match(/\d/)})[0]);var U=W.value(v,"$..navigationEndpoint..url")||"/user/"+P;p={type:"channel",name:M,url:Q.YT+U,title:P.trim(),image:R,thumbnail:R,videoCount:Number(Y.replace(/\D+/g,"")),videoCountLabel:Y,subCount:ee(k),subCountLabel:k};break;case"live":var L=Z(W.value(v,"$..thumbnail..url"))||Z(W.value(v,"$..thumbnails..url"))||Z(W.value(v,"$..thumbnails")),A=W.value(v,"$..title..text")||W.value(v,"$..title..simpleText"),V=W.value(v,"$..shortBylineText..text")||W.value(v,"$..longBylineText..text"),z=W.value(v,"$..shortBylineText..url")||W.value(v,"$..longBylineText..url"),F=W.query(v,"$..viewCountText..text").join("")||W.query(v,"$..viewCountText..simpleText").join("")||"0",H=Number(F.split(/\s+/)[0].split(/[,.]/).join("").trim()),J=W.query(v,"$..description..text").join("")||W.query(v,"$..descriptionSnippet..text").join("");p={type:"live",videoId:$,url:Q.YT+"/watch?v="+$,title:A.trim(),description:J,image:L,thumbnail:L,watching:Number(H),author:{name:V,url:Q.YT+z}}}p&&o.push(p)}catch(e){X(e),u.push(e)}}var K=W.value(s,"$..continuation");return o._ctoken=K,u.length?t(u.pop(),o):t(null,o)}function te(e){var t=e.split(/\s+/),i=t[t.length-1].replace(/[^:.\d]/g,"");if(!i)return{toString:function(){return t[0]},seconds:0,timestamp:0};for(;i[i.length-1].match(/\D/);)i=i.slice(0,-1);for(var n=(i=i.replace(/\./g,":")).split(/[:.]/),a=0,l=0,r=n.length-1;0<=r;r--)if(!(n[r].length<=0)){var u=n[r].replace(/\D/g,"");if(a+=parseInt(u)*(0<l?Math.pow(60,l):1),2<++l)break}return{toString:function(){return a+" seconds ("+i+")"},seconds:a,timestamp:i}}function I(e){var t="",i=Math.floor(e/36e5),n=Math.floor(e/6e4)%60,a=Math.floor(e/1e3)%60;return i&&(t+=i+":"),n&&(t+=n+":"),String(a).length<2&&(t+="0"),t+=a}function v(e){X("fn: _parsePlaylistLastUpdateTime");for(var t=e.trim().split(/[\s.-]+/),i=0;i<t.length;i++){var n=t.slice(i),a=n.join(" "),l=n.reverse().join(" "),r=new Date(a),u=new Date(l);if("Invalid Date"!==r.toString())return o(r);if("Invalid Date"!==u.toString())return o(u)}return""}function o(e){return X("fn: _toInternalDateString"),e.getFullYear()+"-"+e.getMonth()+"-"+e.getDate()}e.exports=function(e,t){return a(e,t)},e.exports.search=a,s.main===e&&(n="superman theme list pewdiepie channel",console.log("test: doing list search"),a({query:n,pageEnd:1},function(e,t){if(e)throw e;var i=t.videos,n=t.playlists,a=t.channels;console.log("videos: "+i.length),console.log("playlists: "+n.length),console.log("channels: "+a.length);for(var l=0;l<i.length;l++){var r=i[l],u=" ("+r.timestamp+")";console.log(r.title+u)}n.forEach(function(e){console.log("playlist: "+e.title+" | "+e.listId)}),a.forEach(function(e){console.log("channel: "+e.title+" | "+e.description)})}))},{"async.parallellimit":void 0,cheerio:void 0,dasu:void 0,fs:void 0,"human-time":void 0,jsonpath:void 0,path:void 0,querystring:void 0,url:void 0}]},{},[1])(1)});
!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{("undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this).ytSearch=e()}}(function(){return function l(r,o,u){function s(t,e){if(!o[t]){if(!r[t]){var i="function"==typeof require&&require;if(!e&&i)return i(t,!0);if(c)return c(t,!0);var n=new Error("Cannot find module '"+t+"'");throw n.code="MODULE_NOT_FOUND",n}var a=o[t]={exports:{}};r[t][0].call(a.exports,function(e){return s(r[t][1][e]||e)},a,a.exports,l,r,o,u)}return o[t].exports}for(var c="function"==typeof require&&require,e=0;e<u.length;e++)s(u[e]);return s}({1:[function(m,e,t){"use strict";var l="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};var G=m("cheerio"),s=m("dasu");m("async.parallellimit");s.follow=!1,s.debug=!1;var W=m("jsonpath"),c="Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html) (yt-search; https://www.npmjs.com/package/yt-search)",d=m("url"),i={};Object.keys(process.env).forEach(function(e){var t=process.env[e];if("0"==t||"false"==t||!t)return i[e]=!1;i[e]=t});var h=i.debug;function X(){h&&console.log.apply(this,arguments)}var n,y=m("querystring"),g=m("human-time"),Q={YT:"https://youtube.com",SEARCH_MOBILE:"https://m.youtube.com/results",SEARCH_DESKTOP:"https://www.youtube.com/results"};function a(e,t){if(!t)return new Promise(function(i,n){a(e,function(e,t){if(e)return n(e);i(t)})});var i=void 0;if((i="string"==typeof e?{query:e}:e).userAgent&&(c=i.userAgent),i.search=i.query||i.search,i.original_search=i.search,i.videoId)return function(e,a){X("fn: getVideoMetaData");var t=void 0;"string"==typeof e&&(t=e);"object"===(void 0===e?"undefined":l(e))&&(t=e.videoId);var i="https://www.youtube.com/watch?hl=en&gl=US&v="+t,n=d.parse(i);n.headers={"user-agent":c,accept:"text/html","accept-encoding":"gzip","accept-language":"en-US"},n.headers["user-agent"]="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1.1 Safari/605.1.15",s.req(n,function(e,t,i){if(e)a(e);else{if(200!==t.status)return a("http status: "+t.status);if(h){var n=m("fs");m("path");n.writeFileSync("dasu.response",t.responseText,"utf8")}try{!function(e,t){X("_parseVideoInitialData");var i=j(S(/ytInitialData.*=\s*{/,e),"{","}");if(!i)return t("could not find inital data in the html document");var n=j(S(/ytInitialPlayerResponse.*=\s*{/,e),"{","}");if(!n)return t("could not find inital player data in the html document");m("fs").writeFileSync("initialPlayerData.json",n);var a=JSON.parse(i),l=JSON.parse(n),r=W.value(a,"$..currentVideoEndpoint..videoId");if(!r)return t("video unavailable");if("ERROR"===W.value(l,"$..status")||"Video unavailable"===W.value(l,"$..reason"))return t("video unavailable");var o=W.value(a,"$..videoPrimaryInfoRenderer..title..text")||W.value(a,"$..videoPrimaryInfoRenderer..title..simpleText")||W.value(a,"$..videoPrimaryRenderer..title..text")||W.value(a,"$..videoPrimaryRenderer..title..simpleText")||W.value(a,"$..title..text")||W.value(a,"$..title..simpleText"),u=W.query(a,"$..description..text").join("")||W.query(l,"$..description..simpleText").join("")||W.query(l,"$..microformat..description..simpleText").join("")||W.query(l,"$..videoDetails..shortDescription").join(""),s=W.value(a,"$..owner..title..text")||W.value(a,"$..owner..title..simpleText"),c=W.value(a,"$..owner..navigationEndpoint..url")||W.value(a,"$..owner..title..url"),d="https://i.ytimg.com/vi/"+r+"/hqdefault.jpg",v=te(I(1e3*Number(W.value(l,"$..videoDetails..lengthSeconds")))),p=W.value(a,"$..sentimentBar..tooltip").split(/[,.]/).join("").split(/\D+/),f=(Number(p[0]),Number(p[1]),W.value(a,"$..dateText")),h={title:o,description:u,url:Q.YT+"/watch?v="+r,videoId:r,seconds:Number(v.seconds),timestamp:v.timestamp,duration:v,views:Number(W.value(l,"$..videoDetails..viewCount")),genre:(W.value(l,"$..category")||"").toLowerCase(),uploadDate:W.value(l,"$..uploadDate"),ago:g(new Date(f)),image:d,thumbnail:d,author:{name:s,url:Q.YT+c}};t(null,h)}(i,a)}catch(e){a(e)}}})}(i.videoId,t);if(i.listId)return function(e,a){X("fn: getPlaylistMetaData");var t=void 0;"string"==typeof e&&(t=e);"object"===(void 0===e?"undefined":l(e))&&(t=e.listId||e.playlistId);var i="https://www.youtube.com/playlist?hl=en&gl=US&list="+t,n=d.parse(i);n.headers={"user-agent":c,accept:"text/html","accept-encoding":"gzip","accept-language":"en-US"},s.req(n,function(e,t,i){if(e)a(e);else{if(200!==t.status)return a("http status: "+t.status);if(h){var n=m("fs");m("path");n.writeFileSync("dasu.response",t.responseText,"utf8")}try{!function(e,t){X("fn: parsePlaylistBody");var i=e.match(/ytInitialData.*=\s*({.*});/)[1];if(!i)throw new Error("failed to parse ytInitialData json data");var n=JSON.parse(i),a=W.value(n,"$..microformat..urlCanonical").split("=")[1],l=W.value(n,"$..sidebar.playlistSidebarRenderer.items[0]..stats[1].simpleText").match(/\d+/),r=W.query(n,"$..contents..tabs[0]..contents[0]..contents[0]..contents")[0],o=[];r.forEach(function(e){var t=e,i={title:W.value(t,"$..title..simpleText"),videoId:W.value(t,"$..videoId"),listId:a,thumbnail:W.value(t,"$..thumbnail..thumbnails[0]..url").split("?")[0],author:{name:W.value(t,"$..shortBylineText..runs[0]..text"),url:"https://youtube.com"+W.value(t,"$..shortBylineText..runs[0]..url")}};o.push(i)});var u={title:W.value(n,"$..microformat..title"),listId:a,url:"https://youtube.com/playlist?list="+a,views:Number(l),date:v(W.value(n,"$..sidebar.playlistSidebarRenderer.items[0]..stats[2]..simpleText")),image:o[0].thumbnail,thumbnail:o[0].thumbnail,videos:o,author:{name:W.value(n,"$..videoOwner..title..runs[0]..text"),url:"https://youtube.com"+W.value(n,"$..videoOwner..navigationEndpoint..url")}};t&&t(null,u)}(i,a)}catch(e){a(e)}}})}(i.listId,t);if(!i.search)return t(Error("yt-search: no query given"));!function v(p,f){var e=y.escape(p.search).split(/\s+/),t=p.hl||"en",i=p.gl||"US",n=p.category||"",a=Number(p.pageStart)||1,l=Number(p.pageEnd)||Number(p.pages)||1;a<=0&&(a=1)<=l&&(l+=1),Number.isNaN(l)&&f("error: pageEnd must be a number"),p.pageStart=a,p.pageEnd=l,p.currentPage=p.currentPage||a;var r="?";-1===(r+="search_query="+e.join("+")).indexOf("&hl=")&&(r+="&hl="+t),-1===r.indexOf("&gl=")&&(r+="&gl="+i),n&&(r+="&category="+n),p.sp&&(r+="&sp="+p.sp);var o=Q.SEARCH_DESKTOP+r,u=d.parse(o);u.headers={"user-agent":c,accept:"text/html","accept-encoding":"gzip","accept-language":"en-US"},X(u),X("getting results: "+p.currentPage),s.req(u,function(e,t,i){if(e)f(e);else{if(200!==t.status)return f("http status: "+t.status);if(h){var n=m("fs");m("path"),n.writeFileSync("dasu.response",t.responseText,"utf8")}try{w(i,function(e,t){if(e)return f(e);var i=t,n=i.filter(x),a=i.filter($),l=i.filter(b),r=i.filter(T);p._data=p._data||{},p._data.videos=p._data.videos||[],p._data.playlists=p._data.playlists||[],p._data.channels=p._data.channels||[],p._data.live=p._data.live||[],n.forEach(function(e){p._data.videos.push(e)}),a.forEach(function(e){p._data.playlists.push(e)}),l.forEach(function(e){p._data.channels.push(e)}),r.forEach(function(e){p._data.live.push(e)}),p.currentPage++;var o=p.currentPage<=p.pageEnd;if(o&&t._sp)p.sp=t._sp,setTimeout(function(){v(p,f)},2500);else{var u=p._data.videos.filter(x),s=p._data.playlists.filter($),c=p._data.channels.filter(b),d=p._data.live.filter(T);f(null,{videos:u,live:d,playlists:s,lists:s,accounts:c,channels:c})}})}catch(e){f(e)}}})}(i,t)}function x(e,t,i){if("video"!==e.type)return!1;var n=e.videoId;return i.findIndex(function(e){return n===e.videoId})===t}function $(e,t,i){if("list"!==e.type)return!1;var n=e.listId;return i.findIndex(function(e){return n===e.listId})===t}function b(e,t,i){if("channel"!==e.type)return!1;var n=e.url;return i.findIndex(function(e){return n===e.url})===t}function T(e,t,i){if("live"!==e.type)return!1;var n=e.videoId;return i.findIndex(function(e){return n===e.videoId})===t}function Z(e){var t=void 0;return"string"!=typeof e?e.length?Z(t=e[0]):void 0:0===(t=(t=(t=(t=e).split("?")[0]).split("/default.jpg").join("/hqdefault.jpg")).split("/default.jpeg").join("/hqdefault.jpeg")).indexOf("//")?"https://"+t.slice(2):t.split("http://").join("https://")}function ee(e){var t=e.split(/\s+/).filter(function(e){return e.match(/\d/)})[0].toLowerCase(),i=Number(t.replace(/\D/g,""));return 0<=t.indexOf("m")?1e6*i:0<=t.indexOf("k")?1e3*i:i}function w(e,t){var n=/{.*}/,i=G.load(e),a=i("div#initial-data").html()||"";if(!(a=n.exec(a)||""))for(var l=i("script"),r=0;r<l.length;r++){i(l[r]).html().split("\n").forEach(function(e){for(var t=void 0;0<=(t=e.indexOf("ytInitialData"));){e=e.slice(t+"ytInitialData".length);var i=n.exec(e);i&&i.length>a.length&&(a=i)}})}if(!a)return t("could not find inital data in the html document");var o=[],u=[],s=JSON.parse(a[0]),c=W.query(s,"$..itemSectionRenderer..contents.*");X("items.length: "+c.length);for(var d=0;d<c.length;d++){var v=c[d],p=void 0,f="unknown",h=v.compactPlaylistRenderer||v.playlistRenderer,m=v.compactChannelRenderer||v.channelRenderer,y=v.compactVideoRenderer||v.videoRenderer,g=h&&W.value(v,"$..playlistId"),x=m&&W.value(v,"$..channelId"),$=y&&W.value(v,"$..videoId");$&&(f="video"),x&&(f="channel"),g&&(f="list"),0<=W.query(v,"$..viewCountText..text").join("").indexOf("watching")&&(f="live");try{switch(f){case"video":var b=Z(W.value(v,"$..thumbnail..url"))||Z(W.value(v,"$..thumbnails..url"))||Z(W.value(v,"$..thumbnails")),T=W.value(v,"$..title..text")||W.value(v,"$..title..simpleText"),w=W.value(v,"$..shortBylineText..text")||W.value(v,"$..longBylineText..text"),I=W.value(v,"$..shortBylineText..url")||W.value(v,"$..longBylineText..url"),S=W.value(v,"$..publishedTimeText..text")||W.value(v,"$..publishedTimeText..simpleText"),j=W.value(v,"$..viewCountText..text")||W.value(v,"$..viewCountText..simpleText")||"0",_=Number(j.split(/\s+/)[0].split(/[,.]/).join("").trim()),D=te(W.value(v,"$..lengthText..text")||W.value(v,"$..lengthText..simpleText")||"0:00"),q=W.query(v,"$..description..text").join("")||W.query(v,"$..descriptionSnippet..text").join("");p={type:"video",videoId:$,url:Q.YT+"/watch?v="+$,title:T.trim(),description:q,image:b,thumbnail:b,seconds:Number(D.seconds),timestamp:D.timestamp,duration:D,ago:S,views:Number(_),author:{name:w,url:Q.YT+I}};break;case"list":var E=Z(W.value(v,"$..thumbnail..url"))||Z(W.value(v,"$..thumbnails..url"))||Z(W.value(v,"$..thumbnails")),N=W.value(v,"$..title..text")||W.value(v,"$..title..simpleText"),C=W.value(v,"$..shortBylineText..text")||W.value(v,"$..longBylineText..text")||W.value(v,"$..shortBylineText..simpleText")||W.value(v,"$..longBylineText..simpleTextn")||"YouTube",O=W.value(v,"$..shortBylineText..url")||W.value(v,"$..longBylineText..url")||"",B=W.value(v,"$..videoCountShortText..text")||W.value(v,"$..videoCountText..text")||W.value(v,"$..videoCountShortText..simpleText")||W.value(v,"$..videoCountText..simpleText")||W.value(v,"$..thumbnailText..text")||W.value(v,"$..thumbnailText..simpleText");p={type:"list",listId:g,url:Q.YT+"/playlist?list="+g,title:N.trim(),image:E,thumbnail:E,videoCount:B,author:{name:C,url:Q.YT+O}};break;case"channel":var R=Z(W.value(v,"$..thumbnail..url"))||Z(W.value(v,"$..thumbnails..url"))||Z(W.value(v,"$..thumbnails")),P=W.value(v,"$..title..text")||W.value(v,"$..title..simpleText")||W.value(v,"$..displayName..text"),M=W.value(v,"$..shortBylineText..text")||W.value(v,"$..longBylineText..text")||W.value(v,"$..displayName..text")||W.value(v,"$..displayName..simpleText"),Y=W.value(v,"$..videoCountText..text")||W.value(v,"$..videoCountText..simpleText")||"0",k=W.value(v,"$..subscriberCountText..text")||W.value(v,"$..subscriberCountText..simpleText");"string"==typeof k&&(k=k.split(/\s+/).filter(function(e){return e.match(/\d/)})[0]);var U=W.value(v,"$..navigationEndpoint..url")||"/user/"+P;p={type:"channel",name:M,url:Q.YT+U,title:P.trim(),image:R,thumbnail:R,videoCount:Number(Y.replace(/\D+/g,"")),videoCountLabel:Y,subCount:ee(k),subCountLabel:k};break;case"live":var L=Z(W.value(v,"$..thumbnail..url"))||Z(W.value(v,"$..thumbnails..url"))||Z(W.value(v,"$..thumbnails")),A=W.value(v,"$..title..text")||W.value(v,"$..title..simpleText"),F=W.value(v,"$..shortBylineText..text")||W.value(v,"$..longBylineText..text"),V=W.value(v,"$..shortBylineText..url")||W.value(v,"$..longBylineText..url"),z=W.query(v,"$..viewCountText..text").join("")||W.query(v,"$..viewCountText..simpleText").join("")||"0",H=Number(z.split(/\s+/)[0].split(/[,.]/).join("").trim()),J=W.query(v,"$..description..text").join("")||W.query(v,"$..descriptionSnippet..text").join("");p={type:"live",videoId:$,url:Q.YT+"/watch?v="+$,title:A.trim(),description:J,image:L,thumbnail:L,watching:Number(H),author:{name:F,url:Q.YT+V}}}p&&u.push(p)}catch(e){X(e),o.push(e)}}var K=W.value(s,"$..continuation");return u._ctoken=K,o.length?t(o.pop(),u):t(null,u)}function te(e){var t=e.split(/\s+/),i=t[t.length-1].replace(/[^:.\d]/g,"");if(!i)return{toString:function(){return t[0]},seconds:0,timestamp:0};for(;i[i.length-1].match(/\D/);)i=i.slice(0,-1);for(var n=(i=i.replace(/\./g,":")).split(/[:.]/),a=0,l=0,r=n.length-1;0<=r;r--)if(!(n[r].length<=0)){var o=n[r].replace(/\D/g,"");if(a+=parseInt(o)*(0<l?Math.pow(60,l):1),2<++l)break}return{toString:function(){return a+" seconds ("+i+")"},seconds:a,timestamp:i}}function I(e){var t="",i=Math.floor(e/36e5),n=Math.floor(e/6e4)%60,a=Math.floor(e/1e3)%60;return i&&(t+=i+":"),n&&(t+=n+":"),String(a).length<2&&(t+="0"),t+=a}function v(e){X("fn: _parsePlaylistLastUpdateTime");for(var t=e.trim().split(/[\s.-]+/),i=0;i<t.length;i++){var n=t.slice(i),a=n.join(" "),l=n.reverse().join(" "),r=new Date(a),o=new Date(l);if("Invalid Date"!==r.toString())return u(r);if("Invalid Date"!==o.toString())return u(o)}return""}function u(e){return X("fn: _toInternalDateString"),e.getFullYear()+"-"+e.getMonth()+"-"+e.getDate()}function S(e,t){var i=S.cache||{};(S.cache=i)[t]=i[t]||{};var n=i[t].lines||t.split("\n");i[t].lines=n,clearTimeout(i[t].timeout),i[t].timeout=setTimeout(function(){delete i[t]},100);for(var a=0;a<n.length;a++){var l=n[a];if(e.test(l))return l}return""}function j(e,t,i){var n=e.indexOf(t),a=e.lastIndexOf(i);return n<0?"":a<0?"":e.slice(n,a+1)}e.exports=function(e,t){return a(e,t)},e.exports.search=a,m.main===e&&(n="superman theme list pewdiepie channel",console.log("test: doing list search"),a({query:n,pageEnd:1},function(e,t){if(e)throw e;var i=t.videos,n=t.playlists,a=t.channels;console.log("videos: "+i.length),console.log("playlists: "+n.length),console.log("channels: "+a.length);for(var l=0;l<i.length;l++){var r=i[l],o=" ("+r.timestamp+")";console.log(r.title+o)}n.forEach(function(e){console.log("playlist: "+e.title+" | "+e.listId)}),a.forEach(function(e){console.log("channel: "+e.title+" | "+e.description)})}))},{"async.parallellimit":void 0,cheerio:void 0,dasu:void 0,fs:void 0,"human-time":void 0,jsonpath:void 0,path:void 0,querystring:void 0,url:void 0}]},{},[1])(1)});
{
"name": "yt-search",
"version": "2.2.0",
"version": "2.2.1",
"description": "search youtube",

@@ -5,0 +5,0 @@ "main": "dist/yt-search.min.js",

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc