Akane710 commited on
Commit
8aa50f6
·
verified ·
1 Parent(s): 97458da

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +51 -20
server.js CHANGED
@@ -1,30 +1,61 @@
1
- 'use strict';
2
 
3
- import express from 'express';
4
- import Lens from 'chrome-lens-ocr';
 
 
5
 
6
- // Constants
7
- const PORT = 7860;
8
- const HOST = '0.0.0.0';
9
-
10
- // App
11
  const app = express();
12
- app.get('/', (req, res) => {
13
- res.send('Hello World from ExpressJS! This example is from the NodeJS Docs: https://nodejs.org/en/docs/guides/nodejs-docker-webapp/');
14
- });
15
- const lens = new Lens();
 
 
 
 
 
 
 
 
16
 
17
- app.get('/scanByUrl', async (req, res) => {
18
- const { url } = req.query;
19
  try {
20
- const data = await lens.scanByURL(url);
21
- const combinedText = data.segments.map(segment => segment.text).join('\n\n');
22
- res.json({ combinedText, detailedData: data });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  } catch (error) {
24
- res.status(500).json({ error: error.message });
 
25
  }
26
  });
27
 
28
- app.listen(PORT, HOST, () => {
29
- console.log(`Running on http://${HOST}:${PORT}`);
 
30
  });
 
1
+ // File: app.js
2
 
3
+ import express from "express";
4
+ import bodyParser from "body-parser";
5
+ import ytdl from "@distube/ytdl-core";
6
+ import yts from "youtube-yts";
7
 
 
 
 
 
 
8
  const app = express();
9
+ const PORT = 3000;
10
+
11
+ // Middleware
12
+ app.use(bodyParser.json());
13
+
14
+ // POST endpoint for downloading YouTube videos
15
+ app.get("/download", async (req, res) => {
16
+ const { url, query } = req.body;
17
+
18
+ if (!url && !query) {
19
+ return res.status(400).json({ error: "Either 'url' or 'query' must be provided." });
20
+ }
21
 
 
 
22
  try {
23
+ let videoInfo;
24
+ if (url) {
25
+ // Fetch video info using the provided URL
26
+ videoInfo = await ytdl.getInfo(url, {
27
+ playerClients: ["IOS", "WEB_CREATOR", "ANDROID", "WEB"],
28
+ });
29
+ } else {
30
+ // Search YouTube and get the first result
31
+ const searchResults = await yts(query);
32
+ if (!searchResults.videos || searchResults.videos.length === 0) {
33
+ return res.status(404).json({ error: "No videos found for the query." });
34
+ }
35
+ const firstResult = searchResults.videos[0];
36
+ videoInfo = await ytdl.getInfo(firstResult.url, {
37
+ playerClients: ["IOS", "WEB_CREATOR", "ANDROID", "WEB"],
38
+ });
39
+ }
40
+
41
+ // Get the best format with video and audio
42
+ const format = ytdl.filterFormats(videoInfo.formats, "videoandaudio")[0];
43
+ if (!format) {
44
+ return res.status(500).json({ error: "No suitable format found." });
45
+ }
46
+
47
+ // Respond with video URL and details
48
+ res.json({
49
+ title: videoInfo.videoDetails.title,
50
+ videoUrl: format.url,
51
+ });
52
  } catch (error) {
53
+ console.error(error);
54
+ res.status(500).json({ error: "Failed to process the request." });
55
  }
56
  });
57
 
58
+ // Start the server
59
+ app.listen(PORT, () => {
60
+ console.log(`Server is running on http://localhost:${PORT}`);
61
  });