ph-scraper-api 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +1 -153
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -1,153 +1 @@
|
|
|
1
|
-
const axios = require('axios');
|
|
2
|
-
const cheerio = require('cheerio');
|
|
3
|
-
const { exec } = require('child_process');
|
|
4
|
-
const { promisify } = require('util');
|
|
5
|
-
const { mkdtemp, rm, readFile } = require('fs').promises;
|
|
6
|
-
const { join } = require('path');
|
|
7
|
-
const { tmpdir } = require('os');
|
|
8
|
-
|
|
9
|
-
const execAsync = promisify(exec);
|
|
10
|
-
const UA = 'Mozilla/5.0 (Linux; Android 11; Redmi Note 8) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36';
|
|
11
|
-
|
|
12
|
-
class PornHub {
|
|
13
|
-
constructor() {
|
|
14
|
-
this.baseUrl = 'https://www.pornhub.com';
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
_parseDuration(iso) {
|
|
18
|
-
if (!iso) return null;
|
|
19
|
-
const match = iso.match(/PT(\d+)H(\d+)M(\d+)S/);
|
|
20
|
-
if (!match) return iso;
|
|
21
|
-
const h = parseInt(match[1]);
|
|
22
|
-
const m = parseInt(match[2]);
|
|
23
|
-
const s = parseInt(match[3]);
|
|
24
|
-
if (h > 0) return `${h}:${String(m).padStart(2, '0')}:${String(s).padStart(2, '0')}`;
|
|
25
|
-
return `${m}:${String(s).padStart(2, '0')}`;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
_extractMediaDefinitions(s) {
|
|
29
|
-
const start = s.indexOf('mediaDefinitions');
|
|
30
|
-
if (start === -1) return null;
|
|
31
|
-
const arrStart = s.indexOf('[', start);
|
|
32
|
-
if (arrStart === -1) return null;
|
|
33
|
-
let depth = 0, end = -1;
|
|
34
|
-
for (let i = arrStart; i < s.length; i++) {
|
|
35
|
-
if (s[i] === '[') depth++;
|
|
36
|
-
else if (s[i] === ']') { depth--; if (depth === 0) { end = i; break; } }
|
|
37
|
-
}
|
|
38
|
-
try { return JSON.parse(s.slice(arrStart, end + 1).replace(/\\\//g, '/')); }
|
|
39
|
-
catch { return null; }
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// --- Updated Search Function ---
|
|
43
|
-
async search(query, limit = 10) {
|
|
44
|
-
try {
|
|
45
|
-
const { data } = await axios.get(`${this.baseUrl}/video/search?search=${encodeURIComponent(query)}`, {
|
|
46
|
-
headers: {
|
|
47
|
-
'User-Agent': UA,
|
|
48
|
-
'Accept-Language': 'en-US,en;q=0.9'
|
|
49
|
-
},
|
|
50
|
-
timeout: 12000
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
const $ = cheerio.load(data);
|
|
54
|
-
const results = [];
|
|
55
|
-
|
|
56
|
-
$('li[data-video-vkey]').each((_, el) => {
|
|
57
|
-
if (results.length >= limit) return false;
|
|
58
|
-
|
|
59
|
-
const anchor = $(el).find('a.imageLink').first();
|
|
60
|
-
const img = $(el).find('img.videoThumb').first();
|
|
61
|
-
|
|
62
|
-
const href = anchor.attr('href') || '';
|
|
63
|
-
const title = $(el).find('.title a').first().text().trim();
|
|
64
|
-
const thumb = img.attr('src') || '';
|
|
65
|
-
const preview = anchor.attr('data-webm') || ''; // වීඩියෝ ප්රිවීව් එක (WebM)
|
|
66
|
-
const duration = $(el).find('.duration').first().text().trim();
|
|
67
|
-
const vkey = $(el).attr('data-video-vkey') || '';
|
|
68
|
-
|
|
69
|
-
if (!title || !href) return;
|
|
70
|
-
|
|
71
|
-
results.push({
|
|
72
|
-
title,
|
|
73
|
-
url: href.startsWith('http') ? href : `${this.baseUrl}${href}`,
|
|
74
|
-
thumb,
|
|
75
|
-
preview,
|
|
76
|
-
duration,
|
|
77
|
-
vkey,
|
|
78
|
-
views: $(el).find('.views var').text().trim()
|
|
79
|
-
});
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
return results;
|
|
83
|
-
} catch (e) {
|
|
84
|
-
throw new Error(`Search failed: ${e.message}`);
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
async download(urlOrVkey) {
|
|
89
|
-
const url = urlOrVkey.includes('viewkey=') ? urlOrVkey : `${this.baseUrl}/view_video.php?viewkey=${urlOrVkey}`;
|
|
90
|
-
const { data } = await axios.get(url, {
|
|
91
|
-
headers: { 'User-Agent': UA },
|
|
92
|
-
timeout: 12000
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
const $ = cheerio.load(data);
|
|
96
|
-
const scripts = $('script').map((_, el) => $(el).html()).get();
|
|
97
|
-
|
|
98
|
-
let mediaDefinitions = null;
|
|
99
|
-
for (const s of scripts) {
|
|
100
|
-
if (!s || !s.includes('mediaDefinitions')) continue;
|
|
101
|
-
mediaDefinitions = this._extractMediaDefinitions(s);
|
|
102
|
-
if (mediaDefinitions) break;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
if (!mediaDefinitions) throw new Error('No video qualities found.');
|
|
106
|
-
|
|
107
|
-
const hlss = mediaDefinitions
|
|
108
|
-
.filter(d => d.format === 'hls' && d.videoUrl && d.quality)
|
|
109
|
-
.sort((a, b) => parseInt(b.quality) - parseInt(a.quality));
|
|
110
|
-
|
|
111
|
-
const jsonLd = $('script[type="application/ld+json"]').first().html();
|
|
112
|
-
let metadata = { title: null, thumb: null, duration: null, hls: {} };
|
|
113
|
-
|
|
114
|
-
if (jsonLd) {
|
|
115
|
-
try {
|
|
116
|
-
const parsed = JSON.parse(jsonLd);
|
|
117
|
-
metadata.title = parsed.name || null;
|
|
118
|
-
metadata.thumb = parsed.thumbnailUrl || null;
|
|
119
|
-
metadata.duration = this._parseDuration(parsed.duration);
|
|
120
|
-
} catch {}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (!metadata.title) metadata.title = $('h1.title span').text().trim() || "No Title";
|
|
124
|
-
|
|
125
|
-
hlss.forEach(d => {
|
|
126
|
-
metadata.hls[`${d.quality}p`] = d.videoUrl;
|
|
127
|
-
});
|
|
128
|
-
|
|
129
|
-
return metadata;
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
async downloadBuffer(url, quality = '720') {
|
|
133
|
-
const info = await this.download(url);
|
|
134
|
-
const hlsUrl = info.hls[`${quality}p`] || Object.values(info.hls)[0];
|
|
135
|
-
|
|
136
|
-
const tmpDir = await mkdtemp(join(tmpdir(), 'phdl-'));
|
|
137
|
-
const outPath = join(tmpDir, 'video.mp4');
|
|
138
|
-
|
|
139
|
-
try {
|
|
140
|
-
await execAsync(
|
|
141
|
-
`ffmpeg -v quiet -y -user_agent "${UA}" -headers "Referer: https://www.pornhub.com/\r\n" -i "${hlsUrl}" -t 300 -c copy -bsf:a aac_adtstoasc "${outPath}"`,
|
|
142
|
-
{ timeout: 120000 }
|
|
143
|
-
);
|
|
144
|
-
const buffer = await readFile(outPath);
|
|
145
|
-
return { title: info.title, buffer };
|
|
146
|
-
} finally {
|
|
147
|
-
await rm(tmpDir, { recursive: true, force: true });
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
|
|
152
|
-
module.exports = new PornHub();
|
|
153
|
-
|
|
1
|
+
function a0_0x5186(_0x49ac84,_0x449b5e){_0x49ac84=_0x49ac84-0x76;const _0x2f6374=a0_0x2f63();let _0x518610=_0x2f6374[_0x49ac84];return _0x518610;}const a0_0x4e9e6e=a0_0x5186;function a0_0x2f63(){const _0x353308=['480p','ffmpeg\x20-v\x20quiet\x20-y\x20-user_agent\x20\x22','keys','/view_video.php?viewkey=','3yVSsnj','replace','duration','a.imageLink','5725786ZlXqxm','baseUrl','path','mediaDefinitions','trim','Mozilla/5.0\x20(Linux;\x20Android\x2011;\x20Redmi\x20Note\x208)\x20AppleWebKit/537.36\x20(KHTML,\x20like\x20Gecko)\x20Chrome/120.0.0.0\x20Mobile\x20Safari/537.36','map','href','thumbnailUrl','658842leaHWx','360p','uploadDate','2173703AWJykb','find','7147lVBbxK','attr','videoUrl','download','padStart','get','818016VrkxfR','No\x20se\x20encontró\x20stream\x20HLS.','h1.title\x20span','child_process','3008570vFuHKR','includes','_parseDuration','first','.title\x20a','html','format','promises','10brXxzy','downloadBuffer','data-video-vkey','hls','15224rigAty','title','axios','cheerio','No\x20se\x20encontraron\x20calidades\x20de\x20video.','script','.views\x20var','https://www.pornhub.com','length','en-US,en;q=0.9','Search\x20failed:\x20','filter','data-webm','each','64IfRImF','push','http','name','startsWith','slice','indexOf','load','27zHoOKf','parse','quality','12KAAtbm','No\x20Title','exports','text','_extractMediaDefinitions','src'];a0_0x2f63=function(){return _0x353308;};return a0_0x2f63();}(function(_0xf3f282,_0x56fae4){const _0xccbfd5=a0_0x5186,_0x4ff6ef=_0xf3f282();while(!![]){try{const _0x3dd3bf=parseInt(_0xccbfd5(0x88))/0x1*(parseInt(_0xccbfd5(0xac))/0x2)+-parseInt(_0xccbfd5(0x76))/0x3*(-parseInt(_0xccbfd5(0x9e))/0x4)+-parseInt(_0xccbfd5(0x9a))/0x5*(parseInt(_0xccbfd5(0x83))/0x6)+-parseInt(_0xccbfd5(0x86))/0x7+-parseInt(_0xccbfd5(0x8e))/0x8*(parseInt(_0xccbfd5(0xb4))/0x9)+parseInt(_0xccbfd5(0x92))/0xa+-parseInt(_0xccbfd5(0x7a))/0xb*(-parseInt(_0xccbfd5(0xb7))/0xc);if(_0x3dd3bf===_0x56fae4)break;else _0x4ff6ef['push'](_0x4ff6ef['shift']());}catch(_0x52c0aa){_0x4ff6ef['push'](_0x4ff6ef['shift']());}}}(a0_0x2f63,0x34fa2));const axios=require(a0_0x4e9e6e(0xa0)),cheerio=require(a0_0x4e9e6e(0xa1)),{exec}=require(a0_0x4e9e6e(0x91)),{promisify}=require('util'),{mkdtemp,rm,readFile}=require('fs')[a0_0x4e9e6e(0x99)],{join}=require(a0_0x4e9e6e(0x7c)),{tmpdir}=require('os'),execAsync=promisify(exec),UA=a0_0x4e9e6e(0x7f);class PornHub{constructor(){const _0x39c71b=a0_0x4e9e6e;this[_0x39c71b(0x7b)]=_0x39c71b(0xa5);}[a0_0x4e9e6e(0x94)](_0x3ae118){const _0x846486=a0_0x4e9e6e;if(!_0x3ae118)return null;const _0x477ffc=_0x3ae118['match'](/PT(\d+)H(\d+)M(\d+)S/);if(!_0x477ffc)return _0x3ae118;const _0x23c7c8=parseInt(_0x477ffc[0x1]),_0x3598c6=parseInt(_0x477ffc[0x2]),_0x506141=parseInt(_0x477ffc[0x3]);if(_0x23c7c8>0x0)return _0x23c7c8+':'+String(_0x3598c6)[_0x846486(0x8c)](0x2,'0')+':'+String(_0x506141)['padStart'](0x2,'0');return _0x3598c6+':'+String(_0x506141)['padStart'](0x2,'0');}[a0_0x4e9e6e(0xbb)](_0x383c28){const _0x41efab=a0_0x4e9e6e,_0x28aa14=_0x383c28[_0x41efab(0xb2)](_0x41efab(0x7d));if(_0x28aa14===-0x1)return null;const _0x3ce4e2=_0x383c28[_0x41efab(0xb2)]('[',_0x28aa14);if(_0x3ce4e2===-0x1)return null;let _0x6e8327=0x0,_0x49f410=-0x1;for(let _0x48f7a6=_0x3ce4e2;_0x48f7a6<_0x383c28['length'];_0x48f7a6++){if(_0x383c28[_0x48f7a6]==='[')_0x6e8327++;else{if(_0x383c28[_0x48f7a6]===']'){_0x6e8327--;if(_0x6e8327===0x0){_0x49f410=_0x48f7a6;break;}}}}try{return JSON[_0x41efab(0xb5)](_0x383c28[_0x41efab(0xb1)](_0x3ce4e2,_0x49f410+0x1)[_0x41efab(0x77)](/\\\//g,'/'));}catch{return null;}}async['search'](_0x43364e,_0xd4cb41=0xa){const _0x41375f=a0_0x4e9e6e;try{const {data:_0x17ba32}=await axios['get'](this[_0x41375f(0x7b)]+'/video/search?search='+encodeURIComponent(_0x43364e),{'headers':{'User-Agent':UA,'Accept-Language':_0x41375f(0xa7)},'timeout':0x2ee0}),_0x34fc42=cheerio[_0x41375f(0xb3)](_0x17ba32),_0x21275d=[];return _0x34fc42('li[data-video-vkey]')[_0x41375f(0xab)]((_0x233f33,_0x4caa71)=>{const _0x2580f2=_0x41375f;if(_0x21275d[_0x2580f2(0xa6)]>=_0xd4cb41)return![];const _0x29f47e=_0x34fc42(_0x4caa71)['find'](_0x2580f2(0x79))['first'](),_0x2d1148=_0x34fc42(_0x4caa71)['find']('img.videoThumb')[_0x2580f2(0x95)](),_0x20db03=_0x29f47e[_0x2580f2(0x89)](_0x2580f2(0x81))||'',_0x5d8a1a=_0x34fc42(_0x4caa71)[_0x2580f2(0x87)](_0x2580f2(0x96))['first']()[_0x2580f2(0xba)]()[_0x2580f2(0x7e)]();if(!_0x5d8a1a||!_0x20db03)return;_0x21275d[_0x2580f2(0xad)]({'title':_0x5d8a1a,'url':_0x20db03[_0x2580f2(0xb0)](_0x2580f2(0xae))?_0x20db03:''+this[_0x2580f2(0x7b)]+_0x20db03,'thumb':_0x2d1148[_0x2580f2(0x89)](_0x2580f2(0xbc))||'','preview':_0x29f47e[_0x2580f2(0x89)](_0x2580f2(0xaa))||'','duration':_0x34fc42(_0x4caa71)[_0x2580f2(0x87)]('.duration')[_0x2580f2(0x95)]()[_0x2580f2(0xba)]()[_0x2580f2(0x7e)](),'vkey':_0x34fc42(_0x4caa71)[_0x2580f2(0x89)](_0x2580f2(0x9c))||'','views':_0x34fc42(_0x4caa71)['find'](_0x2580f2(0xa4))[_0x2580f2(0xba)]()[_0x2580f2(0x7e)]()});}),_0x21275d;}catch(_0x3af732){throw new Error(_0x41375f(0xa8)+_0x3af732['message']);}}async[a0_0x4e9e6e(0x8b)](_0x388efe){const _0x1cda6f=a0_0x4e9e6e,_0x5a08cc=_0x388efe[_0x1cda6f(0x93)](_0x1cda6f(0xae))?_0x388efe:this['baseUrl']+_0x1cda6f(0xc0)+_0x388efe,{data:_0x4050dd}=await axios[_0x1cda6f(0x8d)](_0x5a08cc,{'headers':{'User-Agent':UA,'Accept-Language':_0x1cda6f(0xa7)},'timeout':0x2ee0}),_0x246475=cheerio[_0x1cda6f(0xb3)](_0x4050dd),_0x1f5894=_0x246475(_0x1cda6f(0xa3))[_0x1cda6f(0x80)]((_0x3ee1c7,_0xce05da)=>_0x246475(_0xce05da)[_0x1cda6f(0x97)]())['get']();let _0x289451=null;for(const _0x4c19c5 of _0x1f5894){if(!_0x4c19c5||!_0x4c19c5[_0x1cda6f(0x93)](_0x1cda6f(0x7d)))continue;_0x289451=this['_extractMediaDefinitions'](_0x4c19c5);if(_0x289451)break;}if(!_0x289451)throw new Error(_0x1cda6f(0xa2));const _0x1270b4=_0x289451[_0x1cda6f(0xa9)](_0x7fed5c=>_0x7fed5c[_0x1cda6f(0x98)]===_0x1cda6f(0x9d)&&_0x7fed5c[_0x1cda6f(0x8a)]&&_0x7fed5c[_0x1cda6f(0xb6)])['sort']((_0x4796ca,_0x172ea7)=>parseInt(_0x172ea7[_0x1cda6f(0xb6)])-parseInt(_0x4796ca[_0x1cda6f(0xb6)]));if(!_0x1270b4['length'])throw new Error(_0x1cda6f(0x8f));const _0x4d9b0f=_0x246475('script[type=\x22application/ld+json\x22]')[_0x1cda6f(0x95)]()[_0x1cda6f(0x97)]();let _0x3a133b=null,_0x1fa1ca=null,_0xf1736f=null,_0x6e45a=null;if(_0x4d9b0f)try{const _0x6e591d=JSON[_0x1cda6f(0xb5)](_0x4d9b0f);_0x3a133b=_0x6e591d[_0x1cda6f(0xaf)]||null,_0x1fa1ca=_0x6e591d[_0x1cda6f(0x82)]||null,_0xf1736f=this['_parseDuration'](_0x6e591d[_0x1cda6f(0x78)]),_0x6e45a=_0x6e591d[_0x1cda6f(0x85)]?.[_0x1cda6f(0xb1)](0x0,0xa)||null;}catch{}if(!_0x3a133b)_0x3a133b=_0x246475(_0x1cda6f(0x90))[_0x1cda6f(0xba)]()[_0x1cda6f(0x7e)]()||_0x246475('h1')[_0x1cda6f(0x95)]()[_0x1cda6f(0xba)]()[_0x1cda6f(0x7e)]()||_0x1cda6f(0xb8);const _0x56fd99={};for(const _0x357062 of _0x1270b4){_0x56fd99[_0x357062['quality']+'p']={'quality':_0x357062[_0x1cda6f(0xb6)]+'p','format':_0x1cda6f(0x9d),'url':_0x357062['videoUrl']};}return{'title':_0x3a133b,'thumb':_0x1fa1ca,'duration':_0xf1736f,'uploadDate':_0x6e45a,'hls':_0x56fd99};}async[a0_0x4e9e6e(0x9b)](_0x4da120,_0x1ada80='720'){const _0x5232f5=a0_0x4e9e6e,_0x2a8083=await this[_0x5232f5(0x8b)](_0x4da120),_0x499743=Object[_0x5232f5(0xbf)](_0x2a8083['hls']),_0x30bb14=_0x2a8083['hls'][_0x1ada80+'p']||_0x2a8083[_0x5232f5(0x9d)][_0x5232f5(0xbd)]||_0x2a8083[_0x5232f5(0x9d)][_0x5232f5(0x84)]||_0x2a8083['hls'][_0x499743[0x0]],_0x1b8689=await mkdtemp(join(tmpdir(),'phdl-')),_0x44ea8a=join(_0x1b8689,'video.mp4');try{await execAsync(_0x5232f5(0xbe)+UA+'\x22\x20-headers\x20\x22Referer:\x20https://www.pornhub.com/\x0d\x0a\x22\x20-i\x20\x22'+_0x30bb14['url']+'\x22\x20-t\x20300\x20-c\x20copy\x20-bsf:a\x20aac_adtstoasc\x20\x22'+_0x44ea8a+'\x22',{'timeout':0x1d4c0});const _0x2bb67e=await readFile(_0x44ea8a);return{'title':_0x2a8083[_0x5232f5(0x9f)],'thumb':_0x2a8083['thumb'],'duration':_0x2a8083[_0x5232f5(0x78)],'uploadDate':_0x2a8083[_0x5232f5(0x85)],'buffer':_0x2bb67e,'quality':_0x30bb14[_0x5232f5(0xb6)]};}finally{await rm(_0x1b8689,{'recursive':!![],'force':!![]});}}}module[a0_0x4e9e6e(0xb9)]=new PornHub();
|