microsoft-visualbasic-runtime/Net/HTTP/wget.vb

149 lines
5.3 KiB
VB.net
Raw Normal View History

2020-01-02 19:08:37 +08:00
#Region "Microsoft.VisualBasic::a15da93ae4a68df8fc4475617b2f434f, Microsoft.VisualBasic.Core\Net\HTTP\wget.vb"
2019-09-06 19:20:43 +08:00
2019-11-10 15:16:27 +08:00
' Author:
'
' asuka (amethyst.asuka@gcmodeller.org)
' xie (genetics@smrucc.org)
' xieguigang (xie.guigang@live.com)
'
' Copyright (c) 2018 GPL3 Licensed
'
'
' GNU GENERAL PUBLIC LICENSE (GPL3)
'
'
' This program is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by
' the Free Software Foundation, either version 3 of the License, or
' (at your option) any later version.
'
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
'
' You should have received a copy of the GNU General Public License
' along with this program. If not, see <http://www.gnu.org/licenses/>.
2019-11-10 15:16:27 +08:00
' /********************************************************************************/
2019-11-10 15:16:27 +08:00
' Summaries:
2019-11-10 15:16:27 +08:00
' Class wget
'
' Constructor: (+1 Overloads) Sub New
'
' Function: Download, ToString
'
' Sub: ClearLine, DownloadProcess, ReportRequest, Run
'
'
' /********************************************************************************/
2019-09-06 19:20:43 +08:00
#End Region
Imports System.Net
Imports Microsoft.VisualBasic.Language
2019-09-04 20:38:12 +08:00
Namespace Net.Http
''' <summary>
''' 命令行下的下载程序组件
''' </summary>
Public Class wget
Dim WithEvents task As wgetTask
Dim cursorTop%
2019-12-05 20:44:42 +08:00
''' <summary>
''' Create a new file download task
''' </summary>
''' <param name="url">The remote resource to download.</param>
''' <param name="save">The file save location</param>
2019-09-04 20:38:12 +08:00
Sub New(url$, save$)
task = New wgetTask(url, save)
cursorTop = Console.CursorTop
End Sub
2019-12-05 20:44:42 +08:00
''' <summary>
''' Run the file download task
''' </summary>
2019-09-04 20:38:12 +08:00
Public Sub Run()
Call task.StartTask()
If Not task.isDownloading Then
Call task.Dispose()
Call Console.WriteLine()
Call Console.WriteLine($"{Now.ToString} ({StringFormats.Lanudry(task.downloadSpeed)}/s) - '{task.saveFile.FileName}' saved [{task.saveFile.FileLength}]")
2019-09-04 20:38:12 +08:00
Call Console.WriteLine()
End If
End Sub
2019-10-12 22:41:30 +08:00
Public Overrides Function ToString() As String
Return task.url
End Function
2019-09-04 20:38:12 +08:00
Private Sub DownloadProcess(wget As wgetTask, percentage As Double) Handles task.DownloadProcess
Console.CursorTop = cursorTop
ClearLine()
Console.WriteLine(wget.ToString)
End Sub
Private Sub ClearLine()
2019-09-04 20:38:12 +08:00
Console.CursorLeft = 1
Console.Write(New String(" "c, Console.BufferWidth - 1))
2019-09-04 20:38:12 +08:00
Console.CursorLeft = 1
Console.CursorTop -= 1
2019-09-04 20:38:12 +08:00
End Sub
''' <summary>
''' Do task summary
''' </summary>
''' <param name="req"></param>
''' <param name="resp"></param>
Private Sub ReportRequest(req As WebRequest, resp As WebResponse, remote$) Handles task.ReportRequest
2019-09-04 20:38:12 +08:00
Dim domain As New DomainName(task.url)
2019-10-12 22:41:30 +08:00
Dim contentSize$ = resp.ContentLength
Dim sizePrettyPrint$ = StringFormats.Lanudry(resp.ContentLength) Or "N/A".When(resp.ContentLength = -1)
If contentSize = "-1" Then
contentSize = "<Unknown Size>"
End If
2019-09-04 20:38:12 +08:00
Call Console.WriteLine()
Call ClearLine() : Console.WriteLine($"--{Now.ToString}-- {task.url}")
Call ClearLine() : Console.WriteLine($" => '{task.saveFile.FileName}'")
Call ClearLine() : Console.WriteLine()
Call ClearLine() : Console.WriteLine($"Resolving {resp.ResponseUri.Host} ({domain})... {remote}")
Call ClearLine() : Console.WriteLine($"==> METHOD ... {req.Method}/{req.RequestUri.Scheme} {DirectCast(req, HttpWebRequest).ProtocolVersion}")
2019-10-12 22:41:30 +08:00
Call ClearLine() : Console.WriteLine($"==> SIZE {task.saveFile.FileName} ... {contentSize}")
Call ClearLine() : Console.WriteLine($"==> CONTENT-TYPE ... {resp.ContentType}")
2019-10-12 22:41:30 +08:00
Call ClearLine() : Console.WriteLine($"Length: {contentSize} ({sizePrettyPrint})")
2019-09-04 20:38:12 +08:00
Call Console.WriteLine()
cursorTop = Console.CursorTop
End Sub
2019-10-12 22:41:30 +08:00
''' <summary>
''' 执行有详细进度信息显示的文件下载操作, 如果只需要调用一个单纯的文件下载函数,
''' 请使用<see cref="DownloadFile(String, String, String, String, Integer, DownloadProgressChangedEventHandler, String, String)"/>拓展函数
''' </summary>
''' <param name="url$"></param>
''' <param name="save$"></param>
''' <returns></returns>
Public Shared Function Download(url$, Optional save$ = Nothing) As Boolean
Dim local As New Value(Of String)
Dim task As New wget(url, local = save Or $"./{url.Split("?"c).First.FileName}".AsDefault)
Call task.Run()
Return local.Value.FileLength > 0
End Function
2019-09-04 20:38:12 +08:00
End Class
2019-09-06 19:20:43 +08:00
End Namespace