Akane710 commited on
Commit
94d94c2
·
verified ·
1 Parent(s): a5e6f4c

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +77 -63
server.js CHANGED
@@ -1,71 +1,85 @@
1
- // File: app.js
2
 
3
- import express from "express";
4
- import bodyParser from "body-parser";
5
- import { ytmp4 } from "ruhend-scraper";
6
- import yts from "youtube-yts";
7
 
8
  const app = express();
9
- const PORT = 7860;
10
- const HOST = "0.0.0.0";
11
-
12
- // Middleware
13
- app.use(bodyParser.json());
14
-
15
- // Function to check if input is a URL
16
- const isUrl = (input) => {
17
- try {
18
- new URL(input);
19
- return true;
20
- } catch {
21
- return false;
22
- }
23
- };
24
-
25
- // GET endpoint for downloading YouTube videos
26
- app.get("/download", async (req, res) => {
27
- const { input } = req.query; // Extract single input parameter
28
-
29
- if (!input) {
30
- return res
31
- .status(400)
32
- .json({ error: "'input' query parameter must be provided." });
33
- }
34
-
35
- try {
36
- let videoData;
37
-
38
- if (isUrl(input)) {
39
- // Input is a URL
40
- videoData = await ytmp4(input);
41
- } else {
42
- // Input is a query, perform YouTube search
43
- const searchResults = await yts(input);
44
- if (!searchResults.videos || searchResults.videos.length === 0) {
45
- return res.status(404).json({ error: "No videos found for the query." });
46
- }
47
- const firstResult = searchResults.videos[0];
48
- videoData = await ytmp4(firstResult.url);
49
  }
50
 
51
- // Respond with video details
52
- const { title, video, author, description, duration, views, upload, thumbnail } = videoData;
53
- res.json({
54
- title,
55
- videoUrl: video,
56
- author,
57
- description,
58
- duration,
59
- views,
60
- uploadDate: upload,
61
- thumbnail,
62
- });
63
- } catch (error) {
64
- console.error(error);
65
- res.status(500).json({ error: "Failed to process the request." });
66
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67
  });
68
 
69
- app.listen(PORT, HOST, () => {
70
- console.log(`Running on http://${HOST}:${PORT}`);
 
71
  });
 
1
+ // File: server.js
2
 
3
+ import express from 'express';
4
+ import { CAINode } from 'cainode';
 
 
5
 
6
  const app = express();
7
+ const port = 3000;
8
+
9
+ app.use(express.json());
10
+
11
+ // API endpoint for chatting
12
+ app.get('/chat', async (req, res) => {
13
+ const token = req.query.token || '529e24b4173b29dbc3054fef02a380e1e5b41949';
14
+ const characterId = req.query.characterId || 'smtV3Vyez6ODkwS8BErmBAdgGNj-1XWU73wIFVOY1hQ';
15
+ const chatId = req.query.chatId;
16
+ const voiceId = req.query.voiceId || 'f4b33b1d-2e5a-40a8-8a07-958140cd104d';
17
+ const message = req.query.message;
18
+
19
+ if (!message) {
20
+ return res.status(400).json({ error: 'Message query parameter is required.' });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  }
22
 
23
+ let client;
24
+ try {
25
+ client = new CAINode();
26
+
27
+ // Login to the client
28
+ await client.login(token);
29
+
30
+ // Connect to the character
31
+ await client.character.connect(characterId);
32
+
33
+ let chatSessionId = chatId;
34
+
35
+ // If no chatId provided, create a new conversation
36
+ if (!chatSessionId) {
37
+ const newConversation = await client.character.create_new_conversation();
38
+ chatSessionId = newConversation[0].chat.chat_id;
39
+ }
40
+
41
+ // Set the chat session
42
+ await client.chat.set_conversation_chat(chatSessionId);
43
+
44
+ // Send message and get response
45
+ const response = await client.character.send_message(message);
46
+
47
+ let reply = '';
48
+ let voiceUrl = '';
49
+
50
+ if (response.turn?.candidates?.length > 0) {
51
+ const messageResponse = response.turn.candidates[0].raw_content;
52
+ const turnId = response.turn.turn_key.turn_id;
53
+ const candidateId = response.turn.primary_candidate_id;
54
+
55
+ reply = messageResponse;
56
+
57
+ // Generate voice reply if voiceId is provided
58
+ if (voiceId) {
59
+ const voiceResponse = await client.character.replay_tts(turnId, candidateId, voiceId);
60
+ voiceUrl = voiceResponse.replayUrl;
61
+ }
62
+
63
+ res.json({ chatId: chatSessionId, message: reply, voiceUrl });
64
+ } else {
65
+ res.status(404).json({ error: 'No response content available.' });
66
+ }
67
+
68
+ // Logout and cleanup
69
+ await client.character.disconnect();
70
+ await client.logout();
71
+
72
+ } catch (error) {
73
+ console.error('Error:', error.message);
74
+ res.status(500).json({ error: error.message });
75
+
76
+ if (client) {
77
+ await client.logout();
78
+ }
79
+ }
80
  });
81
 
82
+ // Start the server
83
+ app.listen(port, () => {
84
+ console.log(`Server running at http://localhost:${port}`);
85
  });