ph-scraper-api 1.0.0 → 1.0.1

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.
Files changed (2) hide show
  1. package/index.js +153 -1
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -1 +1,153 @@
1
- function a0_0x1a2d(){const _0x4a234d=['find','sort','each','_extractMediaDefinitions','script[type=\x22application/ld+json\x22]','match','html','thumbnailUrl','child_process','phdl-','hls','11160lnbbqX','message','replace','1084076OqrSsa','text','filter','attr','6670899UTUTFk','first','li[data-video-vkey]','videoUrl','data-video-vkey','ffmpeg\x20-v\x20quiet\x20-y\x20-user_agent\x20\x22','mediaDefinitions','/video/search?search=','444pumfXS','img','quality','baseUrl','No\x20video\x20qualities\x20found.','316Xzebjx','2607336xgnQDX','duration','_parseDuration','indexOf','format','title','get','trim','720','h1.title\x20span','parse','thumb','/view_video.php?viewkey=','forEach','.duration','.views\x20var','values','Search\x20failed:\x20','79568wzlqSn','downloadBuffer','download','padStart','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','promises','\x22\x20-t\x20300\x20-c\x20copy\x20-bsf:a\x20aac_adtstoasc\x20\x22','exports','https://www.pornhub.com','.title\x20a','length','includes','553TwyfGZ','No\x20Title','push','search','5333120SkgReY','15605PuUQrv','map'];a0_0x1a2d=function(){return _0x4a234d;};return a0_0x1a2d();}const a0_0x31751d=a0_0x1cbe;(function(_0x3a0010,_0x24bfa){const _0x9f2360=a0_0x1cbe,_0x1d66ae=_0x3a0010();while(!![]){try{const _0x5527de=-parseInt(_0x9f2360(0x1f0))/0x1+-parseInt(_0x9f2360(0x1ed))/0x2*(-parseInt(_0x9f2360(0x1fc))/0x3)+-parseInt(_0x9f2360(0x201))/0x4*(-parseInt(_0x9f2360(0x225))/0x5)+-parseInt(_0x9f2360(0x202))/0x6+-parseInt(_0x9f2360(0x220))/0x7*(-parseInt(_0x9f2360(0x214))/0x8)+parseInt(_0x9f2360(0x1f4))/0x9+-parseInt(_0x9f2360(0x224))/0xa;if(_0x5527de===_0x24bfa)break;else _0x1d66ae['push'](_0x1d66ae['shift']());}catch(_0x33d86b){_0x1d66ae['push'](_0x1d66ae['shift']());}}}(a0_0x1a2d,0x85a48));function a0_0x1cbe(_0x57809d,_0x17e54e){_0x57809d=_0x57809d-0x1e6;const _0x1a2dfa=a0_0x1a2d();let _0x1cbef0=_0x1a2dfa[_0x57809d];return _0x1cbef0;}const axios=require('axios'),cheerio=require('cheerio'),{exec}=require(a0_0x31751d(0x1ea)),{promisify}=require('util'),{mkdtemp,rm,readFile}=require('fs')[a0_0x31751d(0x219)],{join}=require('path'),{tmpdir}=require('os'),execAsync=promisify(exec),UA=a0_0x31751d(0x218);class PornHub{constructor(){const _0x2e50c4=a0_0x31751d;this[_0x2e50c4(0x1ff)]=_0x2e50c4(0x21c);}['_parseDuration'](_0x41cce3){const _0x4f7509=a0_0x31751d;if(!_0x41cce3)return null;const _0x4c2d92=_0x41cce3[_0x4f7509(0x1e7)](/PT(\d+)H(\d+)M(\d+)S/);if(!_0x4c2d92)return _0x41cce3;const _0x430c9d=parseInt(_0x4c2d92[0x1]),_0x3fba77=parseInt(_0x4c2d92[0x2]),_0x29097a=parseInt(_0x4c2d92[0x3]);if(_0x430c9d>0x0)return _0x430c9d+':'+String(_0x3fba77)[_0x4f7509(0x217)](0x2,'0')+':'+String(_0x29097a)['padStart'](0x2,'0');return _0x3fba77+':'+String(_0x29097a)[_0x4f7509(0x217)](0x2,'0');}[a0_0x31751d(0x22a)](_0x2a3786){const _0x44ec06=a0_0x31751d,_0x22a544=_0x2a3786[_0x44ec06(0x205)](_0x44ec06(0x1fa));if(_0x22a544===-0x1)return null;const _0x14c786=_0x2a3786[_0x44ec06(0x205)]('[',_0x22a544);if(_0x14c786===-0x1)return null;let _0x115e1e=0x0,_0x48012f=-0x1;for(let _0xc70f2c=_0x14c786;_0xc70f2c<_0x2a3786[_0x44ec06(0x21e)];_0xc70f2c++){if(_0x2a3786[_0xc70f2c]==='[')_0x115e1e++;else{if(_0x2a3786[_0xc70f2c]===']'){_0x115e1e--;if(_0x115e1e===0x0){_0x48012f=_0xc70f2c;break;}}}}try{return JSON[_0x44ec06(0x20c)](_0x2a3786['slice'](_0x14c786,_0x48012f+0x1)[_0x44ec06(0x1ef)](/\\\//g,'/'));}catch{return null;}}async[a0_0x31751d(0x223)](_0x5c8ab5,_0x3c6f26=0xa){const _0x374cdf=a0_0x31751d;try{const {data:_0x4ebb65}=await axios[_0x374cdf(0x208)](this['baseUrl']+_0x374cdf(0x1fb)+encodeURIComponent(_0x5c8ab5),{'headers':{'User-Agent':UA},'timeout':0x2ee0}),_0x46070d=cheerio['load'](_0x4ebb65),_0x3d1b05=[];return _0x46070d(_0x374cdf(0x1f6))[_0x374cdf(0x229)]((_0x49c19c,_0x5f262b)=>{const _0x5dcfea=_0x374cdf;if(_0x3d1b05[_0x5dcfea(0x21e)]>=_0x3c6f26)return![];const _0x51aa46=_0x46070d(_0x5f262b)[_0x5dcfea(0x1f3)](_0x5dcfea(0x1f8)),_0xce57e7=_0x46070d(_0x5f262b)[_0x5dcfea(0x227)](_0x5dcfea(0x21d))[_0x5dcfea(0x1f5)]()['text']()['trim']();_0x51aa46&&_0xce57e7&&_0x3d1b05[_0x5dcfea(0x222)]({'title':_0xce57e7,'vkey':_0x51aa46,'duration':_0x46070d(_0x5f262b)[_0x5dcfea(0x227)](_0x5dcfea(0x210))[_0x5dcfea(0x1f1)]()[_0x5dcfea(0x209)](),'views':_0x46070d(_0x5f262b)['find'](_0x5dcfea(0x211))[_0x5dcfea(0x1f1)]()[_0x5dcfea(0x209)](),'url':this[_0x5dcfea(0x1ff)]+_0x5dcfea(0x20e)+_0x51aa46,'thumb':_0x46070d(_0x5f262b)[_0x5dcfea(0x227)](_0x5dcfea(0x1fd))[_0x5dcfea(0x1f3)]('src')});}),_0x3d1b05;}catch(_0x3d7d2d){throw new Error(_0x374cdf(0x213)+_0x3d7d2d[_0x374cdf(0x1ee)]);}}async[a0_0x31751d(0x216)](_0x5041ae){const _0xbb11de=a0_0x31751d,_0x4abefc=_0x5041ae[_0xbb11de(0x21f)]('viewkey=')?_0x5041ae:this[_0xbb11de(0x1ff)]+_0xbb11de(0x20e)+_0x5041ae,{data:_0x4d58e4}=await axios['get'](_0x4abefc,{'headers':{'User-Agent':UA},'timeout':0x2ee0}),_0x2ae564=cheerio['load'](_0x4d58e4),_0x5c69d9=_0x2ae564('script')[_0xbb11de(0x226)]((_0x3812ee,_0x1835d1)=>_0x2ae564(_0x1835d1)[_0xbb11de(0x1e8)]())['get']();let _0x181db7=null;for(const _0x4b463d of _0x5c69d9){if(!_0x4b463d||!_0x4b463d[_0xbb11de(0x21f)](_0xbb11de(0x1fa)))continue;_0x181db7=this['_extractMediaDefinitions'](_0x4b463d);if(_0x181db7)break;}if(!_0x181db7)throw new Error(_0xbb11de(0x200));const _0x7e383c=_0x181db7[_0xbb11de(0x1f2)](_0x1d251a=>_0x1d251a[_0xbb11de(0x206)]===_0xbb11de(0x1ec)&&_0x1d251a[_0xbb11de(0x1f7)]&&_0x1d251a[_0xbb11de(0x1fe)])[_0xbb11de(0x228)]((_0x5de5b4,_0x119696)=>parseInt(_0x119696['quality'])-parseInt(_0x5de5b4[_0xbb11de(0x1fe)])),_0x42222c=_0x2ae564(_0xbb11de(0x1e6))[_0xbb11de(0x1f5)]()['html']();let _0x596eb8={'title':null,'thumb':null,'duration':null,'hls':{}};if(_0x42222c)try{const _0x17c4c4=JSON[_0xbb11de(0x20c)](_0x42222c);_0x596eb8[_0xbb11de(0x207)]=_0x17c4c4['name']||null,_0x596eb8[_0xbb11de(0x20d)]=_0x17c4c4[_0xbb11de(0x1e9)]||null,_0x596eb8['duration']=this[_0xbb11de(0x204)](_0x17c4c4[_0xbb11de(0x203)]);}catch{}if(!_0x596eb8[_0xbb11de(0x207)])_0x596eb8['title']=_0x2ae564(_0xbb11de(0x20b))[_0xbb11de(0x1f1)]()['trim']()||_0xbb11de(0x221);return _0x7e383c[_0xbb11de(0x20f)](_0xa9fb45=>{const _0x2007ab=_0xbb11de;_0x596eb8[_0x2007ab(0x1ec)][_0xa9fb45['quality']+'p']=_0xa9fb45[_0x2007ab(0x1f7)];}),_0x596eb8;}async[a0_0x31751d(0x215)](_0x1b9ae6,_0x16371d=a0_0x31751d(0x20a)){const _0x4b7406=a0_0x31751d,_0x29fddb=await this[_0x4b7406(0x216)](_0x1b9ae6),_0x308fcf=_0x29fddb[_0x4b7406(0x1ec)][_0x16371d+'p']||Object[_0x4b7406(0x212)](_0x29fddb[_0x4b7406(0x1ec)])[0x0],_0x230871=await mkdtemp(join(tmpdir(),_0x4b7406(0x1eb))),_0x22ab13=join(_0x230871,'video.mp4');try{await execAsync(_0x4b7406(0x1f9)+UA+'\x22\x20-headers\x20\x22Referer:\x20https://www.pornhub.com/\x0d\x0a\x22\x20-i\x20\x22'+_0x308fcf+_0x4b7406(0x21a)+_0x22ab13+'\x22',{'timeout':0x1d4c0});const _0x3b797f=await readFile(_0x22ab13);return{'title':_0x29fddb[_0x4b7406(0x207)],'buffer':_0x3b797f};}finally{await rm(_0x230871,{'recursive':!![],'force':!![]});}}}module[a0_0x31751d(0x21b)]=new PornHub();
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
+
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ph-scraper-api",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "A lightweight Node.js scraper for searching and extracting HLS download links from PornHub.",
5
5
  "main": "index.js",
6
6
  "scripts": {