R-Kentaren commited on
Commit
de80f03
·
verified ·
1 Parent(s): 73f1cb7

Create pornhub.js

Browse files
Files changed (1) hide show
  1. nsfw/pornhub.js +68 -0
nsfw/pornhub.js ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ const { PornHub } = require('pornhub.js');
2
+ const fs = require('fs');
3
+ const { exec } = require('child_process');
4
+
5
+ class Ph {
6
+ constructor() {
7
+ this.client = new PornHub();
8
+ }
9
+
10
+ search = async function (query) {
11
+ try {
12
+ const results = await this.client.searchVideo(query);
13
+ if (!results.data || results.data.length === 0) return [];
14
+
15
+ return results.data.sort(() => Math.random() - 0.5);
16
+ } catch (error) {
17
+ throw new Error(error.message);
18
+ }
19
+ }
20
+
21
+ download = async function (url) {
22
+ return new Promise(async (resolve, reject) => {
23
+ try {
24
+ const videoData = await this.client.video(url);
25
+ const resolution = videoData.mediaDefinitions.find((f) => f.format === 'hls' && (f.quality === 480 || f.quality === 720));
26
+
27
+ if (!resolution) return reject(new Error('Video resolution not found'));
28
+
29
+ const m3u8Url = resolution.videoUrl;
30
+ const outputPath = `./video_${Date.now()}.mp4`;
31
+
32
+ exec(`ffmpeg -i "${m3u8Url}" -c copy -bsf:a aac_adtstoasc "${outputPath}"`, (error, stdout, stderr) => {
33
+ if (error) {
34
+ console.error(`FFmpeg Error: ${error.message}`);
35
+ return reject(error);
36
+ }
37
+
38
+ console.log(`FFmpeg Output: ${stdout || stderr}`);
39
+
40
+ fs.readFile(outputPath, async (err, buffer) => {
41
+ if (err) return reject(err);
42
+
43
+ fs.unlink(outputPath, (unlinkErr) => {
44
+ if (unlinkErr) console.error(`Failed to delete file: ${unlinkErr.message}`);
45
+ });
46
+
47
+ resolve({
48
+ title: videoData.title,
49
+ duration: videoData.durationFormatted,
50
+ rating: videoData.vote.rating,
51
+ uploader: videoData.provider.username,
52
+ tags: videoData.tags,
53
+ category: videoData.categories,
54
+ videoBuffer: buffer
55
+ });
56
+ });
57
+ });
58
+ } catch (error) {
59
+ throw new Error(error.message);
60
+ }
61
+ });
62
+ }
63
+ }
64
+
65
+ // Usage:
66
+ const ph = new Ph();
67
+ const resp = await ph.search('girl');
68
+ console.log(resp);