为方便自己看网络小说,自己写个txt按章节分段的小程序

为方便自己看网络小说,自己写个txt按章节分段的小程序
2011年08月08日
  Const ForReading = 1, ForWriting = 2
  Dim f, m
  If ReportFileStatus(FileName) = 1 then
  Set f = objFSO.OpenTextFile(FileName, ForReading)
  While Not f.AtEndOfStream
  m = m & RemoveHTML(f.ReadLine) & ""
  Wend
  ReadTxtFile = m
  f.Close
  Else
  ReadTxtFile = -1
  End if
  End Function
  '写文本文件
  Public Function WriteTxtFile(FileName,TextStr,WriteORAppendType)
  Const ForReading = 1, ForWriting = 2 , ForAppending = 8
  Dim f, m
  select Case WriteORAppendType
  Case 1: '文件进行写操作
  Set f = objFSO.OpenTextFile(FileName, ForWriting, True)
  f.Write TextStr
  f.Close
  If ReportFileStatus(FileName) = 1 then
  WriteTxtFile = 1
  Else
  WriteTxtFile = -1
  End if
  Case 2: '文件末尾进行写操作
  If ReportFileStatus(FileName) = 1 then
  Set f = objFSO.OpenTextFile(FileName, ForAppending ,1)
  f.Write TextStr
  f.Close
  WriteTxtFile = 1
  Else
  WriteTxtFile = -1
  End if
  End select
  End Function
  '判断目录是否存在
  Public Function ReportFolderStatus(fldr)
  Dim msg
  msg = -1
  If (objFSO.FolderExists(fldr)) Then
  msg = 1
  Else
  msg = -1
  End If
  ReportFolderStatus = msg
  End Function
  '创建的文件夹
  Public Function CreateFolderDemo(FolderName)
  Dim f
  If ReportFolderStatus(FolderName) = 1 Then
  CreateFolderDemo = -1
  Else
  Set f = objFSO.CreateFolder(FolderName)
  CreateFolderDemo = 1
  End if
  End Function
  '文件是否存在?
  Public Function ReportFileStatus(FileName)
  Dim msg
  msg = -1
  If (objFSO.FileExists(FileName)) Then
  msg = 1
  Else
  msg = -1
  End If
  ReportFileStatus = msg
  End Function
  '按章节分段
  Function CutHao(str)
  Dim sRegExp, Match, Matches
  Set sRegExp = New RegExp
  sRegExp.IgnoreCase = True
  sRegExp.Pattern = "第[一二两三四五六七八九十○零百0-91234567890]{1,12}章"
  set Matches = sRegExp.Execute(str)
  if Matches.count then
  For Each Match in Matches
  i = i + 1
  Next
  end if
  CutHao=str
  Set sRegExp = Nothing
  End Function
  'HTML编码过滤
  Function RemoveHTML(strHTML)
  Dim objRegExp, Match, Matches
  Set objRegExp = New Regexp
  objRegExp.IgnoreCase = True
  objRegExp.Global = True
  '取闭合的
  objRegExp.Pattern = ""
  '进行匹配
  Set Matches = objRegExp.Execute(strHTML)
  ' 遍历匹配集合,并替换掉匹配的项目
  For Each Match in Matches
  strHtml=Replace(strHTML,Match.Value,"")
  strHtml=Replace(strHTML," ","")
  Next
  RemoveHTML=strHTML
  Set objRegExp = Nothing
  End Function
  %>[b][/b]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现思路: 1. 在服务器上将完整视频文件分成多个小视频文件,并将每个小视频文件的 URL 存储到数据库中。 2. 在微信小程序中,使用 wx.request() 方法向服务器请求视频分段的 URL。 3. 使用 wx.createVideoContext() 方法创建视频上下文对象,并使用它的 src 属性将视频分段的 URL 赋值给它。 4. 在用户点击播放按钮时,调用视频上下文对象的 play() 方法播放视频。 示例代码: 1. 服务器端代码(Node.js): ```javascript const express = require('express'); const app = express(); const fs = require('fs'); const path = require('path'); const bodyParser = require('body-parser'); const MongoClient = require('mongodb').MongoClient; app.use(bodyParser.json()); // 连接 MongoDB 数据库 MongoClient.connect('mongodb://localhost:27017', (err, client) => { if (err) { console.log('MongoDB 连接失败'); return; } console.log('MongoDB 连接成功'); const db = client.db('test'); // 将完整视频文件分成多个小视频文件 app.post('/video/split', (req, res) => { const filePath = req.body.filePath; const chunkSize = req.body.chunkSize; const fileName = path.basename(filePath, path.extname(filePath)); const dirName = path.dirname(filePath); const fileExt = path.extname(filePath); const fileStat = fs.statSync(filePath); const fileSize = fileStat.size; const numChunks = Math.ceil(fileSize / chunkSize); const chunks = []; for (let i = 0; i < numChunks; i++) { const start = i * chunkSize; const end = Math.min(start + chunkSize, fileSize); const chunkPath = path.join(dirName, `${fileName}.${i}${fileExt}`); const chunkStream = fs.createWriteStream(chunkPath); fs.createReadStream(filePath, { start, end }) .on('error', (err) => { console.log(err); }) .pipe(chunkStream) .on('error', (err) => { console.log(err); }); chunks.push({ url: `http://localhost:3000/video/chunk/${fileName}/${i}${fileExt}`, size: end - start }); } // 将视频分段的 URL 存储到数据库中 db.collection('videos').insertOne({ name: fileName, chunks }, (err, result) => { if (err) { console.log(err); res.status(500).send('Internal server error'); } else { res.status(200).send('OK'); } }); }); // 提供视频分段的 URL app.get('/video/chunk/:name/:index', (req, res) => { const name = req.params.name; const index = req.params.index; const chunkPath = path.join(__dirname, 'videos', `${name}.${index}`); if (fs.existsSync(chunkPath)) { res.status(200).sendFile(chunkPath); } else { res.status(404).send('Not found'); } }); app.listen(3000, () => { console.log('Server is running at http://localhost:3000'); }); }); ``` 2. 微信小程序端代码: ```javascript Page({ data: { videoChunks: [], // 视频分段的 URL currentChunkIndex: 0 // 当前播放的视频分段索引 }, onLoad() { wx.request({ url: 'http://localhost:3000/video/split', method: 'POST', data: { filePath: '/path/to/video.mp4', chunkSize: 1024 * 1024 // 1MB }, success: (res) => { if (res.statusCode === 200) { this.setData({ videoChunks: res.data.chunks }); } else { wx.showToast({ title: '视频分段失败', icon: 'none' }); } }, fail: () => { wx.showToast({ title: '服务器请求失败', icon: 'none' }); } }); }, onPlay() { const videoContext = wx.createVideoContext('video'); const currentChunk = this.data.videoChunks[this.data.currentChunkIndex]; videoContext.src = currentChunk.url; videoContext.play(); }, onEnded() { const currentChunkIndex = this.data.currentChunkIndex + 1; if (currentChunkIndex < this.data.videoChunks.length) { this.setData({ currentChunkIndex }, () => { this.onPlay(); }); } } }); ``` 注意事项: 1. 微信小程序只支持 HTTPS 协议的视频 URL,如果服务器没有配置 HTTPS,可以使用 ngrok 等工具将本地服务器的 HTTP 服务转换成 HTTPS 服务。 2. 微信小程序只支持 H.264 编码格式的视频。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值